diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc54aba4f..967ddce335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ #### :boom: Breaking Change +- Fix some Intl bindings (`Intl.Collator.supportedLocalesOf`, `Intl.DateTimeFormat.supportedLocalesOf`, `Intl.ListFormat.supportedLocalesOf`, `Intl.NumberFormat.supportedLocalesOf`, `Intl.PluralRules.supportedLocalesOf`, `Intl.RelativeTimeFormat.supportedLocalesOf`, `Intl.Segmenter.supportedLocalesOf`) which return `array` and not their corresponding main type `t`. Also remove `Intl.PluralRules.selectBigInt` and `Intl.PluralRules.selectRangeBigInt` which don't work in many JS runtimes. https://github.com/rescript-lang/rescript/pull/7995 + #### :eyeglasses: Spec Compliance #### :rocket: New Feature diff --git a/packages/@rescript/runtime/Stdlib_Intl.res b/packages/@rescript/runtime/Stdlib_Intl.res index 1b6d72794b..8717929d38 100644 --- a/packages/@rescript/runtime/Stdlib_Intl.res +++ b/packages/@rescript/runtime/Stdlib_Intl.res @@ -10,16 +10,46 @@ module Segmenter = Stdlib_Intl_Segmenter module Segments = Stdlib_Intl_Segments /** -@throws RangeError +`getCanonicalLocalesExn(locale)` returns the canonical form of `locale`. + +Throws `RangeError` when the locale string is malformed. + +See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/getCanonicalLocales) on MDN. + +## Examples + +```rescript +Intl.getCanonicalLocalesExn("EN-US") == ["en-US"] +``` */ external getCanonicalLocalesExn: string => array = "Intl.getCanonicalLocales" /** -@throws RangeError +`getCanonicalLocalesManyExn(locales)` canonicalises every locale in `locales`. + +Throws `RangeError` when any locale string is malformed. + +See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/getCanonicalLocales) on MDN. + +## Examples + +```rescript +Intl.getCanonicalLocalesManyExn(["EN-US", "fr"]) == ["en-US", "fr"] +``` */ external getCanonicalLocalesManyExn: array => array = "Intl.getCanonicalLocales" /** -@throws RangeError +`supportedValuesOfExn(key)` returns the list of values supported by the runtime for the feature identified by `key`. + +Throws `RangeError` when `key` is unsupported. + +See [`Intl.supportedValuesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf) on MDN. + +## Examples + +```rescript +Intl.supportedValuesOfExn("calendar")->Array.includes("gregory") == true +``` */ external supportedValuesOfExn: string => array = "Intl.supportedValuesOf" diff --git a/packages/@rescript/runtime/Stdlib_Intl_Collator.res b/packages/@rescript/runtime/Stdlib_Intl_Collator.res index 4329a6f1c5..3626e96bc6 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_Collator.res +++ b/packages/@rescript/runtime/Stdlib_Intl_Collator.res @@ -26,14 +26,60 @@ type resolvedOptions = { type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher} +/** +Creates a new `Intl.Collator` instance that can compare strings using locale-aware rules. + +See [`Intl.Collator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) on MDN. + +## Examples + +```rescript +let collator = Intl.Collator.make(~locales=["en"]) +collator->Intl.Collator.compare("apple", "banana") < 0 +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.Collator" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported by the runtime for collation. + +See [`Intl.Collator.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.Collator.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] + +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.Collator.supportedLocalesOf" +/** +`resolvedOptions(collator)` returns the locale and collation settings in use. + +See [`Intl.Collator.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/resolvedOptions) on MDN. + +## Examples + +```rescript +let collator = Intl.Collator.make(~locales=["en-US"]) +Intl.Collator.resolvedOptions(collator).locale == "en-US" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`compare(collator, a, b)` compares two strings using the rules of `collator`. Returns a negative number when `a` comes before `b`, `0` when equal, and a positive number otherwise. + +## Examples + +```rescript +let collator = Intl.Collator.make(~locales=["en-US"]) +collator->Intl.Collator.compare("apple", "banana") < 0 +``` +*/ @send external compare: (t, string, string) => int = "compare" /** diff --git a/packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res b/packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res index 00992bee49..070390d4f3 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res +++ b/packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res @@ -1,3 +1,7 @@ +/*** +Bindings to JavaScript's `Intl.DateTimeFormat`. +*/ + @notUndefined type t @@ -106,22 +110,110 @@ type dateTimeRangePart = { source: dateTimeRangeSource, } +/** +Creates a new `Intl.DateTimeFormat` instance for formatting date values. + +See [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) on MDN. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={timeStyle: #short}) +let sampleDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) +formatter->Intl.DateTimeFormat.format(sampleDate)->String.length > 0 +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.DateTimeFormat" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported by the runtime for date/time formatting. + +See [`Intl.DateTimeFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.DateTimeFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.DateTimeFormat.supportedLocalesOf" +/** +`resolvedOptions(formatter)` returns the actual locale and formatting options in use. + +See [`Intl.DateTimeFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions) on MDN. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"]) +Intl.DateTimeFormat.resolvedOptions(formatter).locale == "en-US" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`format(formatter, date)` returns the formatted string for `date`. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en"]) +let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) +formatter->Intl.DateTimeFormat.format(date)->String.length > 0 +``` +*/ @send external format: (t, Stdlib_Date.t) => string = "format" -@send -external formatToParts: (t, Stdlib_Date.t) => array = "formatToParts" +/** +`formatToParts(formatter, date)` breaks the formatted output into an array of parts. + +See [`Intl.DateTimeFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) on MDN. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en"]) +let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) +formatter->Intl.DateTimeFormat.formatToParts(date)->Array.length > 0 +``` +*/ +@send external formatToParts: (t, Stdlib_Date.t) => array = "formatToParts" + +/** +`formatRange(formatter, ~startDate, ~endDate)` formats the range between `startDate` and `endDate`. + +See [`Intl.DateTimeFormat.prototype.formatRange`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRange) on MDN. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short}) +let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) +let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1) +formatter->Intl.DateTimeFormat.formatRange(~startDate=startDate, ~endDate=endDate)->String.length > 0 +``` +*/ @send external formatRange: (t, ~startDate: Stdlib_Date.t, ~endDate: Stdlib_Date.t) => string = "formatRange" +/** +`formatRangeToParts(formatter, ~startDate, ~endDate)` returns an array describing how the range would be rendered. + +See [`Intl.DateTimeFormat.prototype.formatRangeToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRangeToParts) on MDN. + +## Examples + +```rescript +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short}) +let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) +let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1) +formatter->Intl.DateTimeFormat.formatRangeToParts(~startDate=startDate, ~endDate=endDate)->Array.length > 0 +``` +*/ @send external formatRangeToParts: ( t, diff --git a/packages/@rescript/runtime/Stdlib_Intl_ListFormat.res b/packages/@rescript/runtime/Stdlib_Intl_ListFormat.res index 3f4b0cb41f..86715d4012 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_ListFormat.res +++ b/packages/@rescript/runtime/Stdlib_Intl_ListFormat.res @@ -36,15 +36,73 @@ type resolvedOptions = { type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher} +/** +Creates a new `Intl.ListFormat` instance for formatting lists. + +See [`Intl.ListFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) on MDN. + +## Examples + +```rescript +let formatter = Intl.ListFormat.make(~locales=["en"], ~options={\"type": #conjunction}) +formatter->Intl.ListFormat.format(["apples", "bananas", "cherries"]) == "apples, bananas, and cherries" +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.ListFormat" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for list formatting. + +See [`Intl.ListFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.ListFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.ListFormat.supportedLocalesOf" +/** +`resolvedOptions(formatter)` returns the actual options being used. + +See [`Intl.ListFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/resolvedOptions) on MDN. + +## Examples + +```rescript +let formatter = Intl.ListFormat.make(~locales=["en"]) +Intl.ListFormat.resolvedOptions(formatter).locale == "en" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`format(formatter, items)` returns the formatted list string. + +## Examples + +```rescript +let formatter = Intl.ListFormat.make(~locales=["en"]) +formatter->Intl.ListFormat.format(["a", "b"]) == "a and b" +``` +*/ @send external format: (t, array) => string = "format" + +/** +`formatToParts(formatter, items)` returns the list as an array of parts describing how it would be rendered. + +See [`Intl.ListFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/formatToParts) on MDN. + +## Examples + +```rescript +let formatter = Intl.ListFormat.make(~locales=["en"]) +formatter->Intl.ListFormat.formatToParts(["a", "b"])->Array.length > 0 +``` +*/ @send external formatToParts: (t, array) => array = "formatToParts" /** diff --git a/packages/@rescript/runtime/Stdlib_Intl_Locale.res b/packages/@rescript/runtime/Stdlib_Intl_Locale.res index f250413f53..0e4bf97f75 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_Locale.res +++ b/packages/@rescript/runtime/Stdlib_Intl_Locale.res @@ -1,3 +1,7 @@ +/*** +Bindings to JavaScript's `Intl.Locale`. +*/ + @notUndefined type t @@ -14,20 +18,186 @@ type options = { region?: string, } +/** +Creates a new `Intl.Locale` object from a locale identifier and optional modifiers. + +See [`Intl.Locale`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en-US") +locale->Intl.Locale.language == "en" +``` +*/ @new external make: (string, ~options: options=?) => t = "Intl.Locale" +/** +`baseName(locale)` returns the canonical base name (without Unicode extensions). + +See [`Intl.Locale.prototype.baseName`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/baseName) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("fr-CA") +locale->Intl.Locale.baseName == "fr-CA" +``` +*/ @get external baseName: t => string = "baseName" + +/** +`calendar(locale)` returns the specified calendar, if present. + +See [`Intl.Locale.prototype.calendar`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={calendar: #gregory}) +locale->Intl.Locale.calendar == Some("gregory") +``` +*/ @get external calendar: t => option = "calendar" + +/** +`caseFirst(locale)` returns the case-first ordering setting, if present. + +See [`Intl.Locale.prototype.caseFirst`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/caseFirst) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={caseFirst: #upper}) +locale->Intl.Locale.caseFirst == Some("upper") +``` +*/ @get external caseFirst: t => option = "caseFirst" + +/** +`collation(locale)` returns the collation type, if present. + +See [`Intl.Locale.prototype.collation`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/collation) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={collation: #phonebk}) +locale->Intl.Locale.collation == Some("phonebk") +``` +*/ @get external collation: t => option = "collation" + +/** +`hourCycle(locale)` returns the preferred hour cycle, if present. + +See [`Intl.Locale.prototype.hourCycle`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={hourCycle: #h12}) +locale->Intl.Locale.hourCycle == Some("h12") +``` +*/ @get external hourCycle: t => option = "hourCycle" + +/** +`language(locale)` returns the primary language subtag. + +See [`Intl.Locale.prototype.language`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/language) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("pt-BR") +locale->Intl.Locale.language == "pt" +``` +*/ @get external language: t => string = "language" + +/** +`numberingSystem(locale)` returns the numbering system identifier, if present. + +See [`Intl.Locale.prototype.numberingSystem`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numberingSystem) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={numberingSystem: #latn}) +locale->Intl.Locale.numberingSystem == Some("latn") +``` +*/ @get external numberingSystem: t => option = "numberingSystem" + +/** +`numeric(locale)` indicates whether numeric ordering should be used for region subtags. + +See [`Intl.Locale.prototype.numeric`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numeric) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en", ~options={numeric: true}) +locale->Intl.Locale.numeric == true +``` +*/ @get external numeric: t => bool = "numeric" + +/** +`region(locale)` returns the region subtag, if present. + +See [`Intl.Locale.prototype.region`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/region) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en-US") +locale->Intl.Locale.region == Some("US") +``` +*/ @get external region: t => option = "region" + +/** +`script(locale)` returns the script subtag, if present. + +See [`Intl.Locale.prototype.script`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/script) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("sr-Cyrl") +locale->Intl.Locale.script == Some("Cyrl") +``` +*/ @get external script: t => option = "script" +/** +`maximize(locale)` adds likely subtags to produce the most specific locale. + +See [`Intl.Locale.prototype.maximize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/maximize) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en") +locale->Intl.Locale.maximize->Intl.Locale.region == Some("US") +``` +*/ @send external maximize: t => t = "maximize" + +/** +`minimize(locale)` removes unnecessary subtags while preserving semantics. + +See [`Intl.Locale.prototype.minimize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/minimize) on MDN. + +## Examples + +```rescript +let locale = Intl.Locale.make("en-Latn-US") +locale->Intl.Locale.minimize->Intl.Locale.baseName == "en" +``` +*/ @send external minimize: t => t = "minimize" /** diff --git a/packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res b/packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res index 4962de43d4..bd9d53da77 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res +++ b/packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res @@ -167,49 +167,218 @@ type numberFormatRangePart = { source: rangeSource, } +/** +Creates a new `Intl.NumberFormat` instance for locale-aware number formatting. + +See [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) on MDN. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"], ~options={style: #currency, currency: "USD"}) +formatter->Intl.NumberFormat.format(1234.5) == "$1,234.50" +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.NumberFormat" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for number formatting. + +See [`Intl.NumberFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.NumberFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.NumberFormat.supportedLocalesOf" +/** +`resolvedOptions(formatter)` returns the actual options being used. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"]) +Intl.NumberFormat.resolvedOptions(formatter).locale == "en-US" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`format(formatter, value)` returns the formatted representation of `value`. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"]) +formatter->Intl.NumberFormat.format(1234.5) == "1,234.5" +``` +*/ @send external format: (t, float) => string = "format" +/** +`formatRange(formatter, ~start, ~end)` formats numbers representing a range. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"]) +formatter->Intl.NumberFormat.formatRange(~start=1., ~end=2.)->String.length > 0 +``` +*/ @send -external formatRange: (t, ~start: float, ~end: float) => array = "formatRange" +external formatRange: (t, ~start: float, ~end: float) => string = "formatRange" +/** +`formatToParts(formatter, value)` breaks the formatted result into parts. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"]) +formatter->Intl.NumberFormat.formatToParts(123)->Array.length > 0 +``` +*/ @send external formatToParts: (t, float) => array = "formatToParts" +/** +`formatRangeToParts(formatter, ~start, ~end)` returns how the range would be rendered. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en-US"]) +formatter->Intl.NumberFormat.formatRangeToParts(~start=1., ~end=2.)->Array.length > 0 +``` +*/ @send external formatRangeToParts: (t, ~start: float, ~end: float) => array = - "formatRange" + "formatRangeToParts" +/** +`formatInt(formatter, value)` formats integer values. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatInt(42) == "42" +``` +*/ @send external formatInt: (t, int) => string = "format" +/** +`formatIntRange(formatter, ~start, ~end)` formats integer ranges. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatIntRange(~start=1, ~end=3)->String.length > 0 +``` +*/ @send -external formatIntRange: (t, ~start: int, ~end: int) => array = "formatRange" +external formatIntRange: (t, ~start: int, ~end: int) => string = "formatRange" +/** +`formatIntToParts(formatter, value)` returns formatting parts for an integer. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatIntToParts(123)->Array.length > 0 +``` +*/ @send external formatIntToParts: (t, int) => array = "formatToParts" +/** +`formatIntRangeToParts(formatter, ~start, ~end)` returns how the integer range would be rendered. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatIntRangeToParts(~start=1, ~end=1)->Array.length > 0 +``` +*/ @send external formatIntRangeToParts: (t, ~start: int, ~end: int) => array = - "formatRange" + "formatRangeToParts" +/** +`formatBigInt(formatter, value)` formats bigint values. +*/ @send external formatBigInt: (t, bigint) => string = "format" +/** +`formatBigIntRange(formatter, ~start, ~end)` formats a range of bigint values. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatBigIntRange(~start=1n, ~end=2n) == "1–2" +``` +*/ @send -external formatBigIntRange: (t, ~start: bigint, ~end: bigint) => array = "formatRange" +external formatBigIntRange: (t, ~start: bigint, ~end: bigint) => string = "formatRange" +/** +`formatBigIntToParts(formatter, value)` returns the bigint formatting broken into parts. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatBigIntToParts(5n)->Array.length > 0 +``` +*/ @send external formatBigIntToParts: (t, bigint) => array = "formatToParts" +/** +`formatBigIntRangeToParts(formatter, ~start, ~end)` describes how the bigint range would be rendered. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatBigIntRangeToParts(~start=3n, ~end=4n)->Array.length > 0 +``` +*/ @send -external formatBigIntRangeToParts: (t, ~start: bigint, ~end: bigint) => array = - "formatRange" +external formatBigIntRangeToParts: ( + t, + ~start: bigint, + ~end: bigint, +) => array = "formatRangeToParts" +/** +`formatString(formatter, value)` interprets `value` as a number string and formats it. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatString("1234") == "1,234" +``` +*/ @send external formatString: (t, string) => string = "format" +/** +`formatStringToParts(formatter, value)` returns formatting parts for a numeric string. + +## Examples + +```rescript +let formatter = Intl.NumberFormat.make(~locales=["en"]) +formatter->Intl.NumberFormat.formatStringToParts("123")->Array.length > 0 +``` +*/ @send -external formatStringToParts: (t, string) => array = "formatToParts" +external formatStringToParts: (t, string) => array = "formatToParts" /** `ignore(numberFormat)` ignores the provided numberFormat and returns unit. diff --git a/packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res b/packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res index f0b0c82a4a..d527a4fc8b 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res +++ b/packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res @@ -1,11 +1,53 @@ +/*** +Represents the `useGrouping` option accepted by `Intl.NumberFormat`. + +See [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) on MDN for full option details. +*/ @notUndefined type t type parsed = [#bool(bool) | #always | #auto | #min2] +/** +Constructs a grouping setting from a boolean. + +## Examples + +```rescript +let formatter = + Intl.NumberFormat.make( + ~locales=["en-US"], + ~options={useGrouping: Intl.NumberFormat.Grouping.fromBool(false)}, + ) +formatter->Intl.NumberFormat.format(1234.) == "1234" +``` +*/ external fromBool: bool => t = "%identity" +/** +Constructs a grouping setting from a string literal. + +## Examples + +```rescript +let formatter = + Intl.NumberFormat.make( + ~locales=["en-US"], + ~options={useGrouping: Intl.NumberFormat.Grouping.fromString(#always)}, + ) +formatter->Intl.NumberFormat.format(1234.) == "1,234" +``` +*/ external fromString: [#always | #auto | #min2] => t = "%identity" +/** +`parseJsValue(value)` attempts to interpret a JavaScript `value` as a grouping setting. + +## Examples + +```rescript +Intl.NumberFormat.Grouping.parseJsValue(Js.Json.string("auto")) == Some(#auto) +``` +*/ let parseJsValue = value => switch Stdlib_Type.Classify.classify(value) { | String("always") => Some(#always) diff --git a/packages/@rescript/runtime/Stdlib_Intl_PluralRules.res b/packages/@rescript/runtime/Stdlib_Intl_PluralRules.res index 49348b9aac..c94c534c76 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_PluralRules.res +++ b/packages/@rescript/runtime/Stdlib_Intl_PluralRules.res @@ -39,28 +39,98 @@ type resolvedOptions = { type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher} +/** +Creates a new `Intl.PluralRules` instance to determine plural categories. + +See [`Intl.PluralRules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules) on MDN. + +## Examples + +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +let category = rules->Intl.PluralRules.select(1.0) +category == #one +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.PluralRules" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for plural rules. + +See [`Intl.PluralRules.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.PluralRules.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.PluralRules.supportedLocalesOf" +/** +`resolvedOptions(rules)` returns the plural rule configuration in use. + +## Examples + +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +Intl.PluralRules.resolvedOptions(rules).locale == "en" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" type rule = [#zero | #one | #two | #few | #many | #other] +/** +`select(rules, value)` returns the plural category for the given number. + +## Examples + +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +rules->Intl.PluralRules.select(1.) == #one +``` +*/ @send external select: (t, float) => rule = "select" +/** +`selectInt(rules, value)` is like `select` but accepts an integer. + +## Examples + +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +rules->Intl.PluralRules.selectInt(2) == #other +``` +*/ @send external selectInt: (t, int) => rule = "select" -@send external selectBigInt: (t, bigint) => rule = "select" +/** +`selectRange(rules, ~start, ~end)` returns the category for numbers in the range. + +## Examples + +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +rules->Intl.PluralRules.selectRange(~start=1., ~end=2.) == #other +``` +*/ @send external selectRange: (t, ~start: float, ~end: float) => rule = "selectRange" -@send -external selectRangeInt: (t, ~start: int, ~end: int) => rule = "selectRange" +/** +`selectRangeInt(rules, ~start, ~end)` is the integer version of `selectRange`. + +## Examples +```rescript +let rules = Intl.PluralRules.make(~locales=["en"]) +rules->Intl.PluralRules.selectRangeInt(~start=1, ~end=1) == #other +``` +*/ @send -external selectRangeBigInt: (t, ~start: bigint, ~end: bigint) => rule = "selectRange" +external selectRangeInt: (t, ~start: int, ~end: int) => rule = "selectRange" /** `ignore(pluralRules)` ignores the provided pluralRules and returns unit. diff --git a/packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res b/packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res index 2454473e36..37fa2f5ae9 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res +++ b/packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res @@ -1,3 +1,8 @@ +/*** +Bindings to JavaScript's `Intl.RelativeTimeFormat`. + +See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat) for API details. +*/ @notUndefined type t @@ -27,18 +32,75 @@ type relativeTimePart = { unit?: timeUnit, } +/** +Creates a new `Intl.RelativeTimeFormat` instance for formatting relative time strings. + +See [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat) on MDN. + +## Examples + +```rescript +let formatter = Intl.RelativeTimeFormat.make(~locales=["en-US"]) +formatter->Intl.RelativeTimeFormat.format(1, #day)->String.length > 0 +``` +*/ @new external make: (~locales: array=?, ~options: options=?) => t = "Intl.RelativeTimeFormat" +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for relative time formatting. + +See [`Intl.RelativeTimeFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/supportedLocalesOf) on MDN. + +## Examples + +```rescript +Intl.RelativeTimeFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.RelativeTimeFormat.supportedLocalesOf" +/** +`resolvedOptions(formatter)` returns the locale and options currently in use. + +See [`Intl.RelativeTimeFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/resolvedOptions) on MDN. + +## Examples + +```rescript +let formatter = Intl.RelativeTimeFormat.make(~locales=["en-US"]) +Intl.RelativeTimeFormat.resolvedOptions(formatter).locale == "en-US" +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`format(formatter, value, unit)` returns the formatted string for `value` expressed in `unit`. + +## Examples + +```rescript +let formatter = Intl.RelativeTimeFormat.make(~locales=["en"]) +formatter->Intl.RelativeTimeFormat.format(-1, #day)->String.length > 0 +``` +*/ @send external format: (t, int, timeUnit) => string = "format" -@send -external formatToParts: (t, int, timeUnit) => array = "formatToParts" + +/** +`formatToParts(formatter, value, unit)` returns an array describing how the output string is assembled. + +See [`Intl.RelativeTimeFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts) on MDN. + +## Examples + +```rescript +let formatter = Intl.RelativeTimeFormat.make(~locales=["en"]) +formatter->Intl.RelativeTimeFormat.formatToParts(-1, #day)->Array.length > 0 +``` +*/ +@send external formatToParts: (t, int, timeUnit) => array = "formatToParts" /** `ignore(relativeTimeFormat)` ignores the provided relativeTimeFormat and returns unit. diff --git a/packages/@rescript/runtime/Stdlib_Intl_Segmenter.res b/packages/@rescript/runtime/Stdlib_Intl_Segmenter.res index 16bf7a2c07..ff00a53c64 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_Segmenter.res +++ b/packages/@rescript/runtime/Stdlib_Intl_Segmenter.res @@ -1,3 +1,8 @@ +/*** +Bindings to JavaScript's `Intl.Segmenter`. + +See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) for API details. +*/ @notUndefined type t @@ -21,14 +26,63 @@ type resolvedOptions = {locale: string, granularity: granularity} type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher} -@new external make: (~locales: array=?, ~options: options=?) => t = "Intl.Segmenter" +/** +Creates a new `Intl.Segmenter` instance for segmenting strings. + +See [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) on MDN. + +## Examples + +```rescript +let segmenter = Intl.Segmenter.make(~locales=["en"], ~options={granularity: #word}) +Intl.Segmenter.resolvedOptions(segmenter).granularity == #word +``` +*/ +@new +external make: (~locales: array=?, ~options: options=?) => t = "Intl.Segmenter" + +/** +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for segmentation. + +See [`Intl.Segmenter.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/supportedLocalesOf) on MDN. + +## Examples +```rescript +Intl.Segmenter.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] +``` +*/ @val -external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => t = +external supportedLocalesOf: (array, ~options: supportedLocalesOptions=?) => array = "Intl.Segmenter.supportedLocalesOf" +/** +`resolvedOptions(segmenter)` returns the locale and granularity currently in use. + +See [`Intl.Segmenter.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/resolvedOptions) on MDN. + +## Examples + +```rescript +let segmenter = Intl.Segmenter.make(~locales=["en"], ~options={granularity: #sentence}) +Intl.Segmenter.resolvedOptions(segmenter).granularity == #sentence +``` +*/ @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" +/** +`segment(segmenter, input)` returns a `Segments` object describing `input`. + +See [`Intl.Segmenter.prototype.segment`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/segment) on MDN. + +## Examples + +```rescript +let segmenter = Intl.Segmenter.make(~locales=["en"], ~options={granularity: #word}) +let segments = segmenter->Intl.Segmenter.segment("Hello world") +Intl.Segments.containingWithIndex(segments, 0).segment == "Hello" +``` +*/ @send external segment: (t, string) => Stdlib_Intl_Segments.t = "segment" /** diff --git a/packages/@rescript/runtime/Stdlib_Intl_Segments.res b/packages/@rescript/runtime/Stdlib_Intl_Segments.res index 39f2ff7269..319046c948 100644 --- a/packages/@rescript/runtime/Stdlib_Intl_Segments.res +++ b/packages/@rescript/runtime/Stdlib_Intl_Segments.res @@ -1,6 +1,9 @@ /*** +Bindings to `Segments` objects produced by `Intl.Segmenter.segment`. A Segments instance is an object that represents the segments of a specific string, subject to the locale and options of its constructing Intl.Segmenter instance. -https://tc39.es/ecma402/#sec-segments-objects +https://tc39.es/ecma402/#sec-segments-objects. + +See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/segment) for API details and the [ECMA-402 specification](https://tc39.es/ecma402/#sec-segments-objects) for the object shape. */ @notUndefined type t @@ -12,9 +15,25 @@ type segmentData = { input: string, } +/** +`containing(segments)` returns the segment data for the index supplied when iterating. + +Use this when consuming `Segments` via iteration helpers where the index is already implied. +*/ @send external containing: t => segmentData = "containing" +/** +`containingWithIndex(segments, index)` returns the segment that contains `index` within the original string. + +## Examples + +```rescript +let segmenter = Intl.Segmenter.make(~locales=["en"], ~options={granularity: #word}) +let segments = segmenter->Intl.Segmenter.segment("Hello world") +Intl.Segments.containingWithIndex(segments, 0).segment == "Hello" +``` +*/ @send external containingWithIndex: (t, int) => segmentData = "containing"