git.iko.soy/purescript-intl
on master help sign in
src/Data/Intl/DateTimeFormat.js+1−3
since splitsnapshots
unifiedsplit
@@ -1,14 +1,12 @@
1"use strict";
1"use strict";
2
2
3−exports["undefined"] = undefined;
4−
5exports.dateTimeFormatImpl = function(locales) {
3exports.dateTimeFormatImpl = function(locales) {
6 return function(options) {
4 return function(options) {
7 return new Intl.DateTimeFormat(locales, options);
5 return new Intl.DateTimeFormat(locales, options);
8 };
6 };
9};
7};
10
8
11−exports.createDateTimeFormatterImpl = function(throwError, pure, locales, opts) {
9+exports.createDateTimeFormatImpl = function(throwError, pure, locales, opts) {
12 try {
10 try {
13 return pure(Intl.DateTimeFormat(locales, opts));
11 return pure(Intl.DateTimeFormat(locales, opts));
14 } catch (e) {
12 } catch (e) {
src/Data/Intl/DateTimeFormat.purs+109−114
@@ -1,22 +1,13 @@
1−module Data.Intl.DateTimeFormat
1+module Data.Intl.DateTimeFormat
2 ( LocalesOption
2 ( LocalesOption
3 , LocaleMatcher(..)
3 , LocaleMatcher(..)
4 , TimeZone(..)
4 , TimeZone(..)
5 , FormatMatcher(..)
5 , FormatMatcher(..)
6− , WeekdayYearMonthDayHourMinuteSecond(..)
7− , WeekdayYearMonthDay(..)
8− , YearMonthDay(..)
9− , YearMonth(..)
10− , MonthDay(..)
11− , HourMinuteSecond(..)
12− , HourMinute(..)
13− , Undefined
14− , undefined
15 , DateTimeFormatOptions(..)
6 , DateTimeFormatOptions(..)
16 , DateTimeFormatOptions'
7 , DateTimeFormatOptions'
17 , DateTimeComponents(..)
8 , DateTimeComponents(..)
18− , DateTimeFormatter
9+ , DateTimeFormat
19− , createDateTimeFormatter
10+ , createDateTimeFormat
20 , formatJSDate
11 , formatJSDate
21 , module Data.Intl.DateTimeFormat.Types
12 , module Data.Intl.DateTimeFormat.Types
22 , supportedLocalesOf
13 , supportedLocalesOf
@@ -31,23 +22,18 @@ import Data.Either (Either(..))
31import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
22import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
32import Data.Foreign.Index ((!))
23import Data.Foreign.Index ((!))
33import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
24import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
34−import Data.Generic.Rep (class Generic)
35−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
25+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
36import Data.JSDate (JSDate)
26import Data.JSDate (JSDate)
37import Data.Maybe (Maybe(..), fromMaybe, maybe)
27import Data.Maybe (Maybe(..), fromMaybe, maybe)
38−import Data.Newtype (class Newtype)
28+import Data.Newtype (class Newtype, un)
39import Data.StrMap (union)
29import Data.StrMap (union)
40import Data.Symbol (SProxy(..))
30import Data.Symbol (SProxy(..))
41import Data.Traversable (sequence)
31import Data.Traversable (sequence)
42−import Data.Variant (Variant, default, on)
32+import Data.Variant (Variant, case_, default, on)
43import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
33import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
44import Unsafe.Coerce (unsafeCoerce)
34import Unsafe.Coerce (unsafeCoerce)
45
35
46foreign import data LocalesOption' :: Type
36foreign import data LocalesOption' :: Type
47−foreign import data DateTimeFormat :: Type
48−
49−foreign import data Undefined :: Type
50−foreign import undefined :: Undefined
51
37
52foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
38foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
53
39
@@ -100,85 +86,6 @@ instance showFormatMatcher :: Show FormatMatcher where
100 show FMBasic = "basic"
86 show FMBasic = "basic"
101 show FMBestFit = "best fit"
87 show FMBestFit = "best fit"
102
88
103−newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
104− { weekday :: StringRep
105− , year :: NumericRep
106− , month :: MonthRep
107− , day :: NumericRep
108− , hour :: NumericRep
109− , minute :: NumericRep
110− , second :: NumericRep
111− }
112−
113−derive instance genericWeekdayYearMonthDayHourMinuteSecond :: Generic WeekdayYearMonthDayHourMinuteSecond _
114−
115−instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
116− formatComponent = genericFormatComponent
117−
118−newtype WeekdayYearMonthDay = WeekdayYearMonthDay
119− { weekday :: StringRep
120− , year :: NumericRep
121− , month :: MonthRep
122− , day :: NumericRep
123− }
124−
125−derive instance genericWeekdayYearMonthDay :: Generic WeekdayYearMonthDay _
126−
127−instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
128− formatComponent = genericFormatComponent
129−
130−newtype YearMonthDay = YearMonthDay
131− { year :: NumericRep
132− , month :: MonthRep
133− , day :: NumericRep
134− }
135−
136−derive instance genericYearMonthDay :: Generic YearMonthDay _
137−
138−instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
139− formatComponent = genericFormatComponent
140−
141−newtype YearMonth = YearMonth
142− { year :: NumericRep
143− , month :: MonthRep
144− }
145−
146−derive instance genericYearMonth :: Generic YearMonth _
147−
148−instance formatComponentYearMonth :: FormatComponent YearMonth where
149− formatComponent = genericFormatComponent
150−
151−newtype MonthDay = MonthDay
152− { month :: MonthRep
153− , day :: NumericRep
154− }
155−
156−derive instance genericMonthDay :: Generic MonthDay _
157−
158−instance formatComponentMonthDay :: FormatComponent MonthDay where
159− formatComponent = genericFormatComponent
160−
161−newtype HourMinuteSecond = HourMinuteSecond
162− { hour :: NumericRep
163− , minute :: NumericRep
164− , second :: NumericRep
165− }
166−
167−derive instance genericHourMinuteSecond :: Generic HourMinuteSecond _
168−
169−instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
170− formatComponent = genericFormatComponent
171−
172−newtype HourMinute = HourMinute
173− { hour :: NumericRep
174− , minute :: NumericRep
175− }
176−
177−derive instance genericHourMinute :: Generic HourMinute _
178−
179−instance formatComponentHourMinute :: FormatComponent HourMinute where
180− formatComponent = genericFormatComponent
181−
182type DateTimeComponents a =
89type DateTimeComponents a =
183 Variant
90 Variant
184 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
91 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
@@ -213,7 +120,7 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
213 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
120 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
214 , formatMatcher: maybe undefinedString show opts.formatMatcher
121 , formatMatcher: maybe undefinedString show opts.formatMatcher
215 }
122 }
216− comps' = default (defaultComponentRecord)
123+ comps' = un FormatComponentRecord $ case_
217 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
124 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
218 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
125 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
219 # on (SProxy :: SProxy "yearMonthDay") formatComponent
126 # on (SProxy :: SProxy "yearMonthDay") formatComponent
@@ -279,30 +186,118 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
279 -> DateTimeFormatOptions_
186 -> DateTimeFormatOptions_
280 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
187 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
281
188
282−foreign import data DateTimeFormatter :: Type
189+foreign import data DateTimeFormat :: Type
283
190
284−foreign import createDateTimeFormatterImpl
191+foreign import createDateTimeFormatImpl
285 :: Fn4
192 :: Fn4
286 (forall a. String -> F a)
193 (forall a. String -> F a)
287 (forall a. a -> F a)
194 (forall a. a -> F a)
288 LocalesOption'
195 LocalesOption'
289 DateTimeFormatOptions_
196 DateTimeFormatOptions_
290− (F DateTimeFormatter)
197+ (F DateTimeFormat)
291−
198+
292−createDateTimeFormatter
199+-- | Examples
200+-- | ``` purescript
201+-- | import Data.Variant (inj)
202+-- |
203+-- | fmtHourMinute = F DateTimeFormat
204+-- | fmtHourMinute = createDateTimeFormat locale opts
205+-- | where
206+-- | locale = inj (SProxy :: SProxy "locale") "en-GB"
207+-- | opts = (DateTimeFormatOptions
208+-- | { localeMatcher: Nothing
209+-- | , timeZone: (Just (TimeZone "Europe/London"))
210+-- | , hour12: Just false
211+-- | , formatMatcher: Nothing
212+-- | }
213+-- | (inj (SProxy :: SProxy "hourMinute")
214+-- | (HourMinute {hour: TwoDigit, minute: TwoDigit}))
215+-- |
216+-- |
217+-- | fmtDate :: F DateTimeFormat
218+-- | fmtDate = createDateTimeFormat locale opts
219+-- | wher
220+-- | locale = inj (SProxy :: SProxy "locale") "en-GB"
221+-- | opts :: DateTimeFormatOptions'
222+-- | opts =
223+-- | (DateTimeFormatOptions
224+-- | { localeMatcher: Nothing
225+-- | , timeZone: (Just (TimeZone "Europe/London"))
226+-- | , hour12: Just true
227+-- | , formatMatcher: Nothing
228+-- | }
229+-- | (inj
230+-- | (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond")
231+-- | (WeekdayYearMonthDayHourMinuteSecond
232+-- | { weekday: Short
233+-- | , year: Numeric
234+-- | , month: MonthLong
235+-- | , day: Numeric
236+-- | , hour: Numeric
237+-- | , minute: Numeric
238+-- | , second: Numeric
239+-- | }
240+-- | )
241+-- | )
242+-- | )
243+-- | ```
244+-- | You have predifined types: `WeekdayYearMonthDayHourMinuteSecond`,
245+-- | `WeekdayYearMonthDay`, `YearMonthDay`, `YearMonth`, `MonthDay`,
246+-- | `HourMinuteSecond`, `HourMinute`. But you can also provide your own by
247+-- | implementing `FormatComponent` class (or by deriving it), e.g.
248+-- | ``` purescript
249+-- | newtype EraYear = EraYear
250+-- | { era :: StringRep
251+-- | , year :: NumericRep
252+-- | }
253+-- |
254+-- | derive instance genericEraYear :: Generic EraYear _
255+-- |
256+-- | instance formatComponentEraYear :: FormatComponent EraYear where
257+-- | formatComponent = genericFormatComponent
258+-- |
259+-- | fmtEraYear :: F DateTimeFormat
260+-- | fmtEraYear = createDateTimeFormat locales opts
261+-- | where
262+-- | locale = inj (SProxy :: SProxy "locale") "en-GB"
263+-- | opts :: DateTimeFormatOptions EraYear
264+-- | opts =
265+-- | (DateTimeFormatOptions
266+-- | { localeMatcher: Nothing
267+-- | , timeZone: (Just (TimeZone "Europe/London"))
268+-- | , hour12: Just true
269+-- | , formatMatcher: Nothing
270+-- | }
271+-- | (inj
272+-- | (SProxy :: SProxy "custom")
273+-- | (EraYear
274+-- | { era: Long
275+-- | , year: Numeric
276+-- | }
277+-- | )
278+-- | )
279+-- | )
280+-- | ```
281+createDateTimeFormat
293 :: forall a
282 :: forall a
294 .(FormatComponent a)
283 .(FormatComponent a)
295 => LocalesOption
284 => LocalesOption
296 -> DateTimeFormatOptions a
285 -> DateTimeFormatOptions a
297− -> F DateTimeFormatter
286+ -> F DateTimeFormat
298−createDateTimeFormatter ls opts =
287+createDateTimeFormat ls opts =
299− runFn4 createDateTimeFormatterImpl
288+ runFn4 createDateTimeFormatImpl
300 (throwError <<< pure <<< ForeignError)
289 (throwError <<< pure <<< ForeignError)
301 pure
290 pure
302 (toLocale ls)
291 (toLocale ls)
303 (formatDateTimeOptions opts)
292 (formatDateTimeOptions opts)
304
293
294+-- | Examples
295+-- | ``` purescript
296+-- | formatJSDate (either (const "") id $ runExcept fmtHourMinute) date -- "12:00"
297+-- | formatJSDate (either (const "") id $ fmtDate) date -- "Tue, January 2, 2018, 12:00:00 PM"
298+-- | formatJSDate (either (const "") id $ fmtEraYear) date -- "2018 Anno Domini"
299+-- | ```
305−foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
300+foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
306
301
307foreign import supportedLocalesOfImpl
302foreign import supportedLocalesOfImpl
308 :: Fn3
303 :: Fn3
@@ -310,18 +305,18 @@ foreign import supportedLocalesOfImpl
310 (Array String -> Either String (Array String))
305 (Array String -> Either String (Array String))
311 (Array String)
306 (Array String)
312 (Either String (Array String))
307 (Either String (Array String))
313−
314−foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
315
308
309+foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
310+
316−formatToParts :: DateTimeFormatter -> JSDate -> F (Array FormatParts)
311+formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
317formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
312formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
318
313
319supportedLocalesOf :: Array String -> Either String (Array String)
314supportedLocalesOf :: Array String -> Either String (Array String)
320supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
315supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
321
316
322−foreign import resolvedOptionsImpl :: DateTimeFormatter -> Foreign
317+foreign import resolvedOptionsImpl :: DateTimeFormat -> Foreign
323
318
324−resolvedOptions :: DateTimeFormatter -> F ResolvedOptions
319+resolvedOptions :: DateTimeFormat -> F ResolvedOptions
325resolvedOptions fmt = do
320resolvedOptions fmt = do
326 locale <- obj ! "locale" >>= readString
321 locale <- obj ! "locale" >>= readString
327 calendar <- obj ! "calendar" >>= readString
322 calendar <- obj ! "calendar" >>= readString
src/Data/Intl/DateTimeFormat/Class.pursnew file+41
@@ -0,0 +1,41 @@
1+module Data.Intl.DateTimeFormat.Class where
2+
3+import Data.Maybe (Maybe(..))
4+import Data.Newtype (class Newtype)
5+import Prelude (class Eq, Unit)
6+
7+newtype FormatComponentRecord = FormatComponentRecord
8+ { era :: Maybe String
9+ , weekday :: Maybe String
10+ , year :: Maybe String
11+ , month :: Maybe String
12+ , day :: Maybe String
13+ , hour :: Maybe String
14+ , minute :: Maybe String
15+ , second :: Maybe String
16+ , timeZoneName :: Maybe String
17+ }
18+
19+derive instance newtypeFormatComponentRecord :: Newtype FormatComponentRecord _
20+
21+derive instance eqFormatComponentRecord :: Eq FormatComponentRecord
22+
23+defaultComponentRecord :: FormatComponentRecord
24+defaultComponentRecord = FormatComponentRecord
25+ { era: Nothing
26+ , weekday: Nothing
27+ , year: Nothing
28+ , month: Nothing
29+ , day: Nothing
30+ , hour: Nothing
31+ , minute: Nothing
32+ , second: Nothing
33+ , timeZoneName: Nothing
34+ }
35+
36+
37+class FormatComponent a where
38+ formatComponent :: a -> FormatComponentRecord
39+
40+instance unitFormatComponent :: FormatComponent Unit where
41+ formatComponent _ = defaultComponentRecord
src/Data/Intl/DateTimeFormat/Generic.purs+5−39
@@ -1,51 +1,17 @@
1module Data.Intl.DateTimeFormat.Generic
1module Data.Intl.DateTimeFormat.Generic
2− ( class FormatComponent
3− , formatComponent
4− , class GenericFormatComponent
2+ ( class GenericFormatComponent
5 , genericFormatComponent'
3 , genericFormatComponent'
6 , genericFormatComponent
4 , genericFormatComponent
7 , class GenericFormatComponentFields
5 , class GenericFormatComponentFields
8 , genericFormatComponentFields
6 , genericFormatComponentFields
9− , defaultComponentRecord
10− , FormatComponentRecord
11 ) where
7 ) where
12
8
13import 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(..))
14import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
11import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
15import Data.Maybe (Maybe(..))
12import Data.Maybe (Maybe(..))
16import Data.StrMap (StrMap, lookup, singleton, union)
13import Data.StrMap (StrMap, lookup, singleton, union)
17−import Prelude (Unit, show)
14+import Prelude (show)
18−
19−type 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− }
30−
31−defaultComponentRecord :: FormatComponentRecord
32−defaultComponentRecord =
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− }
43−
44−class FormatComponent a where
45− formatComponent :: a -> FormatComponentRecord
46−
47−instance unitFormatComponent :: FormatComponent Unit where
48− formatComponent _ = defaultComponentRecord
49
15
50class GenericFormatComponent a where
16class GenericFormatComponent a where
51 genericFormatComponent' :: a -> FormatComponentRecord
17 genericFormatComponent' :: a -> FormatComponentRecord
@@ -54,7 +20,7 @@ class GenericFormatComponentFields a where
54 genericFormatComponentFields :: a -> StrMap String
20 genericFormatComponentFields :: a -> StrMap String
55
21
56instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
22instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
57− genericFormatComponent' a =
23+ genericFormatComponent' a = FormatComponentRecord
58 { era: Nothing
24 { era: Nothing
59 , weekday: Nothing
25 , weekday: Nothing
60 , year: Nothing
26 , year: Nothing
@@ -114,7 +80,7 @@ instance genericFormatComponentFieldsProduct
114instance genericFormatComponentRec
80instance genericFormatComponentRec
115 :: (GenericFormatComponentFields a)
81 :: (GenericFormatComponentFields a)
116 => GenericFormatComponent (Rec a) where
82 => GenericFormatComponent (Rec a) where
117− genericFormatComponent' (Rec a) =
83+ genericFormatComponent' (Rec a) = FormatComponentRecord
118 { era: "era" `lookup` u
84 { era: "era" `lookup` u
119 , weekday: "weekday" `lookup` u
85 , weekday: "weekday" `lookup` u
120 , year: "year" `lookup` u
86 , year: "year" `lookup` u
src/Data/Intl/DateTimeFormat/Types.jsnew file+3
@@ -0,0 +1,3 @@
1+"use strict";
2+
3+exports["undefined"] = undefined;
src/Data/Intl/DateTimeFormat/Types.purs+161−3
@@ -6,16 +6,32 @@ module Data.Intl.DateTimeFormat.Types
6 , FormatParts(..)
6 , FormatParts(..)
7 , readFormatParts
7 , readFormatParts
8 , ResolvedOptions(..)
8 , ResolvedOptions(..)
9+ , WeekdayYearMonthDayHourMinuteSecond(..)
10+ , WeekdayYearMonthDay(..)
11+ , YearMonthDay(..)
12+ , YearMonth(..)
13+ , MonthDay(..)
14+ , HourMinuteSecond(..)
15+ , HourMinute(..)
16+ , Undefined
17+ , undefined
9 ) where
18 ) where
10
19
11import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
20import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
12import Data.Foreign.Index ((!))
21import Data.Foreign.Index ((!))
22+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..), defaultComponentRecord)
13−import Data.Maybe (Maybe)
23+import Data.Maybe (Maybe(..))
14−import Data.Newtype (class Newtype)
24+import Data.Newtype (class Newtype, over)
15−import Prelude (class Show, bind, pure, ($), (<>), (>>=))
25+import Prelude (class Eq, class Show, bind, pure, show, ($), (<>), (>>=))
26+
27+foreign import data Undefined :: Type
28+
29+foreign import undefined :: Undefined
16
30
17data StringRep = Narrow | Short | Long
31data StringRep = Narrow | Short | Long
18
32
33+derive instance eqStringRep :: Eq StringRep
34+
19instance showStringRep :: Show StringRep where
35instance showStringRep :: Show StringRep where
20 show Narrow = "narrow"
36 show Narrow = "narrow"
21 show Short = "short"
37 show Short = "short"
@@ -23,12 +39,16 @@ instance showStringRep :: Show StringRep where
23
39
24data NumericRep = Numeric | TwoDigit
40data NumericRep = Numeric | TwoDigit
25
41
42+derive instance eqNumericRep :: Eq NumericRep
43+
26instance showNumericRep :: Show NumericRep where
44instance showNumericRep :: Show NumericRep where
27 show Numeric = "numeric"
45 show Numeric = "numeric"
28 show TwoDigit = "2-digit"
46 show TwoDigit = "2-digit"
29
47
30data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
48data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
31
49
50+derive instance eqMonthRep :: Eq MonthRep
51+
32instance showMonthRep :: Show MonthRep where
52instance showMonthRep :: Show MonthRep where
33 show MonthNumeric = "numeric"
53 show MonthNumeric = "numeric"
34 show MonthTwoDigit = "2-digit"
54 show MonthTwoDigit = "2-digit"
@@ -38,6 +58,8 @@ instance showMonthRep :: Show MonthRep where
38
58
39data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
59data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
40
60
61+derive instance eqTimeZoneNameRep :: Eq TimeZoneNameRep
62+
41instance showTimeZoneRep :: Show TimeZoneNameRep where
63instance showTimeZoneRep :: Show TimeZoneNameRep where
42 show TimeZoneNameShort = "short"
64 show TimeZoneNameShort = "short"
43 show TimeZoneNameLong = "long"
65 show TimeZoneNameLong = "long"
@@ -53,6 +75,8 @@ data FormatParts
53 | DayPeriod String
75 | DayPeriod String
54 | Literal String
76 | Literal String
55
77
78+derive instance eqFormatParts :: Eq FormatParts
79+
56readFormatParts :: Foreign -> F FormatParts
80readFormatParts :: Foreign -> F FormatParts
57readFormatParts val = do
81readFormatParts val = do
58 type_ <- val ! "type" >>= readString
82 type_ <- val ! "type" >>= readString
@@ -86,3 +110,137 @@ newtype ResolvedOptions = ResolvedOptions
86 }
110 }
87
111
88derive instance newtypeResolvedOptions :: Newtype ResolvedOptions _
112derive instance newtypeResolvedOptions :: Newtype ResolvedOptions _
113+
114+derive instance eqResolvedOptions :: Eq ResolvedOptions
115+
116+newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
117+ { weekday :: StringRep
118+ , year :: NumericRep
119+ , month :: MonthRep
120+ , day :: NumericRep
121+ , hour :: NumericRep
122+ , minute :: NumericRep
123+ , second :: NumericRep
124+ }
125+
126+derive instance newtypeWeekdayYearMonthDayHourMinuteSecond :: Newtype WeekdayYearMonthDayHourMinuteSecond _
127+
128+derive instance eqWeekdayYearMonthDayHourMinuteSecond :: Eq WeekdayYearMonthDayHourMinuteSecond
129+
130+instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
131+ formatComponent (WeekdayYearMonthDayHourMinuteSecond { weekday, year, month, day, hour, minute, second }) = over FormatComponentRecord
132+ (\r -> r { weekday = Just (show weekday)
133+ , year = Just (show year)
134+ , month = Just (show month)
135+ , day = Just (show day)
136+ , hour = Just (show hour)
137+ , minute = Just (show minute)
138+ , second = Just (show second)
139+ }
140+ ) defaultComponentRecord
141+
142+newtype WeekdayYearMonthDay = WeekdayYearMonthDay
143+ { weekday :: StringRep
144+ , year :: NumericRep
145+ , month :: MonthRep
146+ , day :: NumericRep
147+ }
148+
149+derive instance newtypeWeekdayYearMonthDay :: Newtype WeekdayYearMonthDay _
150+
151+derive instance eqWeekdayYearMonthDay :: Eq WeekdayYearMonthDay
152+
153+instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
154+ formatComponent (WeekdayYearMonthDay { weekday, year, month, day }) = over FormatComponentRecord
155+ (\r -> r { weekday = Just (show weekday)
156+ , year = Just (show year)
157+ , month = Just (show month)
158+ , day = Just (show day)
159+ }
160+ ) defaultComponentRecord
161+
162+
163+newtype YearMonthDay = YearMonthDay
164+ { year :: NumericRep
165+ , month :: MonthRep
166+ , day :: NumericRep
167+ }
168+
169+derive instance newtypeYearMonthDay :: Newtype YearMonthDay _
170+
171+derive instance eqYearMonthDay :: Eq YearMonthDay
172+
173+instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
174+ formatComponent (YearMonthDay { year, month, day }) = over FormatComponentRecord
175+ (\r -> r { year = Just (show year)
176+ , month = Just (show month)
177+ , day = Just (show day)
178+ }
179+ ) defaultComponentRecord
180+
181+
182+newtype YearMonth = YearMonth
183+ { year :: NumericRep
184+ , month :: MonthRep
185+ }
186+
187+derive instance newtypeYearMonth :: Newtype YearMonth _
188+
189+derive instance eqYearMonth :: Eq YearMonth
190+
191+instance formatComponentYearMonth :: FormatComponent YearMonth where
192+ formatComponent (YearMonth { year, month }) = over FormatComponentRecord
193+ (\r -> r { year = Just (show year)
194+ , month = Just (show month)
195+ }
196+ ) defaultComponentRecord
197+
198+newtype MonthDay = MonthDay
199+ { month :: MonthRep
200+ , day :: NumericRep
201+ }
202+
203+derive instance newtypeMonthDay :: Newtype MonthDay _
204+
205+derive instance eqMonthDay :: Eq MonthDay
206+
207+instance formatComponentMonthDay :: FormatComponent MonthDay where
208+ formatComponent (MonthDay { month, day }) = over FormatComponentRecord
209+ (\r -> r { month = Just (show month)
210+ , day = Just (show day)
211+ }
212+ ) defaultComponentRecord
213+
214+newtype HourMinuteSecond = HourMinuteSecond
215+ { hour :: NumericRep
216+ , minute :: NumericRep
217+ , second :: NumericRep
218+ }
219+
220+derive instance newtypeHourMinuteSecond :: Newtype HourMinuteSecond _
221+
222+derive instance eqHourMinuteSecond :: Eq HourMinuteSecond
223+
224+instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
225+ formatComponent (HourMinuteSecond { hour, minute, second }) = over FormatComponentRecord
226+ (\r -> r { hour = Just (show hour)
227+ , minute = Just (show minute)
228+ , second = Just (show second)
229+ }
230+ ) defaultComponentRecord
231+
232+newtype HourMinute = HourMinute
233+ { hour :: NumericRep
234+ , minute :: NumericRep
235+ }
236+
237+derive instance newtypeHourMinute :: Newtype HourMinute _
238+
239+derive instance eqHourMinute :: Eq HourMinute
240+
241+instance formatComponentHourMinute :: FormatComponent HourMinute where
242+ formatComponent (HourMinute { hour, minute }) = over FormatComponentRecord
243+ (\r -> r { hour = Just (show hour)
244+ , minute = Just (show minute)
245+ }
246+ ) defaultComponentRecord
test/Test/Main.purs+14−13
@@ -11,8 +11,9 @@ import Data.Enum (toEnum)
11import Data.Foldable (intercalate)
11import 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)
14−import 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), createDateTimeFormatter, formatJSDate, resolvedOptions, supportedLocalesOf)
14+import 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
@@ -59,7 +60,7 @@ main = do
59 locales = inj (SProxy :: SProxy "locale") "en-US"
60 locales = inj (SProxy :: SProxy "locale") "en-US"
60
61
61 fmtError =
62 fmtError =
62− createDateTimeFormatter (inj (SProxy :: SProxy "locale") "") opts
63+ createDateTimeFormat (inj (SProxy :: SProxy "locale") "") opts
63 where
64 where
64 opts :: DateTimeFormatOptions'
65 opts :: DateTimeFormatOptions'
65 opts =
66 opts =
@@ -73,7 +74,7 @@ main = do
73 )
74 )
74
75
75 fmtHourMinute = unsafeFromF $
76 fmtHourMinute = unsafeFromF $
76− createDateTimeFormatter locales opts
77+ createDateTimeFormat locales opts
77 where
78 where
78 opts :: DateTimeFormatOptions'
79 opts :: DateTimeFormatOptions'
79 opts =
80 opts =
@@ -87,7 +88,7 @@ main = do
87 )
88 )
88
89
89 fmtHourMinuteSecond = unsafeFromF $
90 fmtHourMinuteSecond = unsafeFromF $
90− createDateTimeFormatter locales opts
91+ createDateTimeFormat locales opts
91 where
92 where
92 opts :: DateTimeFormatOptions'
93 opts :: DateTimeFormatOptions'
93 opts =
94 opts =
@@ -101,7 +102,7 @@ main = do
101 )
102 )
102
103
103 fmtWeekdayYearMonthDayHourMinuteSecond = unsafeFromF $
104 fmtWeekdayYearMonthDayHourMinuteSecond = unsafeFromF $
104− createDateTimeFormatter locales opts
105+ createDateTimeFormat locales opts
105 where
106 where
106 opts :: DateTimeFormatOptions'
107 opts :: DateTimeFormatOptions'
107 opts =
108 opts =
@@ -127,7 +128,7 @@ main = do
127 )
128 )
128
129
129 fmtWeekdayYearMonthDay = unsafeFromF $
130 fmtWeekdayYearMonthDay = unsafeFromF $
130− createDateTimeFormatter locales opts
131+ createDateTimeFormat locales opts
131 where
132 where
132 opts :: DateTimeFormatOptions'
133 opts :: DateTimeFormatOptions'
133 opts =
134 opts =
@@ -150,7 +151,7 @@ main = do
150 )
151 )
151
152
152 fmtYearMonthDay = unsafeFromF $
153 fmtYearMonthDay = unsafeFromF $
153− createDateTimeFormatter locales opts
154+ createDateTimeFormat locales opts
154 where
155 where
155 opts :: DateTimeFormatOptions'
156 opts :: DateTimeFormatOptions'
156 opts =
157 opts =
@@ -172,7 +173,7 @@ main = do
172 )
173 )
173
174
174 fmtYearMonth = unsafeFromF $
175 fmtYearMonth = unsafeFromF $
175− createDateTimeFormatter locales opts
176+ createDateTimeFormat locales opts
176 where
177 where
177 opts :: DateTimeFormatOptions'
178 opts :: DateTimeFormatOptions'
178 opts =
179 opts =
@@ -193,7 +194,7 @@ main = do
193 )
194 )
194
195
195 fmtMonthDay = unsafeFromF $
196 fmtMonthDay = unsafeFromF $
196− createDateTimeFormatter locales opts
197+ createDateTimeFormat locales opts
197 where
198 where
198 opts :: DateTimeFormatOptions'
199 opts :: DateTimeFormatOptions'
199 opts =
200 opts =
@@ -214,7 +215,7 @@ main = do
214 )
215 )
215
216
216 fmtEraYear = unsafeFromF $
217 fmtEraYear = unsafeFromF $
217− createDateTimeFormatter locales opts
218+ createDateTimeFormat locales opts
218 where
219 where
219 opts :: DateTimeFormatOptions EraYear
220 opts :: DateTimeFormatOptions EraYear
220 opts =
221 opts =
@@ -235,7 +236,7 @@ main = do
235 )
236 )
236
237
237 fmtEra = unsafeFromF $
238 fmtEra = unsafeFromF $
238− createDateTimeFormatter locales opts
239+ createDateTimeFormat locales opts
239 where
240 where
240 opts :: DateTimeFormatOptions Era
241 opts :: DateTimeFormatOptions Era
241 opts =
242 opts =