Skip to content

Commit

Permalink
Makes most strings tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
gampleman committed Sep 5, 2023
1 parent d06492d commit 35a2902
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

17 changes: 5 additions & 12 deletions src/String/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,14 @@ decapitalize word =

{-| Capitalize the first character of each word in a string.
toTitleCase "this is a phrase" == "This Is A Phrase"
toTitleCase "this is a phrase" --> "This Is A Phrase"
toTitleCase "hello, world" == "Hello, World"
toTitleCase "hello, world" --> "Hello, World"
-}
toTitleCase : String -> String
toTitleCase ws =
let
uppercaseMatch =
Regex.replace (regexFromString "\\w+") (.match >> toSentenceCase)
in
ws
|> Regex.replace
(regexFromString "^([a-z])|\\s+([a-z])")
(.match >> uppercaseMatch)
Regex.replace (regexFromString "^([^\\s])|\\s[^\\s]") (.match >> String.toUpper) ws


{-| Replace text within a portion of a string given a substitution
Expand Down Expand Up @@ -202,7 +195,7 @@ softBreak width string =

softBreakRegexp : Int -> Regex.Regex
softBreakRegexp width =
regexFromString <| ".{1," ++ String.fromInt width ++ "}(\\s+|$)|\\S+?(\\s+|$)"
regexFromString <| ".{0," ++ String.fromInt (width - 1) ++ "}(\\s|$)|\\S+?(\\s|$)"


{-| Trim the whitespace of both sides of the string and compress
Expand All @@ -214,7 +207,7 @@ repeated whitespace internally to a single whitespace char.
clean : String -> String
clean string =
string
|> Regex.replace (regexFromString "\\s\\s+") (always " ")
|> Regex.replace (regexFromString "\\s+") (always " ")
|> String.trim


Expand Down
6 changes: 3 additions & 3 deletions tests/String/CamelizeTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ camelizeTest =
|> String.contains "-"
|> Expect.equal False
|> Expect.onFail "Camelize should remove underscores"
, fuzz Fuzz.string "It is the same lowercased string after removing the dashes and spaces" <|
, fuzz Fuzz.string "It is the same uppercased string after removing the dashes and spaces" <|
\s ->
let
expected =
replace "-" ""
>> replace "_" ""
>> Regex.replace (Regex.fromString "\\s+" |> Maybe.withDefault Regex.never) (\_ -> "")
>> String.toLower
>> String.toUpper
in
camelize s
|> String.toLower
|> String.toUpper
|> Expect.equal (expected s)
, fuzz (validWords '-') "The first letter after each dash is capitalized" <|
\s ->
Expand Down
4 changes: 2 additions & 2 deletions tests/String/HumanizeTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ humanizeTest =
String.replace "-" ""
>> String.replace "_" ""
>> Regex.replace (regex "\\s+") (\_ -> "")
>> String.toLower
>> String.toUpper
in
humanize s
|> String.replace " " ""
|> String.toLower
|> String.toUpper
|> Expect.equal (expected s)
, fuzz (validWords []) "It adds a space before each group of uppercase letter" <|
\s ->
Expand Down
74 changes: 18 additions & 56 deletions tests/String/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Char.Extra
import Expect
import Fuzz exposing (Fuzzer, string)
import Regex exposing (Regex)
import String exposing (fromChar, replace, toLower, toUpper, uncons)
import String exposing (fromChar, replace, toLower, uncons)
import String.Extra
import Test exposing (Test, describe, fuzz, fuzz2, test)
import Tuple exposing (first, second)
Expand All @@ -20,31 +20,17 @@ toSentenceCaseTest =
describe "toSentenceCase"
[ fuzz Fuzz.string "It converts the first char of the string to uppercase" <|
\string ->
let
result =
string
|> String.Extra.toSentenceCase
|> uncons
|> Maybe.map (first >> fromChar)
|> Maybe.withDefault ""

expected =
string
|> uncons
|> Maybe.map (first >> fromChar >> toUpper)
|> Maybe.withDefault ""
in
Expect.equal expected result
string
|> String.Extra.toSentenceCase
|> String.left 1
|> (\pref -> String.startsWith pref (String.toUpper string))
|> Expect.equal True
, fuzz Fuzz.string "The tail of the string remains untouched" <|
\string ->
let
result =
(String.Extra.toSentenceCase >> tail) string

expected =
tail string
in
Expect.equal expected result
string
|> String.Extra.toSentenceCase
|> String.endsWith (tail string)
|> Expect.equal True
]


Expand Down Expand Up @@ -100,21 +86,6 @@ toTitleCaseTest =
|> List.map String.Extra.toSentenceCase
in
Expect.equal expected result
, fuzz (Fuzz.list Fuzz.string) "It does not change the length of the string" <|
\strings ->
let
result =
strings
|> String.join " "
|> String.Extra.toTitleCase
|> String.length

expected =
strings
|> String.join " "
|> String.length
in
Expect.equal expected result
]


Expand Down Expand Up @@ -162,7 +133,7 @@ softBreakTest =
String.Extra.softBreak width (String.trim string)
|> String.concat
|> Expect.equal (String.trim string)
, fuzz2 Fuzz.string (Fuzz.intRange 1 10) "The list should not have more elements than words" <|
, fuzz2 (Fuzz.map String.Extra.clean Fuzz.string) (Fuzz.intRange 1 10) "The list should not have more elements than words on a clean string" <|
\string width ->
String.Extra.softBreak width string
|> List.length
Expand All @@ -173,22 +144,13 @@ softBreakTest =
cleanTest : Test
cleanTest =
describe "clean"
[ {- Test.skip <|
fuzz Fuzz.string "The String.split result is the same as String.words" <|
\string ->
let
result =
string
|> clean
|> String.split " "
expected =
String.words string
in
Expect.equal expected result
,
-}
fuzz Fuzz.string "It trims the string on the left side" <|
[ fuzz Fuzz.string "The String.split result is the same as String.words" <|
\string ->
string
|> String.Extra.clean
|> String.split " "
|> Expect.equal (String.words string)
, fuzz Fuzz.string "It trims the string on the left side" <|
\string ->
string
|> String.Extra.clean
Expand Down

0 comments on commit 35a2902

Please sign in to comment.