@@ -4,7 +4,7 @@ module Elm.Compiler.Module
4 ( Interface, Interfaces
4 ( Interface, Interfaces
5 , ModuleName.Raw
5 , ModuleName.Raw
6 , nameToPath
6 , nameToPath
7− , nameToString, nameFromString
7+ , nameToString, nameFromText
8 , hyphenate, dehyphenate
8 , hyphenate, dehyphenate
9 , RawForJson(RawForJson), fromJson
9 , RawForJson(RawForJson), fromJson
10 , interfaceAliasedTypes, programTypes
10 , interfaceAliasedTypes, programTypes
@@ -78,39 +78,43 @@ nameToString name =
78 Text.unpack name
78 Text.unpack name
81−nameFromString :: String -> Maybe ModuleName.Raw
81+nameFromText :: Text -> Maybe ModuleName.Raw
82−nameFromString =
82+nameFromText =
83− fromString '.'
83+ fromText '.'
86−hyphenate :: ModuleName.Raw -> String
86+hyphenate :: ModuleName.Raw -> Text
87−hyphenate names =
87+hyphenate name =
88− Text.unpack (Text.replace "." "-" names)
88+ Text.replace "." "-" name
91−dehyphenate :: String -> Maybe ModuleName.Raw
91+dehyphenate :: Text -> Maybe ModuleName.Raw
92dehyphenate =
92dehyphenate =
93− fromString '-'
93+ fromText '-'
96−fromString :: Char -> String -> Maybe ModuleName.Raw
96+fromText :: Char -> Text -> Maybe ModuleName.Raw
97−fromString sep raw =
97+fromText sep name =
98− do chunks <- mapM isLegit names
99+ chunks =
99− return (Text.pack (List.intercalate "." chunks))
100+ Text.splitOn (Text.singleton sep) name
101− names =
102+ if all isGoodChunk chunks then Just name else Nothing
102− filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
103+
104− isLegit name =
105+isGoodChunk :: Text -> Bool
105− case name of
106+isGoodChunk chunk =
106− [] ->
107+ case Text.uncons chunk of
107− Nothing
108+ Nothing ->
109− char:rest ->
111+ Just (first, rest) ->
110− if Char.isUpper char && all legitChar rest then Just name else Nothing
112+ Char.isUpper first && Text.all isGoodChar rest
112− legitChar char =
114+
115+isGoodChar :: Char -> Bool
113− Char.isAlphaNum char || char == '_'
117+ Char.isAlphaNum char || char == '_'
@@ -124,16 +128,12 @@ instance Json.ToJSON RawForJson where
125instance Json.FromJSON RawForJson where
129instance Json.FromJSON RawForJson where
126 parseJSON (Json.String text) =
130 parseJSON (Json.String text) =
127− let
131+ case nameFromText text of
131− case nameFromString rawString of
132− Nothing ->
132+ Nothing ->
133− fail (rawString ++ " is not a valid module name")
133+ fail (Text.unpack text ++ " is not a valid module name")
135− Just name ->
135+ Just name ->
136− return (RawForJson name)
136+ return (RawForJson name)
138 parseJSON _ =
138 parseJSON _ =
139 fail "expecting the module name to be a string"
139 fail "expecting the module name to be a string"