Skip to content

Commit

Permalink
Simplify constructors for Intl types
Browse files Browse the repository at this point in the history
  • Loading branch information
cknitt committed Mar 4, 2024
1 parent 3298a84 commit d664fe2
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 204 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next version

- BREAKING: Intl types: simplify bindings for constructors / functions with optional arguments. https://github.com/rescript-association/rescript-core/pull/198
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197
- Add `Array.flatMapWithIndex` https://github.com/rescript-association/rescript-core/pull/199

Expand Down
12 changes: 3 additions & 9 deletions src/intl/Core__Intl__Collator.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@ type resolvedOptions = {

type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher}

@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, 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"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.Collator"

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

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
10 changes: 2 additions & 8 deletions src/intl/Core__Intl__DateTimeFormat.res
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,10 @@ type dateTimeRangePart = {
source: dateTimeRangeSource,
}

@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, options) => t = "Intl.DateTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.DateTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.DateTimeFormat"

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

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
12 changes: 3 additions & 9 deletions src/intl/Core__Intl__ListFormat.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@ type resolvedOptions = {

type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher}

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

@val external supportedLocalesOf: array<string> => t = "Intl.ListFormat.supportedLocalesOf"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.ListFormat"

@val
external supportedLocalesOfWithOptions: (array<string>, supportedLocalesOptions) => t =
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t =
"Intl.ListFormat.supportedLocalesOf"

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
3 changes: 1 addition & 2 deletions src/intl/Core__Intl__Locale.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type options = {
region?: string,
}

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

@get external baseName: t => string = "baseName"
@get external calendar: t => option<string> = "calendar"
Expand Down
11 changes: 2 additions & 9 deletions src/intl/Core__Intl__NumberFormat.res
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,10 @@ type numberFormatRangePart = {
source: rangeSource,
}

@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, 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"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.NumberFormat"

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

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
10 changes: 2 additions & 8 deletions src/intl/Core__Intl__PluralRules.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ type resolvedOptions = {

type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher}

@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, options) => t = "Intl.PluralRules"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.PluralRules"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.PluralRules"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.PluralRules"

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

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
12 changes: 3 additions & 9 deletions src/intl/Core__Intl__RelativeTimeFormat.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ type relativeTimePart = {
unit?: timeUnit,
}

@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, options) => t = "Intl.RelativeTimeFormat"
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.RelativeTimeFormat"
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.RelativeTimeFormat"
@new
external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.RelativeTimeFormat"

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

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
12 changes: 3 additions & 9 deletions src/intl/Core__Intl__Segmenter.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ type resolvedOptions = {locale: string, granularity: granularity}

type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher}

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

@val external supportedLocalesOf: array<string> => t = "Intl.Segmenter.supportedLocalesOf"
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.Segmenter"

@val
external supportedLocalesOfWithOptions: (array<string>, supportedLocalesOptions) => t =
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t =
"Intl.Segmenter.supportedLocalesOf"

@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
Expand Down
12 changes: 6 additions & 6 deletions test/intl/Intl__CollatorTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ console.log("---");

console.log("Intl.Collator");

new Intl.Collator();
new Intl.Collator(undefined, undefined);

new Intl.Collator("en-US");
new Intl.Collator(["en-US"], undefined);

var _collator = new Intl.Collator([
"en-US",
"en-GB"
]);
], undefined);

