diff --git a/src/Web/Intl/PluralRules.js b/src/Web/Intl/PluralRules.js new file mode 100644 index 0000000..1ae920a --- /dev/null +++ b/src/Web/Intl/PluralRules.js @@ -0,0 +1,21 @@ +"use strict"; + +export function _new(locales, opts) { + return new Intl.PluralRules(locales, opts); +} + +export function _resolvedOptions(pluralRules) { + return pluralRules.resolvedOptions(); +} + +export function _select(pluralRules, n) { + return pluralRules.select(n); +} + +export function _selectRange(pluralRules, n, n2) { + return pluralRules.selectRange(n, n2); +} + +export function _supportedLocalesOf(locales, opts) { + return Intl.PluralRules.supportedLocalesOf(locales, opts); +} diff --git a/src/Web/Intl/PluralRules.purs b/src/Web/Intl/PluralRules.purs new file mode 100644 index 0000000..0c7e399 --- /dev/null +++ b/src/Web/Intl/PluralRules.purs @@ -0,0 +1,110 @@ +module Web.Intl.PluralRules + -- * Types + ( PluralRules + , PluralRulesOptions + + -- * Constructor + , new + , new_ + + -- * Methods + , supportedLocalesOf + , supportedLocalesOf_ + , select + , selectRange + , resolvedOptions + ) where + +import Data.Function.Uncurried (Fn2, Fn3) +import Data.Function.Uncurried as Function.Uncurried +import Effect (Effect) +import Effect.Uncurried (EffectFn1, EffectFn2) +import Effect.Uncurried as Effect.Uncurried +import Prim.Row (class Union) +import Unsafe.Coerce as Unsafe.Coerce +import Web.Intl.LocaleOptions (LocaleOptions) + +type PluralRulesOptions = + ( localeMatcher :: String + , type :: String + , minimumIntegerDigits :: Int + , minimumFractionDigits :: Int + , maximumFractionDigits :: Int + , minimumSignificantDigits :: Int + , maximumSignificantDigits :: Int + ) + +foreign import data PluralRules :: Type + +foreign import _new + :: EffectFn2 + (Array String) + (Record PluralRulesOptions) + PluralRules + +new + :: forall options options' + . Union options options' PluralRulesOptions + => Array String + -> Record options + -> Effect PluralRules +new locales options = + Effect.Uncurried.runEffectFn2 _new locales (Unsafe.Coerce.unsafeCoerce options) + +new_ :: Array String -> Effect PluralRules +new_ locales = + new locales {} + +foreign import _supportedLocalesOf + :: Fn2 + (Array String) + (Record LocaleOptions) + (Array String) + +supportedLocalesOf + :: forall options options' + . Union options options' LocaleOptions + => Array String + -> Record options + -> Array String +supportedLocalesOf locales options = + Function.Uncurried.runFn2 _supportedLocalesOf locales (Unsafe.Coerce.unsafeCoerce options) + +supportedLocalesOf_ :: Array String -> Array String +supportedLocalesOf_ locales = + supportedLocalesOf locales {} + +foreign import _select + :: Fn2 + PluralRules + Int + String + +select :: PluralRules -> Int -> String +select = + Function.Uncurried.runFn2 _select + +foreign import _selectRange + :: Fn3 + PluralRules + Int + Int + String + +selectRange :: PluralRules -> Int -> Int -> String +selectRange = + Function.Uncurried.runFn3 _selectRange + +type ResolvedOptions = + { locale :: String + , type :: String + , pluralCategories :: Array String + } + +foreign import _resolvedOptions + :: EffectFn1 + PluralRules + ResolvedOptions + +resolvedOptions :: PluralRules -> Effect ResolvedOptions +resolvedOptions = Effect.Uncurried.runEffectFn1 _resolvedOptions \ No newline at end of file diff --git a/test/Main.purs b/test/Main.purs index b40f8eb..c57b2d5 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -12,12 +12,14 @@ import Record as Record import Test.Assert as Test import Web.Intl.DateTimeFormat as DateTimeFormat import Web.Intl.NumberFormat as NumberFormat +import Web.Intl.PluralRules as PluralRules import Web.Intl.RelativeTimeFormat as RelativeTimeFormat main :: Effect Unit main = do test_DateTimeFormat test_NumberFormat + test_PluralRules test_RelativeTimeFormat test_DateTimeFormat :: Effect Unit @@ -218,6 +220,39 @@ test_NumberFormat = do -- <#> \part -> part { value = String.trim part.value } -- } +test_PluralRules :: Effect Unit +test_PluralRules = do + Console.log "PluralRules.supportedLocalesOf" + Test.assertEqual + { actual: PluralRules.supportedLocalesOf [ "en-US" ] { localeMatcher: "best fit" } + , expected: [ "en-US" ] + } + + Console.log "PluralRules.supportedLocalesOf_" + Test.assertEqual + { actual: PluralRules.supportedLocalesOf_ [ "en-US" ] + , expected: [ "en-US" ] + } + + pluralRules <- PluralRules.new [ "en-US" ] { type: "ordinal" } + + Console.log "PluralRules##select" + Test.assertEqual + { actual: PluralRules.select pluralRules 1 + , expected: "one" + } + + Console.log "PluralRules##resolvedOptions" + resolvedOptions <- PluralRules.resolvedOptions pluralRules + Test.assertEqual + { actual: resolvedOptions + , expected: + { locale: "en" + , pluralCategories: [ "few", "one", "two", "other" ] + , type: "ordinal" + } + } + test_RelativeTimeFormat :: Effect Unit test_RelativeTimeFormat = do Console.log "RelativeTimeFormat.supportedLocalesOf" @@ -234,7 +269,7 @@ test_RelativeTimeFormat = do format <- RelativeTimeFormat.new [ "en-US" ] { numeric: "auto" } - Console.logShow "RelativeTimeFormat##format" + Console.log "RelativeTimeFormat##format" Test.assertEqual { actual: RelativeTimeFormat.format format (-1) "day" , expected: "yesterday"