Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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