elm-community / string-extra

String helper functions for Elm.
http://package.elm-lang.org/packages/elm-community/string-extra/latest
BSD 3-Clause "New" or "Revised" License
34 stars 26 forks source link

Make String.toTitleCase work with non-english alphabets #36

Open Herteby opened 5 years ago

Herteby commented 5 years ago

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.

Herteby commented 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)
Herteby commented 5 years ago

Or a third option:

toTitleCase : String -> String
toTitleCase =
    Regex.replace (regexFromString "^(.)|\\s(.)") (.match >> String.toUpper)