Skip to content

Adapt String.words to handle blank text in a more standard way. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
24 changes: 22 additions & 2 deletions src/String/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module String.Extra exposing
( toSentenceCase, toTitleCase, decapitalize
, camelize, classify, underscored, dasherize, humanize
, replaceSlice, insertAt, nonEmpty, nonBlank, removeAccents
, break, softBreak
, break, softBreak, words
, wrap, wrapWith, softWrap, softWrapWith, quote, surround
, isBlank, countOccurrences
, clean, unquote, unsurround, unindent, ellipsis, softEllipsis, ellipsisWith, stripTags, pluralize
Expand Down Expand Up @@ -33,7 +33,7 @@ Functions borrowed from the Rails Inflector class

## Splitting

@docs break, softBreak
@docs break, softBreak, words


## Wrapping
Expand Down Expand Up @@ -897,3 +897,23 @@ regexEscape =
regexFromString : String -> Regex
regexFromString =
Regex.fromString >> Maybe.withDefault Regex.never


{-| Adapt the standard library implementation to behave "better" on blank text.
It follows the definition that the empty string is never a word,
so there are 0 words in blank text.

words "" == []

words " \t\n \r\n\n\u00a0 " == []

Aside for blank text (as judged by `isBlank`, `String.Extra.words` behaves like `String.words`.

-}
words : String -> List String
words text =
if isBlank text then
[]

else
String.words text
41 changes: 41 additions & 0 deletions tests/WordsTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module WordsTest exposing (allTests)

import Expect
import Fuzz exposing (Fuzzer, list, string)
import List exposing (foldl)
import String.Extra exposing (isBlank)
import Test exposing (Test, fuzz)


fuzzMultilineText : Fuzzer String
fuzzMultilineText =
let
-- Is there a system-independent value for newline?
newlineCharacter =
"\n"

appendLine text followingLine =
text ++ followingLine ++ newlineCharacter
in
list string
|> Fuzz.map (foldl appendLine "")


allTests : Test
allTests =
fuzz fuzzMultilineText "match String.words when not blank; return [] otherwise" <|
\text ->
let
-- I don't know how to write a fuzzer that generates
-- only non-blank collections of lines, so I combined the two tests,
-- which I otherwise dislike doing.
expected =
if isBlank text then
[]

else
String.words text
in
Expect.equal
expected
(String.Extra.words text)