git.iko.soy/purescript-intl
on master help sign in
createDateTimeFormatter - might error
commitaf417204f79f6b9835b0d4c047be67f49bf7db99 authorMarcin Szamotulski <mszamot@gmail.com> dateSun 18 Jun 2017, 18:34 (9 years ago) parent7a9fc5c
3 files changed+49−22
src/Data/Intl/DateTimeFormat.js+6−4 src/Data/Intl/DateTimeFormat.purs+15−7 test/Test/Main.purs+28−11
src/Data/Intl/DateTimeFormat.js+6−4
@@ -8,10 +8,12 @@ exports.dateTimeFormatImpl = function(locales) {
88 };
99};
1010
11−exports.createDateTimeFormatterImpl = function(locales) {
12− return function(opts) {
13− return Intl.DateTimeFormat(locales, opts);
14− };
11+exports.createDateTimeFormatterImpl = function(Left, Right, locales, opts) {
12+ try {
13+ return Right(Intl.DateTimeFormat(locales, opts));
14+ } catch (e) {
15+ return Left(e.message);
16+ }
1517};
1618
1719exports.formatJSDate = function(formatter) {
src/Data/Intl/DateTimeFormat.purs+15−7
@@ -29,7 +29,7 @@ import Data.Intl.DateTimeFormat.Types
2929import Data.Either (Either(..))
3030import Data.Foreign (F, Foreign, isUndefined, readString)
3131import Data.Foreign.Index ((!))
32−import Data.Function.Uncurried (Fn3, runFn3)
32+import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
3333import Data.Generic.Rep (class Generic)
3434import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
3535import Data.JSDate (JSDate)
@@ -281,21 +281,29 @@ formatDateTimeOptions (DateTimeFormatOptions opts comps) = merge opts' (compsCoe
281281foreign import data DateTimeFormatter :: Type
282282
283283foreign import createDateTimeFormatterImpl
284− :: LocalesOption'
285− -> DateTimeFormatOptions_
286− -> DateTimeFormatter
284+ :: Fn4
285+ (String -> Either String DateTimeFormatter)
286+ (DateTimeFormatter -> Either String DateTimeFormatter)
287+ LocalesOption'
288+ DateTimeFormatOptions_
289+ (Either String DateTimeFormatter)
287290
288291createDateTimeFormatter
289292 :: forall a
290293 .(FormatComponent a)
291294 => LocalesOption
292295 -> DateTimeFormatOptions a
293− -> DateTimeFormatter
294−createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
296+ -> Either String DateTimeFormatter
297+createDateTimeFormatter ls opts = runFn4 createDateTimeFormatterImpl Left Right (toLocale ls) (formatDateTimeOptions opts)
295298
296299foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
297300
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))
299307
300308foreign import formatToPartsImpl :: DateTimeFormatter -> JSDate -> Array Foreign
301309
test/Test/Main.purs+28−11
@@ -6,7 +6,7 @@ import Control.Monad.Eff (Eff)
66import Control.Monad.Eff.Console (CONSOLE)
77import Control.Monad.Except (runExcept)
88import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
9−import Data.Either (Either(..), isLeft)
9+import Data.Either (Either(..), fromRight, isLeft)
1010import Data.Enum (toEnum)
1111import Data.Foldable (intercalate)
1212import Data.Foreign (renderForeignError)
@@ -18,7 +18,7 @@ import Data.Maybe (Maybe(..), isJust, isNothing)
1818import Data.Symbol (SProxy(..))
1919import Data.Time (Time(..))
2020import Data.Variant (inj)
21−import Partial.Unsafe (unsafeCrashWith)
21+import Partial.Unsafe (unsafeCrashWith, unsafePartial)
2222import Test.Assert (ASSERT, assert')
2323
2424type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
@@ -55,7 +55,21 @@ main = do
5555 locales :: LocalesOption
5656 locales = inj (SProxy :: SProxy "locale") "en-US"
5757
58− fmtHourMinute =
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+
72+ fmtHourMinute = unsafePartial $ fromRight $
5973 createDateTimeFormatter locales opts
6074 where
6175 opts :: DateTimeFormatOptions'
@@ -69,7 +83,7 @@ main = do
6983 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
7084 )
7185
72− fmtHourMinuteSecond =
86+ fmtHourMinuteSecond = unsafePartial $ fromRight $
7387 createDateTimeFormatter locales opts
7488 where
7589 opts :: DateTimeFormatOptions'
@@ -83,7 +97,7 @@ main = do
8397 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
8498 )
8599
86− fmtWeekdayYearMonthDayHourMinuteSecond =
100+ fmtWeekdayYearMonthDayHourMinuteSecond = unsafePartial $ fromRight $
87101 createDateTimeFormatter locales opts
88102 where
89103 opts :: DateTimeFormatOptions'
@@ -109,7 +123,7 @@ main = do
109123 )
110124 )
111125
112− fmtWeekdayYearMonthDay =
126+ fmtWeekdayYearMonthDay = unsafePartial $ fromRight $
113127 createDateTimeFormatter locales opts
114128 where
115129 opts :: DateTimeFormatOptions'
@@ -132,7 +146,7 @@ main = do
132146 )
133147 )
134148
135− fmtYearMonthDay =
149+ fmtYearMonthDay = unsafePartial $ fromRight $
136150 createDateTimeFormatter locales opts
137151 where
138152 opts :: DateTimeFormatOptions'
@@ -154,7 +168,7 @@ main = do
154168 )
155169 )
156170
157− fmtYearMonth =
171+ fmtYearMonth = unsafePartial $ fromRight $
158172 createDateTimeFormatter locales opts
159173 where
160174 opts :: DateTimeFormatOptions'
@@ -175,7 +189,7 @@ main = do
175189 )
176190 )
177191
178− fmtMonthDay =
192+ fmtMonthDay = unsafePartial $ fromRight $
179193 createDateTimeFormatter locales opts
180194 where
181195 opts :: DateTimeFormatOptions'
@@ -196,7 +210,7 @@ main = do
196210 )
197211 )
198212
199− fmtEraYear =
213+ fmtEraYear = unsafePartial $ fromRight $
200214 createDateTimeFormatter locales opts
201215 where
202216 opts :: DateTimeFormatOptions EraYear
@@ -217,7 +231,7 @@ main = do
217231 )
218232 )
219233
220− fmtEra =
234+ fmtEra = unsafePartial $ fromRight $
221235 createDateTimeFormatter locales opts
222236 where
223237 opts :: DateTimeFormatOptions Era
@@ -262,6 +276,9 @@ main = do
262276 case jsDate of
263277 Nothing -> unsafeCrashWith "jsDate not parsed"
264278 Just d -> do
279+
280+ assert' "fmtError expected to return error" $ isLeft fmtError
281+
265282 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
266283 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"
267284 assert' ("fmtWeekdayYearMonthDayHourMinuteSecond: got: '" <> fmtDate1 <> "' expected: '" <> fmtDate1Expected <> "'")