git.iko.soy/purescript-intl
on master help sign in
supportedLocalesOf
d37cd34Marcin Szamotulski
src/Data/Intl/DateTimeFormat.js+8
@@ -19,3 +19,11 @@ exports.formatJSDate = function(formatter) {
19 return formatter.format(date)
19 return formatter.format(date)
20 };
20 };
21};
21};
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
19 , createDateTimeFormatter
19 , createDateTimeFormatter
20 , formatJSDate
20 , formatJSDate
21 , module Data.Intl.DateTimeFormat.Types
21 , module Data.Intl.DateTimeFormat.Types
22+ , supportedLocalesOf
22 ) where
23 ) where
23
24
24import Data.Intl.DateTimeFormat.Types
25import Data.Intl.DateTimeFormat.Types
25
26
27+import Data.Either (Either(..))
28+import Data.Function.Uncurried (Fn3, runFn3)
26import Data.Generic.Rep (class Generic)
29import Data.Generic.Rep (class Generic)
27import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
30import Data.Intl.DateTimeFormat.Generic (class FormatComponent, defaultComponentRecord, formatComponent, genericFormatComponent)
28import Data.JSDate (JSDate)
31import Data.JSDate (JSDate)
@@ -286,3 +289,8 @@ createDateTimeFormatter
286createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
289createDateTimeFormatter ls opts = createDateTimeFormatterImpl (toLocale ls) (formatDateTimeOptions opts)
287
290
288foreign import formatJSDate :: DateTimeFormatter -> JSDate -> String
291foreign 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
5import Control.Monad.Eff (Eff)
5import Control.Monad.Eff (Eff)
6import Control.Monad.Eff.Console (CONSOLE)
6import Control.Monad.Eff.Console (CONSOLE)
7import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
7import Data.DateTime (Date, DateTime(..), Month(..), canonicalDate)
8+import Data.Either (Either(..), isLeft)
8import Data.Enum (toEnum)
9import Data.Enum (toEnum)
9import Data.Generic.Rep (class Generic)
10import 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)
11import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
12import Data.Intl.DateTimeFormat.Generic (class FormatComponent, genericFormatComponent)
12import Data.JSDate (JSDate, fromDateTime)
13import Data.JSDate (JSDate, fromDateTime)
13import Data.Maybe (Maybe(..))
14import Data.Maybe (Maybe(..))
@@ -294,3 +295,9 @@ main = do
294 fmtDate9Expected = "1 2, 2018 AD"
295 fmtDate9Expected = "1 2, 2018 AD"
295 assert' ("fmtEra: got '" <> fmtDate9 <> "' expected: '" <> fmtDate9Expected <> "'")
296 assert' ("fmtEra: got '" <> fmtDate9 <> "' expected: '" <> fmtDate9Expected <> "'")
296 $ fmtDate9 == fmtDate9Expected
297 $ 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