on e4797db help sign in
94 lines 1.8 kB history raw
1module Generate.JavaScript.Mode
2 ( Mode(..)
3 , Target(..)
4 , debug
5 , dev
6 , prod
7 , isServer
8 )
9 where
12import qualified Data.List as List
13import qualified Data.Map as Map
15import qualified AST.Optimized as Opt
16import qualified Elm.Interface as I
17import qualified Elm.Name as N
18import qualified Generate.JavaScript.Name as Name
22-- MODE
25data Mode
26 = Dev Target (Maybe I.Interfaces)
27 | Prod Target ShortFieldNames
30data Target = Client | Server
33debug :: Target -> I.Interfaces -> Mode
34debug target interfaces =
35 Dev target (Just interfaces)
38dev :: Target -> Mode
39dev target =
40 Dev target Nothing
43prod :: Target -> Opt.Graph -> Mode
44prod target (Opt.Graph _ _ fieldCounts) =
45 Prod target (shortenFieldNames fieldCounts)
49-- IS SERVER?
52isServer :: Mode -> Bool
53isServer mode =
54 case mode of
55 Dev target _ -> isServerHelp target
56 Prod target _ -> isServerHelp target
59isServerHelp :: Target -> Bool
60isServerHelp target =
61 case target of
62 Client -> False
63 Server -> True
67-- SHORTEN FIELD NAMES
70type ShortFieldNames =
71 Map.Map N.Name Name.Name
74shortenFieldNames :: Map.Map N.Name Int -> ShortFieldNames
75shortenFieldNames frequencies =
76 Map.foldr addToShortNames Map.empty $
77 Map.foldrWithKey addToBuckets Map.empty frequencies
80addToBuckets :: N.Name -> Int -> Map.Map Int [N.Name] -> Map.Map Int [N.Name]
81addToBuckets field frequency buckets =
82 -- TODO try using an IntMap for buckets
83 Map.insertWith (++) frequency [field] buckets
86addToShortNames :: [N.Name] -> ShortFieldNames -> ShortFieldNames
87addToShortNames fields shortNames =
88 List.foldl' addField shortNames fields
91addField :: ShortFieldNames -> N.Name -> ShortFieldNames
92addField shortNames field =
93 let rename = Name.fromInt (Map.size shortNames) in
94 Map.insert field rename shortNames