on 665d3d9 help sign in
106 lines 2.0 kB history raw
1module Generate.JavaScript.Mode
2 ( Mode(..)
3 , Target(..)
4 , debug
5 , dev
6 , prod
7 , isDebug
8 , isServer
9 )
10 where
13import qualified Data.List as List
14import qualified Data.Map as Map
15import qualified Data.Maybe as Maybe
17import qualified AST.Optimized as Opt
18import qualified Elm.Interface as I
19import qualified Elm.Name as N
20import qualified Generate.JavaScript.Name as Name
24-- MODE
27data Mode
28 = Dev Target (Maybe I.Interfaces)
29 | Prod Target ShortFieldNames
32data Target = Client | Server
35debug :: Target -> I.Interfaces -> Mode
36debug target interfaces =
37 Dev target (Just interfaces)
40dev :: Target -> Mode
41dev target =
42 Dev target Nothing
45prod :: Target -> Opt.Graph -> Mode
46prod target (Opt.Graph _ _ fieldCounts) =
47 Prod target (shortenFieldNames fieldCounts)
51-- IS DEBUG?
54isDebug :: Mode -> Bool
55isDebug mode =
56 case mode of
57 Dev _ mi -> Maybe.isJust mi
58 Prod _ _ -> False
61-- IS SERVER?
64isServer :: Mode -> Bool
65isServer mode =
66 case mode of
67 Dev target _ -> isServerHelp target
68 Prod target _ -> isServerHelp target
71isServerHelp :: Target -> Bool
72isServerHelp target =
73 case target of
74 Client -> False
75 Server -> True
79-- SHORTEN FIELD NAMES
82type ShortFieldNames =
83 Map.Map N.Name Name.Name
86shortenFieldNames :: Map.Map N.Name Int -> ShortFieldNames
87shortenFieldNames frequencies =
88 Map.foldr addToShortNames Map.empty $
89 Map.foldrWithKey addToBuckets Map.empty frequencies
92addToBuckets :: N.Name -> Int -> Map.Map Int [N.Name] -> Map.Map Int [N.Name]
93addToBuckets field frequency buckets =
94 -- TODO try using an IntMap for buckets
95 Map.insertWith (++) frequency [field] buckets
98addToShortNames :: [N.Name] -> ShortFieldNames -> ShortFieldNames
99addToShortNames fields shortNames =
100 List.foldl' addField shortNames fields
103addField :: ShortFieldNames -> N.Name -> ShortFieldNames
104addField shortNames field =
105 let rename = Name.fromInt (Map.size shortNames) in
106 Map.insert field rename shortNames