Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/String/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module String.Extra exposing
, camelize, classify, underscored, dasherize, humanize
, replaceSlice, insertAt, nonEmpty, nonBlank, removeAccents
, break, softBreak
, joinMap
, wrap, wrapWith, softWrap, softWrapWith, quote, surround
, isBlank, countOccurrences
, clean, unquote, unsurround, unindent, ellipsis, softEllipsis, ellipsisWith, stripTags, pluralize
Expand Down Expand Up @@ -36,6 +37,11 @@ Functions borrowed from the Rails Inflector class
@docs break, softBreak


## Joining

@docs joinMap


## Wrapping

@docs wrap, wrapWith, softWrap, softWrapWith, quote, surround
Expand Down Expand Up @@ -205,6 +211,26 @@ softBreakRegexp width =
regexFromString <| ".{1," ++ String.fromInt width ++ "}(\\s+|$)|\\S+?(\\s+|$)"


{-| A more performant and easier way of writing

list
|> List.map f
|> String.join sep

-}
joinMap : (a -> String) -> String -> List a -> String
joinMap f sep list =
case list of
[ only ] ->
f only

first :: rest ->
f first ++ sep ++ joinMap f sep rest

[] ->
""


{-| Trim the whitespace of both sides of the string and compress
repeated whitespace internally to a single whitespace char.

Expand Down
35 changes: 34 additions & 1 deletion tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
module Tests exposing (breakTest, cleanTest, countOccurrencesTest, decapitalizeTest, ellipsisTest, insertAtProducer, insertAtTest, isBlankTest, nonBlankTest, pluralizeTest, softBreakTest, surroundTest, tail, toSentenceCaseTest, toTitleCaseTest, unquoteTest, wrapTest)
module Tests exposing
( breakTest
, cleanTest
, countOccurrencesTest
, decapitalizeTest
, ellipsisTest
, insertAtProducer
, insertAtTest
, isBlankTest
, joinMapTest
, nonBlankTest
, pluralizeTest
, softBreakTest
, surroundTest
, tail
, toSentenceCaseTest
, toTitleCaseTest
, unquoteTest
, wrapTest
)

import Expect
import Fuzz exposing (..)
Expand Down Expand Up @@ -168,6 +187,20 @@ softBreakTest =
]


joinMapTest : Test
joinMapTest =
describe "joinMap"
[ fuzz2 string (list int) "Should yield the same result as List.map combined with String.join" <|
\sep list ->
Expect.equal
(list
|> List.map String.fromInt
|> String.join sep
)
(joinMap String.fromInt sep list)
]


cleanTest : Test
cleanTest =
describe "clean"
Expand Down