This repository has been archived by the owner on Apr 28, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Normalize.elm
71 lines (55 loc) · 1.6 KB
/
Normalize.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized)
import Char
import Regex
import String.Extra
normalizeIfElmReserved : String -> String
normalizeIfElmReserved name =
if name == "type" then
"type_"
else
name
underscores : String -> { leading : String, trailing : String, remaining : String }
underscores string =
case Regex.find Regex.All (Regex.regex "^(_*)([^_]?.*[^_]?)(_*)$") string |> List.head |> Maybe.map .submatches of
Just [ Just leading, Just remaining, Just trailing ] ->
{ leading = leading
, trailing = trailing
, remaining = remaining
}
Nothing ->
Debug.crash "Got nothing"
_ ->
Debug.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