Skip to content

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

Open
Herteby wants to merge 1 commit into
elm-community:masterfrom
Herteby:master
Open

Make String.toTitleCase work with non-english alphabets#36
Herteby wants to merge 1 commit into
elm-community:masterfrom
Herteby:master

Conversation

@Herteby

@Herteby Herteby commented Jan 15, 2019

Copy link
Copy Markdown

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.

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!
@Herteby

Herteby commented Jan 15, 2019

Copy link
Copy Markdown
Author

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

Herteby commented Jan 15, 2019

Copy link
Copy Markdown
Author

Or a third option:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant