git.iko.soy/purescript-intl
on master help sign in
.travis.yml+1−1
since splitsnapshots
unifiedsplit
@@ -16,7 +16,7 @@ script:
16 - bower install --production
16 - bower install --production
17 - npm run -s build
17 - npm run -s build
18 - bower install
18 - bower install
19− - LC_TIME="en_GB.UTF-8" npm -s test
19+ - npm -s test
20after_success:
20after_success:
21- >-
21- >-
22 test $TRAVIS_TAG &&
22 test $TRAVIS_TAG &&
README.md+1−1
@@ -1,7 +1,7 @@
1# PureScript bindings to Intl - JavaScript Internalisation API
1# PureScript bindings to Intl - JavaScript Internalisation API
2
2
3[![Maintainer: coot](https://img.shields.io/badge/maintainer-coot-lightgrey.svg)](http://github.com/coot)
3[![Maintainer: coot](https://img.shields.io/badge/maintainer-coot-lightgrey.svg)](http://github.com/coot)
4−[![documentation](https://pursuit.purescript.org/packages/purescript-intl/badge)](https://pursuit.purescript.org/packages/purescript-intl)
4+[![Documentation](https://pursuit.purescript.org/packages/purescript-intl/badge)](https://pursuit.purescript.org/packages/purescript-intl)
5[![Build Status](https://travis-ci.org/coot/purescript-intl.svg?branch=master)](https://travis-ci.org/coot/purescript-intl)
5[![Build Status](https://travis-ci.org/coot/purescript-intl.svg?branch=master)](https://travis-ci.org/coot/purescript-intl)
6
6
7[Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl)
7[Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl)
bower.json+1−1
@@ -9,7 +9,7 @@
9 "license": "MPL-2.0",
9 "license": "MPL-2.0",
10 "repository": {
10 "repository": {
11 "type": "git",
11 "type": "git",
12− "url": "git://github.com/purescript/purescript-prelude.git"
12+ "url": "git://github.com/coot/purescript-intl.git"
13 },
13 },
14 "dependencies": {
14 "dependencies": {
15 "purescript-prelude": "^3.0.0",
15 "purescript-prelude": "^3.0.0",
src/Data/Intl/DateTimeFormat.js+16−6
@@ -1,17 +1,17 @@
1"use strict";
1"use strict";
2
2
3−exports["undefined"] = undefined;
4−
5exports.dateTimeFormatImpl = function(locales) {
3exports.dateTimeFormatImpl = function(locales) {
6 return function(options) {
4 return function(options) {
7 return new Intl.DateTimeFormat(locales, options);
5 return new Intl.DateTimeFormat(locales, options);
8 };
6 };
9};
7};
10
8
11−exports.createDateTimeFormatterImpl = function(locales) {
9+exports.createDateTimeFormatImpl = function(throwError, pure, locales, opts) {
12− return function(opts) {
10+ try {
13− return Intl.DateTimeFormat(locales, opts);
11+ return pure(Intl.DateTimeFormat(locales, opts));
14− };
12+ } catch (e) {
13+ return throwError(e.message);
14+ }
15};
15};
16
16
17exports.formatJSDate = function(formatter) {
17exports.formatJSDate = function(formatter) {
@@ -20,6 +20,12 @@ exports.formatJSDate = function(formatter) {
20 };
20 };
21};
21};
22
22
23+exports.formatToPartsImpl = function(formatter) {
24+ return function(date) {
25+ return formatter.formatToParts(date);
26+ };
27+};
28+
23exports.supportedLocalesOfImpl = function(Left, Right, locales) {
29exports.supportedLocalesOfImpl = function(Left, Right, locales) {
24 try {
30 try {
25 return Right(Intl.DateTimeFormat.supportedLocalesOf(locales))
31 return Right(Intl.DateTimeFormat.supportedLocalesOf(locales))
@@ -27,3 +33,7 @@ exports.supportedLocalesOfImpl = function(Left, Right, locales) {
27 return Left(e.message)
33 return Left(e.message)
28 }
34 }
29}
35}
36+
37+exports.resolvedOptionsImpl = function(formatter) {
38+ return formatter.resolvedOptions();
39+};
src/Data/Intl/DateTimeFormat.purs+163−116
@@ -1,47 +1,39 @@
1−module Data.Intl.DateTimeFormat
1+module Data.Intl.DateTimeFormat
2 ( LocalesOption
2 ( LocalesOption
3 , LocaleMatcher(..)
3 , LocaleMatcher(..)
4 , TimeZone(..)
4 , TimeZone(..)
5 , FormatMatcher(..)
5 , FormatMatcher(..)
6− , WeekdayYearMonthDayHourMinuteSecond(..)
7− , WeekdayYearMonthDay(..)
8− , YearMonthDay(..)
9− , YearMonth(..)
10− , MonthDay(..)
11− , HourMinuteSecond(..)
12− , HourMinute(..)
13− , Undefined
14− , undefined
15 , DateTimeFormatOptions(..)
6 , DateTimeFormatOptions(..)
16 , DateTimeFormatOptions'
7 , DateTimeFormatOptions'
17 , DateTimeComponents(..)
8 , DateTimeComponents(..)
18− , DateTimeFormatter
9+ , DateTimeFormat
19− , createDateTimeFormatter
10+ , createDateTimeFormat
20 , formatJSDate
11 , formatJSDate
21 , module Data.Intl.DateTimeFormat.Types
12 , module Data.Intl.DateTimeFormat.Types
22 , supportedLocalesOf
13 , supportedLocalesOf
14+ , formatToParts
15+ , resolvedOptions
23 ) where
16 ) where
24
17
25import Data.Intl.DateTimeFormat.Types
18import Data.Intl.DateTimeFormat.Types
26
19
20+import Control.Monad.Error.Class (throwError)
27import Data.Either (Either(..))
21import Data.Either (Either(..))
22+import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
23+import Data.Foreign.Index ((!))
28−import Data.Function.Uncurried (Fn3, runFn3)
24+import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
29−import Data.Generic.Rep (class Generic)
30−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
25+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(FormatComponentRecord), formatComponent)
31import Data.JSDate (JSDate)
26import Data.JSDate (JSDate)
32−import Data.Maybe (Maybe, fromMaybe, maybe)
27+import Data.Maybe (Maybe(..), fromMaybe, maybe)
33−import Data.Newtype (class Newtype)
28+import Data.Newtype (class Newtype, un)
34import Data.StrMap (union)
29import Data.StrMap (union)
35import Data.Symbol (SProxy(..))
30import Data.Symbol (SProxy(..))
31+import Data.Traversable (sequence)
36−import Data.Variant (Variant, default, on)
32+import Data.Variant (Variant, case_, default, on)
37−import Prelude (class Eq, class Show, Unit, id, show, (#), ($))
33+import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
38import Unsafe.Coerce (unsafeCoerce)
34import Unsafe.Coerce (unsafeCoerce)
39
35
40foreign import data LocalesOption' :: Type
36foreign import data LocalesOption' :: Type
41−foreign import data DateTimeFormat :: Type
42−
43−foreign import data Undefined :: Type
44−foreign import undefined :: Undefined
45
37
46foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
38foreign import dateTimeFormatImpl :: LocalesOption' -> DateTimeFormatOptions_ -> DateTimeFormat
47
39
@@ -94,85 +86,6 @@ instance showFormatMatcher :: Show FormatMatcher where
94 show FMBasic = "basic"
86 show FMBasic = "basic"
95 show FMBestFit = "best fit"
87 show FMBestFit = "best fit"
96
88
97−newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
98− { weekday :: StringRep
99− , year :: NumericRep
100− , month :: MonthRep
101− , day :: NumericRep
102− , hour :: NumericRep
103− , minute :: NumericRep
104− , second :: NumericRep
105− }
106−
107−derive instance genericWeekdayYearMonthDayHourMinuteSecond :: Generic WeekdayYearMonthDayHourMinuteSecond _
108−
109−instance formatComponentWeekdayYearMonthDayHourMinuteSecond :: FormatComponent WeekdayYearMonthDayHourMinuteSecond where
110− formatComponent = genericFormatComponent
111−
112−newtype WeekdayYearMonthDay = WeekdayYearMonthDay
113− { weekday :: StringRep
114− , year :: NumericRep
115− , month :: MonthRep
116− , day :: NumericRep
117− }
118−
119−derive instance genericWeekdayYearMonthDay :: Generic WeekdayYearMonthDay _
120−
121−instance formatComponentWeekdayYearMonthDay :: FormatComponent WeekdayYearMonthDay where
122− formatComponent = genericFormatComponent
123−
124−newtype YearMonthDay = YearMonthDay
125− { year :: NumericRep
126− , month :: MonthRep
127− , day :: NumericRep
128− }
129−
130−derive instance genericYearMonthDay :: Generic YearMonthDay _
131−
132−instance formatComponentYearMonthDay :: FormatComponent YearMonthDay where
133− formatComponent = genericFormatComponent
134−
135−newtype YearMonth = YearMonth
136− { year :: NumericRep
137− , month :: MonthRep
138− }
139−
140−derive instance genericYearMonth :: Generic YearMonth _
141−
142−instance formatComponentYearMonth :: FormatComponent YearMonth where
143− formatComponent = genericFormatComponent
144−
145−newtype MonthDay = MonthDay
146− { month :: MonthRep
147− , day :: NumericRep
148− }
149−
150−derive instance genericMonthDay :: Generic MonthDay _
151−
152−instance formatComponentMonthDay :: FormatComponent MonthDay where
153− formatComponent = genericFormatComponent
154−
155−newtype HourMinuteSecond = HourMinuteSecond
156− { hour :: NumericRep
157− , minute :: NumericRep
158− , second :: NumericRep
159− }
160−
161−derive instance genericHourMinuteSecond :: Generic HourMinuteSecond _
162−
163−instance formatComponentHourMinuteSecond :: FormatComponent HourMinuteSecond where
164− formatComponent = genericFormatComponent
165−
166−newtype HourMinute = HourMinute
167− { hour :: NumericRep
168− , minute :: NumericRep
169− }
170−
171−derive instance genericHourMinute :: Generic HourMinute _
172−
173−instance formatComponentHourMinute :: FormatComponent HourMinute where
174− formatComponent = genericFormatComponent
175−
176type DateTimeComponents a =
89type DateTimeComponents a =
177 Variant
90 Variant
178 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
91 ( weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond
@@ -207,7 +120,7 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
207 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
120 , hour12: fromMaybe (unsafeCoerce undefined) opts.hour12
208 , formatMatcher: maybe undefinedString show opts.formatMatcher
121 , formatMatcher: maybe undefinedString show opts.formatMatcher
209 }
122 }
210− comps' = default (defaultComponentRecord)
123+ comps' = un FormatComponentRecord $ case_
211 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
124 # on (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond") formatComponent
212 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
125 # on (SProxy :: SProxy "weekdayYearMonthDay") formatComponent
213 # on (SProxy :: SProxy "yearMonthDay") formatComponent
126 # on (SProxy :: SProxy "yearMonthDay") formatComponent
@@ -273,24 +186,158 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
273 -> DateTimeFormatOptions_
186 -> DateTimeFormatOptions_
274 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
187 merge a b = unsafeCoerce $ union (unsafeCoerce a) (unsafeCoerce b)
275
188
276−foreign import data DateTimeFormatter :: Type
189+foreign import data DateTimeFormat :: Type
277−
278−foreign import createDateTimeFormatterImpl
279− :: LocalesOption'
280− -> DateTimeFormatOptions_
281− -> DateTimeFormatter
282
190
283−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
284 :: forall a
282 :: forall a
285 .(FormatComponent a)
283 .(FormatComponent a)
286 => LocalesOption
284 => LocalesOption
287 -> DateTimeFormatOptions a
285 -> DateTimeFormatOptions a
288− -> DateTimeFormatter
286+ -> F DateTimeFormat
287+createDateTimeFormat ls opts =
288+ runFn4 createDateTimeFormatImpl
289+ (throwError <<< pure <<< ForeignError)
290+ pure
291+ (toLocale ls)
289−createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
292+ (formatDateTimeOptions opts)
290−
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+-- | ```
291−foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
300+foreign import formatJSDate :: DateTimeFormat -> JSDate -> String
292−
301+
293−foreign import supportedLocalesOfImpl :: Fn3 (String -> Either String (Array String)) (Array String -> Either String (Array String)) (Array String) (Either String (Array String))
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)
312+formatToParts formatter date = sequence $ readFormatParts <$> formatToPartsImpl formatter date
294
313
295supportedLocalesOf :: Array String -> Either String (Array String)
314supportedLocalesOf :: Array String -> Either String (Array String)
296supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
315supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
316+
317+foreign import resolvedOptionsImpl :: DateTimeFormat -> Foreign
318+
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
333+
334+ pure $ ResolvedOptions { locale, calendar, numberingSystem, timeZone, era, weekday, year, day, hour, minute, second, timeZoneName }
335+
336+ where
337+ obj = resolvedOptionsImpl fmt
338+
339+ readMaybeString :: Foreign -> F (Maybe String)
340+ readMaybeString f =
341+ if isUndefined f
342+ then pure Nothing
343+ else Just <$> readString f
src/Data/Intl/DateTimeFormat/Class.pursnew file+41
@@ -0,0 +1,41 @@
1+module Data.Intl.DateTimeFormat.Class where
2+
3+import Data.Maybe (Maybe(..))
4+import Data.Newtype (class Newtype)
5+import Prelude (class Eq, Unit)
6+
7+newtype FormatComponentRecord = FormatComponentRecord
8+ { era :: Maybe String
9+ , weekday :: Maybe String
10+ , year :: Maybe String
11+ , month :: Maybe String
12+ , day :: Maybe String
13+ , hour :: Maybe String
14+ , minute :: Maybe String
15+ , second :: Maybe String
16+ , timeZoneName :: Maybe String
17+ }
18+
19+derive instance newtypeFormatComponentRecord :: Newtype FormatComponentRecord _
20+
21+derive instance eqFormatComponentRecord :: Eq FormatComponentRecord
22+
23+defaultComponentRecord :: FormatComponentRecord
24+defaultComponentRecord = FormatComponentRecord
25+ { era: Nothing
26+ , weekday: Nothing
27+ , year: Nothing
28+ , month: Nothing
29+ , day: Nothing
30+ , hour: Nothing
31+ , minute: Nothing
32+ , second: Nothing
33+ , timeZoneName: Nothing
34+ }
35+
36+
37+class FormatComponent a where
38+ formatComponent :: a -> FormatComponentRecord
39+
40+instance unitFormatComponent :: FormatComponent Unit where
41+ formatComponent _ = defaultComponentRecord
src/Data/Intl/DateTimeFormat/Generic.purs+5−39
@@ -1,51 +1,17 @@
1module Data.Intl.DateTimeFormat.Generic
1module Data.Intl.DateTimeFormat.Generic
2− ( class FormatComponent
3− , formatComponent
4− , class GenericFormatComponent
2+ ( class GenericFormatComponent
5 , genericFormatComponent'
3 , genericFormatComponent'
6 , genericFormatComponent
4 , genericFormatComponent
7 , class GenericFormatComponentFields
5 , class GenericFormatComponentFields
8 , genericFormatComponentFields
6 , genericFormatComponentFields
9− , defaultComponentRecord
10− , FormatComponentRecord
11 ) where
7 ) where
12
8
13import Data.Generic.Rep (class Generic, Constructor(Constructor), Field(..), NoConstructors, Product(..), Rec(..), from)
9import Data.Generic.Rep (class Generic, Constructor(Constructor), Field(..), NoConstructors, Product(..), Rec(..), from)
10+import Data.Intl.DateTimeFormat.Class (FormatComponentRecord(..))
14import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
11import Data.Intl.DateTimeFormat.Types (MonthRep, NumericRep, TimeZoneNameRep, StringRep)
15import Data.Maybe (Maybe(..))
12import Data.Maybe (Maybe(..))
16import Data.StrMap (StrMap, lookup, singleton, union)
13import Data.StrMap (StrMap, lookup, singleton, union)
17−import Prelude (Unit, show)
14+import Prelude (show)
18−
19−type FormatComponentRecord =
20− { era :: Maybe String
21− , weekday :: Maybe String
22− , year :: Maybe String
23− , month :: Maybe String
24− , day :: Maybe String
25− , hour :: Maybe String
26− , minute :: Maybe String
27− , second :: Maybe String
28− , timeZoneName :: Maybe String
29− }
30−
31−defaultComponentRecord :: FormatComponentRecord
32−defaultComponentRecord =
33− { era: Nothing
34− , weekday: Nothing
35− , year: Nothing
36− , month: Nothing
37− , day: Nothing
38− , hour: Nothing
39− , minute: Nothing
40− , second: Nothing
41− , timeZoneName: Nothing
42− }
43−
44−class FormatComponent a where
45− formatComponent :: a -> FormatComponentRecord
46−
47−instance unitFormatComponent :: FormatComponent Unit where
48− formatComponent _ = defaultComponentRecord
49
15
50class GenericFormatComponent a where
16class GenericFormatComponent a where
51 genericFormatComponent' :: a -> FormatComponentRecord
17 genericFormatComponent' :: a -> FormatComponentRecord
@@ -54,7 +20,7 @@ class GenericFormatComponentFields a where
54 genericFormatComponentFields :: a -> StrMap String
20 genericFormatComponentFields :: a -> StrMap String
55
21
56instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
22instance genericFormatComponentNoConsturctors :: GenericFormatComponent NoConstructors where
57− genericFormatComponent' a =
23+ genericFormatComponent' a = FormatComponentRecord
58 { era: Nothing
24 { era: Nothing
59 , weekday: Nothing
25 , weekday: Nothing
60 , year: Nothing
26 , year: Nothing
@@ -114,7 +80,7 @@ instance genericFormatComponentFieldsProduct
114instance genericFormatComponentRec
80instance genericFormatComponentRec
115 :: (GenericFormatComponentFields a)
81 :: (GenericFormatComponentFields a)
116 => GenericFormatComponent (Rec a) where
82 => GenericFormatComponent (Rec a) where
117− genericFormatComponent' (Rec a) =
83+ genericFormatComponent' (Rec a) = FormatComponentRecord
118 { era: "era" `lookup` u
84 { era: "era" `lookup` u
119 , weekday: "weekday" `lookup` u
85 , weekday: "weekday" `lookup` u
120 , year: "year" `lookup` u
86 , year: "year" `lookup` u
src/Data/Intl/DateTimeFormat/Types.jsnew file+3
@@ -0,0 +1,3 @@
1+"use strict";
2+
3+exports["undefined"] = undefined;
src/Data/Intl/DateTimeFormat/Types.purs+216−2
@@ -1,9 +1,37 @@
1−module Data.Intl.DateTimeFormat.Types where
1+module Data.Intl.DateTimeFormat.Types
2+ ( StringRep(..)
3+ , NumericRep(..)
4+ , MonthRep(..)
5+ , TimeZoneNameRep(..)
6+ , FormatParts(..)
7+ , readFormatParts
8+ , ResolvedOptions(..)
9+ , WeekdayYearMonthDayHourMinuteSecond(..)
10+ , WeekdayYearMonthDay(..)
11+ , YearMonthDay(..)
12+ , YearMonth(..)
13+ , MonthDay(..)
14+ , HourMinuteSecond(..)
15+ , HourMinute(..)
16+ , Undefined
17+ , undefined
18+ ) where
2
19
20+import Data.Foreign (F, Foreign, ForeignError(..), fail, readString)
21+import 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)
3−import Prelude (class Show)
25+import Prelude (class Eq, class Show, bind, pure, show, ($), (<>), (>>=))
26+
27+foreign import data Undefined :: Type
28+
29+foreign import undefined :: Undefined
4
30
5data StringRep = Narrow | Short | Long
31data StringRep = Narrow | Short | Long
6
32
33+derive instance eqStringRep :: Eq StringRep
34+
7instance showStringRep :: Show StringRep where
35instance showStringRep :: Show StringRep where
8 show Narrow = "narrow"
36 show Narrow = "narrow"
9 show Short = "short"
37 show Short = "short"
@@ -11,12 +39,16 @@ instance showStringRep :: Show StringRep where
11
39
12data NumericRep = Numeric | TwoDigit
40data NumericRep = Numeric | TwoDigit
13
41
42+derive instance eqNumericRep :: Eq NumericRep
43+
14instance showNumericRep :: Show NumericRep where
44instance showNumericRep :: Show NumericRep where
15 show Numeric = "numeric"
45 show Numeric = "numeric"
16 show TwoDigit = "2-digit"
46 show TwoDigit = "2-digit"
17
47
18data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
48data MonthRep = MonthNumeric | MonthTwoDigit | MonthNarrow | MonthShort | MonthLong
19
49
50+derive instance eqMonthRep :: Eq MonthRep
51+
20instance showMonthRep :: Show MonthRep where
52instance showMonthRep :: Show MonthRep where
21 show MonthNumeric = "numeric"
53 show MonthNumeric = "numeric"
22 show MonthTwoDigit = "2-digit"
54 show MonthTwoDigit = "2-digit"
@@ -26,7 +58,189 @@ instance showMonthRep :: Show MonthRep where
26
58
27data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
59data TimeZoneNameRep = TimeZoneNameShort | TimeZoneNameLong
28
60
61+derive instance eqTimeZoneNameRep :: Eq TimeZoneNameRep
62+
29instance showTimeZoneRep :: Show TimeZoneNameRep where
63instance showTimeZoneRep :: Show TimeZoneNameRep where
30 show TimeZoneNameShort = "short"
64 show TimeZoneNameShort = "short"
31 show TimeZoneNameLong = "long"
65 show TimeZoneNameLong = "long"
32
66
67+data FormatParts
68+ = Era String
69+ | Year String
70+ | Month String
71+ | Weekday String
72+ | Day String
73+ | Hour String
74+ | Minute String
75+ | DayPeriod String
76+ | Literal String
77+
78+derive instance eqFormatParts :: Eq FormatParts
79+
80+readFormatParts :: Foreign -> F FormatParts
81+readFormatParts val = do
82+ type_ <- val ! "type" >>= readString
83+ value <- val ! "value" >>= readString
84+ case type_ of
85+ "era" -> pure $ Era value
86+ "year" -> pure $ Year value
87+ "month" -> pure $ Month value
88+ "weekday" -> pure $ Weekday value
89+ "month" -> pure $ Month value
90+ "day" -> pure $ Day value
91+ "hour" -> pure $ Hour value
92+ "minute" -> pure $ Minute value
93+ "dayperiod" -> pure $ DayPeriod value
94+ "literal" -> pure $ Literal value
95+ _ -> fail $ ForeignError ("type " <> type_ <> " is not supported by purescript-intl")
96+
97+newtype ResolvedOptions = ResolvedOptions
98+ { locale :: String
99+ , numberingSystem :: String
100+ , calendar :: String
101+ , timeZone :: String
102+ , era :: Maybe String
103+ , weekday :: Maybe String
104+ , year :: Maybe String
105+ , day :: Maybe String
106+ , hour :: Maybe String
107+ , minute :: Maybe String
108+ , second :: Maybe String
109+ , timeZoneName :: Maybe String
110+ }
111+
112+derive instance newtypeResolvedOptions :: Newtype ResolvedOptions _
113+
114+derive instance eqResolvedOptions :: Eq ResolvedOptions
115+
116+newtype WeekdayYearMonthDayHourMinuteSecond = WeekdayYearMonthDayHourMinuteSecond
117+ { weekday :: StringRep
118+ , year :: NumericRep
119+ , month :: MonthRep
120+ , day :: NumericRep
121+ , hour :: NumericRep
122+ , minute :: NumericRep
123+ , second :: NumericRep
124+ }
125+
126+derive instance newtypeWeekdayYearMonthDayHourMinuteSecond :: Newtype WeekdayYearMonthDayHourMinuteSecond _
127+
128+derive instance eqWeekdayYearMonthDayHourMinuteSecond :: Eq WeekdayYearMonthDayHourMinuteSecond
129+
130+instance 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
141+
142+newtype WeekdayYearMonthDay = WeekdayYearMonthDay
143+ { weekday :: StringRep
144+ , year :: NumericRep
145+ , month :: MonthRep
146+ , day :: NumericRep
147+ }
148+
149+derive instance newtypeWeekdayYearMonthDay :: Newtype WeekdayYearMonthDay _
150+
151+derive instance eqWeekdayYearMonthDay :: Eq WeekdayYearMonthDay
152+
153+instance 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+
162+
163+newtype YearMonthDay = YearMonthDay
164+ { year :: NumericRep
165+ , month :: MonthRep
166+ , day :: NumericRep
167+ }
168+
169+derive instance newtypeYearMonthDay :: Newtype YearMonthDay _
170+
171+derive instance eqYearMonthDay :: Eq YearMonthDay
172+
173+instance 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+
181+
182+newtype YearMonth = YearMonth
183+ { year :: NumericRep
184+ , month :: MonthRep
185+ }
186+
187+derive instance newtypeYearMonth :: Newtype YearMonth _
188+
189+derive instance eqYearMonth :: Eq YearMonth
190+
191+instance 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
197+
198+newtype MonthDay = MonthDay
199+ { month :: MonthRep
200+ , day :: NumericRep
201+ }
202+
203+derive instance newtypeMonthDay :: Newtype MonthDay _
204+
205+derive instance eqMonthDay :: Eq MonthDay
206+
207+instance 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
213+
214+newtype HourMinuteSecond = HourMinuteSecond
215+ { hour :: NumericRep
216+ , minute :: NumericRep
217+ , second :: NumericRep
218+ }
219+
220+derive instance newtypeHourMinuteSecond :: Newtype HourMinuteSecond _
221+
222+derive instance eqHourMinuteSecond :: Eq HourMinuteSecond
223+
224+instance 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
231+
232+newtype HourMinute = HourMinute
233+ { hour :: NumericRep
234+ , minute :: NumericRep
235+ }
236+
237+derive instance newtypeHourMinute :: Newtype HourMinute _
238+
239+derive instance eqHourMinute :: Eq HourMinute
240+
241+instance 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
test/Test/Main.purs+64−24
@@ -4,18 +4,22 @@ import Prelude
4
4
5import Control.Monad.Eff (Eff)
5import Control.Monad.Eff (Eff)
6import Control.Monad.Eff.Console (CONSOLE)
6import Control.Monad.Eff.Console (CONSOLE)
7+import Control.Monad.Except (runExcept)
7import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
8import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
8−import Data.Either (Either(..), isLeft)
9+import Data.Either (Either(..), fromRight, isLeft)
9import Data.Enum (toEnum)
10import Data.Enum (toEnum)
11+import Data.Foldable (intercalate)
12+import Data.Foreign (F, renderForeignError)
10import Data.Generic.Rep (class Generic)
13import Data.Generic.Rep (class Generic)
11−import Data.Intl.DateTimeFormat (DateTimeFormatOptions(..), DateTimeFormatOptions', HourMinute(HourMinute), HourMinuteSecond(HourMinuteSecond), LocalesOption, MonthDay(..), MonthRep(..), NumericRep(Numeric, TwoDigit), StringRep(..), TimeZone(TimeZone), WeekdayYearMonthDay(..), WeekdayYearMonthDayHourMinuteSecond(WeekdayYearMonthDayHourMinuteSecond), YearMonth(..), YearMonthDay(..), createDateTimeFormatter, formatJSDate, supportedLocalesOf)
14+import 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)
12−import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
15+import Data.Intl.DateTimeFormat.Class (class FormatComponent, FormatComponentRecord(..))
16+import Data.Intl.DateTimeFormat.Generic (genericFormatComponent)
13import Data.JSDate (JSDate, fromDateTime)
17import Data.JSDate (JSDate, fromDateTime)
14−import Data.Maybe (Maybe(..))
18+import Data.Maybe (Maybe(..), isJust, isNothing)
15import Data.Symbol (SProxy(..))
19import Data.Symbol (SProxy(..))
16import Data.Time (Time(..))
20import Data.Time (Time(..))
17import Data.Variant (inj)
21import Data.Variant (inj)
18−import Partial.Unsafe (unsafeCrashWith)
22+import Partial.Unsafe (unsafeCrashWith, unsafePartial)
19import Test.Assert (ASSERT, assert')
23import Test.Assert (ASSERT, assert')
20
24
21type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
25type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
@@ -33,7 +37,7 @@ instance formatComponentEraYear :: FormatComponent EraYear where
33newtype Era = Era StringRep
37newtype Era = Era StringRep
34
38
35instance formatComponentEra :: FormatComponent Era where
39instance formatComponentEra :: FormatComponent Era where
36− formatComponent (Era r) =
40+ formatComponent (Era r) = FormatComponentRecord
37 { era: Just (show r)
41 { era: Just (show r)
38 , weekday: Nothing
42 , weekday: Nothing
39 , year: Nothing
43 , year: Nothing
@@ -49,11 +53,28 @@ main :: Tests
49main = do
53main = do
50 let
54 let
51
55
56+ unsafeFromF :: forall a. F a -> a
57+ unsafeFromF = unsafePartial fromRight <<< runExcept
58+
52 locales :: LocalesOption
59 locales :: LocalesOption
53 locales = inj (SProxy :: SProxy "locale") "en-US"
60 locales = inj (SProxy :: SProxy "locale") "en-US"
54
61
62+ fmtError =
63+ createDateTimeFormat (inj (SProxy :: SProxy "locale") "") opts
64+ where
65+ opts :: DateTimeFormatOptions'
66+ opts =
67+ (DateTimeFormatOptions
68+ { localeMatcher: Nothing
69+ , timeZone: (Just (TimeZone "Europe/London"))
70+ , hour12: Just false
71+ , formatMatcher: Nothing
72+ }
73+ (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
74+ )
75+
55− fmtHourMinute =
76+ fmtHourMinute = unsafeFromF $
56− createDateTimeFormatter locales opts
77+ createDateTimeFormat locales opts
57 where
78 where
58 opts :: DateTimeFormatOptions'
79 opts :: DateTimeFormatOptions'
59 opts =
80 opts =
@@ -66,8 +87,8 @@ main = do
66 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
87 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
67 )
88 )
68
89
69− fmtHourMinuteSecond =
90+ fmtHourMinuteSecond = unsafeFromF $
70− createDateTimeFormatter locales opts
91+ createDateTimeFormat locales opts
71 where
92 where
72 opts :: DateTimeFormatOptions'
93 opts :: DateTimeFormatOptions'
73 opts =
94 opts =
@@ -80,8 +101,8 @@ main = do
80 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
101 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
81 )
102 )
82
103
83− fmtWeekdayYearMonthDayHourMinuteSecond =
104+ fmtWeekdayYearMonthDayHourMinuteSecond = unsafeFromF $
84− createDateTimeFormatter locales opts
105+ createDateTimeFormat locales opts
85 where
106 where
86 opts :: DateTimeFormatOptions'
107 opts :: DateTimeFormatOptions'
87 opts =
108 opts =
@@ -106,8 +127,8 @@ main = do
106 )
127 )
107 )
128 )
108
129
109− fmtWeekdayYearMonthDay =
130+ fmtWeekdayYearMonthDay = unsafeFromF $
110− createDateTimeFormatter locales opts
131+ createDateTimeFormat locales opts
111 where
132 where
112 opts :: DateTimeFormatOptions'
133 opts :: DateTimeFormatOptions'
113 opts =
134 opts =
@@ -129,8 +150,8 @@ main = do
129 )
150 )
130 )
151 )
131
152
132− fmtYearMonthDay =
153+ fmtYearMonthDay = unsafeFromF $
133− createDateTimeFormatter locales opts
154+ createDateTimeFormat locales opts
134 where
155 where
135 opts :: DateTimeFormatOptions'
156 opts :: DateTimeFormatOptions'
136 opts =
157 opts =
@@ -151,8 +172,8 @@ main = do
151 )
172 )
152 )
173 )
153
174
154− fmtYearMonth =
175+ fmtYearMonth = unsafeFromF $
155− createDateTimeFormatter locales opts
176+ createDateTimeFormat locales opts
156 where
177 where
157 opts :: DateTimeFormatOptions'
178 opts :: DateTimeFormatOptions'
158 opts =
179 opts =
@@ -172,8 +193,8 @@ main = do
172 )
193 )
173 )
194 )
174
195
175− fmtMonthDay =
196+ fmtMonthDay = unsafeFromF $
176− createDateTimeFormatter locales opts
197+ createDateTimeFormat locales opts
177 where
198 where
178 opts :: DateTimeFormatOptions'
199 opts :: DateTimeFormatOptions'
179 opts =
200 opts =
@@ -193,8 +214,8 @@ main = do
193 )
214 )
194 )
215 )
195
216
196− fmtEraYear =
217+ fmtEraYear = unsafeFromF $
197− createDateTimeFormatter locales opts
218+ createDateTimeFormat locales opts
198 where
219 where
199 opts :: DateTimeFormatOptions EraYear
220 opts :: DateTimeFormatOptions EraYear
200 opts =
221 opts =
@@ -214,8 +235,8 @@ main = do
214 )
235 )
215 )
236 )
216
237
217− fmtEra =
238+ fmtEra = unsafeFromF $
218− createDateTimeFormatter locales opts
239+ createDateTimeFormat locales opts
219 where
240 where
220 opts :: DateTimeFormatOptions Era
241 opts :: DateTimeFormatOptions Era
221 opts =
242 opts =
@@ -259,6 +280,9 @@ main = do
259 case jsDate of
280 case jsDate of
260 Nothing -> unsafeCrashWith "jsDate not parsed"
281 Nothing -> unsafeCrashWith "jsDate not parsed"
261 Just d -> do
282 Just d -> do
283+
284+ assert' "fmtError expected to return error" $ isLeft (runExcept fmtError)
285+
262 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
286 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
263 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"
287 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"
264 assert' ("fmtWeekdayYearMonthDayHourMinuteSecond: got: '" <> fmtDate1 <> "' expected: '" <> fmtDate1Expected <> "'")
288 assert' ("fmtWeekdayYearMonthDayHourMinuteSecond: got: '" <> fmtDate1 <> "' expected: '" <> fmtDate1Expected <> "'")
@@ -301,3 +325,19 @@ main = do
301
325
302 let err = supportedLocalesOf ["en_US"]
326 let err = supportedLocalesOf ["en_US"]
303 assert' ("supportedLocalesOf got: " <> show err) $ isLeft err
327 assert' ("supportedLocalesOf got: " <> show err) $ isLeft err
328+
329+ -- formatToParts is not supported in node
330+ -- let parts = runExcept $ formatToParts fmtHourMinute d
331+ -- assert' ("") $ isRight parts
332+
333+ case runExcept (resolvedOptions fmtHourMinuteSecond) of
334+ Left errs-> assert' ("resolvedOptions error: " <> (intercalate "; " $ renderForeignError <$> errs)) false
335+ Right (ResolvedOptions {locale, year, day, weekday, hour, minute, second}) -> do
336+ assert' ("resolvedOptions locale: got: " <> locale) $ (locale == "en-US") || (locale == "en")
337+ assert' ("resolvedOptions year: got Just") $ isNothing year
338+ assert' ("resolvedOptions day: got Just") $ isNothing day
339+ assert' ("resolvedOptions weekday: got Just") $ isNothing weekday
340+ assert' ("resolvedOptions hour: got Nothing") $ isJust hour
341+ assert' ("resolvedOptions minute: got Nothing") $ isJust minute
342+ assert' ("resolvedOptions second: got Nothing") $ isJust second
343+