git.iko.soy/purescript-intl
on 986e20e help sign in
6 files changed+37−192
since splitsnapshots
unifiedsplit
src/Data/Intl/DateTimeFormat.js+2 src/Data/Intl/DateTimeFormat.purs+7−94 src/Data/Intl/DateTimeFormat/Class.purs+3−8 src/Data/Intl/DateTimeFormat/Generic.purs+3−3 src/Data/Intl/DateTimeFormat/Types.purs+20−84 test/Test/Main.purs+2−3
src/Data/Intl/DateTimeFormat.js+2
@@ -1,5 +1,7 @@
11"use strict";
22
3+exports["undefined"] = undefined;
4+
35exports.dateTimeFormatImpl = function(locales) {
46 return function(options) {
57 return new Intl.DateTimeFormat(locales, options);
src/Data/Intl/DateTimeFormat.purs+7−94
@@ -1,4 +1,4 @@
1−module Data.Intl.DateTimeFormat
1+module Data.Intl.DateTimeFormat
22 ( LocalesOption
33 , LocaleMatcher(..)
44 , TimeZone(..)
@@ -22,14 +22,15 @@ import Data.Either (Either(..))
2222import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
2323import Data.Foreign.Index ((!))
2424import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
25−import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
25+import Data.Generic.Rep (class Generic)
26+import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
2627import Data.JSDate (JSDate)
2728import Data.Maybe (Maybe(..), fromMaybe, maybe)
28−import Data.Newtype (class Newtype, un)
29+import Data.Newtype (class Newtype)
2930import Data.StrMap (union)
3031import Data.Symbol (SProxy(..))
3132import Data.Traversable (sequence)
32−import Data.Variant (Variant, case_, default, on)
33+import Data.Variant (Variant, default, on)
3334import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
3435import Unsafe.Coerce (unsafeCoerce)
3536
@@ -120,7 +121,7 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
120121 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
121122 , formatMatcher: maybe undefinedString show opts.formatMatcher
122123 }
123− comps' = un FormatComponentRecord $ case_
124+ comps' = default (defaultComponentRecord)
124125 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
125126 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
126127 # on (SProxy :: SProxy "yearMonthDay") formatComponent
@@ -196,88 +197,6 @@ foreign import createDateTimeFormatImpl
196197 DateTimeFormatOptions_
197198 (F DateTimeFormat)
198199
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−-- | ```
281200createDateTimeFormat
282201 :: forall a
283202 .(FormatComponent a)
@@ -291,12 +210,6 @@ createDateTimeFormat ls opts =
291210 (toLocale ls)
292211 (formatDateTimeOptions opts)
293212
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−-- | ```
300213foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
301214
302215foreign import supportedLocalesOfImpl
@@ -305,7 +218,7 @@ foreign import supportedLocalesOfImpl
305218 (Array String -> Either String (Array String))
306219 (Array String)
307220 (Either String (Array String))
308−
221+
309222foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
310223
311224formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
src/Data/Intl/DateTimeFormat/Class.purs+3−8
@@ -1,10 +1,9 @@
11module Data.Intl.DateTimeFormat.Class where
22
33import Data.Maybe (Maybe(..))
4−import Data.Newtype (class Newtype)
5−import Prelude (class Eq, Unit)
4+import Prelude (Unit)
65
7−newtype FormatComponentRecord = FormatComponentRecord
6+type FormatComponentRecord =
87 { era :: Maybe String
98 , weekday :: Maybe String
109 , year :: Maybe String
@@ -16,12 +15,8 @@ newtype FormatComponentRecord = FormatComponentRecord
1615 , timeZoneName :: Maybe String
1716 }
1817
19−derive instance newtypeFormatComponentRecord :: Newtype FormatComponentRecord _
20−
21−derive instance eqFormatComponentRecord :: Eq FormatComponentRecord
22−
2318defaultComponentRecord :: FormatComponentRecord
24−defaultComponentRecord = FormatComponentRecord
19+defaultComponentRecord =
2520 { era: Nothing
2621 , weekday: Nothing
2722 , year: Nothing
src/Data/Intl/DateTimeFormat/Generic.purs+3−3
@@ -7,7 +7,7 @@ module Data.Intl.DateTimeFormat.Generic
77 ) where
88
99import 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)
1111import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
1212import Data.Maybe (Maybe(..))
1313import Data.StrMap (StrMap, lookup, singleton, union)
@@ -20,7 +20,7 @@ class GenericFormatComponentFields a where
2020 genericFormatComponentFields :: a -> StrMap String
2121
2222instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
23− genericFormatComponent' a = FormatComponentRecord
23+ genericFormatComponent' a =
2424 { era: Nothing
2525 , weekday: Nothing
2626 , year: Nothing
@@ -80,7 +80,7 @@ instance genericFormatComponentFieldsProduct
8080instance genericFormatComponentRec
8181 :: (GenericFormatComponentFields a)
8282 => GenericFormatComponent (Rec a) where
83− genericFormatComponent' (Rec a) = FormatComponentRecord
83+ genericFormatComponent' (Rec a) =
8484 { era: "era" `lookup` u
8585 , weekday: "weekday" `lookup` u
8686 , year: "year" `lookup` u
src/Data/Intl/DateTimeFormat/Types.purs+20−84
@@ -19,19 +19,18 @@ module Data.Intl.DateTimeFormat.Types
1919
2020import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
2121import Data.Foreign.Index ((!))
22−import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..), defaultComponentRecord)
23−import Data.Maybe (Maybe(..))
24−import Data.Newtype (class Newtype, over)
25−import Prelude (class Eq, class Show, bind, pure, show, ($), (<>), (>>=))
22+import Data.Generic (class Generic)
23+import Data.Intl.DateTimeFormat.Class (class FormatComponent)
24+import Data.Intl.DateTimeFormat.Generic (genericFormatComponent)
25+import Data.Maybe (Maybe)
26+import Data.Newtype (class Newtype)
27+import Prelude (class Show, bind, pure, ($), (<>), (>>=))
2628
2729foreign import data Undefined :: Type
28−
2930foreign import undefined :: Undefined
3031
3132data StringRep = Narrow | Short | Long
3233
33−derive instance eqStringRep :: Eq StringRep
34−
3534instance showStringRep :: Show StringRep where
3635 show Narrow = "narrow"
3736 show Short = "short"
@@ -39,16 +38,12 @@ instance showStringRep :: Show StringRep where
3938
4039data NumericRep = Numeric | TwoDigit
4140
42−derive instance eqNumericRep :: Eq NumericRep
43−
4441instance showNumericRep :: Show NumericRep where
4542 show Numeric = "numeric"
4643 show TwoDigit = "2-digit"
4744
4845data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
4946
50−derive instance eqMonthRep :: Eq MonthRep
51−
5247instance showMonthRep :: Show MonthRep where
5348 show MonthNumeric = "numeric"
5449 show MonthTwoDigit = "2-digit"
@@ -58,8 +53,6 @@ instance showMonthRep :: Show MonthRep where
5853
5954data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
6055
61−derive instance eqTimeZoneNameRep :: Eq TimeZoneNameRep
62−
6356instance showTimeZoneRep :: Show TimeZoneNameRep where
6457 show TimeZoneNameShort = "short"
6558 show TimeZoneNameLong = "long"
@@ -75,8 +68,6 @@ data FormatParts
7568 | DayPeriod String
7669 | Literal String
7770
78−derive instance eqFormatParts :: Eq FormatParts
79−
8071readFormatParts :: Foreign -> F FormatParts
8172readFormatParts val = do
8273 type_ <- val ! "type" >>= readString
@@ -111,8 +102,6 @@ newtype ResolvedOptions = ResolvedOptions
111102
112103derive instance newtypeResolvedOptions :: Newtype ResolvedOptions _
113104
114−derive instance eqResolvedOptions :: Eq ResolvedOptions
115−
116105newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
117106 { weekday :: StringRep
118107 , year :: NumericRep
@@ -123,21 +112,10 @@ newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecon
123112 , second :: NumericRep
124113 }
125114
126−derive instance newtypeWeekdayYearMonthDayHourMinuteSecond :: Newtype WeekdayYearMonthDayHourMinuteSecond _
127−
128−derive instance eqWeekdayYearMonthDayHourMinuteSecond :: Eq WeekdayYearMonthDayHourMinuteSecond
115+derive instance genericWeekdayYearMonthDayHourMinuteSecond :: Generic WeekdayYearMonthDayHourMinuteSecond _
129116
130117instance 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
118+ formatComponent = genericFormatComponent
141119
142120newtype WeekdayYearMonthDay = WeekdayYearMonthDay
143121 { weekday :: StringRep
@@ -146,19 +124,10 @@ newtype WeekdayYearMonthDay = WeekdayYearMonthDay
146124 , day :: NumericRep
147125 }
148126
149−derive instance newtypeWeekdayYearMonthDay :: Newtype WeekdayYearMonthDay _
150−
151−derive instance eqWeekdayYearMonthDay :: Eq WeekdayYearMonthDay
127+derive instance genericWeekdayYearMonthDay :: Generic WeekdayYearMonthDay _
152128
153129instance 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−
130+ formatComponent = genericFormatComponent
162131
163132newtype YearMonthDay = YearMonthDay
164133 { year :: NumericRep
@@ -166,50 +135,30 @@ newtype YearMonthDay = YearMonthDay
166135 , day :: NumericRep
167136 }
168137
169−derive instance newtypeYearMonthDay :: Newtype YearMonthDay _
170−
171−derive instance eqYearMonthDay :: Eq YearMonthDay
138+derive instance genericYearMonthDay :: Generic YearMonthDay _
172139
173140instance 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−
141+ formatComponent = genericFormatComponent
181142
182143newtype YearMonth = YearMonth
183144 { year :: NumericRep
184145 , month :: MonthRep
185146 }
186147
187−derive instance newtypeYearMonth :: Newtype YearMonth _
188−
189−derive instance eqYearMonth :: Eq YearMonth
148+derive instance genericYearMonth :: Generic YearMonth _
190149
191150instance 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
151+ formatComponent = genericFormatComponent
197152
198153newtype MonthDay = MonthDay
199154 { month :: MonthRep
200155 , day :: NumericRep
201156 }
202157
203−derive instance newtypeMonthDay :: Newtype MonthDay _
204−
205−derive instance eqMonthDay :: Eq MonthDay
158+derive instance genericMonthDay :: Generic MonthDay _
206159
207160instance 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
161+ formatComponent = genericFormatComponent
213162
214163newtype HourMinuteSecond = HourMinuteSecond
215164 { hour :: NumericRep
@@ -217,30 +166,17 @@ newtype HourMinuteSecond = HourMinuteSecond
217166 , second :: NumericRep
218167 }
219168
220−derive instance newtypeHourMinuteSecond :: Newtype HourMinuteSecond _
221−
222−derive instance eqHourMinuteSecond :: Eq HourMinuteSecond
169+derive instance genericHourMinuteSecond :: Generic HourMinuteSecond _
223170
224171instance 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
172+ formatComponent = genericFormatComponent
231173
232174newtype HourMinute = HourMinute
233175 { hour :: NumericRep
234176 , minute :: NumericRep
235177 }
236178
237−derive instance newtypeHourMinute :: Newtype HourMinute _
238−
239−derive instance eqHourMinute :: Eq HourMinute
179+derive instance genericHourMinute :: Generic HourMinute _
240180
241181instance 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
182+ formatComponent = genericFormatComponent
test/Test/Main.purs+2−3
@@ -12,8 +12,7 @@ import Data.Foldable (intercalate)
1212import Data.Foreign (F, renderForeignError)
1313import Data.Generic.Rep (class Generic)
1414import 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.Class (class FormatComponent, FormatComponentRecord(..))
16−import Data.Intl.DateTimeFormat.Generic (genericFormatComponent)
15+import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
1716import Data.JSDate (JSDate, fromDateTime)
1817import Data.Maybe (Maybe(..), isJust, isNothing)
1918import Data.Symbol (SProxy(..))
@@ -37,7 +36,7 @@ instance formatComponentEraYear :: FormatComponent EraYear where
3736newtype Era = Era StringRep
3837
3938instance formatComponentEra :: FormatComponent Era where
40− formatComponent (Era r) = FormatComponentRecord
39+ formatComponent (Era r) =
4140 { era: Just (show r)
4241 , weekday: Nothing
4342 , year: Nothing