Skip to content

Conversation

@Herteby
Copy link

@Herteby Herteby commented Jan 15, 2019

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
Copy link
Author

Herteby commented Jan 15, 2019

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
Copy link
Author

Herteby commented Jan 15, 2019

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