var collator = new Intl.Collator("en-US", {
var collator = new Intl.Collator(["en-US"], {
sensitivity: "base",
ignorePunctuation: true,
numeric: true,
Expand All @@ -24,7 +24,7 @@ var collator = new Intl.Collator("en-US", {
Intl.Collator.supportedLocalesOf([
"en-US",
"en-GB"
]);
], undefined);

Intl.Collator.supportedLocalesOf([
"en-US",
Expand All @@ -37,7 +37,7 @@ console.log(collator.resolvedOptions());

console.log(collator.compare("hi", "hï"));

console.log(Intl.Collator.supportedLocalesOf(["hi"]));
console.log(Intl.Collator.supportedLocalesOf(["hi"], undefined));

export {
_collator ,
Expand Down
12 changes: 6 additions & 6 deletions test/intl/Intl__CollatorTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Console.log("---")
Console.log("Intl.Collator")

let _collator = Intl.Collator.make()
let _collator = Intl.Collator.makeWithLocale("en-US")
let _collator = Intl.Collator.makeWithLocales(["en-US", "en-GB"])
let collator = Intl.Collator.makeWithLocaleAndOptions(
"en-US",
{caseFirst: #upper, sensitivity: #base, ignorePunctuation: true, numeric: true},
let _collator = Intl.Collator.make(~locales=["en-US"])
let _collator = Intl.Collator.make(~locales=["en-US", "en-GB"])
let collator = Intl.Collator.make(
~locales=["en-US"],
~options={caseFirst: #upper, sensitivity: #base, ignorePunctuation: true, numeric: true},
)
Intl.Collator.supportedLocalesOf(["en-US", "en-GB"])->ignore
Intl.Collator.supportedLocalesOfWithOptions(["en-US", "en-GB"], {localeMatcher: #lookup})->ignore
Intl.Collator.supportedLocalesOf(["en-US", "en-GB"], ~options={localeMatcher: #lookup})->ignore

collator->Intl.Collator.resolvedOptions->Console.log
collator->Intl.Collator.compare("hi", "hï")->Console.log
Expand Down
4 changes: 2 additions & 2 deletions test/intl/Intl__DateTimeFormatTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ console.log("Intl.DateTimeFormat");
Intl.DateTimeFormat.supportedLocalesOf([
"en-US",
"en-GB"
]);
], undefined);

Intl.DateTimeFormat.supportedLocalesOf([
"en-US",
Expand Down Expand Up @@ -77,7 +77,7 @@ var formatter$6 = new Intl.DateTimeFormat(undefined, (newrecord$4.timeZoneName =

console.log(formatter$6.format(new Date(Date.now())));

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

var timeZone = resolvedOptions.timeZone;

Expand Down
18 changes: 9 additions & 9 deletions test/intl/Intl__DateTimeFormatTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Console.log("---")
Console.log("Intl.DateTimeFormat")

Intl.DateTimeFormat.supportedLocalesOf(["en-US", "en-GB"])->ignore
Intl.DateTimeFormat.supportedLocalesOfWithOptions(
Intl.DateTimeFormat.supportedLocalesOf(
["en-US", "en-GB"],
{localeMatcher: #lookup},
~options={localeMatcher: #lookup},
)->ignore

let formatter = Intl.DateTimeFormat.makeWithOptions({dateStyle: #full, timeStyle: #full})
let formatter = Intl.DateTimeFormat.make(~options={dateStyle: #full, timeStyle: #full})

Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

Expand All @@ -35,23 +35,23 @@ let options: Intl.DateTimeFormat.options = {
fractionalSecondDigits: #3,
timeZoneName: #longGeneric,
}
let formatter = Intl.DateTimeFormat.makeWithOptions(options)
let formatter = Intl.DateTimeFormat.make(~options)

Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #long})
let formatter = Intl.DateTimeFormat.make(~options={...options, timeZoneName: #long})
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #longOffset})
let formatter = Intl.DateTimeFormat.make(~options={...options, timeZoneName: #longOffset})
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #short})
let formatter = Intl.DateTimeFormat.make(~options={...options, timeZoneName: #short})
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #shortGeneric})
let formatter = Intl.DateTimeFormat.make(~options={...options, timeZoneName: #shortGeneric})
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let formatter = Intl.DateTimeFormat.makeWithOptions({...options, timeZoneName: #shortOffset})
let formatter = Intl.DateTimeFormat.make(~options={...options, timeZoneName: #shortOffset})
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))

let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
Expand Down
13 changes: 3 additions & 10 deletions test/intl/Intl__ListFormatTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@ console.log("---");

console.log("Intl.ListFormat");

new Intl.ListFormat();

new Intl.ListFormat("en-US");
new Intl.ListFormat(undefined, undefined);

new Intl.ListFormat([
"en-US",
"en-GB"
]);

new Intl.ListFormat("en-US", {
type: "conjunction",
style: "long"
});
], undefined);

var _formatter = new Intl.ListFormat([
"en-US",
Expand All @@ -30,7 +23,7 @@ var _formatter = new Intl.ListFormat([
Intl.ListFormat.supportedLocalesOf([
"en-US",
"en-GB"
]);
], undefined);

Intl.ListFormat.supportedLocalesOf([
"en-US",
Expand Down
17 changes: 6 additions & 11 deletions test/intl/Intl__ListFormatTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ Console.log("---")
Console.log("Intl.ListFormat")

let _formatter = Intl.ListFormat.make()
let _formatter = Intl.ListFormat.makeWithLocale("en-US")
let _formatter = Intl.ListFormat.makeWithLocales(["en-US", "en-GB"])
let _formatter = Intl.ListFormat.makeWithLocaleAndOptions(
"en-US",
{style: #long, \"type": #conjunction},
)
let _formatter = Intl.ListFormat.makeWithLocalesAndOptions(
["en-US", "en-GB"],
{style: #long, \"type": #conjunction},
let _formatter = Intl.ListFormat.make(~locales=["en-US", "en-GB"])
let _formatter = Intl.ListFormat.make(
~locales=["en-US", "en-GB"],
~options={style: #long, \"type": #conjunction},
)
Intl.ListFormat.supportedLocalesOf(["en-US", "en-GB"])->ignore
Intl.ListFormat.supportedLocalesOfWithOptions(["en-US", "en-GB"], {localeMatcher: #lookup})->ignore
Intl.ListFormat.supportedLocalesOf(["en-US", "en-GB"], ~options={localeMatcher: #lookup})->ignore

let formatter = Intl.ListFormat.makeWithOptions({style: #long, \"type": #conjunction})
let formatter = Intl.ListFormat.make(~options={style: #long, \"type": #conjunction})

formatter->Intl.ListFormat.resolvedOptions->Console.log
formatter->Intl.ListFormat.format(["one", "two", "three"])->Console.log
Expand Down
2 changes: 1 addition & 1 deletion test/intl/Intl__LocaleTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ console.log("---");

console.log("Intl.Locale");

var _locale = new Intl.Locale("en-US");
var _locale = new Intl.Locale("en-US", undefined);

var locale = new Intl.Locale("en-US", {
calendar: "hebrew",
Expand Down
4 changes: 2 additions & 2 deletions test/intl/Intl__LocaleTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Console.log("---")
Console.log("Intl.Locale")

let _locale = Intl.Locale.make("en-US")
let locale = Intl.Locale.makeWithOptions(
let locale = Intl.Locale.make(
"en-US",
{
~options={
calendar: #hebrew,
collation: #compat,
hourCycle: #h24,
Expand Down
4 changes: 2 additions & 2 deletions test/intl/Intl__NumberFormatTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ console.log("---");

console.log("Intl.NumberFormat");

var currencyFormatter = new Intl.NumberFormat("fr-FR", {
var currencyFormatter = new Intl.NumberFormat(["fr-FR"], {
currency: "EUR",
style: "currency"
});

console.log(Intl.NumberFormat.supportedLocalesOf([
"fr-FR",
"en-US"
]));
], undefined));

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

Expand Down
Loading

0 comments on commit d664fe2

Please sign in to comment.