@optique/temporal 0.4.0-dev.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2025 Hong Minhee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,346 @@
1
+ const { Temporal } = require("@js-temporal/polyfill");
2
+ //#region rolldown:runtime
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+
24
+ //#endregion
25
+ const __optique_core_message = __toESM(require("@optique/core/message"));
26
+
27
+ //#region src/index.ts
28
+ /**
29
+ * Creates a ValueParser for parsing Temporal.Instant from ISO 8601 strings.
30
+ *
31
+ * Accepts strings like:
32
+ *
33
+ * - `"2020-01-23T17:04:36.491865121Z"`
34
+ * - `"2020-01-23T17:04:36Z"`
35
+ *
36
+ * @param options Configuration options for the instant parser.
37
+ * @returns A ValueParser that parses strings into Temporal.Instant values.
38
+ */
39
+ function instant(options = {}) {
40
+ return {
41
+ metavar: options.metavar ?? "TIMESTAMP",
42
+ parse(input) {
43
+ try {
44
+ const value = Temporal.Instant.from(input);
45
+ return {
46
+ success: true,
47
+ value
48
+ };
49
+ } catch {
50
+ return {
51
+ success: false,
52
+ error: __optique_core_message.message`Invalid instant: ${input}. Expected ISO 8601 format like ${"2020-01-23T17:04:36Z"}.`
53
+ };
54
+ }
55
+ },
56
+ format(value) {
57
+ return value.toString();
58
+ }
59
+ };
60
+ }
61
+ /**
62
+ * Creates a ValueParser for parsing Temporal.Duration from ISO 8601 duration strings.
63
+ *
64
+ * Accepts strings like:
65
+ *
66
+ * - `"PT1H30M"`
67
+ * - `"P1DT12H"`
68
+ * - `"PT30S"`
69
+ *
70
+ * @param options Configuration options for the duration parser.
71
+ * @returns A ValueParser that parses strings into Temporal.Duration values.
72
+ */
73
+ function duration(options = {}) {
74
+ return {
75
+ metavar: options.metavar ?? "DURATION",
76
+ parse(input) {
77
+ try {
78
+ const value = Temporal.Duration.from(input);
79
+ return {
80
+ success: true,
81
+ value
82
+ };
83
+ } catch {
84
+ return {
85
+ success: false,
86
+ error: __optique_core_message.message`Invalid duration: ${input}. Expected ISO 8601 format like ${"PT1H30M"}.`
87
+ };
88
+ }
89
+ },
90
+ format(value) {
91
+ return value.toString();
92
+ }
93
+ };
94
+ }
95
+ /**
96
+ * Creates a ValueParser for parsing Temporal.ZonedDateTime from ISO 8601 strings with timezone.
97
+ *
98
+ * Accepts strings like:
99
+ *
100
+ * - `"2020-01-23T17:04:36.491865121+01:00[Europe/Paris]"`
101
+ * - `"2020-01-23T17:04:36Z[UTC]"`
102
+ *
103
+ * @param options Configuration options for the zoned datetime parser.
104
+ * @returns A ValueParser that parses strings into Temporal.ZonedDateTime values.
105
+ */
106
+ function zonedDateTime(options = {}) {
107
+ return {
108
+ metavar: options.metavar ?? "ZONED_DATETIME",
109
+ parse(input) {
110
+ try {
111
+ const value = Temporal.ZonedDateTime.from(input);
112
+ return {
113
+ success: true,
114
+ value
115
+ };
116
+ } catch {
117
+ return {
118
+ success: false,
119
+ error: __optique_core_message.message`Invalid zoned datetime: ${input}. Expected ISO 8601 format with timezone like ${"2020-01-23T17:04:36+01:00[Europe/Paris]"}.`
120
+ };
121
+ }
122
+ },
123
+ format(value) {
124
+ return value.toString();
125
+ }
126
+ };
127
+ }
128
+ /**
129
+ * Creates a ValueParser for parsing Temporal.PlainDate from ISO 8601 date strings.
130
+ *
131
+ * Accepts strings like:
132
+ *
133
+ * - `"2020-01-23"`
134
+ * - `"2020-12-31"`
135
+ *
136
+ * @param options Configuration options for the plain date parser.
137
+ * @returns A ValueParser that parses strings into Temporal.PlainDate values.
138
+ */
139
+ function plainDate(options = {}) {
140
+ return {
141
+ metavar: options.metavar ?? "DATE",
142
+ parse(input) {
143
+ try {
144
+ const value = Temporal.PlainDate.from(input);
145
+ return {
146
+ success: true,
147
+ value
148
+ };
149
+ } catch {
150
+ return {
151
+ success: false,
152
+ error: __optique_core_message.message`Invalid date: ${input}. Expected ISO 8601 format like ${"2020-01-23"}.`
153
+ };
154
+ }
155
+ },
156
+ format(value) {
157
+ return value.toString();
158
+ }
159
+ };
160
+ }
161
+ /**
162
+ * Creates a ValueParser for parsing Temporal.PlainTime from ISO 8601 time strings.
163
+ *
164
+ * Accepts strings like:
165
+ *
166
+ * - `"17:04:36"`
167
+ * - `"17:04:36.491865121"`
168
+ *
169
+ * @param options Configuration options for the plain time parser.
170
+ * @returns A ValueParser that parses strings into Temporal.PlainTime values.
171
+ */
172
+ function plainTime(options = {}) {
173
+ return {
174
+ metavar: options.metavar ?? "TIME",
175
+ parse(input) {
176
+ try {
177
+ const value = Temporal.PlainTime.from(input);
178
+ return {
179
+ success: true,
180
+ value
181
+ };
182
+ } catch {
183
+ return {
184
+ success: false,
185
+ error: __optique_core_message.message`Invalid time: ${input}. Expected ISO 8601 format like ${"17:04:36"}.`
186
+ };
187
+ }
188
+ },
189
+ format(value) {
190
+ return value.toString();
191
+ }
192
+ };
193
+ }
194
+ /**
195
+ * Creates a ValueParser for parsing Temporal.PlainDateTime from ISO 8601 datetime strings.
196
+ *
197
+ * Accepts strings like:
198
+ *
199
+ * - `"2020-01-23T17:04:36"`
200
+ * - `"2020-01-23T17:04:36.491865121"`
201
+ *
202
+ * @param options Configuration options for the plain datetime parser.
203
+ * @returns A ValueParser that parses strings into Temporal.PlainDateTime values.
204
+ */
205
+ function plainDateTime(options = {}) {
206
+ return {
207
+ metavar: options.metavar ?? "DATETIME",
208
+ parse(input) {
209
+ try {
210
+ const value = Temporal.PlainDateTime.from(input);
211
+ return {
212
+ success: true,
213
+ value
214
+ };
215
+ } catch {
216
+ return {
217
+ success: false,
218
+ error: __optique_core_message.message`Invalid datetime: ${input}. Expected ISO 8601 format like ${"2020-01-23T17:04:36"}.`
219
+ };
220
+ }
221
+ },
222
+ format(value) {
223
+ return value.toString();
224
+ }
225
+ };
226
+ }
227
+ /**
228
+ * Creates a ValueParser for parsing Temporal.PlainYearMonth from ISO 8601 year-month strings.
229
+ *
230
+ * Accepts strings like:
231
+ *
232
+ * - `"2020-01"`
233
+ * - `"2020-12"`
234
+ *
235
+ * @param options Configuration options for the plain year-month parser.
236
+ * @returns A ValueParser that parses strings into Temporal.PlainYearMonth values.
237
+ */
238
+ function plainYearMonth(options = {}) {
239
+ return {
240
+ metavar: options.metavar ?? "YEAR-MONTH",
241
+ parse(input) {
242
+ try {
243
+ const value = Temporal.PlainYearMonth.from(input);
244
+ return {
245
+ success: true,
246
+ value
247
+ };
248
+ } catch {
249
+ return {
250
+ success: false,
251
+ error: __optique_core_message.message`Invalid year-month: ${input}. Expected ISO 8601 format like ${"2020-01"}.`
252
+ };
253
+ }
254
+ },
255
+ format(value) {
256
+ return value.toString();
257
+ }
258
+ };
259
+ }
260
+ /**
261
+ * Creates a ValueParser for parsing Temporal.PlainMonthDay from ISO 8601 month-day strings.
262
+ *
263
+ * Accepts strings like:
264
+ *
265
+ * - `"--01-23"`
266
+ * - `"--12-31"`
267
+ *
268
+ * @param options Configuration options for the plain month-day parser.
269
+ * @returns A ValueParser that parses strings into Temporal.PlainMonthDay values.
270
+ */
271
+ function plainMonthDay(options = {}) {
272
+ return {
273
+ metavar: options.metavar ?? "--MONTH-DAY",
274
+ parse(input) {
275
+ try {
276
+ const value = Temporal.PlainMonthDay.from(input);
277
+ return {
278
+ success: true,
279
+ value
280
+ };
281
+ } catch {
282
+ return {
283
+ success: false,
284
+ error: __optique_core_message.message`Invalid month-day: ${input}. Expected ISO 8601 format like ${"--01-23"}.`
285
+ };
286
+ }
287
+ },
288
+ format(value) {
289
+ return value.toString();
290
+ }
291
+ };
292
+ }
293
+ /**
294
+ * Creates a ValueParser for parsing IANA Time Zone Database identifiers.
295
+ *
296
+ * Accepts strings like:
297
+ *
298
+ * - `"Asia/Seoul"`
299
+ * - `"America/New_York"`
300
+ * - `"Europe/London"`
301
+ * - `"UTC"`
302
+ *
303
+ * @param options Configuration options for the timezone parser.
304
+ * @returns A ValueParser that parses and validates timezone identifiers.
305
+ */
306
+ function timeZone(options = {}) {
307
+ return {
308
+ metavar: options.metavar ?? "TIMEZONE",
309
+ parse(input) {
310
+ try {
311
+ Temporal.ZonedDateTime.from({
312
+ year: 2020,
313
+ month: 1,
314
+ day: 1,
315
+ hour: 0,
316
+ minute: 0,
317
+ second: 0,
318
+ timeZone: input
319
+ });
320
+ return {
321
+ success: true,
322
+ value: input
323
+ };
324
+ } catch {
325
+ return {
326
+ success: false,
327
+ error: __optique_core_message.message`Invalid timezone identifier: ${input}. Must be a valid IANA timezone like "Asia/Seoul" or "UTC".`
328
+ };
329
+ }
330
+ },
331
+ format(value) {
332
+ return value;
333
+ }
334
+ };
335
+ }
336
+
337
+ //#endregion
338
+ exports.duration = duration;
339
+ exports.instant = instant;
340
+ exports.plainDate = plainDate;
341
+ exports.plainDateTime = plainDateTime;
342
+ exports.plainMonthDay = plainMonthDay;
343
+ exports.plainTime = plainTime;
344
+ exports.plainYearMonth = plainYearMonth;
345
+ exports.timeZone = timeZone;
346
+ exports.zonedDateTime = zonedDateTime;
@@ -0,0 +1,243 @@
1
+ import { ValueParser } from "@optique/core/valueparser";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ /**
6
+ * IANA Time Zone Database identifier.
7
+ *
8
+ * Represents valid timezone identifiers that follow the IANA timezone naming
9
+ * convention:
10
+ *
11
+ * - Two-level: `"Asia/Seoul"`, `"America/New_York"`, `"Europe/London"`
12
+ * - Three-level: `"America/Argentina/Buenos_Aires"`, `"America/Kentucky/Louisville"`
13
+ * - Special case: `"UTC"`
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const seoul: TimeZone = "Asia/Seoul";
18
+ * const utc: TimeZone = "UTC";
19
+ * const buenosAires: TimeZone = "America/Argentina/Buenos_Aires";
20
+ * ```
21
+ */
22
+ type TimeZone = `${string}/${string}/${string}` | `${string}/${string}` | "UTC";
23
+ /**
24
+ * Options for creating an instant parser.
25
+ */
26
+ interface InstantOptions {
27
+ /**
28
+ * The metavariable name for this parser. This is used in help messages to
29
+ * indicate what kind of value this parser expects. Usually a single
30
+ * word in uppercase, like `TIMESTAMP` or `INSTANT`.
31
+ * @default `"TIMESTAMP"`
32
+ */
33
+ readonly metavar?: string;
34
+ }
35
+ /**
36
+ * Options for creating a duration parser.
37
+ */
38
+ interface DurationOptions {
39
+ /**
40
+ * The metavariable name for this parser. This is used in help messages to
41
+ * indicate what kind of value this parser expects. Usually a single
42
+ * word in uppercase, like `DURATION`.
43
+ * @default `"DURATION"`
44
+ */
45
+ readonly metavar?: string;
46
+ }
47
+ /**
48
+ * Options for creating a zoned datetime parser.
49
+ */
50
+ interface ZonedDateTimeOptions {
51
+ /**
52
+ * The metavariable name for this parser. This is used in help messages to
53
+ * indicate what kind of value this parser expects. Usually a single
54
+ * word in uppercase, like `ZONED_DATETIME`.
55
+ * @default `"ZONED_DATETIME"`
56
+ */
57
+ readonly metavar?: string;
58
+ }
59
+ /**
60
+ * Options for creating a plain date parser.
61
+ */
62
+ interface PlainDateOptions {
63
+ /**
64
+ * The metavariable name for this parser. This is used in help messages to
65
+ * indicate what kind of value this parser expects. Usually a single
66
+ * word in uppercase, like `DATE`.
67
+ * @default `"DATE"`
68
+ */
69
+ readonly metavar?: string;
70
+ }
71
+ /**
72
+ * Options for creating a plain time parser.
73
+ */
74
+ interface PlainTimeOptions {
75
+ /**
76
+ * The metavariable name for this parser. This is used in help messages to
77
+ * indicate what kind of value this parser expects. Usually a single
78
+ * word in uppercase, like `TIME`.
79
+ * @default `"TIME"`
80
+ */
81
+ readonly metavar?: string;
82
+ }
83
+ /**
84
+ * Options for creating a plain datetime parser.
85
+ */
86
+ interface PlainDateTimeOptions {
87
+ /**
88
+ * The metavariable name for this parser. This is used in help messages to
89
+ * indicate what kind of value this parser expects. Usually a single
90
+ * word in uppercase, like `DATETIME`.
91
+ * @default `"DATETIME"`
92
+ */
93
+ readonly metavar?: string;
94
+ }
95
+ /**
96
+ * Options for creating a plain year-month parser.
97
+ */
98
+ interface PlainYearMonthOptions {
99
+ /**
100
+ * The metavariable name for this parser. This is used in help messages to
101
+ * indicate what kind of value this parser expects. Usually a single
102
+ * word in uppercase, like `YEAR-MONTH`.
103
+ * @default `"YEAR-MONTH"`
104
+ */
105
+ readonly metavar?: string;
106
+ }
107
+ /**
108
+ * Options for creating a plain month-day parser.
109
+ */
110
+ interface PlainMonthDayOptions {
111
+ /**
112
+ * The metavariable name for this parser. This is used in help messages to
113
+ * indicate what kind of value this parser expects. Usually a single
114
+ * word in uppercase, like `--MONTH-DAY`.
115
+ * @default `"--MONTH-DAY"`
116
+ */
117
+ readonly metavar?: string;
118
+ }
119
+ /**
120
+ * Options for creating a timezone parser.
121
+ */
122
+ interface TimeZoneOptions {
123
+ /**
124
+ * The metavariable name for this parser. This is used in help messages to
125
+ * indicate what kind of value this parser expects. Usually a single
126
+ * word in uppercase, like `TIMEZONE`.
127
+ * @default `"TIMEZONE"`
128
+ */
129
+ readonly metavar?: string;
130
+ }
131
+ /**
132
+ * Creates a ValueParser for parsing Temporal.Instant from ISO 8601 strings.
133
+ *
134
+ * Accepts strings like:
135
+ *
136
+ * - `"2020-01-23T17:04:36.491865121Z"`
137
+ * - `"2020-01-23T17:04:36Z"`
138
+ *
139
+ * @param options Configuration options for the instant parser.
140
+ * @returns A ValueParser that parses strings into Temporal.Instant values.
141
+ */
142
+ declare function instant(options?: InstantOptions): ValueParser<Temporal.Instant>;
143
+ /**
144
+ * Creates a ValueParser for parsing Temporal.Duration from ISO 8601 duration strings.
145
+ *
146
+ * Accepts strings like:
147
+ *
148
+ * - `"PT1H30M"`
149
+ * - `"P1DT12H"`
150
+ * - `"PT30S"`
151
+ *
152
+ * @param options Configuration options for the duration parser.
153
+ * @returns A ValueParser that parses strings into Temporal.Duration values.
154
+ */
155
+ declare function duration(options?: DurationOptions): ValueParser<Temporal.Duration>;
156
+ /**
157
+ * Creates a ValueParser for parsing Temporal.ZonedDateTime from ISO 8601 strings with timezone.
158
+ *
159
+ * Accepts strings like:
160
+ *
161
+ * - `"2020-01-23T17:04:36.491865121+01:00[Europe/Paris]"`
162
+ * - `"2020-01-23T17:04:36Z[UTC]"`
163
+ *
164
+ * @param options Configuration options for the zoned datetime parser.
165
+ * @returns A ValueParser that parses strings into Temporal.ZonedDateTime values.
166
+ */
167
+ declare function zonedDateTime(options?: ZonedDateTimeOptions): ValueParser<Temporal.ZonedDateTime>;
168
+ /**
169
+ * Creates a ValueParser for parsing Temporal.PlainDate from ISO 8601 date strings.
170
+ *
171
+ * Accepts strings like:
172
+ *
173
+ * - `"2020-01-23"`
174
+ * - `"2020-12-31"`
175
+ *
176
+ * @param options Configuration options for the plain date parser.
177
+ * @returns A ValueParser that parses strings into Temporal.PlainDate values.
178
+ */
179
+ declare function plainDate(options?: PlainDateOptions): ValueParser<Temporal.PlainDate>;
180
+ /**
181
+ * Creates a ValueParser for parsing Temporal.PlainTime from ISO 8601 time strings.
182
+ *
183
+ * Accepts strings like:
184
+ *
185
+ * - `"17:04:36"`
186
+ * - `"17:04:36.491865121"`
187
+ *
188
+ * @param options Configuration options for the plain time parser.
189
+ * @returns A ValueParser that parses strings into Temporal.PlainTime values.
190
+ */
191
+ declare function plainTime(options?: PlainTimeOptions): ValueParser<Temporal.PlainTime>;
192
+ /**
193
+ * Creates a ValueParser for parsing Temporal.PlainDateTime from ISO 8601 datetime strings.
194
+ *
195
+ * Accepts strings like:
196
+ *
197
+ * - `"2020-01-23T17:04:36"`
198
+ * - `"2020-01-23T17:04:36.491865121"`
199
+ *
200
+ * @param options Configuration options for the plain datetime parser.
201
+ * @returns A ValueParser that parses strings into Temporal.PlainDateTime values.
202
+ */
203
+ declare function plainDateTime(options?: PlainDateTimeOptions): ValueParser<Temporal.PlainDateTime>;
204
+ /**
205
+ * Creates a ValueParser for parsing Temporal.PlainYearMonth from ISO 8601 year-month strings.
206
+ *
207
+ * Accepts strings like:
208
+ *
209
+ * - `"2020-01"`
210
+ * - `"2020-12"`
211
+ *
212
+ * @param options Configuration options for the plain year-month parser.
213
+ * @returns A ValueParser that parses strings into Temporal.PlainYearMonth values.
214
+ */
215
+ declare function plainYearMonth(options?: PlainYearMonthOptions): ValueParser<Temporal.PlainYearMonth>;
216
+ /**
217
+ * Creates a ValueParser for parsing Temporal.PlainMonthDay from ISO 8601 month-day strings.
218
+ *
219
+ * Accepts strings like:
220
+ *
221
+ * - `"--01-23"`
222
+ * - `"--12-31"`
223
+ *
224
+ * @param options Configuration options for the plain month-day parser.
225
+ * @returns A ValueParser that parses strings into Temporal.PlainMonthDay values.
226
+ */
227
+ declare function plainMonthDay(options?: PlainMonthDayOptions): ValueParser<Temporal.PlainMonthDay>;
228
+ /**
229
+ * Creates a ValueParser for parsing IANA Time Zone Database identifiers.
230
+ *
231
+ * Accepts strings like:
232
+ *
233
+ * - `"Asia/Seoul"`
234
+ * - `"America/New_York"`
235
+ * - `"Europe/London"`
236
+ * - `"UTC"`
237
+ *
238
+ * @param options Configuration options for the timezone parser.
239
+ * @returns A ValueParser that parses and validates timezone identifiers.
240
+ */
241
+ declare function timeZone(options?: TimeZoneOptions): ValueParser<TimeZone>;
242
+ //#endregion
243
+ export { DurationOptions, InstantOptions, PlainDateOptions, PlainDateTimeOptions, PlainMonthDayOptions, PlainTimeOptions, PlainYearMonthOptions, TimeZone, TimeZoneOptions, ZonedDateTimeOptions, duration, instant, plainDate, plainDateTime, plainMonthDay, plainTime, plainYearMonth, timeZone, zonedDateTime };
@@ -0,0 +1,244 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { ValueParser } from "@optique/core/valueparser";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * IANA Time Zone Database identifier.
8
+ *
9
+ * Represents valid timezone identifiers that follow the IANA timezone naming
10
+ * convention:
11
+ *
12
+ * - Two-level: `"Asia/Seoul"`, `"America/New_York"`, `"Europe/London"`
13
+ * - Three-level: `"America/Argentina/Buenos_Aires"`, `"America/Kentucky/Louisville"`
14
+ * - Special case: `"UTC"`
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const seoul: TimeZone = "Asia/Seoul";
19
+ * const utc: TimeZone = "UTC";
20
+ * const buenosAires: TimeZone = "America/Argentina/Buenos_Aires";
21
+ * ```
22
+ */
23
+ type TimeZone = `${string}/${string}/${string}` | `${string}/${string}` | "UTC";
24
+ /**
25
+ * Options for creating an instant parser.
26
+ */
27
+ interface InstantOptions {
28
+ /**
29
+ * The metavariable name for this parser. This is used in help messages to
30
+ * indicate what kind of value this parser expects. Usually a single
31
+ * word in uppercase, like `TIMESTAMP` or `INSTANT`.
32
+ * @default `"TIMESTAMP"`
33
+ */
34
+ readonly metavar?: string;
35
+ }
36
+ /**
37
+ * Options for creating a duration parser.
38
+ */
39
+ interface DurationOptions {
40
+ /**
41
+ * The metavariable name for this parser. This is used in help messages to
42
+ * indicate what kind of value this parser expects. Usually a single
43
+ * word in uppercase, like `DURATION`.
44
+ * @default `"DURATION"`
45
+ */
46
+ readonly metavar?: string;
47
+ }
48
+ /**
49
+ * Options for creating a zoned datetime parser.
50
+ */
51
+ interface ZonedDateTimeOptions {
52
+ /**
53
+ * The metavariable name for this parser. This is used in help messages to
54
+ * indicate what kind of value this parser expects. Usually a single
55
+ * word in uppercase, like `ZONED_DATETIME`.
56
+ * @default `"ZONED_DATETIME"`
57
+ */
58
+ readonly metavar?: string;
59
+ }
60
+ /**
61
+ * Options for creating a plain date parser.
62
+ */
63
+ interface PlainDateOptions {
64
+ /**
65
+ * The metavariable name for this parser. This is used in help messages to
66
+ * indicate what kind of value this parser expects. Usually a single
67
+ * word in uppercase, like `DATE`.
68
+ * @default `"DATE"`
69
+ */
70
+ readonly metavar?: string;
71
+ }
72
+ /**
73
+ * Options for creating a plain time parser.
74
+ */
75
+ interface PlainTimeOptions {
76
+ /**
77
+ * The metavariable name for this parser. This is used in help messages to
78
+ * indicate what kind of value this parser expects. Usually a single
79
+ * word in uppercase, like `TIME`.
80
+ * @default `"TIME"`
81
+ */
82
+ readonly metavar?: string;
83
+ }
84
+ /**
85
+ * Options for creating a plain datetime parser.
86
+ */
87
+ interface PlainDateTimeOptions {
88
+ /**
89
+ * The metavariable name for this parser. This is used in help messages to
90
+ * indicate what kind of value this parser expects. Usually a single
91
+ * word in uppercase, like `DATETIME`.
92
+ * @default `"DATETIME"`
93
+ */
94
+ readonly metavar?: string;
95
+ }
96
+ /**
97
+ * Options for creating a plain year-month parser.
98
+ */
99
+ interface PlainYearMonthOptions {
100
+ /**
101
+ * The metavariable name for this parser. This is used in help messages to
102
+ * indicate what kind of value this parser expects. Usually a single
103
+ * word in uppercase, like `YEAR-MONTH`.
104
+ * @default `"YEAR-MONTH"`
105
+ */
106
+ readonly metavar?: string;
107
+ }
108
+ /**
109
+ * Options for creating a plain month-day parser.
110
+ */
111
+ interface PlainMonthDayOptions {
112
+ /**
113
+ * The metavariable name for this parser. This is used in help messages to
114
+ * indicate what kind of value this parser expects. Usually a single
115
+ * word in uppercase, like `--MONTH-DAY`.
116
+ * @default `"--MONTH-DAY"`
117
+ */
118
+ readonly metavar?: string;
119
+ }
120
+ /**
121
+ * Options for creating a timezone parser.
122
+ */
123
+ interface TimeZoneOptions {
124
+ /**
125
+ * The metavariable name for this parser. This is used in help messages to
126
+ * indicate what kind of value this parser expects. Usually a single
127
+ * word in uppercase, like `TIMEZONE`.
128
+ * @default `"TIMEZONE"`
129
+ */
130
+ readonly metavar?: string;
131
+ }
132
+ /**
133
+ * Creates a ValueParser for parsing Temporal.Instant from ISO 8601 strings.
134
+ *
135
+ * Accepts strings like:
136
+ *
137
+ * - `"2020-01-23T17:04:36.491865121Z"`
138
+ * - `"2020-01-23T17:04:36Z"`
139
+ *
140
+ * @param options Configuration options for the instant parser.
141
+ * @returns A ValueParser that parses strings into Temporal.Instant values.
142
+ */
143
+ declare function instant(options?: InstantOptions): ValueParser<Temporal.Instant>;
144
+ /**
145
+ * Creates a ValueParser for parsing Temporal.Duration from ISO 8601 duration strings.
146
+ *
147
+ * Accepts strings like:
148
+ *
149
+ * - `"PT1H30M"`
150
+ * - `"P1DT12H"`
151
+ * - `"PT30S"`
152
+ *
153
+ * @param options Configuration options for the duration parser.
154
+ * @returns A ValueParser that parses strings into Temporal.Duration values.
155
+ */
156
+ declare function duration(options?: DurationOptions): ValueParser<Temporal.Duration>;
157
+ /**
158
+ * Creates a ValueParser for parsing Temporal.ZonedDateTime from ISO 8601 strings with timezone.
159
+ *
160
+ * Accepts strings like:
161
+ *
162
+ * - `"2020-01-23T17:04:36.491865121+01:00[Europe/Paris]"`
163
+ * - `"2020-01-23T17:04:36Z[UTC]"`
164
+ *
165
+ * @param options Configuration options for the zoned datetime parser.
166
+ * @returns A ValueParser that parses strings into Temporal.ZonedDateTime values.
167
+ */
168
+ declare function zonedDateTime(options?: ZonedDateTimeOptions): ValueParser<Temporal.ZonedDateTime>;
169
+ /**
170
+ * Creates a ValueParser for parsing Temporal.PlainDate from ISO 8601 date strings.
171
+ *
172
+ * Accepts strings like:
173
+ *
174
+ * - `"2020-01-23"`
175
+ * - `"2020-12-31"`
176
+ *
177
+ * @param options Configuration options for the plain date parser.
178
+ * @returns A ValueParser that parses strings into Temporal.PlainDate values.
179
+ */
180
+ declare function plainDate(options?: PlainDateOptions): ValueParser<Temporal.PlainDate>;
181
+ /**
182
+ * Creates a ValueParser for parsing Temporal.PlainTime from ISO 8601 time strings.
183
+ *
184
+ * Accepts strings like:
185
+ *
186
+ * - `"17:04:36"`
187
+ * - `"17:04:36.491865121"`
188
+ *
189
+ * @param options Configuration options for the plain time parser.
190
+ * @returns A ValueParser that parses strings into Temporal.PlainTime values.
191
+ */
192
+ declare function plainTime(options?: PlainTimeOptions): ValueParser<Temporal.PlainTime>;
193
+ /**
194
+ * Creates a ValueParser for parsing Temporal.PlainDateTime from ISO 8601 datetime strings.
195
+ *
196
+ * Accepts strings like:
197
+ *
198
+ * - `"2020-01-23T17:04:36"`
199
+ * - `"2020-01-23T17:04:36.491865121"`
200
+ *
201
+ * @param options Configuration options for the plain datetime parser.
202
+ * @returns A ValueParser that parses strings into Temporal.PlainDateTime values.
203
+ */
204
+ declare function plainDateTime(options?: PlainDateTimeOptions): ValueParser<Temporal.PlainDateTime>;
205
+ /**
206
+ * Creates a ValueParser for parsing Temporal.PlainYearMonth from ISO 8601 year-month strings.
207
+ *
208
+ * Accepts strings like:
209
+ *
210
+ * - `"2020-01"`
211
+ * - `"2020-12"`
212
+ *
213
+ * @param options Configuration options for the plain year-month parser.
214
+ * @returns A ValueParser that parses strings into Temporal.PlainYearMonth values.
215
+ */
216
+ declare function plainYearMonth(options?: PlainYearMonthOptions): ValueParser<Temporal.PlainYearMonth>;
217
+ /**
218
+ * Creates a ValueParser for parsing Temporal.PlainMonthDay from ISO 8601 month-day strings.
219
+ *
220
+ * Accepts strings like:
221
+ *
222
+ * - `"--01-23"`
223
+ * - `"--12-31"`
224
+ *
225
+ * @param options Configuration options for the plain month-day parser.
226
+ * @returns A ValueParser that parses strings into Temporal.PlainMonthDay values.
227
+ */
228
+ declare function plainMonthDay(options?: PlainMonthDayOptions): ValueParser<Temporal.PlainMonthDay>;
229
+ /**
230
+ * Creates a ValueParser for parsing IANA Time Zone Database identifiers.
231
+ *
232
+ * Accepts strings like:
233
+ *
234
+ * - `"Asia/Seoul"`
235
+ * - `"America/New_York"`
236
+ * - `"Europe/London"`
237
+ * - `"UTC"`
238
+ *
239
+ * @param options Configuration options for the timezone parser.
240
+ * @returns A ValueParser that parses and validates timezone identifiers.
241
+ */
242
+ declare function timeZone(options?: TimeZoneOptions): ValueParser<TimeZone>;
243
+ //#endregion
244
+ export { DurationOptions, InstantOptions, PlainDateOptions, PlainDateTimeOptions, PlainMonthDayOptions, PlainTimeOptions, PlainYearMonthOptions, TimeZone, TimeZoneOptions, ZonedDateTimeOptions, duration, instant, plainDate, plainDateTime, plainMonthDay, plainTime, plainYearMonth, timeZone, zonedDateTime };
package/dist/index.js ADDED
@@ -0,0 +1,315 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { message } from "@optique/core/message";
3
+
4
+ //#region src/index.ts
5
+ /**
6
+ * Creates a ValueParser for parsing Temporal.Instant from ISO 8601 strings.
7
+ *
8
+ * Accepts strings like:
9
+ *
10
+ * - `"2020-01-23T17:04:36.491865121Z"`
11
+ * - `"2020-01-23T17:04:36Z"`
12
+ *
13
+ * @param options Configuration options for the instant parser.
14
+ * @returns A ValueParser that parses strings into Temporal.Instant values.
15
+ */
16
+ function instant(options = {}) {
17
+ return {
18
+ metavar: options.metavar ?? "TIMESTAMP",
19
+ parse(input) {
20
+ try {
21
+ const value = Temporal.Instant.from(input);
22
+ return {
23
+ success: true,
24
+ value
25
+ };
26
+ } catch {
27
+ return {
28
+ success: false,
29
+ error: message`Invalid instant: ${input}. Expected ISO 8601 format like ${"2020-01-23T17:04:36Z"}.`
30
+ };
31
+ }
32
+ },
33
+ format(value) {
34
+ return value.toString();
35
+ }
36
+ };
37
+ }
38
+ /**
39
+ * Creates a ValueParser for parsing Temporal.Duration from ISO 8601 duration strings.
40
+ *
41
+ * Accepts strings like:
42
+ *
43
+ * - `"PT1H30M"`
44
+ * - `"P1DT12H"`
45
+ * - `"PT30S"`
46
+ *
47
+ * @param options Configuration options for the duration parser.
48
+ * @returns A ValueParser that parses strings into Temporal.Duration values.
49
+ */
50
+ function duration(options = {}) {
51
+ return {
52
+ metavar: options.metavar ?? "DURATION",
53
+ parse(input) {
54
+ try {
55
+ const value = Temporal.Duration.from(input);
56
+ return {
57
+ success: true,
58
+ value
59
+ };
60
+ } catch {
61
+ return {
62
+ success: false,
63
+ error: message`Invalid duration: ${input}. Expected ISO 8601 format like ${"PT1H30M"}.`
64
+ };
65
+ }
66
+ },
67
+ format(value) {
68
+ return value.toString();
69
+ }
70
+ };
71
+ }
72
+ /**
73
+ * Creates a ValueParser for parsing Temporal.ZonedDateTime from ISO 8601 strings with timezone.
74
+ *
75
+ * Accepts strings like:
76
+ *
77
+ * - `"2020-01-23T17:04:36.491865121+01:00[Europe/Paris]"`
78
+ * - `"2020-01-23T17:04:36Z[UTC]"`
79
+ *
80
+ * @param options Configuration options for the zoned datetime parser.
81
+ * @returns A ValueParser that parses strings into Temporal.ZonedDateTime values.
82
+ */
83
+ function zonedDateTime(options = {}) {
84
+ return {
85
+ metavar: options.metavar ?? "ZONED_DATETIME",
86
+ parse(input) {
87
+ try {
88
+ const value = Temporal.ZonedDateTime.from(input);
89
+ return {
90
+ success: true,
91
+ value
92
+ };
93
+ } catch {
94
+ return {
95
+ success: false,
96
+ error: message`Invalid zoned datetime: ${input}. Expected ISO 8601 format with timezone like ${"2020-01-23T17:04:36+01:00[Europe/Paris]"}.`
97
+ };
98
+ }
99
+ },
100
+ format(value) {
101
+ return value.toString();
102
+ }
103
+ };
104
+ }
105
+ /**
106
+ * Creates a ValueParser for parsing Temporal.PlainDate from ISO 8601 date strings.
107
+ *
108
+ * Accepts strings like:
109
+ *
110
+ * - `"2020-01-23"`
111
+ * - `"2020-12-31"`
112
+ *
113
+ * @param options Configuration options for the plain date parser.
114
+ * @returns A ValueParser that parses strings into Temporal.PlainDate values.
115
+ */
116
+ function plainDate(options = {}) {
117
+ return {
118
+ metavar: options.metavar ?? "DATE",
119
+ parse(input) {
120
+ try {
121
+ const value = Temporal.PlainDate.from(input);
122
+ return {
123
+ success: true,
124
+ value
125
+ };
126
+ } catch {
127
+ return {
128
+ success: false,
129
+ error: message`Invalid date: ${input}. Expected ISO 8601 format like ${"2020-01-23"}.`
130
+ };
131
+ }
132
+ },
133
+ format(value) {
134
+ return value.toString();
135
+ }
136
+ };
137
+ }
138
+ /**
139
+ * Creates a ValueParser for parsing Temporal.PlainTime from ISO 8601 time strings.
140
+ *
141
+ * Accepts strings like:
142
+ *
143
+ * - `"17:04:36"`
144
+ * - `"17:04:36.491865121"`
145
+ *
146
+ * @param options Configuration options for the plain time parser.
147
+ * @returns A ValueParser that parses strings into Temporal.PlainTime values.
148
+ */
149
+ function plainTime(options = {}) {
150
+ return {
151
+ metavar: options.metavar ?? "TIME",
152
+ parse(input) {
153
+ try {
154
+ const value = Temporal.PlainTime.from(input);
155
+ return {
156
+ success: true,
157
+ value
158
+ };
159
+ } catch {
160
+ return {
161
+ success: false,
162
+ error: message`Invalid time: ${input}. Expected ISO 8601 format like ${"17:04:36"}.`
163
+ };
164
+ }
165
+ },
166
+ format(value) {
167
+ return value.toString();
168
+ }
169
+ };
170
+ }
171
+ /**
172
+ * Creates a ValueParser for parsing Temporal.PlainDateTime from ISO 8601 datetime strings.
173
+ *
174
+ * Accepts strings like:
175
+ *
176
+ * - `"2020-01-23T17:04:36"`
177
+ * - `"2020-01-23T17:04:36.491865121"`
178
+ *
179
+ * @param options Configuration options for the plain datetime parser.
180
+ * @returns A ValueParser that parses strings into Temporal.PlainDateTime values.
181
+ */
182
+ function plainDateTime(options = {}) {
183
+ return {
184
+ metavar: options.metavar ?? "DATETIME",
185
+ parse(input) {
186
+ try {
187
+ const value = Temporal.PlainDateTime.from(input);
188
+ return {
189
+ success: true,
190
+ value
191
+ };
192
+ } catch {
193
+ return {
194
+ success: false,
195
+ error: message`Invalid datetime: ${input}. Expected ISO 8601 format like ${"2020-01-23T17:04:36"}.`
196
+ };
197
+ }
198
+ },
199
+ format(value) {
200
+ return value.toString();
201
+ }
202
+ };
203
+ }
204
+ /**
205
+ * Creates a ValueParser for parsing Temporal.PlainYearMonth from ISO 8601 year-month strings.
206
+ *
207
+ * Accepts strings like:
208
+ *
209
+ * - `"2020-01"`
210
+ * - `"2020-12"`
211
+ *
212
+ * @param options Configuration options for the plain year-month parser.
213
+ * @returns A ValueParser that parses strings into Temporal.PlainYearMonth values.
214
+ */
215
+ function plainYearMonth(options = {}) {
216
+ return {
217
+ metavar: options.metavar ?? "YEAR-MONTH",
218
+ parse(input) {
219
+ try {
220
+ const value = Temporal.PlainYearMonth.from(input);
221
+ return {
222
+ success: true,
223
+ value
224
+ };
225
+ } catch {
226
+ return {
227
+ success: false,
228
+ error: message`Invalid year-month: ${input}. Expected ISO 8601 format like ${"2020-01"}.`
229
+ };
230
+ }
231
+ },
232
+ format(value) {
233
+ return value.toString();
234
+ }
235
+ };
236
+ }
237
+ /**
238
+ * Creates a ValueParser for parsing Temporal.PlainMonthDay from ISO 8601 month-day strings.
239
+ *
240
+ * Accepts strings like:
241
+ *
242
+ * - `"--01-23"`
243
+ * - `"--12-31"`
244
+ *
245
+ * @param options Configuration options for the plain month-day parser.
246
+ * @returns A ValueParser that parses strings into Temporal.PlainMonthDay values.
247
+ */
248
+ function plainMonthDay(options = {}) {
249
+ return {
250
+ metavar: options.metavar ?? "--MONTH-DAY",
251
+ parse(input) {
252
+ try {
253
+ const value = Temporal.PlainMonthDay.from(input);
254
+ return {
255
+ success: true,
256
+ value
257
+ };
258
+ } catch {
259
+ return {
260
+ success: false,
261
+ error: message`Invalid month-day: ${input}. Expected ISO 8601 format like ${"--01-23"}.`
262
+ };
263
+ }
264
+ },
265
+ format(value) {
266
+ return value.toString();
267
+ }
268
+ };
269
+ }
270
+ /**
271
+ * Creates a ValueParser for parsing IANA Time Zone Database identifiers.
272
+ *
273
+ * Accepts strings like:
274
+ *
275
+ * - `"Asia/Seoul"`
276
+ * - `"America/New_York"`
277
+ * - `"Europe/London"`
278
+ * - `"UTC"`
279
+ *
280
+ * @param options Configuration options for the timezone parser.
281
+ * @returns A ValueParser that parses and validates timezone identifiers.
282
+ */
283
+ function timeZone(options = {}) {
284
+ return {
285
+ metavar: options.metavar ?? "TIMEZONE",
286
+ parse(input) {
287
+ try {
288
+ Temporal.ZonedDateTime.from({
289
+ year: 2020,
290
+ month: 1,
291
+ day: 1,
292
+ hour: 0,
293
+ minute: 0,
294
+ second: 0,
295
+ timeZone: input
296
+ });
297
+ return {
298
+ success: true,
299
+ value: input
300
+ };
301
+ } catch {
302
+ return {
303
+ success: false,
304
+ error: message`Invalid timezone identifier: ${input}. Must be a valid IANA timezone like "Asia/Seoul" or "UTC".`
305
+ };
306
+ }
307
+ },
308
+ format(value) {
309
+ return value;
310
+ }
311
+ };
312
+ }
313
+
314
+ //#endregion
315
+ export { duration, instant, plainDate, plainDateTime, plainMonthDay, plainTime, plainYearMonth, timeZone, zonedDateTime };
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@optique/temporal",
3
+ "version": "0.4.0-dev.49+a1186942",
4
+ "description": "Temporal value parsers for Optique",
5
+ "keywords": [
6
+ "CLI",
7
+ "command-line",
8
+ "commandline",
9
+ "parser",
10
+ "temporal",
11
+ "time",
12
+ "datetime"
13
+ ],
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Hong Minhee",
17
+ "email": "hong@minhee.org",
18
+ "url": "https://hongminhee.org/"
19
+ },
20
+ "homepage": "https://optique.dev/",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/dahlia/optique.git",
24
+ "directory": "packages/temporal/"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/dahlia/optique/issues"
28
+ },
29
+ "funding": [
30
+ "https://github.com/sponsors/dahlia"
31
+ ],
32
+ "engines": {
33
+ "node": ">=20.0.0",
34
+ "bun": ">=1.2.0",
35
+ "deno": ">=2.3.0"
36
+ },
37
+ "files": [
38
+ "dist/",
39
+ "package.json",
40
+ "README.md"
41
+ ],
42
+ "type": "module",
43
+ "module": "./dist/index.js",
44
+ "main": "./dist/index.cjs",
45
+ "types": "./dist/index.d.ts",
46
+ "exports": {
47
+ ".": {
48
+ "types": {
49
+ "import": "./dist/index.d.ts",
50
+ "require": "./dist/index.d.cts"
51
+ },
52
+ "import": "./dist/index.js",
53
+ "require": "./dist/index.cjs"
54
+ }
55
+ },
56
+ "sideEffects": false,
57
+ "dependencies": {
58
+ "@js-temporal/polyfill": "^0.5.1",
59
+ "@optique/core": ""
60
+ },
61
+ "devDependencies": {
62
+ "@types/node": "^20.19.9",
63
+ "tsdown": "^0.13.0",
64
+ "typescript": "^5.8.3"
65
+ },
66
+ "scripts": {
67
+ "build": "tsdown",
68
+ "prepublish": "tsdown",
69
+ "test": "tsdown && node --experimental-transform-types --test",
70
+ "test:bun": "tsdown && bun test",
71
+ "test:deno": "deno test",
72
+ "test-all": "tsdown && node --experimental-transform-types --test && bun test && deno test"
73
+ }
74
+ }