git.iko.soy/purescript-intl
on master help sign in
DateTimeFormat.Types
8c30a6cMarcin Szamotulski
src/Data/Intl/DateTimeFormat.purs+12−13
@@ -22,15 +22,14 @@ import Data.Either (Either(..))
22import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
22import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
23import Data.Foreign.Index ((!))
23import Data.Foreign.Index ((!))
24import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
24import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
25−import Data.Generic.Rep (class Generic)
26−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
25+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
27import Data.JSDate (JSDate)
26import Data.JSDate (JSDate)
28import Data.Maybe (Maybe(..), fromMaybe, maybe)
27import Data.Maybe (Maybe(..), fromMaybe, maybe)
29−import Data.Newtype (class Newtype)
28+import Data.Newtype (class Newtype, un)
30import Data.StrMap (union)
29import Data.StrMap (union)
31import Data.Symbol (SProxy(..))
30import Data.Symbol (SProxy(..))
32import Data.Traversable (sequence)
31import Data.Traversable (sequence)
33−import Data.Variant (Variant, default, on)
32+import Data.Variant (Variant, case_, default, on)
34import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
33import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
35import Unsafe.Coerce (unsafeCoerce)
34import Unsafe.Coerce (unsafeCoerce)
36
35
@@ -121,15 +120,15 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
121 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
120 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
122 , formatMatcher: maybe undefinedString show opts.formatMatcher
121 , formatMatcher: maybe undefinedString show opts.formatMatcher
123 }
122 }
124− comps' = default (defaultComponentRecord)
123+ comps' = case_
125− # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
124+ # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") (un FormatComponentRecord <<< formatComponent)
126− # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
125+ # on (SProxy :: SProxy "weekdayYearMonthDay") (un FormatComponentRecord <<< formatComponent)
127− # on (SProxy :: SProxy "yearMonthDay") formatComponent
126+ # on (SProxy :: SProxy "yearMonthDay") (un FormatComponentRecord <<< formatComponent)
128− # on (SProxy :: SProxy "yearMonth") formatComponent
127+ # on (SProxy :: SProxy "yearMonth") (un FormatComponentRecord <<< formatComponent)
129− # on (SProxy :: SProxy "monthDay") formatComponent
128+ # on (SProxy :: SProxy "monthDay") (un FormatComponentRecord <<< formatComponent)
130− # on (SProxy :: SProxy "hourMinuteSecond") formatComponent
129+ # on (SProxy :: SProxy "hourMinuteSecond") (un FormatComponentRecord <<< formatComponent)
131− # on (SProxy :: SProxy "hourMinute") formatComponent
130+ # on (SProxy :: SProxy "hourMinute") (un FormatComponentRecord <<< formatComponent)
132− # on (SProxy :: SProxy "custom") formatComponent
131+ # on (SProxy :: SProxy "custom") (un FormatComponentRecord <<< formatComponent)
133 $ comps
132 $ comps
134
133
135 toUndefined :: Maybe String -> String
134 toUndefined :: Maybe String -> String
src/Data/Intl/DateTimeFormat/Class.purs+5−2
@@ -1,9 +1,10 @@
1module Data.Intl.DateTimeFormat.Class where
1module Data.Intl.DateTimeFormat.Class where
2
2
3import Data.Maybe (Maybe(..))
3import Data.Maybe (Maybe(..))
4+import Data.Newtype (class Newtype)
4import Prelude (Unit)
5import Prelude (Unit)
5
6
6−type FormatComponentRecord =
7+newtype FormatComponentRecord = FormatComponentRecord
7 { era :: Maybe String
8 { era :: Maybe String
8 , weekday :: Maybe String
9 , weekday :: Maybe String
9 , year :: Maybe String
10 , year :: Maybe String
@@ -15,8 +16,10 @@ type FormatComponentRecord =
15 , timeZoneName :: Maybe String
16 , timeZoneName :: Maybe String
16 }
17 }
17
18
19+derive instance newtypeFormatComponentRecord :: Newtype FormatComponentRecord _
20+
18defaultComponentRecord :: FormatComponentRecord
21defaultComponentRecord :: FormatComponentRecord
19−defaultComponentRecord =
22+defaultComponentRecord = FormatComponentRecord
20 { era: Nothing
23 { era: Nothing
21 , weekday: Nothing
24 , weekday: Nothing
22 , year: Nothing
25 , year: Nothing
src/Data/Intl/DateTimeFormat/Generic.purs+3−3
@@ -7,7 +7,7 @@ module Data.Intl.DateTimeFormat.Generic
7 ) where
7 ) where
8
8
9import Data.Generic.Rep (class Generic, Constructor(Constructor), Field(..), NoConstructors, Product(..), Rec(..), from)
9import Data.Generic.Rep (class Generic, Constructor(Constructor), Field(..), NoConstructors, Product(..), Rec(..), from)
10−import Data.Intl.DateTimeFormat.Class (FormatComponentRecord)
10+import Data.Intl.DateTimeFormat.Class (FormatComponentRecord(..))
11import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
11import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
12import Data.Maybe (Maybe(..))
12import Data.Maybe (Maybe(..))
13import Data.StrMap (StrMap, lookup, singleton, union)
13import Data.StrMap (StrMap, lookup, singleton, union)
@@ -20,7 +20,7 @@ class GenericFormatComponentFields a where
20 genericFormatComponentFields :: a -> StrMap String
20 genericFormatComponentFields :: a -> StrMap String
21
21
22instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
22instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
23− genericFormatComponent' a =
23+ genericFormatComponent' a = FormatComponentRecord
24 { era: Nothing
24 { era: Nothing
25 , weekday: Nothing
25 , weekday: Nothing
26 , year: Nothing
26 , year: Nothing
@@ -80,7 +80,7 @@ instance genericFormatComponentFieldsProduct
80instance genericFormatComponentRec
80instance genericFormatComponentRec
81 :: (GenericFormatComponentFields a)
81 :: (GenericFormatComponentFields a)
82 => GenericFormatComponent (Rec a) where
82 => GenericFormatComponent (Rec a) where
83− genericFormatComponent' (Rec a) =
83+ genericFormatComponent' (Rec a) = FormatComponentRecord
84 { era: "era" `lookup` u
84 { era: "era" `lookup` u
85 , weekday: "weekday" `lookup` u
85 , weekday: "weekday" `lookup` u
86 , year: "year" `lookup` u
86 , year: "year" `lookup` u
src/Data/Intl/DateTimeFormat/Types.purs+50−27
@@ -19,12 +19,10 @@ module Data.Intl.DateTimeFormat.Types
19
19
20import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
20import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
21import Data.Foreign.Index ((!))
21import Data.Foreign.Index ((!))
22−import Data.Generic (class Generic)
23−import Data.Intl.DateTimeFormat.Class (class FormatComponent)
22+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..), defaultComponentRecord)
24−import Data.Intl.DateTimeFormat.Generic (genericFormatComponent)
25−import Data.Maybe (Maybe)
23+import Data.Maybe (Maybe(..))
26−import Data.Newtype (class Newtype)
24+import Data.Newtype (class Newtype, over)
27−import Prelude (class Show, bind, pure, ($), (<>), (>>=))
25+import Prelude (class Show, bind, pure, show, ($), (<>), (>>=))
28
26
29foreign import data Undefined :: Type
27foreign import data Undefined :: Type
30foreign import undefined :: Undefined
28foreign import undefined :: Undefined
@@ -112,10 +110,17 @@ newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecon
112 , second :: NumericRep
110 , second :: NumericRep
113 }
111 }
114
112
115−derive instance genericWeekdayYearMonthDayHourMinuteSecond :: Generic WeekdayYearMonthDayHourMinuteSecond _
116−
117instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
113instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
118− formatComponent = genericFormatComponent
114+ formatComponent (WeekdayYearMonthDayHourMinuteSecond { weekday, year, month, day, hour, minute, second }) = over FormatComponentRecord
115+ (\r -> r { weekday = Just (show weekday)
116+ , year = Just (show year)
117+ , month = Just (show month)
118+ , day = Just (show day)
119+ , hour = Just (show hour)
120+ , minute = Just (show minute)
121+ , second = Just (show second)
122+ }
123+ ) defaultComponentRecord
119
124
120newtype WeekdayYearMonthDay = WeekdayYearMonthDay
125newtype WeekdayYearMonthDay = WeekdayYearMonthDay
121 { weekday :: StringRep
126 { weekday :: StringRep
@@ -124,10 +129,15 @@ newtype WeekdayYearMonthDay = WeekdayYearMonthDay
124 , day :: NumericRep
129 , day :: NumericRep
125 }
130 }
126
131
127−derive instance genericWeekdayYearMonthDay :: Generic WeekdayYearMonthDay _
128−
129instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
132instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
130− formatComponent = genericFormatComponent
133+ formatComponent (WeekdayYearMonthDay { weekday, year, month, day }) = over FormatComponentRecord
134+ (\r -> r { weekday = Just (show weekday)
135+ , year = Just (show year)
136+ , month = Just (show month)
137+ , day = Just (show day)
138+ }
139+ ) defaultComponentRecord
140+
131
141
132newtype YearMonthDay = YearMonthDay
142newtype YearMonthDay = YearMonthDay
133 { year :: NumericRep
143 { year :: NumericRep
@@ -135,30 +145,38 @@ newtype YearMonthDay = YearMonthDay
135 , day :: NumericRep
145 , day :: NumericRep
136 }
146 }
137
147
138−derive instance genericYearMonthDay :: Generic YearMonthDay _
139−
140instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
148instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
141− formatComponent = genericFormatComponent
149+ formatComponent (YearMonthDay { year, month, day }) = over FormatComponentRecord
150+ (\r -> r { year = Just (show year)
151+ , month = Just (show month)
152+ , day = Just (show day)
153+ }
154+ ) defaultComponentRecord
155+
142
156
143newtype YearMonth = YearMonth
157newtype YearMonth = YearMonth
144 { year :: NumericRep
158 { year :: NumericRep
145 , month :: MonthRep
159 , month :: MonthRep
146 }
160 }
147
161
148−derive instance genericYearMonth :: Generic YearMonth _
149−
150instance formatComponentYearMonth :: FormatComponent YearMonth where
162instance formatComponentYearMonth :: FormatComponent YearMonth where
151− formatComponent = genericFormatComponent
163+ formatComponent (YearMonth { year, month }) = over FormatComponentRecord
164+ (\r -> r { year = Just (show year)
165+ , month = Just (show month)
166+ }
167+ ) defaultComponentRecord
152
168
153newtype MonthDay = MonthDay
169newtype MonthDay = MonthDay
154 { month :: MonthRep
170 { month :: MonthRep
155 , day :: NumericRep
171 , day :: NumericRep
156 }
172 }
157
173
158−derive instance genericMonthDay :: Generic MonthDay _
159−
160instance formatComponentMonthDay :: FormatComponent MonthDay where
174instance formatComponentMonthDay :: FormatComponent MonthDay where
161− formatComponent = genericFormatComponent
175+ formatComponent (MonthDay { month, day }) = over FormatComponentRecord
176+ (\r -> r { month = Just (show month)
177+ , day = Just (show day)
178+ }
179+ ) defaultComponentRecord
162
180
163newtype HourMinuteSecond = HourMinuteSecond
181newtype HourMinuteSecond = HourMinuteSecond
164 { hour :: NumericRep
182 { hour :: NumericRep
@@ -166,17 +184,22 @@ newtype HourMinuteSecond = HourMinuteSecond
166 , second :: NumericRep
184 , second :: NumericRep
167 }
185 }
168
186
169−derive instance genericHourMinuteSecond :: Generic HourMinuteSecond _
170−
171instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
187instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
172− formatComponent = genericFormatComponent
188+ formatComponent (HourMinuteSecond { hour, minute, second }) = over FormatComponentRecord
189+ (\r -> r { hour = Just (show hour)
190+ , minute = Just (show minute)
191+ , second = Just (show second)
192+ }
193+ ) defaultComponentRecord
173
194
174newtype HourMinute = HourMinute
195newtype HourMinute = HourMinute
175 { hour :: NumericRep
196 { hour :: NumericRep
176 , minute :: NumericRep
197 , minute :: NumericRep
177 }
198 }
178
199
179−derive instance genericHourMinute :: Generic HourMinute _
180−
181instance formatComponentHourMinute :: FormatComponent HourMinute where
200instance formatComponentHourMinute :: FormatComponent HourMinute where
182− formatComponent = genericFormatComponent
201+ formatComponent (HourMinute { hour, minute }) = over FormatComponentRecord
202+ (\r -> r { hour = Just (show hour)
203+ , minute = Just (show minute)
204+ }
205+ ) defaultComponentRecord
test/Test/Main.purs+3−2
@@ -12,7 +12,8 @@ import Data.Foldable (intercalate)
12import Data.Foreign (F, renderForeignError)
12import Data.Foreign (F, renderForeignError)
13import Data.Generic.Rep (class Generic)
13import Data.Generic.Rep (class Generic)
14import Data.Intl.DateTimeFormat (DateTimeFormatOptions(DateTimeFormatOptions), DateTimeFormatOptions', HourMinute(HourMinute), HourMinuteSecond(HourMinuteSecond), LocalesOption, MonthDay(MonthDay), MonthRep(MonthTwoDigit, MonthNumeric, MonthNarrow, MonthShort, MonthLong), NumericRep(Numeric, TwoDigit), ResolvedOptions(..), StringRep(Short, Long), TimeZone(TimeZone), WeekdayYearMonthDay(WeekdayYearMonthDay), WeekdayYearMonthDayHourMinuteSecond(WeekdayYearMonthDayHourMinuteSecond), YearMonth(YearMonth), YearMonthDay(YearMonthDay), createDateTimeFormat, formatJSDate, resolvedOptions, supportedLocalesOf)
14import Data.Intl.DateTimeFormat (DateTimeFormatOptions(DateTimeFormatOptions), DateTimeFormatOptions', HourMinute(HourMinute), HourMinuteSecond(HourMinuteSecond), LocalesOption, MonthDay(MonthDay), MonthRep(MonthTwoDigit, MonthNumeric, MonthNarrow, MonthShort, MonthLong), NumericRep(Numeric, TwoDigit), ResolvedOptions(..), StringRep(Short, Long), TimeZone(TimeZone), WeekdayYearMonthDay(WeekdayYearMonthDay), WeekdayYearMonthDayHourMinuteSecond(WeekdayYearMonthDayHourMinuteSecond), YearMonth(YearMonth), YearMonthDay(YearMonthDay), createDateTimeFormat, formatJSDate, resolvedOptions, supportedLocalesOf)
15−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
15+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..))
16+import Data.Intl.DateTimeFormat.Generic (genericFormatComponent)
16import Data.JSDate (JSDate, fromDateTime)
17import Data.JSDate (JSDate, fromDateTime)
17import Data.Maybe (Maybe(..), isJust, isNothing)
18import Data.Maybe (Maybe(..), isJust, isNothing)
18import Data.Symbol (SProxy(..))
19import Data.Symbol (SProxy(..))
@@ -36,7 +37,7 @@ instance formatComponentEraYear :: FormatComponent EraYear where
36newtype Era = Era StringRep
37newtype Era = Era StringRep
37
38
38instance formatComponentEra :: FormatComponent Era where
39instance formatComponentEra :: FormatComponent Era where
39− formatComponent (Era r) =
40+ formatComponent (Era r) = FormatComponentRecord
40 { era: Just (show r)
41 { era: Just (show r)
41 , weekday: Nothing
42 , weekday: Nothing
42 , year: Nothing
43 , year: Nothing