Skip to content

Add strongly typed options to Intl modules #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181
- Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191
- Rename `Object.empty` to `Object.make` for consistency.
- Add proper options types in `Intl` modules.

## 1.0.0

Expand Down
20 changes: 15 additions & 5 deletions src/intl/Core__Intl__Collator.res
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
type t

type options = {
usage?: [#sort | #search],
localeMathcer?: [#lookup | #"best fit"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo (occurs several times)

collation?: string,
numeric?: bool,
caseFirst?: [#upper | #lower | #"false"],
sensitivity?: [#base | #accent | #case | #variant],
ignorePunctuation?: bool,
}

@new external make: unit => t = "Intl.Collator"
@new external makeWithLocale: string => t = "Intl.Collator"
@new external makeWithLocales: array<string> => t = "Intl.Collator"
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.Collator"
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.Collator"
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.Collator"
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.Collator"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.Collator"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.Collator"

@val external supportedLocalesOf: array<string> => t = "Intl.Collator.supportedLocalesOf"
@val
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
external supportedLocalesOfWithOptions: (array<string>, options) => t =
"Intl.Collator.supportedLocalesOf"

@send external resolvedOptions: t => {..} = "resolvedOptions"
@send external resolvedOptions: t => options = "resolvedOptions"

@send external compare: (t, string, string) => int = "compare"
33 changes: 28 additions & 5 deletions src/intl/Core__Intl__DateTimeFormat.res
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
type t

type options = {
localeMathcer?: [#lookup | #"best fit"],
calendar?: string,
numberingSystem?: string,
hour12?: bool,
hourCycle?: [#h11 | #h12 | #h23 | #h24],
timeZone?: string,
weekday?: [#long | #short | #narrow],
era?: [#long | #short | #narrow],
year?: [#numeric | #"2-digit"],
month?: [#numeric | #"2-digit" | #long | #short | #narrow],
day?: [#numeric | #"2-digit"],
dayPeriod?: [#long | #short | #narrow],
hour?: [#numeric | #"2-digit"],
minute?: [#numeric | #"2-digit"],
second?: [#numeric | #"2-digit"],
fractionalSecondDigits?: int,
timeZoneName?: [#long | #short | #shortOffset | #longOffset | #shortGeneric | #longGeneric],
formatMatcher?: [#basic | #"best fit"],
dateStyle?: [#full | #long | #medium | #short],
timeStyle?: [#full | #long | #medium | #short],
}

@new external make: unit => t = "Intl.DateTimeFormat"
@new external makeWithLocale: string => t = "Intl.DateTimeFormat"
@new external makeWithLocales: array<string> => t = "Intl.DateTimeFormat"
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.DateTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.DateTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.DateTimeFormat"
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.DateTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.DateTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat"

@val external supportedLocalesOf: array<string> => t = "Intl.DateTimeFormat.supportedLocalesOf"
@val
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
external supportedLocalesOfWithOptions: (array<string>, options) => t =
"Intl.DateTimeFormat.supportedLocalesOf"

@send external resolvedOptions: t => {..} = "resolvedOptions"
@send external resolvedOptions: t => options = "resolvedOptions"

@send external format: (t, Core__Date.t) => string = "format"
@send
Expand Down
14 changes: 13 additions & 1 deletion src/intl/Core__Intl__Locale.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
type t

type options = {
language?: string,
script?: string,
region?: string,
calendar?: string,
collation?: string,
numberingSystem?: string,
caseFirst?: [#upper | #lower | #"false"],
hourCycle?: [#h11 | #h12 | #h23 | #h24],
numeric?: bool,
}

@new external make: string => t = "Intl.Locale"
@new external makeWithOptions: (string, {..}) => t = "Intl.Locale"
@new external makeWithOptions: (string, options) => t = "Intl.Locale"

@get external baseName: t => string = "baseName"
@get external calendar: t => option<string> = "calendar"
Expand Down
44 changes: 39 additions & 5 deletions src/intl/Core__Intl__NumberFormat.res
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
type t

type options = {
localeMathcer?: [#lookup | #"best fit"],
numberingSystem?: string,
compactDisplay?: [#compact | #short | #long],
currency?: string,
currencyDisplay?: [#symbol | #narrowSymbol | #code | #name],
currencySign?: [#standard | #accounting],
notation?: [#standard | #scientific | #engineering | #compact],
signDisplay?: [#auto | #always | #exceptZero | #negative | #never],
style?: [#decimal | #currency | #percent | #unit],
unit?: string,
unitDisplay?: [#long | #short | #narrow],
useGrouping?: bool, // TODO: also [#always | #auto | #min2]
roundingMode?: [
| #ceil
| #floot
| #expand
| #trunc
| #halfCeil
| #halfFloor
| #haldExpand
| #halfTrunc
| #halfEven
],
roundingPriority?: [#auto | #morePrecision | #lessPrecision],
roundingIncrement?: int,
trailingZeroDisplay?: [#auto | #stripIfInteger],
minimumIntegerDigits?: int,
minimumFractionDigits?: int,
maximumFractionDigits?: int,
minimumSignificantDigits?: int,
maximumSignificantDigits?: int,
}

@new external make: unit => t = "Intl.NumberFormat"
@new external makeWithLocale: string => t = "Intl.NumberFormat"
@new external makeWithLocales: array<string> => t = "Intl.NumberFormat"
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.NumberFormat"
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.NumberFormat"
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.NumberFormat"
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.NumberFormat"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.NumberFormat"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.NumberFormat"

@val external supportedLocalesOf: array<string> => t = "Intl.NumberFormat.supportedLocalesOf"
@val
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
external supportedLocalesOfWithOptions: (array<string>, options) => t =
"Intl.NumberFormat.supportedLocalesOf"

@send external resolvedOptions: t => {..} = "resolvedOptions"
@send external resolvedOptions: t => options = "resolvedOptions"

@send external format: (t, float) => string = "format"
@send
Expand Down
33 changes: 28 additions & 5 deletions src/intl/Core__Intl__PluralRules.res
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
type t

type options = {
localeMathcer?: [#lookup | #"best fit"],
@as("type") type_?: [#cardinal | #ordinal],
roundingMode?: [
| #ceil
| #floot
| #expand
| #trunc
| #halfCeil
| #halfFloor
| #haldExpand
| #halfTrunc
| #halfEven
],
roundingPriority?: [#auto | #morePrecision | #lessPrecision],
roundingIncrement?: int,
minimumIntegerDigits?: int,
minimumFractionDigits?: int,
maximumFractionDigits?: int,
minimumSignificantDigits?: int,
maximumSignificantDigits?: int,
}

@new external make: unit => t = "Intl.PluralRules"
@new external makeWithLocale: string => t = "Intl.PluralRules"
@new external makeWithLocales: array<string> => t = "Intl.PluralRules"
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.PluralRules"
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.PluralRules"
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.PluralRules"
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.PluralRules"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.PluralRules"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.PluralRules"

@val external supportedLocalesOf: array<string> => t = "Intl.PluralRules.supportedLocalesOf"
@val
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
external supportedLocalesOfWithOptions: (array<string>, options) => t =
"Intl.PluralRules.supportedLocalesOf"

@send external resolvedOptions: t => {..} = "resolvedOptions"
@send external resolvedOptions: t => options = "resolvedOptions"

type rule = [#zero | #one | #two | #few | #many | #other]

Expand Down
17 changes: 12 additions & 5 deletions src/intl/Core__Intl__RelativeTimeFormat.res
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
type t

type options = {
localeMathcer?: [#lookup | #"best fit"],
numberingSystem?: string,
style?: [#long | #short | #narrow],
numeric?: [#always | #auto],
}

@new external make: unit => t = "Intl.RelativeTimeFormat"
@new external makeWithLocale: string => t = "Intl.RelativeTimeFormat"
@new external makeWithLocales: array<string> => t = "Intl.RelativeTimeFormat"
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.RelativeTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.RelativeTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.RelativeTimeFormat"
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.RelativeTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.RelativeTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.RelativeTimeFormat"

@val
external supportedLocalesOf: array<string> => t = "Intl.RelativeTimeFormat.supportedLocalesOf"
@val
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
external supportedLocalesOfWithOptions: (array<string>, options) => t =
"Intl.RelativeTimeFormat.supportedLocalesOf"

@send external resolvedOptions: t => {..} = "resolvedOptions"
@send external resolvedOptions: t => options = "resolvedOptions"

type timeUnit = [#year | #quarter | #month | #week | #day | #hour | #minute | #second]

Expand Down
11 changes: 5 additions & 6 deletions test/TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ console.log(Intl.NumberFormat.supportedLocalesOf([

console.log(currencyFormatter.format(123.23));

var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();

console.log(resolvedOptions.timeZone);

console.info("");

console.info("JSON");
Expand Down Expand Up @@ -306,10 +310,6 @@ if (globalThis.hello !== undefined) {
console.log("hello");
}

var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();

var timeZone = resolvedOptions.timeZone;

var z = 1.2 % 1.4;

var intFromBigInt = Core__BigInt.toInt(BigInt("10000000000"));
Expand All @@ -336,6 +336,7 @@ export {
dict2 ,
f ,
currencyFormatter ,
resolvedOptions ,
json ,
map ,
myObject ,
Expand All @@ -350,8 +351,6 @@ export {
x ,
array$1 as array,
timeout ,
resolvedOptions ,
timeZone ,
z ,
intFromBigInt ,
Bugfix ,
Expand Down
7 changes: 3 additions & 4 deletions test/TempTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ Console.info("Intl")
Console.info("---")
let currencyFormatter = Intl.NumberFormat.makeWithLocaleAndOptions(
"fr-FR",
{"currency": "EUR", "style": "currency"},
{currency: "EUR", style: #currency},
)

Console.log(Intl.NumberFormat.supportedLocalesOf(["fr-FR", "en-US"]))
Console.log(currencyFormatter->Intl.NumberFormat.format(123.23))
let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
Console.log(resolvedOptions.timeZone)

Console.info("")
Console.info("JSON")
Expand Down Expand Up @@ -191,9 +193,6 @@ if globalThis["hello"] !== undefined {
Console.log("hello")
}

let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
let timeZone = resolvedOptions["timeZone"]

let z = Float.mod(1.2, 1.4)

let intFromBigInt = BigInt.fromString("10000000000")->BigInt.toInt
Expand Down