git.iko.soy/purescript-intl
on master help sign in
createDateTimeFormatter - might error
af41720Marcin Szamotulski
src/Data/Intl/DateTimeFormat.js+6−4
@@ -8,10 +8,12 @@ exports.dateTimeFormatImpl = function(locales) {
8 };
8 };
9};
9};
10
10
11−exports.createDateTimeFormatterImpl = function(locales) {
11+exports.createDateTimeFormatterImpl = function(Left, Right, locales, opts) {
12− return function(opts) {
12+ try {
13− return Intl.DateTimeFormat(locales, opts);
13+ return Right(Intl.DateTimeFormat(locales, opts));
14− };
14+ } catch (e) {
15+ return Left(e.message);
16+ }
15};
17};
16
18
17exports.formatJSDate = function(formatter) {
19exports.formatJSDate = function(formatter) {
src/Data/Intl/DateTimeFormat.purs+15−7
@@ -29,7 +29,7 @@ import Data.Intl.DateTimeFormat.Types
29import Data.Either (Either(..))
29import Data.Either (Either(..))
30import Data.Foreign (F, Foreign, isUndefined, readString)
30import Data.Foreign (F, Foreign, isUndefined, readString)
31import Data.Foreign.Index ((!))
31import Data.Foreign.Index ((!))
32−import Data.Function.Uncurried (Fn3, runFn3)
32+import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
33import Data.Generic.Rep (class Generic)
33import Data.Generic.Rep (class Generic)
34import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
34import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
35import Data.JSDate (JSDate)
35import Data.JSDate (JSDate)
@@ -281,21 +281,29 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
281foreign import data DateTimeFormatter :: Type
281foreign import data DateTimeFormatter :: Type
282
282
283foreign import createDateTimeFormatterImpl
283foreign import createDateTimeFormatterImpl
284+ :: Fn4
285+ (String -> Either String DateTimeFormatter)
286+ (DateTimeFormatter -> Either String DateTimeFormatter)
284− :: LocalesOption'
287+ LocalesOption'
285− -> DateTimeFormatOptions_
288+ DateTimeFormatOptions_
286− -> DateTimeFormatter
289+ (Either String DateTimeFormatter)
287
290
288createDateTimeFormatter
291createDateTimeFormatter
289 :: forall a
292 :: forall a
290 .(FormatComponent a)
293 .(FormatComponent a)
291 => LocalesOption
294 => LocalesOption
292 -> DateTimeFormatOptions a
295 -> DateTimeFormatOptions a
293− -> DateTimeFormatter
296+ -> Either String DateTimeFormatter
294−createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
297+createDateTimeFormatter ls opts = runFn4 createDateTimeFormatterImpl Left Right (toLocale ls) (formatDateTimeOptions opts)
295
298
296foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
299foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
297
300
298−foreign import supportedLocalesOfImpl :: Fn3 (String -> Either String (Array String)) (Array String -> Either String (Array String)) (Array String) (Either String (Array String))
301+foreign import supportedLocalesOfImpl
302+ :: Fn3
303+ (String -> Either String (Array String))
304+ (Array String -> Either String (Array String))
305+ (Array String)
306+ (Either String (Array String))
299
307
300foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
308foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
301
309
test/Test/Main.purs+28−11
@@ -6,7 +6,7 @@ import Control.Monad.Eff (Eff)
6import Control.Monad.Eff.Console (CONSOLE)
6import Control.Monad.Eff.Console (CONSOLE)
7import Control.Monad.Except (runExcept)
7import Control.Monad.Except (runExcept)
8import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
8import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
9−import Data.Either (Either(..), isLeft)
9+import Data.Either (Either(..), fromRight, isLeft)
10import Data.Enum (toEnum)
10import Data.Enum (toEnum)
11import Data.Foldable (intercalate)
11import Data.Foldable (intercalate)
12import Data.Foreign (renderForeignError)
12import Data.Foreign (renderForeignError)
@@ -18,7 +18,7 @@ import Data.Maybe (Maybe(..), isJust, isNothing)
18import Data.Symbol (SProxy(..))
18import Data.Symbol (SProxy(..))
19import Data.Time (Time(..))
19import Data.Time (Time(..))
20import Data.Variant (inj)
20import Data.Variant (inj)
21−import Partial.Unsafe (unsafeCrashWith)
21+import Partial.Unsafe (unsafeCrashWith, unsafePartial)
22import Test.Assert (ASSERT, assert')
22import Test.Assert (ASSERT, assert')
23
23
24type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
24type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
@@ -55,7 +55,21 @@ main = do
55 locales :: LocalesOption
55 locales :: LocalesOption
56 locales = inj (SProxy :: SProxy "locale") "en-US"
56 locales = inj (SProxy :: SProxy "locale") "en-US"
57
57
58+ fmtError =
59+ createDateTimeFormatter (inj (SProxy :: SProxy "locale") "") opts
60+ where
61+ opts :: DateTimeFormatOptions'
62+ opts =
63+ (DateTimeFormatOptions
64+ { localeMatcher: Nothing
65+ , timeZone: (Just (TimeZone "Europe/London"))
66+ , hour12: Just false
67+ , formatMatcher: Nothing
68+ }
69+ (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
70+ )
71+
58− fmtHourMinute =
72+ fmtHourMinute = unsafePartial $ fromRight $
59 createDateTimeFormatter locales opts
73 createDateTimeFormatter locales opts
60 where
74 where
61 opts :: DateTimeFormatOptions'
75 opts :: DateTimeFormatOptions'
@@ -69,7 +83,7 @@ main = do
69 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
83 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
70 )
84 )
71
85
72− fmtHourMinuteSecond =
86+ fmtHourMinuteSecond = unsafePartial $ fromRight $
73 createDateTimeFormatter locales opts
87 createDateTimeFormatter locales opts
74 where
88 where
75 opts :: DateTimeFormatOptions'
89 opts :: DateTimeFormatOptions'
@@ -83,7 +97,7 @@ main = do
83 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
97 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
84 )
98 )
85
99
86− fmtWeekdayYearMonthDayHourMinuteSecond =
100+ fmtWeekdayYearMonthDayHourMinuteSecond = unsafePartial $ fromRight $
87 createDateTimeFormatter locales opts
101 createDateTimeFormatter locales opts
88 where
102 where
89 opts :: DateTimeFormatOptions'
103 opts :: DateTimeFormatOptions'
@@ -109,7 +123,7 @@ main = do
109 )
123 )
110 )
124 )
111
125
112− fmtWeekdayYearMonthDay =
126+ fmtWeekdayYearMonthDay = unsafePartial $ fromRight $
113 createDateTimeFormatter locales opts
127 createDateTimeFormatter locales opts
114 where
128 where
115 opts :: DateTimeFormatOptions'
129 opts :: DateTimeFormatOptions'
@@ -132,7 +146,7 @@ main = do
132 )
146 )
133 )
147 )
134
148
135− fmtYearMonthDay =
149+ fmtYearMonthDay = unsafePartial $ fromRight $
136 createDateTimeFormatter locales opts
150 createDateTimeFormatter locales opts
137 where
151 where
138 opts :: DateTimeFormatOptions'
152 opts :: DateTimeFormatOptions'
@@ -154,7 +168,7 @@ main = do
154 )
168 )
155 )
169 )
156
170
157− fmtYearMonth =
171+ fmtYearMonth = unsafePartial $ fromRight $
158 createDateTimeFormatter locales opts
172 createDateTimeFormatter locales opts
159 where
173 where
160 opts :: DateTimeFormatOptions'
174 opts :: DateTimeFormatOptions'
@@ -175,7 +189,7 @@ main = do
175 )
189 )
176 )
190 )
177
191
178− fmtMonthDay =
192+ fmtMonthDay = unsafePartial $ fromRight $
179 createDateTimeFormatter locales opts
193 createDateTimeFormatter locales opts
180 where
194 where
181 opts :: DateTimeFormatOptions'
195 opts :: DateTimeFormatOptions'
@@ -196,7 +210,7 @@ main = do
196 )
210 )
197 )
211 )
198
212
199− fmtEraYear =
213+ fmtEraYear = unsafePartial $ fromRight $
200 createDateTimeFormatter locales opts
214 createDateTimeFormatter locales opts
201 where
215 where
202 opts :: DateTimeFormatOptions EraYear
216 opts :: DateTimeFormatOptions EraYear
@@ -217,7 +231,7 @@ main = do
217 )
231 )
218 )
232 )
219
233
220− fmtEra =
234+ fmtEra = unsafePartial $ fromRight $
221 createDateTimeFormatter locales opts
235 createDateTimeFormatter locales opts
222 where
236 where
223 opts :: DateTimeFormatOptions Era
237 opts :: DateTimeFormatOptions Era
@@ -262,6 +276,9 @@ main = do
262 case jsDate of
276 case jsDate of
263 Nothing -> unsafeCrashWith "jsDate not parsed"
277 Nothing -> unsafeCrashWith "jsDate not parsed"
264 Just d -> do
278 Just d -> do
279+
280+ assert' "fmtError expected to return error" $ isLeft fmtError
281+
265 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
282 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
266 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"
283 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"
267 assert' ("fmtWeekdayYearMonthDayHourMinuteSecond: got: '" <> fmtDate1 <> "' expected: '" <> fmtDate1Expected <> "'")
284 assert' ("fmtWeekdayYearMonthDayHourMinuteSecond: got: '" <> fmtDate1 <> "' expected: '" <> fmtDate1Expected <> "'")