Skip to content

Commit

Permalink
Implement PluralRules
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-murphy committed Jun 7, 2023
1 parent da5576c commit f453cef
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Web/Intl/PluralRules.js
Original file line number Diff line number Diff line change
@@ -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);
}
110 changes: 110 additions & 0 deletions src/Web/Intl/PluralRules.purs
Original file line number Diff line number Diff line change
@@ -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
37 changes: 36 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit f453cef

Please sign in to comment.