@@ -4,7 +4,7 @@ module Elm.Compiler.Module
44 ( Interface, Interfaces
55 , ModuleName.Raw
66 , nameToPath
7− , nameToString, nameFromString
7+ , nameToString, nameFromText
88 , hyphenate, dehyphenate
99 , RawForJson(RawForJson), fromJson
1010 , interfaceAliasedTypes, programTypes
@@ -78,39 +78,43 @@ nameToString name =
7878 Text.unpack name
7979
8080
81−nameFromString :: String -> Maybe ModuleName.Raw
82−nameFromString =
83− fromString '.'
81+nameFromText :: Text -> Maybe ModuleName.Raw
82+nameFromText =
83+ fromText '.'
8484
8585
86−hyphenate :: ModuleName.Raw -> String
87−hyphenate names =
88− Text.unpack (Text.replace "." "-" names)
86+hyphenate :: ModuleName.Raw -> Text
87+hyphenate name =
88+ Text.replace "." "-" name
8989
9090
91−dehyphenate :: String -> Maybe ModuleName.Raw
91+dehyphenate :: Text -> Maybe ModuleName.Raw
9292dehyphenate =
93− fromString '-'
93+ fromText '-'
9494
9595
96−fromString :: Char -> String -> Maybe ModuleName.Raw
97−fromString sep raw =
98− do chunks <- mapM isLegit names
99− return (Text.pack (List.intercalate "." chunks))
100− where
101− names =
102− filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
96+fromText :: Char -> Text -> Maybe ModuleName.Raw
97+fromText sep name =
98+ let
99+ chunks =
100+ Text.splitOn (Text.singleton sep) name
101+ in
102+ if all isGoodChunk chunks then Just name else Nothing
103+
103104
104− isLegit name =
105− case name of
106− [] ->
107− Nothing
105+isGoodChunk :: Text -> Bool
106+isGoodChunk chunk =
107+ case Text.uncons chunk of
108+ Nothing ->
109+ False
108110
109− char:rest ->
110− if Char.isUpper char && all legitChar rest then Just name else Nothing
111+ Just (first, rest) ->
112+ Char.isUpper first && Text.all isGoodChar rest
111113
112− legitChar char =
113− Char.isAlphaNum char || char == '_'
114+
115+isGoodChar :: Char -> Bool
116+isGoodChar char =
117+ Char.isAlphaNum char || char == '_'
114118
115119
116120
@@ -124,16 +128,12 @@ instance Json.ToJSON RawForJson where
124128
125129instance Json.FromJSON RawForJson where
126130 parseJSON (Json.String text) =
127− let
128− rawString =
129− Text.unpack text
130− in
131− case nameFromString rawString of
132− Nothing ->
133− fail (rawString ++ " is not a valid module name")
134−
135− Just name ->
136− return (RawForJson name)
131+ case nameFromText text of
132+ Nothing ->
133+ fail (Text.unpack text ++ " is not a valid module name")
134+
135+ Just name ->
136+ return (RawForJson name)
137137
138138 parseJSON _ =
139139 fail "expecting the module name to be a string"