From 4589d1accce3b3d0f3b327f57b9c375022438224 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 09:51:54 +0100 Subject: [PATCH 1/7] feat(Intl.DateTimeFormat): strongly typed options --- src/intl/Core__Intl__DateTimeFormat.res | 33 +++++++++++++++++++++---- test/TempTests.mjs | 11 ++++----- test/TempTests.res | 5 ++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/intl/Core__Intl__DateTimeFormat.res b/src/intl/Core__Intl__DateTimeFormat.res index 97a5b158..e6fd8f22 100644 --- a/src/intl/Core__Intl__DateTimeFormat.res +++ b/src/intl/Core__Intl__DateTimeFormat.res @@ -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 => t = "Intl.DateTimeFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.DateTimeFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.DateTimeFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.DateTimeFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.DateTimeFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.DateTimeFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat" @val external supportedLocalesOf: array => t = "Intl.DateTimeFormat.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, 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 diff --git a/test/TempTests.mjs b/test/TempTests.mjs index 753da260..3cb38752 100644 --- a/test/TempTests.mjs +++ b/test/TempTests.mjs @@ -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"); @@ -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")); @@ -336,6 +336,7 @@ export { dict2 , f , currencyFormatter , + resolvedOptions , json , map , myObject , @@ -350,8 +351,6 @@ export { x , array$1 as array, timeout , - resolvedOptions , - timeZone , z , intFromBigInt , Bugfix , diff --git a/test/TempTests.res b/test/TempTests.res index 56bdcc40..77192f82 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -51,6 +51,8 @@ let currencyFormatter = Intl.NumberFormat.makeWithLocaleAndOptions( 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") @@ -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 From bfd977836b4c37c2a8f9c2b3b744b2a07eee5e70 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 09:55:52 +0100 Subject: [PATCH 2/7] feat(Intl.NumberFormat): strongly typed options --- src/intl/Core__Intl__NumberFormat.res | 44 ++++++++++++++++++++++++--- test/TempTests.res | 2 +- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/intl/Core__Intl__NumberFormat.res b/src/intl/Core__Intl__NumberFormat.res index 50f9866a..62b24acf 100644 --- a/src/intl/Core__Intl__NumberFormat.res +++ b/src/intl/Core__Intl__NumberFormat.res @@ -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 => t = "Intl.NumberFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.NumberFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.NumberFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.NumberFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.NumberFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.NumberFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.NumberFormat" @val external supportedLocalesOf: array => t = "Intl.NumberFormat.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, options) => t = "Intl.NumberFormat.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => options = "resolvedOptions" @send external format: (t, float) => string = "format" @send diff --git a/test/TempTests.res b/test/TempTests.res index 77192f82..3c8bbfe5 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -46,7 +46,7 @@ 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"])) From 03e4c6622cffeadc9e9a9dc615743adccfe74e87 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 10:00:24 +0100 Subject: [PATCH 3/7] feat(Intl.PluralRules): strongly typed options --- src/intl/Core__Intl__PluralRules.res | 33 +++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/intl/Core__Intl__PluralRules.res b/src/intl/Core__Intl__PluralRules.res index 6d71bcbf..f5a1f7ce 100644 --- a/src/intl/Core__Intl__PluralRules.res +++ b/src/intl/Core__Intl__PluralRules.res @@ -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 => t = "Intl.PluralRules" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.PluralRules" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.PluralRules" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.PluralRules" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.PluralRules" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.PluralRules" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.PluralRules" @val external supportedLocalesOf: array => t = "Intl.PluralRules.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, options) => t = "Intl.PluralRules.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => options = "resolvedOptions" type rule = [#zero | #one | #two | #few | #many | #other] From ccb3ab42e522f988cd1d70458e3686ff67d21f06 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 10:03:58 +0100 Subject: [PATCH 4/7] feat(Intl.Collator): strongly typed options --- src/intl/Core__Intl__Collator.res | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/intl/Core__Intl__Collator.res b/src/intl/Core__Intl__Collator.res index dce5716c..12d6c89f 100644 --- a/src/intl/Core__Intl__Collator.res +++ b/src/intl/Core__Intl__Collator.res @@ -1,17 +1,27 @@ type t +type options = { + usage?: [#sort | #search], + localeMathcer?: [#lookup | #"best fit"], + 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 => t = "Intl.Collator" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.Collator" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.Collator" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.Collator" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.Collator" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.Collator" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.Collator" @val external supportedLocalesOf: array => t = "Intl.Collator.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, options) => t = "Intl.Collator.supportedLocalesOf" -@send external resolvedOptions: t => {..} = "resolvedOptions" +@send external resolvedOptions: t => options = "resolvedOptions" @send external compare: (t, string, string) => int = "compare" From ea199ea4ebf6f393b74a1df59de000fbba6fb1c2 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 10:06:41 +0100 Subject: [PATCH 5/7] feat(Intl.RelativeTimeFormat): strongly typed options --- src/intl/Core__Intl__RelativeTimeFormat.res | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/intl/Core__Intl__RelativeTimeFormat.res b/src/intl/Core__Intl__RelativeTimeFormat.res index 8a561977..86367aac 100644 --- a/src/intl/Core__Intl__RelativeTimeFormat.res +++ b/src/intl/Core__Intl__RelativeTimeFormat.res @@ -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 => t = "Intl.RelativeTimeFormat" -@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.RelativeTimeFormat" -@new external makeWithLocalesAndOptions: (array, {..}) => t = "Intl.RelativeTimeFormat" -@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.RelativeTimeFormat" +@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.RelativeTimeFormat" +@new external makeWithLocalesAndOptions: (array, options) => t = "Intl.RelativeTimeFormat" +@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.RelativeTimeFormat" @val external supportedLocalesOf: array => t = "Intl.RelativeTimeFormat.supportedLocalesOf" @val -external supportedLocalesOfWithOptions: (array, {..}) => t = +external supportedLocalesOfWithOptions: (array, 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] From 25bd1af2724da41e9db72e90db19a93b590dc862 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 10:09:24 +0100 Subject: [PATCH 6/7] feat(Intl.Locale): strongly typed options --- src/intl/Core__Intl__Locale.res | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/intl/Core__Intl__Locale.res b/src/intl/Core__Intl__Locale.res index b2ecec9b..9494a8e9 100644 --- a/src/intl/Core__Intl__Locale.res +++ b/src/intl/Core__Intl__Locale.res @@ -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 = "calendar" From 8298258ff6e40639c17fb1df65658e20926f7847 Mon Sep 17 00:00:00 2001 From: glennsl Date: Thu, 15 Feb 2024 10:13:42 +0100 Subject: [PATCH 7/7] docs: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ce313b..2b52a5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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