@@ -1,343 +0,0 @@
1−module Data.Intl.DateTimeFormat
6− , DateTimeFormatOptions(..)
7− , DateTimeFormatOptions'
8− , DateTimeComponents(..)
10− , createDateTimeFormat
12− , module Data.Intl.DateTimeFormat.Types
18−import Data.Intl.DateTimeFormat.Types
20−import Control.Monad.Error.Class (throwError)
21−import Data.Either (Either(..))
22−import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
23−import Data.Foreign.Index ((!))
24−import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
25−import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
26−import Data.JSDate (JSDate)
27−import Data.Maybe (Maybe(..), fromMaybe, maybe)
28−import Data.Newtype (class Newtype, un)
29−import Data.StrMap (union)
30−import Data.Symbol (SProxy(..))
31−import Data.Traversable (sequence)
32−import Data.Variant (Variant, case_, default, on)
33−import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
34−import Unsafe.Coerce (unsafeCoerce)
36−foreign import data LocalesOption' :: Type
38−foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
40−newtype DateTimeFormatOptions_ = DateTimeFormatOptions_
41− { localeMatcher :: String
44− , formatMatcher :: String
53− , timeZoneName :: String
56−type LocalesOption = Variant (locale :: String, locales :: Array String, undefined :: Undefined)
58−toLocale :: LocalesOption -> LocalesOption'
59−toLocale = unsafeCoerce $
61− # on (SProxy :: SProxy "locale") id
62− # on (SProxy :: SProxy "locales") id
68−instance showLocaleMatcher :: Show LocaleMatcher where
69− show LMLookup = "lookup"
70− show LMBestFit = "best fit"
72−newtype TimeZone = TimeZone String
74−instance showTimeZone :: Show TimeZone where
75− show (TimeZone tz) = tz
77−derive instance newtypeTimeZone :: Newtype TimeZone _
79−derive instance eqTimeZone :: Eq TimeZone
85−instance showFormatMatcher :: Show FormatMatcher where
86− show FMBasic = "basic"
87− show FMBestFit = "best fit"
89−type DateTimeComponents a =
91− ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
92− , weekdayYearMonthDay :: WeekdayYearMonthDay
93− , yearMonthDay :: YearMonthDay
94− , yearMonth :: YearMonth
95− , monthDay :: MonthDay
96− , hourMinuteSecond :: HourMinuteSecond
97− , hourMinute :: HourMinute
101−data DateTimeFormatOptions a = DateTimeFormatOptions
102− { localeMatcher :: Maybe LocaleMatcher
103− , timeZone :: Maybe TimeZone
104− , hour12 :: Maybe Boolean
105− , formatMatcher :: Maybe FormatMatcher
107− (DateTimeComponents a)
109−type DateTimeFormatOptions' = DateTimeFormatOptions Unit
111−formatDateTimeOptions :: forall a. (FormatComponent a) => DateTimeFormatOptions a -> DateTimeFormatOptions_
112−formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoerce comps')
115− undefinedString :: String
116− undefinedString = unsafeCoerce undefined
118− opts' = { localeMatcher: maybe undefinedString show opts.localeMatcher
119− , timeZone: maybe undefinedString show opts.timeZone
120− , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
121− , formatMatcher: maybe undefinedString show opts.formatMatcher
123− comps' = un FormatComponentRecord $ case_
124− # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
125− # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
126− # on (SProxy :: SProxy "yearMonthDay") formatComponent
127− # on (SProxy :: SProxy "yearMonth") formatComponent
128− # on (SProxy :: SProxy "monthDay") formatComponent
129− # on (SProxy :: SProxy "hourMinuteSecond") formatComponent
130− # on (SProxy :: SProxy "hourMinute") formatComponent
131− # on (SProxy :: SProxy "custom") formatComponent
134− toUndefined :: Maybe String -> String
135− toUndefined m = fromMaybe (unsafeCoerce undefined) m
138− :: { weekday :: Maybe String
139− , era :: Maybe String
140− , year :: Maybe String
141− , month :: Maybe String
142− , day :: Maybe String
143− , hour :: Maybe String
144− , minute :: Maybe String
145− , second :: Maybe String
146− , timeZoneName :: Maybe String
148− -> { weekday :: String
156− , timeZoneName :: String
158− compsCoerce { weekday, era, year, month, day, hour, minute, second, timeZoneName }
159− = { weekday: toUndefined weekday
160− , era: toUndefined era
161− , year: toUndefined year
162− , month: toUndefined month
163− , day: toUndefined day
164− , hour: toUndefined hour
165− , minute: toUndefined minute
166− , second: toUndefined second
167− , timeZoneName: toUndefined timeZoneName
171− :: { localeMatcher :: String
172− , timeZone :: String
174− , formatMatcher :: String
176− -> { weekday :: String
184− , timeZoneName :: String
186− -> DateTimeFormatOptions_
187− merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
189−foreign import data DateTimeFormat :: Type
191−foreign import createDateTimeFormatImpl
193− (forall a. String -> F a)
194− (forall a. a -> F a)
196− DateTimeFormatOptions_
201−-- | import Data.Variant (inj)
203−-- | fmtHourMinute = F DateTimeFormat
204−-- | fmtHourMinute = createDateTimeFormat locale opts
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
213−-- | (inj (SProxy :: SProxy "hourMinute")
214−-- | (HourMinute {hour: TwoDigit, minute: TwoDigit}))
217−-- | fmtDate :: F DateTimeFormat
218−-- | fmtDate = createDateTimeFormat locale opts
220−-- | locale = inj (SProxy :: SProxy "locale") "en-GB"
221−-- | opts :: DateTimeFormatOptions'
223−-- | (DateTimeFormatOptions
224−-- | { localeMatcher: Nothing
225−-- | , timeZone: (Just (TimeZone "Europe/London"))
226−-- | , hour12: Just true
227−-- | , formatMatcher: Nothing
230−-- | (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond")
231−-- | (WeekdayYearMonthDayHourMinuteSecond
232−-- | { weekday: Short
234−-- | , month: MonthLong
237−-- | , minute: Numeric
238−-- | , second: Numeric
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.
249−-- | newtype EraYear = EraYear
250−-- | { era :: StringRep
251−-- | , year :: NumericRep
254−-- | derive instance genericEraYear :: Generic EraYear _
256−-- | instance formatComponentEraYear :: FormatComponent EraYear where
257−-- | formatComponent = genericFormatComponent
259−-- | fmtEraYear :: F DateTimeFormat
260−-- | fmtEraYear = createDateTimeFormat locales opts
262−-- | locale = inj (SProxy :: SProxy "locale") "en-GB"
263−-- | opts :: DateTimeFormatOptions EraYear
265−-- | (DateTimeFormatOptions
266−-- | { localeMatcher: Nothing
267−-- | , timeZone: (Just (TimeZone "Europe/London"))
268−-- | , hour12: Just true
269−-- | , formatMatcher: Nothing
272−-- | (SProxy :: SProxy "custom")
283− .(FormatComponent a)
285− -> DateTimeFormatOptions a
287−createDateTimeFormat ls opts =
288− runFn4 createDateTimeFormatImpl
289− (throwError <<< pure <<< ForeignError)
292− (formatDateTimeOptions opts)
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"
300−foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
302−foreign import supportedLocalesOfImpl
304− (String -> Either String (Array String))
305− (Array String -> Either String (Array String))
307− (Either String (Array String))
309−foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
311−formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
312−formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
314−supportedLocalesOf :: Array String -> Either String (Array String)
315−supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
317−foreign import resolvedOptionsImpl :: DateTimeFormat -> Foreign
319−resolvedOptions :: DateTimeFormat -> F ResolvedOptions
320−resolvedOptions fmt = do
321− locale <- obj ! "locale" >>= readString
322− calendar <- obj ! "calendar" >>= readString
323− numberingSystem <- obj ! "numberingSystem" >>= readString
324− timeZone <- obj ! "timeZone" >>= readString
325− era <- obj ! "era" >>= readMaybeString
326− weekday <- obj ! "weekday" >>= readMaybeString
327− year <- obj ! "year" >>= readMaybeString
328− day <- obj ! "day" >>= readMaybeString
329− hour <- obj ! "hour" >>= readMaybeString
330− minute <- obj ! "minute" >>= readMaybeString
331− second <- obj ! "second" >>= readMaybeString
332− timeZoneName <- obj ! "timeZoneName" >>= readMaybeString
334− pure $ ResolvedOptions { locale, calendar, numberingSystem, timeZone, era, weekday, year, day, hour, minute, second, timeZoneName }
337− obj = resolvedOptionsImpl fmt
339− readMaybeString :: Foreign -> F (Maybe String)
343− else Just <$> readString f