git.iko.soy/purescript-intl
on 010ca64 help sign in
132 lines 4.7 kB history raw
1module Data.Intl.DateTimeFormat.Generic
2 ( class FormatComponent
3 , formatComponent
4 , class GenericFormatComponent
5 , genericFormatComponent'
6 , genericFormatComponent
7 , class GenericFormatComponentFields
8 , genericFormatComponentFields
9 , defaultComponentRecord
10 , FormatComponentRecord
11 ) where
13import Data.Generic.Rep (class Generic, Constructor(Constructor), Field(..), NoConstructors, Product(..), Rec(..), from)
14import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
15import Data.Maybe (Maybe(..))
16import Data.StrMap (StrMap, lookup, singleton, union)
17import Prelude (Unit, show)
19type FormatComponentRecord =
20 { era :: Maybe String
21 , weekday :: Maybe String
22 , year :: Maybe String
23 , month :: Maybe String
24 , day :: Maybe String
25 , hour :: Maybe String
26 , minute :: Maybe String
27 , second :: Maybe String
28 , timeZoneName :: Maybe String
29 }
31defaultComponentRecord :: FormatComponentRecord
32defaultComponentRecord =
33 { era: Nothing
34 , weekday: Nothing
35 , year: Nothing
36 , month: Nothing
37 , day: Nothing
38 , hour: Nothing
39 , minute: Nothing
40 , second: Nothing
41 , timeZoneName: Nothing
42 }
44class FormatComponent a where
45 formatComponent :: a -> FormatComponentRecord
47instance unitFormatComponent :: FormatComponent Unit where
48 formatComponent _ = defaultComponentRecord
50class GenericFormatComponent a where
51 genericFormatComponent' :: a -> FormatComponentRecord
53class GenericFormatComponentFields a where
54 genericFormatComponentFields :: a -> StrMap String
56instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
57 genericFormatComponent' a =
58 { era: Nothing
59 , weekday: Nothing
60 , year: Nothing
61 , month: Nothing
62 , day: Nothing
63 , hour: Nothing
64 , minute: Nothing
65 , second: Nothing
66 , timeZoneName: Nothing
67 }
69instance genericFormatComponentConstructor :: (GenericFormatComponent a) => GenericFormatComponent (Constructor name a) where
70 genericFormatComponent' (Constructor a) = genericFormatComponent' a
72instance genericFormatComponentFieldsEra
73 :: GenericFormatComponentFields (Field "era" StringRep) where
74 genericFormatComponentFields (Field a) = singleton "era" (show a)
76instance genericFormatComponentFieldsWeekday
77 :: GenericFormatComponentFields (Field "weekday" StringRep) where
78 genericFormatComponentFields (Field a) = singleton "weekday" (show a)
80instance genericFormatComponentFieldsYear
81 :: GenericFormatComponentFields (Field "year" NumericRep) where
82 genericFormatComponentFields (Field a) = singleton "year" (show a)
84instance genericFormatComponentFieldsMonth
85 :: GenericFormatComponentFields (Field "month" MonthRep) where
86 genericFormatComponentFields (Field a) = singleton "month" (show a)
88instance genericFormatComponentFieldsDay
89 :: GenericFormatComponentFields (Field "day" NumericRep) where
90 genericFormatComponentFields (Field a) = singleton "day" (show a)
92instance genericFormatComponentFieldsHour
93 :: GenericFormatComponentFields (Field "hour" NumericRep) where
94 genericFormatComponentFields (Field a) = singleton "hour" (show a)
96instance genericFormatComponentFieldsMinute
97 :: GenericFormatComponentFields (Field "minute" NumericRep) where
98 genericFormatComponentFields (Field a) = singleton "minute" (show a)
100instance genericFormatComponentFieldsSecond
101 :: GenericFormatComponentFields (Field "second" NumericRep) where
102 genericFormatComponentFields (Field a) = singleton "second" (show a)
104instance genericFormatComponentFieldsTimeZoneName
105 :: GenericFormatComponentFields (Field "timeZoneName" TimeZoneNameRep) where
106 genericFormatComponentFields (Field a) = singleton "timeZoneName" (show a)
108instance genericFormatComponentFieldsProduct
109 :: (GenericFormatComponentFields a, GenericFormatComponentFields b)
110 => GenericFormatComponentFields (Product a b) where
111 genericFormatComponentFields (Product a b) =
112 union (genericFormatComponentFields a) (genericFormatComponentFields b)
114instance genericFormatComponentRec
115 :: (GenericFormatComponentFields a)
116 => GenericFormatComponent (Rec a) where
117 genericFormatComponent' (Rec a) =
118 { era: "era" `lookup` u
119 , weekday: "weekday" `lookup` u
120 , year: "year" `lookup` u
121 , month: "month" `lookup` u
122 , day: "day" `lookup` u
123 , hour: "hour" `lookup` u
124 , minute: "minute" `lookup` u
125 , second: "second" `lookup` u
126 , timeZoneName: "timeZoneName" `lookup` u
127 }
128 where
129 u = genericFormatComponentFields a
131genericFormatComponent :: forall a rep. Generic a rep => GenericFormatComponent rep => a -> FormatComponentRecord
132genericFormatComponent a = genericFormatComponent' (from a)