Skip to content

Commit b7e8c2f

Browse files
Intl docstrings + fix of some Intl bindings (#7995)
* Intl docstrings * Fix examples * Some reverts * Changelog * Fix tests * Remove bigint plural rules select functions (not supported by most runtimes) * Changelog
1 parent fd0404d commit b7e8c2f

12 files changed

+842
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#### :boom: Breaking Change
1616

17+
- 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<string>` 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
18+
1719
#### :eyeglasses: Spec Compliance
1820

1921
#### :rocket: New Feature

packages/@rescript/runtime/Stdlib_Intl.res

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,46 @@ module Segmenter = Stdlib_Intl_Segmenter
1010
module Segments = Stdlib_Intl_Segments
1111

1212
/**
13-
@throws RangeError
13+
`getCanonicalLocalesExn(locale)` returns the canonical form of `locale`.
14+
15+
Throws `RangeError` when the locale string is malformed.
16+
17+
See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/getCanonicalLocales) on MDN.
18+
19+
## Examples
20+
21+
```rescript
22+
Intl.getCanonicalLocalesExn("EN-US") == ["en-US"]
23+
```
1424
*/
1525
external getCanonicalLocalesExn: string => array<string> = "Intl.getCanonicalLocales"
1626

1727
/**
18-
@throws RangeError
28+
`getCanonicalLocalesManyExn(locales)` canonicalises every locale in `locales`.
29+
30+
Throws `RangeError` when any locale string is malformed.
31+
32+
See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/getCanonicalLocales) on MDN.
33+
34+
## Examples
35+
36+
```rescript
37+
Intl.getCanonicalLocalesManyExn(["EN-US", "fr"]) == ["en-US", "fr"]
38+
```
1939
*/
2040
external getCanonicalLocalesManyExn: array<string> => array<string> = "Intl.getCanonicalLocales"
2141

2242
/**
23-
@throws RangeError
43+
`supportedValuesOfExn(key)` returns the list of values supported by the runtime for the feature identified by `key`.
44+
45+
Throws `RangeError` when `key` is unsupported.
46+
47+
See [`Intl.supportedValuesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf) on MDN.
48+
49+
## Examples
50+
51+
```rescript
52+
Intl.supportedValuesOfExn("calendar")->Array.includes("gregory") == true
53+
```
2454
*/
2555
external supportedValuesOfExn: string => array<string> = "Intl.supportedValuesOf"

packages/@rescript/runtime/Stdlib_Intl_Collator.res

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,60 @@ type resolvedOptions = {
2626

2727
type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher}
2828

29+
/**
30+
Creates a new `Intl.Collator` instance that can compare strings using locale-aware rules.
31+
32+
See [`Intl.Collator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) on MDN.
33+
34+
## Examples
35+
36+
```rescript
37+
let collator = Intl.Collator.make(~locales=["en"])
38+
collator->Intl.Collator.compare("apple", "banana") < 0
39+
```
40+
*/
2941
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.Collator"
3042

43+
/**
44+
`supportedLocalesOf(locales, ~options)` filters `locales` to those supported by the runtime for collation.
45+
46+
See [`Intl.Collator.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/supportedLocalesOf) on MDN.
47+
48+
## Examples
49+
50+
```rescript
51+
Intl.Collator.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"]
52+
53+
```
54+
*/
3155
@val
32-
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t =
56+
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => array<string> =
3357
"Intl.Collator.supportedLocalesOf"
3458

59+
/**
60+
`resolvedOptions(collator)` returns the locale and collation settings in use.
61+
62+
See [`Intl.Collator.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/resolvedOptions) on MDN.
63+
64+
## Examples
65+
66+
```rescript
67+
let collator = Intl.Collator.make(~locales=["en-US"])
68+
Intl.Collator.resolvedOptions(collator).locale == "en-US"
69+
```
70+
*/
3571
@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
3672

73+
/**
74+
`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.
75+
76+
## Examples
77+
78+
```rescript
79+
let collator = Intl.Collator.make(~locales=["en-US"])
80+
collator->Intl.Collator.compare("apple", "banana") < 0
81+
```
82+
*/
3783
@send external compare: (t, string, string) => int = "compare"
3884

3985
/**

packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/***
2+
Bindings to JavaScript's `Intl.DateTimeFormat`.
3+
*/
4+
15
@notUndefined
26
type t
37

@@ -106,22 +110,110 @@ type dateTimeRangePart = {
106110
source: dateTimeRangeSource,
107111
}
108112

113+
/**
114+
Creates a new `Intl.DateTimeFormat` instance for formatting date values.
115+
116+
See [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) on MDN.
117+
118+
## Examples
119+
120+
```rescript
121+
let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={timeStyle: #short})
122+
let sampleDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1)
123+
formatter->Intl.DateTimeFormat.format(sampleDate)->String.length > 0
124+
```
125+
*/
109126
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.DateTimeFormat"
110127

128+
/**
129+
`supportedLocalesOf(locales, ~options)` filters `locales` to those supported by the runtime for date/time formatting.
130+
131+
See [`Intl.DateTimeFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/supportedLocalesOf) on MDN.
132+
133+
## Examples
134+
135+
```rescript
136+
Intl.DateTimeFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"]
137+
```
138+
*/
111139
@val
112-
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t =
140+
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => array<string> =
113141
"Intl.DateTimeFormat.supportedLocalesOf"
114142

