@@ -1,13 +1,22 @@
1−module Data.Intl.DateTimeFormat
1+module Data.Intl.DateTimeFormat
22 ( LocalesOption
33 , LocaleMatcher(..)
44 , TimeZone(..)
55 , FormatMatcher(..)
6+ , WeekdayYearMonthDayHourMinuteSecond(..)
7+ , WeekdayYearMonthDay(..)
8+ , YearMonthDay(..)
9+ , YearMonth(..)
10+ , MonthDay(..)
11+ , HourMinuteSecond(..)
12+ , HourMinute(..)
13+ , Undefined
14+ , undefined
615 , DateTimeFormatOptions(..)
716 , DateTimeFormatOptions'
817 , DateTimeComponents(..)
9− , DateTimeFormat
10− , createDateTimeFormat
18+ , DateTimeFormatter
19+ , createDateTimeFormatter
1120 , formatJSDate
1221 , module Data.Intl.DateTimeFormat.Types
1322 , supportedLocalesOf
@@ -22,18 +31,23 @@ import Data.Either (Either(..))
2231import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
2332import Data.Foreign.Index ((!))
2433import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
25−import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
34+import Data.Generic.Rep (class Generic)
35+import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
2636import Data.JSDate (JSDate)
2737import Data.Maybe (Maybe(..), fromMaybe, maybe)
28−import Data.Newtype (class Newtype, un)
38+import Data.Newtype (class Newtype)
2939import Data.StrMap (union)
3040import Data.Symbol (SProxy(..))
3141import Data.Traversable (sequence)
32−import Data.Variant (Variant, case_, default, on)
42+import Data.Variant (Variant, default, on)
3343import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
3444import Unsafe.Coerce (unsafeCoerce)
3545
3646foreign import data LocalesOption' :: Type
47+foreign import data DateTimeFormat :: Type
48+
49+foreign import data Undefined :: Type
50+foreign import undefined :: Undefined
3751
3852foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
3953
@@ -86,6 +100,85 @@ instance showFormatMatcher :: Show FormatMatcher where
86100 show FMBasic = "basic"
87101 show FMBestFit = "best fit"
88102
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+
89182type DateTimeComponents a =
90183 Variant
91184 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
@@ -120,7 +213,7 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
120213 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
121214 , formatMatcher: maybe undefinedString show opts.formatMatcher
122215 }
123− comps' = un FormatComponentRecord $ case_
216+ comps' = default (defaultComponentRecord)
124217 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
125218 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
126219 # on (SProxy :: SProxy "yearMonthDay") formatComponent
@@ -186,118 +279,30 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
186279 -> DateTimeFormatOptions_
187280 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
188281
189−foreign import data DateTimeFormat :: Type
282+foreign import data DateTimeFormatter :: Type
190283
191−foreign import createDateTimeFormatImpl
284+foreign import createDateTimeFormatterImpl
192285 :: Fn4
193286 (forall a. String -> F a)
194287 (forall a. a -> F a)
195288 LocalesOption'
196289 DateTimeFormatOptions_
197− (F DateTimeFormat)
198−
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
290+ (F DateTimeFormatter)
291+
292+createDateTimeFormatter
282293 :: forall a
283294 .(FormatComponent a)
284295 => LocalesOption
285296 -> DateTimeFormatOptions a
286− -> F DateTimeFormat
287−createDateTimeFormat ls opts =
288− runFn4 createDateTimeFormatImpl
297+ -> F DateTimeFormatter
298+createDateTimeFormatter ls opts =
299+ runFn4 createDateTimeFormatterImpl
289300 (throwError <<< pure <<< ForeignError)
290301 pure
291302 (toLocale ls)
292303 (formatDateTimeOptions opts)
293304
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−-- | ```
300−foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
305+foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
301306
302307foreign import supportedLocalesOfImpl
303308 :: Fn3
@@ -305,18 +310,18 @@ foreign import supportedLocalesOfImpl
305310 (Array String -> Either String (Array String))
306311 (Array String)
307312 (Either String (Array String))
313+
314+foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
308315
309−foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
310−
311−formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
316+formatToParts :: DateTimeFormatter -> JSDate -> F (Array FormatParts)
312317formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
313318
314319supportedLocalesOf :: Array String -> Either String (Array String)
315320supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
316321
317−foreign import resolvedOptionsImpl :: DateTimeFormat -> Foreign
322+foreign import resolvedOptionsImpl :: DateTimeFormatter -> Foreign
318323
319−resolvedOptions :: DateTimeFormat -> F ResolvedOptions
324+resolvedOptions :: DateTimeFormatter -> F ResolvedOptions
320325resolvedOptions fmt = do
321326 locale <- obj ! "locale" >>= readString
322327 calendar <- obj ! "calendar" >>= readString