git.iko.soy/purescript-intl
on master help sign in
createDateTimeFormatter - use F monad
commitb9e6360c79eab2d2c48c2d0bb91f3edd600e3e04 authorMarcin Szamotulski <mszamot@gmail.com> dateSun 18 Jun 2017, 22:37 (9 years ago) parentaf41720
3 files changed+30−21
src/Data/Intl/DateTimeFormat.js+3−3 src/Data/Intl/DateTimeFormat.purs+13−7 test/Test/Main.purs+14−11
src/Data/Intl/DateTimeFormat.js+3−3
@@ -8,11 +8,11 @@ exports.dateTimeFormatImpl = function(locales) {
88 };
99};
1010
11−exports.createDateTimeFormatterImpl = function(Left, Right, locales, opts) {
11+exports.createDateTimeFormatterImpl = function(throwError, pure, locales, opts) {
1212 try {
13− return Right(Intl.DateTimeFormat(locales, opts));
13+ return pure(Intl.DateTimeFormat(locales, opts));
1414 } catch (e) {
15− return Left(e.message);
15+ return throwError(e.message);
1616 }
1717};
1818
src/Data/Intl/DateTimeFormat.purs+13−7
@@ -26,8 +26,9 @@ module Data.Intl.DateTimeFormat
2626
2727import Data.Intl.DateTimeFormat.Types
2828
29+import Control.Monad.Error.Class (throwError)
2930import Data.Either (Either(..))
30−import Data.Foreign (F, Foreign, isUndefined, readString)
31+import Data.Foreign (F, Foreign, ForeignError(..), isUndefined, readString)
3132import Data.Foreign.Index ((!))
3233import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4)
3334import Data.Generic.Rep (class Generic)
@@ -39,7 +40,7 @@ import Data.StrMap (union)
3940import Data.Symbol (SProxy(..))
4041import Data.Traversable (sequence)
4142import Data.Variant (Variant, default, on)
42−import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (>>=))
43+import Prelude (class Eq, class Show, Unit, bind, id, pure, show, (#), ($), (<$>), (<<<), (>>=))
4344import Unsafe.Coerce (unsafeCoerce)
4445
4546foreign import data LocalesOption' :: Type
@@ -282,19 +283,24 @@ foreign import data DateTimeFormatter :: Type
282283
283284foreign import createDateTimeFormatterImpl
284285 :: Fn4
285− (String -> Either String DateTimeFormatter)
286− (DateTimeFormatter -> Either String DateTimeFormatter)
286+ (forall a. String -> F a)
287+ (forall a. a -> F a)
287288 LocalesOption'
288289 DateTimeFormatOptions_
289− (Either String DateTimeFormatter)
290+ (F DateTimeFormatter)
290291
291292createDateTimeFormatter
292293 :: forall a
293294 .(FormatComponent a)
294295 => LocalesOption
295296 -> DateTimeFormatOptions a
296− -> Either String DateTimeFormatter
297−createDateTimeFormatter ls opts = runFn4 createDateTimeFormatterImpl Left Right (toLocale ls) (formatDateTimeOptions opts)
297+ -> F DateTimeFormatter
298+createDateTimeFormatter ls opts =
299+ runFn4 createDateTimeFormatterImpl
300+ (throwError <<< pure <<< ForeignError)
301+ pure
302+ (toLocale ls)
303+ (formatDateTimeOptions opts)
298304
299305foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
300306
test/Test/Main.purs+14−11
@@ -9,7 +9,7 @@ import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
99import Data.Either (Either(..), fromRight, isLeft)
1010import Data.Enum (toEnum)
1111import Data.Foldable (intercalate)
12−import Data.Foreign (renderForeignError)
12+import Data.Foreign (F, renderForeignError)
1313import Data.Generic.Rep (class Generic)
1414import 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), createDateTimeFormatter, formatJSDate, resolvedOptions, supportedLocalesOf)
1515import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
@@ -52,6 +52,9 @@ main :: Tests
5252main = do
5353 let
5454
55+ unsafeFromF :: forall a. F a -> a
56+ unsafeFromF = unsafePartial fromRight <<< runExcept
57+
5558 locales :: LocalesOption
5659 locales = inj (SProxy :: SProxy "locale") "en-US"
5760
@@ -69,7 +72,7 @@ main = do
6972 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
7073 )
7174
72− fmtHourMinute = unsafePartial $ fromRight $
75+ fmtHourMinute = unsafeFromF $
7376 createDateTimeFormatter locales opts
7477 where
7578 opts :: DateTimeFormatOptions'
@@ -83,7 +86,7 @@ main = do
8386 (inj (SProxy :: SProxy "hourMinute") (HourMinute {hour: TwoDigit, minute: TwoDigit}))
8487 )
8588
86− fmtHourMinuteSecond = unsafePartial $ fromRight $
89+ fmtHourMinuteSecond = unsafeFromF $
8790 createDateTimeFormatter locales opts
8891 where
8992 opts :: DateTimeFormatOptions'
@@ -97,7 +100,7 @@ main = do
97100 (inj (SProxy :: SProxy "hourMinuteSecond") (HourMinuteSecond {hour: Numeric, minute: Numeric, second: Numeric}))
98101 )
99102
100− fmtWeekdayYearMonthDayHourMinuteSecond = unsafePartial $ fromRight $
103+ fmtWeekdayYearMonthDayHourMinuteSecond = unsafeFromF $
101104 createDateTimeFormatter locales opts
102105 where
103106 opts :: DateTimeFormatOptions'
@@ -123,7 +126,7 @@ main = do
123126 )
124127 )
125128
126− fmtWeekdayYearMonthDay = unsafePartial $ fromRight $
129+ fmtWeekdayYearMonthDay = unsafeFromF $
127130 createDateTimeFormatter locales opts
128131 where
129132 opts :: DateTimeFormatOptions'
@@ -146,7 +149,7 @@ main = do
146149 )
147150 )
148151
149− fmtYearMonthDay = unsafePartial $ fromRight $
152+ fmtYearMonthDay = unsafeFromF $
150153 createDateTimeFormatter locales opts
151154 where
152155 opts :: DateTimeFormatOptions'
@@ -168,7 +171,7 @@ main = do
168171 )
169172 )
170173
171− fmtYearMonth = unsafePartial $ fromRight $
174+ fmtYearMonth = unsafeFromF $
172175 createDateTimeFormatter locales opts
173176 where
174177 opts :: DateTimeFormatOptions'
@@ -189,7 +192,7 @@ main = do
189192 )
190193 )
191194
192− fmtMonthDay = unsafePartial $ fromRight $
195+ fmtMonthDay = unsafeFromF $
193196 createDateTimeFormatter locales opts
194197 where
195198 opts :: DateTimeFormatOptions'
@@ -210,7 +213,7 @@ main = do
210213 )
211214 )
212215
213− fmtEraYear = unsafePartial $ fromRight $
216+ fmtEraYear = unsafeFromF $
214217 createDateTimeFormatter locales opts
215218 where
216219 opts :: DateTimeFormatOptions EraYear
@@ -231,7 +234,7 @@ main = do
231234 )
232235 )
233236
234− fmtEra = unsafePartial $ fromRight $
237+ fmtEra = unsafeFromF $
235238 createDateTimeFormatter locales opts
236239 where
237240 opts :: DateTimeFormatOptions Era
@@ -277,7 +280,7 @@ main = do
277280 Nothing -> unsafeCrashWith "jsDate not parsed"
278281 Just d -> do
279282
280− assert' "fmtError expected to return error" $ isLeft fmtError
283+ assert' "fmtError expected to return error" $ isLeft (runExcept fmtError)
281284
282285 let fmtDate1 = formatJSDate fmtWeekdayYearMonthDayHourMinuteSecond d
283286 fmtDate1Expected = "Tue, January 2, 2018, 12:00:00 PM"