Skip to content

Instantly share code, notes, and snippets.

@fredcy
Last active June 9, 2016 10:59
Show Gist options
  • Save fredcy/d48cf3d2b22193011c73af143ed714f5 to your computer and use it in GitHub Desktop.
Save fredcy/d48cf3d2b22193011c73af143ed714f5 to your computer and use it in GitHub Desktop.
Convert snake case to camel case in Elm
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