Skip to content

Commit bc18ff7

Browse files
committed
feat(Intl.DateTimeFormat): strongly typed options
1 parent 154f9f2 commit bc18ff7

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

src/intl/Core__Intl__DateTimeFormat.res

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
type t
22

3+
type options = {
4+
localeMathcer?: [#lookup | #"best fit"],
5+
calendar?: string,
6+
numberingSystem?: string,
7+
hour12?: bool,
8+
hourCycle?: [#h11 | #h12 | #h23 | #h24],
9+
timeZone?: string,
10+
weekday?: [#long | #short | #narrow],
11+
era?: [#long | #short | #narrow],
12+
year?: [#numeric | #"2-digit"],
13+
month?: [#numeric | #"2-digit" | #long | #short | #narrow],
14+
day?: [#numeric | #"2-digit"],
15+
dayPeriod?: [#long | #short | #narrow],
16+
hour?: [#numeric | #"2-digit"],
17+
minute?: [#numeric | #"2-digit"],
18+
second?: [#numeric | #"2-digit"],
19+
fractionalSecondDigits?: int,
20+
timeZoneName?: [#long | #short | #shortOffset | #longOffset | #shortGeneric | #longGeneric],
21+
formatMatcher?: [#basic | #"best fit"],
22+
dateStyle?: [#full | #long | #medium | #short],
23+
timeStyle?: [#full | #long | #medium | #short],
24+
}
25+
326
@new external make: unit => t = "Intl.DateTimeFormat"
427
@new external makeWithLocale: string => t = "Intl.DateTimeFormat"
528
@new external makeWithLocales: array<string> => t = "Intl.DateTimeFormat"
6-
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.DateTimeFormat"
7-
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.DateTimeFormat"
8-
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.DateTimeFormat"
29+
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.DateTimeFormat"
30+
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.DateTimeFormat"
31+
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat"
932

1033
@val external supportedLocalesOf: array<string> => t = "Intl.DateTimeFormat.supportedLocalesOf"
1134
@val
12-
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
35+
external supportedLocalesOfWithOptions: (array<string>, options) => t =
1336
"Intl.DateTimeFormat.supportedLocalesOf"
1437

15-
@send external resolvedOptions: t => {..} = "resolvedOptions"
38+
@send external resolvedOptions: t => options = "resolvedOptions"
1639

1740
@send external format: (t, Core__Date.t) => string = "format"
1841
@send

test/TempTests.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ console.log(Intl.NumberFormat.supportedLocalesOf([
109109

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

112+
var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();
113+
114+
console.log(resolvedOptions.timeZone);
115+
112116
console.info("");
113117

114118
console.info("JSON");
@@ -306,10 +310,6 @@ if (globalThis.hello !== undefined) {
306310
console.log("hello");
307311
}
308312

309-
var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();
310-
311-
var timeZone = resolvedOptions.timeZone;
312-
313313
var z = 1.2 % 1.4;
314314

315315
var intFromBigInt = Core__BigInt.toInt(BigInt("10000000000"));
@@ -336,6 +336,7 @@ export {
336336
dict2 ,
337337
f ,
338338
currencyFormatter ,
339+
resolvedOptions ,
339340
json ,
340341
map ,
341342
myObject ,
@@ -350,8 +351,6 @@ export {
350351
x ,
351352
array$1 as array,
352353
timeout ,
353-
resolvedOptions ,
354-
timeZone ,
355354
z ,
356355
intFromBigInt ,
357356
Bugfix ,

test/TempTests.res

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ let currencyFormatter = Intl.NumberFormat.makeWithLocaleAndOptions(
5151

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

5557
Console.info("")
5658
Console.info("JSON")
@@ -191,9 +193,6 @@ if globalThis["hello"] !== undefined {
191193
Console.log("hello")
192194
}
193195

194-
let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
195-
let timeZone = resolvedOptions["timeZone"]
196-
197196
let z = Float.mod(1.2, 1.4)
198197

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

0 commit comments

Comments
 (0)