Open Herteby opened 5 years ago
Here's an alternative version which more closely follows the original:
toTitleCase : String -> String
toTitleCase ws =
let
uppercaseMatch =
Regex.replace (regexFromString "\\S+") (.match >> String.toSentenceCase)
in
ws
|> Regex.replace
(regexFromString "^(.)|\\s+(.)")
(.match >> uppercaseMatch)
Or a third option:
toTitleCase : String -> String
toTitleCase =
Regex.replace (regexFromString "^(.)|\\s(.)") (.match >> String.toUpper)
The behavior might slightly differ from the original in cases where there's a special character in front of a word. It's a lot simpler though!
The original has two issues:
[a-z]
only matches precisely a - z, and\w
only matches english word characters.