@@ -1,22 +1,13 @@
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
156 , DateTimeFormatOptions(..)
167 , DateTimeFormatOptions'
178 , DateTimeComponents(..)
18− , DateTimeFormatter
19− , createDateTimeFormatter
9+ , DateTimeFormat
10+ , createDateTimeFormat
2011 , formatJSDate
2112 , module Data.Intl.DateTimeFormat.Types
2213 , supportedLocalesOf
@@ -26,27 +17,23 @@ module Data.Intl.DateTimeFormat
2617
2718import Data.Intl.DateTimeFormat.Types
2819
20+import Control.Monad.Error.Class (throwError)
2921import Data.Either (Either(..))
30−import Data.Foreign (F, Foreign, isUndefined, readString)
22+import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
3123import Data.Foreign.Index ((!))
32−import Data.Function.Uncurried (Fn3, runFn3)
33−import Data.Generic.Rep (class Generic)
34−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
24+import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
25+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
3526import Data.JSDate (JSDate)
3627import Data.Maybe (Maybe(..), fromMaybe, maybe)
37−import Data.Newtype (class Newtype)
28+import Data.Newtype (class Newtype, un)
3829import Data.StrMap (union)
3930import Data.Symbol (SProxy(..))
4031import Data.Traversable (sequence)
41−import Data.Variant (Variant, default, on)
42−import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (>>=))
32+import Data.Variant (Variant, case_, default, on)
33+import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
4334import Unsafe.Coerce (unsafeCoerce)
4435
4536foreign import data LocalesOption' :: Type
46−foreign import data DateTimeFormat :: Type
47−
48−foreign import data Undefined :: Type
49−foreign import undefined :: Undefined
5037
5138foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
5239
@@ -99,85 +86,6 @@ instance showFormatMatcher :: Show FormatMatcher where
9986 show FMBasic = "basic"
10087 show FMBestFit = "best fit"
10188
102−newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
103− { weekday :: StringRep
104− , year :: NumericRep
105− , month :: MonthRep
106− , day :: NumericRep
107− , hour :: NumericRep
108− , minute :: NumericRep
109− , second :: NumericRep
110− }
111−
112−derive instance genericWeekdayYearMonthDayHourMinuteSecond :: Generic WeekdayYearMonthDayHourMinuteSecond _
113−
114−instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
115− formatComponent = genericFormatComponent
116−
117−newtype WeekdayYearMonthDay = WeekdayYearMonthDay
118− { weekday :: StringRep
119− , year :: NumericRep
120− , month :: MonthRep
121− , day :: NumericRep
122− }
123−
124−derive instance genericWeekdayYearMonthDay :: Generic WeekdayYearMonthDay _
125−
126−instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
127− formatComponent = genericFormatComponent
128−
129−newtype YearMonthDay = YearMonthDay
130− { year :: NumericRep
131− , month :: MonthRep
132− , day :: NumericRep
133− }
134−
135−derive instance genericYearMonthDay :: Generic YearMonthDay _
136−
137−instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
138− formatComponent = genericFormatComponent
139−
140−newtype YearMonth = YearMonth
141− { year :: NumericRep
142− , month :: MonthRep
143− }
144−
145−derive instance genericYearMonth :: Generic YearMonth _
146−
147−instance formatComponentYearMonth :: FormatComponent YearMonth where
148− formatComponent = genericFormatComponent
149−
150−newtype MonthDay = MonthDay
151− { month :: MonthRep
152− , day :: NumericRep
153− }
154−
155−derive instance genericMonthDay :: Generic MonthDay _
156−
157−instance formatComponentMonthDay :: FormatComponent MonthDay where
158− formatComponent = genericFormatComponent
159−
160−newtype HourMinuteSecond = HourMinuteSecond
161− { hour :: NumericRep
162− , minute :: NumericRep
163− , second :: NumericRep
164− }
165−
166−derive instance genericHourMinuteSecond :: Generic HourMinuteSecond _
167−
168−instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
169− formatComponent = genericFormatComponent
170−
171−newtype HourMinute = HourMinute
172− { hour :: NumericRep
173− , minute :: NumericRep
174− }
175−
176−derive instance genericHourMinute :: Generic HourMinute _
177−
178−instance formatComponentHourMinute :: FormatComponent HourMinute where
179− formatComponent = genericFormatComponent
180−
18189type DateTimeComponents a =
18290 Variant
18391 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
@@ -212,7 +120,7 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
212120 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
213121 , formatMatcher: maybe undefinedString show opts.formatMatcher
214122 }
215− comps' = default (defaultComponentRecord)
123+ comps' = un FormatComponentRecord $ case_
216124 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
217125 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
218126 # on (SProxy :: SProxy "yearMonthDay") formatComponent
@@ -278,36 +186,137 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
278186 -> DateTimeFormatOptions_
279187 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
280188
281−foreign import data DateTimeFormatter :: Type
282−
283−foreign import createDateTimeFormatterImpl
284− :: LocalesOption'
285− -> DateTimeFormatOptions_
286− -> DateTimeFormatter
189+foreign import data DateTimeFormat :: Type
287190
288−createDateTimeFormatter
191+foreign import createDateTimeFormatImpl
192+ :: Fn4
193+ (forall a. String -> F a)
194+ (forall a. a -> F a)
195+ LocalesOption'
196+ 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
289282 :: forall a
290283 .(FormatComponent a)
291284 => LocalesOption
292285 -> DateTimeFormatOptions a
293− -> DateTimeFormatter
294−createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
295−
296−foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
297−
298−foreign import supportedLocalesOfImpl :: Fn3 (String -> Either String (Array String)) (Array String -> Either String (Array String)) (Array String) (Either String (Array String))
299−
300−foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
301−
302−formatToParts :: DateTimeFormatter -> JSDate -> F (Array FormatParts)
286+ -> F DateTimeFormat
287+createDateTimeFormat ls opts =
288+ runFn4 createDateTimeFormatImpl
289+ (throwError <<< pure <<< ForeignError)
290+ pure
291+ (toLocale ls)
292+ (formatDateTimeOptions opts)
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+-- | ```
300+foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
301+
302+foreign import supportedLocalesOfImpl
303+ :: Fn3
304+ (String -> Either String (Array String))
305+ (Array String -> Either String (Array String))
306+ (Array String)
307+ (Either String (Array String))
308+
309+foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
310+
311+formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
303312formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
304313
305314supportedLocalesOf :: Array String -> Either String (Array String)
306315supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
307316
308−foreign import resolvedOptionsImpl :: DateTimeFormatter -> Foreign
317+foreign import resolvedOptionsImpl :: DateTimeFormat -> Foreign
309318
310−resolvedOptions :: DateTimeFormatter -> F ResolvedOptions
319+resolvedOptions :: DateTimeFormat -> F ResolvedOptions
311320resolvedOptions fmt = do
312321 locale <- obj ! "locale" >>= readString
313322 calendar <- obj ! "calendar" >>= readString