Last active
June 9, 2016 10:59
-
-
Save fredcy/d48cf3d2b22193011c73af143ed714f5 to your computer and use it in GitHub Desktop.
Convert snake case to camel case in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
convert : String -> String | |
convert snakeStr = | |
let | |
repl : Regex.Match -> String | |
repl match = | |
case List.head match.submatches of | |
Just submatch -> | |
submatch |> Maybe.withDefault "" |> String.toUpper | |
Nothing -> | |
Debug.log "error: no submatch" "ERROR" | |
in | |
Regex.replace Regex.All (Regex.regex "_([a-z])") repl snakeStr | |
convert2 snakeStr = | |
let | |
repl : Regex.Match -> String | |
repl match = | |
case match.submatches of | |
[ submatch ] -> | |
submatch |> Maybe.withDefault "" |> String.toUpper | |
_ -> | |
Debug.log "error: not one submatch" "ERROR" | |
in | |
Regex.replace Regex.All (Regex.regex "_([a-z])") repl snakeStr | |
convert3 snakeStr = | |
let | |
repl match = | |
(List.head match.submatches) | |
`Maybe.andThen` | |
(Maybe.map String.toUpper) | |
|> Maybe.withDefault "" | |
in | |
Regex.replace Regex.All (Regex.regex "_([a-z])") repl snakeStr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment