module Graphql.Generator.Normalize exposing (capitalized, decapitalized) import Char import MyDebug import Regex import String.Extra normalizeIfElmReserved : String -> String normalizeIfElmReserved name = if List.member name elmReservedWords then name ++ "_" else name underscores : String -> { leading : String, trailing : String, remaining : String } underscores string = let regexFromString = Regex.fromString >> Maybe.withDefault Regex.never in case Regex.find (regexFromString "^(_*)([^_]?.*[^_]?)(_*)$") string |> List.head |> Maybe.map .submatches of Just [ leading, Just remaining, trailing ] -> { leading = Maybe.withDefault "" leading , trailing = Maybe.withDefault "" trailing , remaining = remaining } Nothing -> MyDebug.crash "Got nothing" _ -> MyDebug.crash ("Unexpected regex result for name " ++ string) isAllUpper : String -> Bool isAllUpper string = String.toUpper string == string capitilize : String -> String capitilize string = case string |> String.toList of firstChar :: rest -> (Char.toUpper firstChar :: rest) |> String.fromList [] -> "" capitalized : String -> String capitalized name = let group = underscores name in (if isAllUpper group.remaining then group.remaining |> String.toLower |> String.Extra.classify else group.remaining |> capitilize ) ++ group.leading ++ group.trailing decapitalized : String -> String decapitalized name = name |> capitalized |> String.Extra.decapitalize |> normalizeIfElmReserved {-| Taken from -} elmReservedWords : List String elmReservedWords = [ "as" , "case" , "else" , "exposing" , "if" , "import" , "in" , "infix" -- `infix` is a keyword rather than reserved, but it can't be used as a top-level identifier , "let" , "module" , "of" , "port" , "then" , "type" , "where" ]