Skip to content

Commit

Permalink
Normalize all Elm keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
madscoaducom committed Feb 12, 2018
1 parent a9bbff3 commit f56a556
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 66 deletions.
56 changes: 43 additions & 13 deletions src/Graphqelm/Generator/Normalize.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized)
module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized, elmKeywords)

import Char
import Regex
import String.Extra


elmKeywords : List String
elmKeywords =
[ "type"
, "alias"
, "port"
, "if"
, "then"
, "else"
, "case"
, "of"
, "let"
, "in"
, "infix"
, "left"
, "right"
, "non"
, "module"
, "import"
, "exposing"
, "as"
, "where"
, "effect"
, "command"
, "subscription"
, "true"
, "false"
, "null"
]


normalizeIfElmReserved : String -> String
normalizeIfElmReserved name =
if name == "type" then
"type_"
if (List.member name elmKeywords) then
name ++ "_"
else
name

Expand Down Expand Up @@ -51,16 +81,16 @@ capitalized name =
group =
underscores name
in
(if isAllUpper group.remaining then
group.remaining
|> String.toLower
|> String.Extra.classify
else
group.remaining
|> capitilize
)
++ group.leading
++ group.trailing
(if isAllUpper group.remaining then
group.remaining
|> String.toLower
|> String.Extra.classify
else
group.remaining
|> capitilize
)
++ group.leading
++ group.trailing


decapitalized : String -> String
Expand Down
116 changes: 63 additions & 53 deletions tests/Generator/NormalizeTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,69 @@ import Graphqelm.Generator.Normalize as Normalize
import Test exposing (Test, describe, only, test)


-- Taken from https://github.com/elm-lang/elm-compiler/blob/master/src/Parse/Primitives/Keyword.hs


all : Test
all =
describe "normalize"
[ test "leaves valid names untouched" <|
\() ->
Normalize.decapitalized "validCamelCaseName"
|> Expect.equal "validCamelCaseName"
, test "type field name" <|
\() ->
Normalize.decapitalized "type"
|> Expect.equal "type_"
, test "leaves valid snake_case names untouched" <|
\() ->
Normalize.decapitalized "year_budget"
|> Expect.equal "year_budget"
, test "corrects casing on otherwise valid snake_case names" <|
\() ->
Normalize.capitalized "year_budget"
|> Expect.equal "Year_budget"
, test "already normalized module name" <|
\() ->
Normalize.capitalized "MyInterface"
|> Expect.equal "MyInterface"
, test "module name with leading underscore" <|
\() ->
Normalize.capitalized "_ModelMutationType"
|> Expect.equal "ModelMutationType_"
, test "normalizes all uppercase names to capitalized" <|
\() ->
Normalize.capitalized "ENUMERATION"
|> Expect.equal "Enumeration"
, test "uppercase with leading underscore" <|
\() ->
Normalize.capitalized "_My_Module_Name"
|> Expect.equal "My_Module_Name_"
, test "normalizes enums that follow convention into class case" <|
\() ->
Normalize.capitalized "MOBILE_WEB"
|> Expect.equal "MobileWeb"
, test "leaves unconventional but valid names untouched" <|
\() ->
Normalize.capitalized "BillingSubscriptions_UpdateBillingEmailsPayload"
|> Expect.equal "BillingSubscriptions_UpdateBillingEmailsPayload"
, test "fixes first letter only in all lowercase" <|
\() ->
Normalize.capitalized "hello"
|> Expect.equal "Hello"
, test "fixes single letter names" <|
\() ->
Normalize.decapitalized "X"
|> Expect.equal "x"
, test "puts multiple leading underscores to the back" <|
\() ->
Normalize.decapitalized "________x"
|> Expect.equal "x________"
describe "normalize" <|
[ describe "add _ to all Elm keywords" <|
List.map
(\keyword ->
test (keyword ++ " field name") <|
\() ->
Normalize.decapitalized keyword
|> Expect.equal (keyword ++ "_")
)
Normalize.elmKeywords
, describe "non keywords handling" <|
[ test "leaves valid names untouched" <|
\() ->
Normalize.decapitalized "validCamelCaseName"
|> Expect.equal "validCamelCaseName"
, test "leaves valid snake_case names untouched" <|
\() ->
Normalize.decapitalized "year_budget"
|> Expect.equal "year_budget"
, test "corrects casing on otherwise valid snake_case names" <|
\() ->
Normalize.capitalized "year_budget"
|> Expect.equal "Year_budget"
, test "already normalized module name" <|
\() ->
Normalize.capitalized "MyInterface"
|> Expect.equal "MyInterface"
, test "module name with leading underscore" <|
\() ->
Normalize.capitalized "_ModelMutationType"
|> Expect.equal "ModelMutationType_"
, test "normalizes all uppercase names to capitalized" <|
\() ->
Normalize.capitalized "ENUMERATION"
|> Expect.equal "Enumeration"
, test "uppercase with leading underscore" <|
\() ->
Normalize.capitalized "_My_Module_Name"
|> Expect.equal "My_Module_Name_"
, test "normalizes enums that follow convention into class case" <|
\() ->
Normalize.capitalized "MOBILE_WEB"
|> Expect.equal "MobileWeb"
, test "leaves unconventional but valid names untouched" <|
\() ->
Normalize.capitalized "BillingSubscriptions_UpdateBillingEmailsPayload"
|> Expect.equal "BillingSubscriptions_UpdateBillingEmailsPayload"
, test "fixes first letter only in all lowercase" <|
\() ->
Normalize.capitalized "hello"
|> Expect.equal "Hello"
, test "fixes single letter names" <|
\() ->
Normalize.decapitalized "X"
|> Expect.equal "x"
, test "puts multiple leading underscores to the back" <|
\() ->
Normalize.decapitalized "________x"
|> Expect.equal "x________"
]
]

0 comments on commit f56a556

Please sign in to comment.