Skip to content

Commit

Permalink
Merge pull request #20 from pete-murphy/locale-sensitive-functions
Browse files Browse the repository at this point in the history
feat: Add locale-sensitive functions
  • Loading branch information
pete-murphy authored Dec 7, 2023
2 parents a9a08ba + c66eb39 commit 1d08a1e
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 28 deletions.
27 changes: 19 additions & 8 deletions src/JS/Intl/Collator.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module JS.Intl.Collator
, supportedLocalesOf_
, compare
, resolvedOptions

-- * Options
, convertOptionsWithDefaults
) where

import Prelude hiding (compare)
Expand Down Expand Up @@ -61,6 +64,20 @@ defaultOptions =
, collation: Collation.toString Collation.Default
}

convertOptionsWithDefaults
:: forall provided all
. ConvertOptionsWithDefaults
ToCollatorOptions
{ | CollatorOptions }
provided
all
=> provided
-> all
convertOptionsWithDefaults =
ConvertableOptions.convertOptionsWithDefaults
ToCollatorOptions
defaultOptions

foreign import _new
:: EffectFn2
(Array Locale)
Expand Down Expand Up @@ -89,10 +106,7 @@ new locales providedOptions =
Effect.Uncurried.runEffectFn2
_new
locales
options
where
options :: { | CollatorOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToCollatorOptions defaultOptions providedOptions
(convertOptionsWithDefaults providedOptions)

instance ConvertOption ToCollatorOptions "localeMatcher" LocaleMatcher String where
convertOption _ _ = LocaleMatcher.toString
Expand Down Expand Up @@ -150,10 +164,7 @@ supportedLocalesOf locales providedOptions =
Function.Uncurried.runFn2
_supportedLocalesOf
locales
options
where
options :: { | CollatorOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToCollatorOptions defaultOptions providedOptions
(convertOptionsWithDefaults providedOptions)

supportedLocalesOf_
:: Array Locale
Expand Down
31 changes: 21 additions & 10 deletions src/JS/Intl/DateTimeFormat.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module JS.Intl.DateTimeFormat
, formatRangeToParts
, formatToParts
, resolvedOptions

-- * Options
, convertOptionsWithDefaults
) where

import Prelude
Expand Down Expand Up @@ -92,6 +95,20 @@ defaultOptions :: { | DateTimeFormatOptions }
defaultOptions =
Unsafe.Coerce.unsafeCoerce {}

convertOptionsWithDefaults
:: forall provided all
. ConvertOptionsWithDefaults
ToDateTimeFormatOptions
{ | DateTimeFormatOptions }
provided
all
=> provided
-> all
convertOptionsWithDefaults =
ConvertableOptions.convertOptionsWithDefaults
ToDateTimeFormatOptions
defaultOptions

foreign import _new
:: EffectFn2
(Array Locale)
Expand All @@ -116,14 +133,11 @@ new
=> Array Locale
-> { | provided }
-> Effect DateTimeFormat
new locales provided =
new locales providedOptions =
Effect.Uncurried.runEffectFn2
_new
locales
options
where
options :: { | DateTimeFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToDateTimeFormatOptions defaultOptions provided
(convertOptionsWithDefaults providedOptions)

instance ConvertOption ToDateTimeFormatOptions "localeMatcher" LocaleMatcher String where
convertOption _ _ = LocaleMatcher.toString
Expand Down Expand Up @@ -240,14 +254,11 @@ supportedLocalesOf
=> Array Locale
-> { | provided }
-> Array String
supportedLocalesOf locales provided =
supportedLocalesOf locales providedOptions =
Function.Uncurried.runFn2
_supportedLocalesOf
locales
options
where
options :: { | DateTimeFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToDateTimeFormatOptions defaultOptions provided
(convertOptionsWithDefaults providedOptions)

supportedLocalesOf_
:: Array Locale
Expand Down
31 changes: 21 additions & 10 deletions src/JS/Intl/NumberFormat.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module JS.Intl.NumberFormat
, formatRangeToParts
, formatToParts
, resolvedOptions

-- * Options
, convertOptionsWithDefaults
) where

import Prelude
Expand Down Expand Up @@ -95,6 +98,20 @@ defaultOptions :: { | NumberFormatOptions }
defaultOptions =
Unsafe.Coerce.unsafeCoerce {}

convertOptionsWithDefaults
:: forall provided all
. ConvertOptionsWithDefaults
ToNumberFormatOptions
{ | NumberFormatOptions }
provided
all
=> provided
-> all
convertOptionsWithDefaults =
ConvertableOptions.convertOptionsWithDefaults
ToNumberFormatOptions
defaultOptions

foreign import _new
:: EffectFn2
(Array Locale)
Expand All @@ -113,14 +130,11 @@ new
=> Array Locale
-> { | provided }
-> Effect NumberFormat
new locales provided =
new locales providedOptions =
Effect.Uncurried.runEffectFn2
_new
locales
options
where
options :: { | NumberFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToNumberFormatOptions defaultOptions provided
(convertOptionsWithDefaults providedOptions)

instance ConvertOption ToNumberFormatOptions "localeMatcher" LocaleMatcher String where
convertOption _ _ = LocaleMatcher.toString
Expand Down Expand Up @@ -262,14 +276,11 @@ supportedLocalesOf
=> Array Locale
-> { | provided }
-> Array String
supportedLocalesOf locales provided =
supportedLocalesOf locales providedOptions =
Function.Uncurried.runFn2
_supportedLocalesOf
locales
options
where
options :: { | NumberFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToNumberFormatOptions defaultOptions provided
(convertOptionsWithDefaults providedOptions)

supportedLocalesOf_
:: Array Locale
Expand Down
13 changes: 13 additions & 0 deletions src/JS/LocaleSensitive/Date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

export function _toLocaleString(locales, options, date) {
return date.toLocaleString(locales, options);
}

export function _toLocaleDateString(locales, options, date) {
return date.toLocaleDateString(locales, options);
}

export function _toLocaleTimeString(locales, options, date) {
return date.toLocaleTimeString(locales, options);
}
103 changes: 103 additions & 0 deletions src/JS/LocaleSensitive/Date.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
module JS.LocaleSensitive.Date
( toLocaleString
, toLocaleDateString
, toLocaleTimeString
) where

import ConvertableOptions (class ConvertOptionsWithDefaults)
import Data.DateTime (DateTime)
import Data.JSDate (JSDate)
import Data.JSDate as JSDate
import Effect (Effect)
import Effect.Uncurried (EffectFn3)
import Effect.Uncurried as Effect.Uncurried
import JS.Intl.DateTimeFormat (DateTimeFormatOptions, ToDateTimeFormatOptions)
import JS.Intl.DateTimeFormat as DateTimeFormat
import JS.Intl.Locale (Locale)

foreign import _toLocaleString
:: EffectFn3
(Array Locale)
{ | DateTimeFormatOptions }
JSDate
String

foreign import _toLocaleDateString
:: EffectFn3
(Array Locale)
{ | DateTimeFormatOptions }
JSDate
String

foreign import _toLocaleTimeString
:: EffectFn3
(Array Locale)
{ | DateTimeFormatOptions }
JSDate
String

-- | Returns a string with a language-sensitive representation of a date and
-- | time in the user agent's timezone.
-- | When formatting large numbers of dates, it is better to use
-- | `Intl.DateTimeFormat.format`.
toLocaleString
:: forall provided
. ConvertOptionsWithDefaults
ToDateTimeFormatOptions
{ | DateTimeFormatOptions }
{ | provided }
{ | DateTimeFormatOptions }
=> Array Locale
-> { | provided }
-> DateTime
-> Effect String
toLocaleString locales providedOptions dateTime =
Effect.Uncurried.runEffectFn3
_toLocaleString
locales
(DateTimeFormat.convertOptionsWithDefaults providedOptions)
(JSDate.fromDateTime dateTime)

-- | Returns a string with a language-sensitive representation of a date in the
-- | user agent's timezone.
-- | When formatting large numbers of dates, it is better to use
-- | `Intl.DateTimeFormat.format`.
toLocaleDateString
:: forall provided
. ConvertOptionsWithDefaults
ToDateTimeFormatOptions
{ | DateTimeFormatOptions }
{ | provided }
{ | DateTimeFormatOptions }
=> Array Locale
-> { | provided }
-> DateTime
-> Effect String
toLocaleDateString locales providedOptions dateTime =
Effect.Uncurried.runEffectFn3
_toLocaleDateString
locales
(DateTimeFormat.convertOptionsWithDefaults providedOptions)
(JSDate.fromDateTime dateTime)

-- | Returns a string with a language-sensitive representation of a time in the
-- | user agent's timezone.
-- | When formatting large numbers of dates, it is better to use
-- | `Intl.DateTimeFormat.format`.
toLocaleTimeString
:: forall provided
. ConvertOptionsWithDefaults
ToDateTimeFormatOptions
{ | DateTimeFormatOptions }
{ | provided }
{ | DateTimeFormatOptions }
=> Array Locale
-> { | provided }
-> DateTime
-> Effect String
toLocaleTimeString locales providedOptions dateTime =
Effect.Uncurried.runEffectFn3
_toLocaleTimeString
locales
(DateTimeFormat.convertOptionsWithDefaults providedOptions)
(JSDate.fromDateTime dateTime)
5 changes: 5 additions & 0 deletions src/JS/LocaleSensitive/Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

export function _toLocaleString(locales, options, number) {
return number.toLocaleString(locales, options);
}
38 changes: 38 additions & 0 deletions src/JS/LocaleSensitive/Number.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module JS.LocaleSensitive.Number
( toLocaleString
) where

import ConvertableOptions (class ConvertOptionsWithDefaults)
import Effect (Effect)
import Effect.Uncurried (EffectFn3)
import Effect.Uncurried as Effect.Uncurried
import JS.Intl.Locale (Locale)
import JS.Intl.NumberFormat (NumberFormatOptions, ToNumberFormatOptions)
import JS.Intl.NumberFormat as NumberFormat

foreign import _toLocaleString
:: EffectFn3
(Array Locale)
{ | NumberFormatOptions }
Number
String

-- | Returns a string with a language-sensitive representation of this number.
-- | When formatting large numbers of numbers, it is better to use
-- | `Intl.NumberFormat.format` instead.
toLocaleString
:: forall provided
. ConvertOptionsWithDefaults
ToNumberFormatOptions
{ | NumberFormatOptions }
{ | provided }
{ | NumberFormatOptions }
=> Array Locale
-> { | provided }
-> Number
-> Effect String
toLocaleString locales providedOptions =
Effect.Uncurried.runEffectFn3
_toLocaleString
locales
(NumberFormat.convertOptionsWithDefaults providedOptions)
13 changes: 13 additions & 0 deletions src/JS/LocaleSensitive/String.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

export function _localeCompare(locales, options, a, b) {
return a.localeCompare(b, locales, options);
}

export function _toLocaleLowerCase(locales, string) {
return string.toLocaleLowerCase(locales);
}

export function _toLocaleUpperCase(locales, string) {
return string.toLocaleUpperCase(locales);
}
Loading

0 comments on commit 1d08a1e

Please sign in to comment.