git.iko.soy/purescript-intl
on master help sign in
supportedLocalesOf
commitd37cd34d73b36a02fc7bf1b67fb57433a45231eb authorMarcin Szamotulski <mszamot@gmail.com> dateSat 17 Jun 2017, 23:09 (9 years ago) parentfbf1ad2
3 files changed+24−1
src/Data/Intl/DateTimeFormat.js+8 src/Data/Intl/DateTimeFormat.purs+8 test/Test/Main.purs+8−1
src/Data/Intl/DateTimeFormat.js+8
@@ -19,3 +19,11 @@ exports.formatJSDate = function(formatter) {
1919 return formatter.format(date)
2020 };
2121};
22+
23+exports.supportedLocalesOfImpl = function(Left, Right, locales) {
24+ try {
25+ return Right(Intl.DateTimeFormat.supportedLocalesOf(locales))
26+ } catch (e) {
27+ return Left(e.message)
28+ }
29+}
src/Data/Intl/DateTimeFormat.purs+8
@@ -19,10 +19,13 @@ module Data.Intl.DateTimeFormat
1919 , createDateTimeFormatter
2020 , formatJSDate
2121 , module Data.Intl.DateTimeFormat.Types
22+ , supportedLocalesOf
2223 ) where
2324
2425import Data.Intl.DateTimeFormat.Types
2526
27+import Data.Either (Either(..))
28+import Data.Function.Uncurried (Fn3, runFn3)
2629import Data.Generic.Rep (class Generic)
2730import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
2831import Data.JSDate (JSDate)
@@ -286,3 +289,8 @@ createDateTimeFormatter
286289createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
287290
288291foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
292+
293+foreign import supportedLocalesOfImpl :: Fn3 (String -> Either String (Array String)) (Array String -> Either String (Array String)) (Array String) (Either String (Array String))
294+
295+supportedLocalesOf :: Array String -> Either String (Array String)
296+supportedLocalesOf locales = runFn3 supportedLocalesOfImpl Left Right locales
test/Test/Main.purs+8−1
@@ -5,9 +5,10 @@ import Prelude
55import Control.Monad.Eff (Eff)
66import Control.Monad.Eff.Console (CONSOLE)
77import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
8+import Data.Either (Either(..), isLeft)
89import Data.Enum (toEnum)
910import Data.Generic.Rep (class Generic)
10−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)
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)
1112import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
1213import Data.JSDate (JSDate, fromDateTime)
1314import Data.Maybe (Maybe(..))
@@ -294,3 +295,9 @@ main = do
294295 fmtDate9Expected = "1 2, 2018 AD"
295296 assert' ("fmtEra: got '" <> fmtDate9 <> "' expected: '" <> fmtDate9Expected <> "'")
296297 $ fmtDate9 == fmtDate9Expected
298+
299+ let slocs = supportedLocalesOf ["en-US", "pl-PL"]
300+ assert' ("supportedLocalesOf: got: " <> show slocs) $ slocs == Right ["en-US", "pl-PL"]
301+
302+ let err = supportedLocalesOf ["en_US"]
303+ assert' ("supportedLocalesOf got: " <> show err) $ isLeft err