git.iko.soy/purescript-intl
on master help sign in
3 files changed+130−13
since splitsnapshots
unifiedsplit
src/Data/Intl/DateTimeFormat.purs+99−11 src/Data/Intl/DateTimeFormat/Class.purs+3−1 src/Data/Intl/DateTimeFormat/Types.purs+28−1
src/Data/Intl/DateTimeFormat.purs+99−11
@@ -1,4 +1,4 @@
1−module Data.Intl.DateTimeFormat
1+module Data.Intl.DateTimeFormat
22 ( LocalesOption
33 , LocaleMatcher(..)
44 , TimeZone(..)
@@ -120,15 +120,15 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
120120 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
121121 , formatMatcher: maybe undefinedString show opts.formatMatcher
122122 }
123− comps' = case_
124− # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") (un FormatComponentRecord <<< formatComponent)
125− # on (SProxy :: SProxy "weekdayYearMonthDay") (un FormatComponentRecord <<< formatComponent)
126− # on (SProxy :: SProxy "yearMonthDay") (un FormatComponentRecord <<< formatComponent)
127− # on (SProxy :: SProxy "yearMonth") (un FormatComponentRecord <<< formatComponent)
128− # on (SProxy :: SProxy "monthDay") (un FormatComponentRecord <<< formatComponent)
129− # on (SProxy :: SProxy "hourMinuteSecond") (un FormatComponentRecord <<< formatComponent)
130− # on (SProxy :: SProxy "hourMinute") (un FormatComponentRecord <<< formatComponent)
131− # on (SProxy :: SProxy "custom") (un FormatComponentRecord <<< formatComponent)
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
132132 $ comps
133133
134134 toUndefined :: Maybe String -> String
@@ -196,6 +196,88 @@ foreign import createDateTimeFormatImpl
196196 DateTimeFormatOptions_
197197 (F DateTimeFormat)
198198
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+-- | ```
199281createDateTimeFormat
200282 :: forall a
201283 .(FormatComponent a)
@@ -209,6 +291,12 @@ createDateTimeFormat ls opts =
209291 (toLocale ls)
210292 (formatDateTimeOptions opts)
211293
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+-- | ```
212300foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
213301
214302foreign import supportedLocalesOfImpl
@@ -217,7 +305,7 @@ foreign import supportedLocalesOfImpl
217305 (Array String -> Either String (Array String))
218306 (Array String)
219307 (Either String (Array String))
220−
308+
221309foreign import formatToPartsImpl :: DateTimeFormat -> JSDate -> Array Foreign
222310
223311formatToParts :: DateTimeFormat -> JSDate -> F (Array FormatParts)
src/Data/Intl/DateTimeFormat/Class.purs+3−1
@@ -2,7 +2,7 @@ module Data.Intl.DateTimeFormat.Class where
22
33import Data.Maybe (Maybe(..))
44import Data.Newtype (class Newtype)
5−import Prelude (Unit)
5+import Prelude (class Eq, Unit)
66
77newtype FormatComponentRecord = FormatComponentRecord
88 { era :: Maybe String
@@ -18,6 +18,8 @@ newtype FormatComponentRecord = FormatComponentRecord
1818
1919derive instance newtypeFormatComponentRecord :: Newtype FormatComponentRecord _
2020
21+derive instance eqFormatComponentRecord :: Eq FormatComponentRecord
22+
2123defaultComponentRecord :: FormatComponentRecord
2224defaultComponentRecord = FormatComponentRecord
2325 { era: Nothing
src/Data/Intl/DateTimeFormat/Types.purs+28−1
@@ -22,13 +22,16 @@ import Data.Foreign.Index ((!))
2222import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..), defaultComponentRecord)
2323import Data.Maybe (Maybe(..))
2424import Data.Newtype (class Newtype, over)
25−import Prelude (class Show, bind, pure, show, ($), (<>), (>>=))
25+import Prelude (class Eq, class Show, bind, pure, show, ($), (<>), (>>=))
2626
2727foreign import data Undefined :: Type
28+
2829foreign import undefined :: Undefined
2930
3031data StringRep = Narrow | Short | Long
3132
33+derive instance eqStringRep :: Eq StringRep
34+
3235instance showStringRep :: Show StringRep where
3336 show Narrow = "narrow"
3437 show Short = "short"
@@ -36,12 +39,16 @@ instance showStringRep :: Show StringRep where
3639
3740data NumericRep = Numeric | TwoDigit
3841
42+derive instance eqNumericRep :: Eq NumericRep
43+
3944instance showNumericRep :: Show NumericRep where
4045 show Numeric = "numeric"
4146 show TwoDigit = "2-digit"
4247
4348data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
4449
50+derive instance eqMonthRep :: Eq MonthRep
51+
4552instance showMonthRep :: Show MonthRep where
4653 show MonthNumeric = "numeric"
4754 show MonthTwoDigit = "2-digit"
@@ -51,6 +58,8 @@ instance showMonthRep :: Show MonthRep where
5158
5259data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
5360
61+derive instance eqTimeZoneNameRep :: Eq TimeZoneNameRep
62+
5463instance showTimeZoneRep :: Show TimeZoneNameRep where
5564 show TimeZoneNameShort = "short"
5665 show TimeZoneNameLong = "long"
@@ -66,6 +75,8 @@ data FormatParts
6675 | DayPeriod String
6776 | Literal String
6877
78+derive instance eqFormatParts :: Eq FormatParts
79+
6980readFormatParts :: Foreign -> F FormatParts
7081readFormatParts val = do
7182 type_ <- val ! "type" >>= readString
@@ -100,6 +111,8 @@ newtype ResolvedOptions = ResolvedOptions
100111
101112derive instance newtypeResolvedOptions :: Newtype ResolvedOptions _
102113
114+derive instance eqResolvedOptions :: Eq ResolvedOptions
115+
103116newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
104117 { weekday :: StringRep
105118 , year :: NumericRep
@@ -112,6 +125,8 @@ newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecon
112125
113126derive instance newtypeWeekdayYearMonthDayHourMinuteSecond :: Newtype WeekdayYearMonthDayHourMinuteSecond _
114127
128+derive instance eqWeekdayYearMonthDayHourMinuteSecond :: Eq WeekdayYearMonthDayHourMinuteSecond
129+
115130instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
116131 formatComponent (WeekdayYearMonthDayHourMinuteSecond { weekday, year, month, day, hour, minute, second }) = over FormatComponentRecord
117132 (\r -> r { weekday = Just (show weekday)
@@ -133,6 +148,8 @@ newtype WeekdayYearMonthDay = WeekdayYearMonthDay
133148
134149derive instance newtypeWeekdayYearMonthDay :: Newtype WeekdayYearMonthDay _
135150
151+derive instance eqWeekdayYearMonthDay :: Eq WeekdayYearMonthDay
152+
136153instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
137154 formatComponent (WeekdayYearMonthDay { weekday, year, month, day }) = over FormatComponentRecord
138155 (\r -> r { weekday = Just (show weekday)
@@ -151,6 +168,8 @@ newtype YearMonthDay = YearMonthDay
151168
152169derive instance newtypeYearMonthDay :: Newtype YearMonthDay _
153170
171+derive instance eqYearMonthDay :: Eq YearMonthDay
172+
154173instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
155174 formatComponent (YearMonthDay { year, month, day }) = over FormatComponentRecord
156175 (\r -> r { year = Just (show year)
@@ -167,6 +186,8 @@ newtype YearMonth = YearMonth
167186
168187derive instance newtypeYearMonth :: Newtype YearMonth _
169188
189+derive instance eqYearMonth :: Eq YearMonth
190+
170191instance formatComponentYearMonth :: FormatComponent YearMonth where
171192 formatComponent (YearMonth { year, month }) = over FormatComponentRecord
172193 (\r -> r { year = Just (show year)
@@ -181,6 +202,8 @@ newtype MonthDay = MonthDay
181202
182203derive instance newtypeMonthDay :: Newtype MonthDay _
183204
205+derive instance eqMonthDay :: Eq MonthDay
206+
184207instance formatComponentMonthDay :: FormatComponent MonthDay where
185208 formatComponent (MonthDay { month, day }) = over FormatComponentRecord
186209 (\r -> r { month = Just (show month)
@@ -196,6 +219,8 @@ newtype HourMinuteSecond = HourMinuteSecond
196219
197220derive instance newtypeHourMinuteSecond :: Newtype HourMinuteSecond _
198221
222+derive instance eqHourMinuteSecond :: Eq HourMinuteSecond
223+
199224instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
200225 formatComponent (HourMinuteSecond { hour, minute, second }) = over FormatComponentRecord
201226 (\r -> r { hour = Just (show hour)
@@ -211,6 +236,8 @@ newtype HourMinute = HourMinute
211236
212237derive instance newtypeHourMinute :: Newtype HourMinute _
213238
239+derive instance eqHourMinute :: Eq HourMinute
240+
214241instance formatComponentHourMinute :: FormatComponent HourMinute where
215242 formatComponent (HourMinute { hour, minute }) = over FormatComponentRecord
216243 (\r -> r { hour = Just (show hour)