143+
/**
144+
`resolvedOptions(formatter)` returns the actual locale and formatting options in use.
145+
146+
See [`Intl.DateTimeFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions) on MDN.
147+
148+
## Examples
149+
150+
```rescript
151+
let formatter = Intl.DateTimeFormat.make(~locales=["en-US"])
152+
Intl.DateTimeFormat.resolvedOptions(formatter).locale == "en-US"
153+
```
154+
*/
115155
@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
116156

157+
/**
158+
`format(formatter, date)` returns the formatted string for `date`.
159+
160+
## Examples
161+
162+
```rescript
163+
let formatter = Intl.DateTimeFormat.make(~locales=["en"])
164+
let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1)
165+
formatter->Intl.DateTimeFormat.format(date)->String.length > 0
166+
```
167+
*/
117168
@send external format: (t, Stdlib_Date.t) => string = "format"
118-
@send
119-
external formatToParts: (t, Stdlib_Date.t) => array<dateTimePart> = "formatToParts"
120169

170+
/**
171+
`formatToParts(formatter, date)` breaks the formatted output into an array of parts.
172+
173+
See [`Intl.DateTimeFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) on MDN.
174+
175+
## Examples
176+
177+
```rescript
178+
let formatter = Intl.DateTimeFormat.make(~locales=["en"])
179+
let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1)
180+
formatter->Intl.DateTimeFormat.formatToParts(date)->Array.length > 0
181+
```
182+
*/
183+
@send external formatToParts: (t, Stdlib_Date.t) => array<dateTimePart> = "formatToParts"
184+
185+
/**
186+
`formatRange(formatter, ~startDate, ~endDate)` formats the range between `startDate` and `endDate`.
187+
188+
See [`Intl.DateTimeFormat.prototype.formatRange`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRange) on MDN.
189+
190+
## Examples
191+
192+
```rescript
193+
let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short})
194+
let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1)
195+
let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1)
196+
formatter->Intl.DateTimeFormat.formatRange(~startDate=startDate, ~endDate=endDate)->String.length > 0
197+
```
198+
*/
121199
@send
122200
external formatRange: (t, ~startDate: Stdlib_Date.t, ~endDate: Stdlib_Date.t) => string =
123201
"formatRange"
124202

203+
/**
204+
`formatRangeToParts(formatter, ~startDate, ~endDate)` returns an array describing how the range would be rendered.
205+
206+
See [`Intl.DateTimeFormat.prototype.formatRangeToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRangeToParts) on MDN.
207+
208+
## Examples
209+
210+
```rescript
211+
let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short})
212+
let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1)
213+
let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1)
214+
formatter->Intl.DateTimeFormat.formatRangeToParts(~startDate=startDate, ~endDate=endDate)->Array.length > 0
215+
```
216+
*/
125217
@send
126218
external formatRangeToParts: (
127219
t,

packages/@rescript/runtime/Stdlib_Intl_ListFormat.res

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,73 @@ type resolvedOptions = {
3636

3737
type supportedLocalesOptions = {localeMatcher: Stdlib_Intl_Common.localeMatcher}
3838

39+
/**
40+
Creates a new `Intl.ListFormat` instance for formatting lists.
41+
42+
See [`Intl.ListFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) on MDN.
43+
44+
## Examples
45+
46+
```rescript
47+
let formatter = Intl.ListFormat.make(~locales=["en"], ~options={\"type": #conjunction})
48+
formatter->Intl.ListFormat.format(["apples", "bananas", "cherries"]) == "apples, bananas, and cherries"
49+
```
50+
*/
3951
@new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.ListFormat"
4052

53+
/**
54+
`supportedLocalesOf(locales, ~options)` filters `locales` to those supported for list formatting.
55+
56+
See [`Intl.ListFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf) on MDN.
57+
58+
## Examples
59+
60+
```rescript
61+
Intl.ListFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"]
62+
```
63+
*/
4164
@val
42-
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t =
65+
external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => array<string> =
4366
"Intl.ListFormat.supportedLocalesOf"
4467

68+
/**
69+
`resolvedOptions(formatter)` returns the actual options being used.
70+
71+
See [`Intl.ListFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/resolvedOptions) on MDN.
72+
73+
## Examples
74+
75+
```rescript
76+
let formatter = Intl.ListFormat.make(~locales=["en"])
77+
Intl.ListFormat.resolvedOptions(formatter).locale == "en"
78+
```
79+
*/
4580
@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
4681

82+
/**
83+
`format(formatter, items)` returns the formatted list string.
84+
85+
## Examples
86+
87+
```rescript
88+
let formatter = Intl.ListFormat.make(~locales=["en"])
89+
formatter->Intl.ListFormat.format(["a", "b"]) == "a and b"
90+
```
91+
*/
4792
@send external format: (t, array<string>) => string = "format"
93+
94+
/**
95+
`formatToParts(formatter, items)` returns the list as an array of parts describing how it would be rendered.
96+
97+
See [`Intl.ListFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/formatToParts) on MDN.
98+
99+
## Examples
100+
101+
```rescript
102+
let formatter = Intl.ListFormat.make(~locales=["en"])
103+
formatter->Intl.ListFormat.formatToParts(["a", "b"])->Array.length > 0
104+
```
105+
*/
48106
@send external formatToParts: (t, array<string>) => array<listPart> = "formatToParts"
49107

50108
/**

0 commit comments

Comments
 (0)