temporal-fmt 0.9.0 → 0.9.2

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.
Files changed (105) hide show
  1. package/dist/calendarUtils.cjs +4 -259
  2. package/dist/calendarUtils.cjs.map +1 -1
  3. package/dist/calendarUtils.js +1 -33
  4. package/dist/chunk-2JHEV7ZF.js +7 -0
  5. package/dist/chunk-2JHEV7ZF.js.map +1 -0
  6. package/dist/chunk-3MZLTVP3.js +2 -0
  7. package/dist/{chunk-MXXYIMFJ.js.map → chunk-3MZLTVP3.js.map} +1 -1
  8. package/dist/chunk-4JWGUR4O.js +11 -0
  9. package/dist/{chunk-AXQLUIDY.js.map → chunk-4JWGUR4O.js.map} +1 -1
  10. package/dist/chunk-5U5WJ465.js +36 -0
  11. package/dist/chunk-5U5WJ465.js.map +1 -0
  12. package/dist/chunk-C5YESWFT.js +2 -0
  13. package/dist/chunk-C5YESWFT.js.map +1 -0
  14. package/dist/chunk-C6ZZ62ET.js +2 -0
  15. package/dist/{chunk-YS52LOMJ.js.map → chunk-C6ZZ62ET.js.map} +1 -1
  16. package/dist/chunk-EHLRZULM.js +5 -0
  17. package/dist/{chunk-4ZBGVYLR.js.map → chunk-EHLRZULM.js.map} +1 -1
  18. package/dist/chunk-F4RGUDA3.js +2 -0
  19. package/dist/chunk-F4RGUDA3.js.map +1 -0
  20. package/dist/chunk-J2AQNXF2.js +2 -0
  21. package/dist/{chunk-GRJQHQLW.js.map → chunk-J2AQNXF2.js.map} +1 -1
  22. package/dist/chunk-NBXF7V5B.js +2 -0
  23. package/dist/chunk-NBXF7V5B.js.map +1 -0
  24. package/dist/chunk-R52YOKI3.js +7 -0
  25. package/dist/{chunk-M33NSC4E.js.map → chunk-R52YOKI3.js.map} +1 -1
  26. package/dist/chunk-UJR6DHXY.js +2 -0
  27. package/dist/chunk-UJR6DHXY.js.map +1 -0
  28. package/dist/chunk-VQXUMFB2.js +21 -0
  29. package/dist/{chunk-VHSZL3P3.js.map → chunk-VQXUMFB2.js.map} +1 -1
  30. package/dist/chunk-XH46JNPK.js +2 -0
  31. package/dist/chunk-XH46JNPK.js.map +1 -0
  32. package/dist/chunk-YB6YAG7E.js +35 -0
  33. package/dist/chunk-YB6YAG7E.js.map +1 -0
  34. package/dist/chunk-ZGMTY52O.js +2 -0
  35. package/dist/chunk-ZGMTY52O.js.map +1 -0
  36. package/dist/duration.cjs +9 -624
  37. package/dist/duration.cjs.map +1 -1
  38. package/dist/duration.js +1 -29
  39. package/dist/format.cjs +18 -769
  40. package/dist/format.cjs.map +1 -1
  41. package/dist/format.js +1 -20
  42. package/dist/formatDuration.d.cts +6 -0
  43. package/dist/formatDuration.d.ts +6 -0
  44. package/dist/index.cjs +146 -6672
  45. package/dist/index.cjs.map +1 -1
  46. package/dist/index.js +58 -3004
  47. package/dist/index.js.map +1 -1
  48. package/dist/interval.cjs +18 -952
  49. package/dist/interval.cjs.map +1 -1
  50. package/dist/interval.js +1 -41
  51. package/dist/localeRegistry.cjs +15 -328
  52. package/dist/localeRegistry.cjs.map +1 -1
  53. package/dist/localeRegistry.js +1 -11
  54. package/dist/localeVocab.d.cts +3 -0
  55. package/dist/localeVocab.d.ts +3 -0
  56. package/dist/parse.cjs +53 -1637
  57. package/dist/parse.cjs.map +1 -1
  58. package/dist/parse.js +1 -20
  59. package/dist/parsePattern.d.cts +1 -0
  60. package/dist/parsePattern.d.ts +1 -0
  61. package/dist/pattern.d.cts +1 -0
  62. package/dist/pattern.d.ts +1 -0
  63. package/dist/recurrence.cjs +20 -450
  64. package/dist/recurrence.cjs.map +1 -1
  65. package/dist/recurrence.js +1 -20
  66. package/dist/relativeTime.cjs +24 -177
  67. package/dist/relativeTime.cjs.map +1 -1
  68. package/dist/relativeTime.js +1 -14
  69. package/dist/rounding.d.cts +1 -0
  70. package/dist/rounding.d.ts +1 -0
  71. package/dist/serialization.d.cts +1 -1
  72. package/dist/serialization.d.ts +1 -1
  73. package/dist/timezone.cjs +20 -387
  74. package/dist/timezone.cjs.map +1 -1
  75. package/dist/timezone.js +1 -26
  76. package/dist/typeGuards.d.cts +9 -9
  77. package/dist/typeGuards.d.ts +9 -9
  78. package/package.json +4 -2
  79. package/dist/chunk-4ZBGVYLR.js +0 -182
  80. package/dist/chunk-AXQLUIDY.js +0 -183
  81. package/dist/chunk-CKVHXFX7.js +0 -902
  82. package/dist/chunk-CKVHXFX7.js.map +0 -1
  83. package/dist/chunk-CSUIKL73.js +0 -215
  84. package/dist/chunk-CSUIKL73.js.map +0 -1
  85. package/dist/chunk-EDUFSRWJ.js +0 -9
  86. package/dist/chunk-EDUFSRWJ.js.map +0 -1
  87. package/dist/chunk-FQA4XJME.js +0 -57
  88. package/dist/chunk-FQA4XJME.js.map +0 -1
  89. package/dist/chunk-FTMLZ4EN.js +0 -196
  90. package/dist/chunk-FTMLZ4EN.js.map +0 -1
  91. package/dist/chunk-GRJQHQLW.js +0 -170
  92. package/dist/chunk-JCIMP43G.js +0 -602
  93. package/dist/chunk-JCIMP43G.js.map +0 -1
  94. package/dist/chunk-KDYK72Z2.js +0 -75
  95. package/dist/chunk-KDYK72Z2.js.map +0 -1
  96. package/dist/chunk-M33NSC4E.js +0 -169
  97. package/dist/chunk-MXXYIMFJ.js +0 -29
  98. package/dist/chunk-NFKQ3EXC.js +0 -188
  99. package/dist/chunk-NFKQ3EXC.js.map +0 -1
  100. package/dist/chunk-O6GVMBLW.js +0 -403
  101. package/dist/chunk-O6GVMBLW.js.map +0 -1
  102. package/dist/chunk-VHSZL3P3.js +0 -242
  103. package/dist/chunk-VSUCBUTC.js +0 -295
  104. package/dist/chunk-VSUCBUTC.js.map +0 -1
  105. package/dist/chunk-YS52LOMJ.js +0 -51
@@ -1,602 +0,0 @@
1
- import {
2
- MAX_FORMAT_LENGTH
3
- } from "./chunk-EDUFSRWJ.js";
4
- import {
5
- FormatSyntaxError,
6
- InvalidDurationError,
7
- canonicalCacheKey
8
- } from "./chunk-O6GVMBLW.js";
9
- import {
10
- asDateFieldView
11
- } from "./chunk-4ZBGVYLR.js";
12
-
13
- // src/rounding.ts
14
- var MS_PER_UNIT = {
15
- day: 864e5,
16
- hour: 36e5,
17
- minute: 6e4,
18
- second: 1e3,
19
- millisecond: 1
20
- };
21
- function applyMode(ms, mode, increment) {
22
- const sign = ms < 0 ? -1 : 1;
23
- const abs = Math.abs(ms);
24
- const stepped = abs / increment;
25
- let rounded;
26
- switch (mode) {
27
- case "nearest":
28
- rounded = Math.round(stepped);
29
- break;
30
- case "floor":
31
- rounded = Math.floor(stepped);
32
- break;
33
- case "ceil":
34
- rounded = Math.ceil(stepped);
35
- break;
36
- case "trunc":
37
- rounded = Math.trunc(stepped);
38
- break;
39
- }
40
- return sign * rounded * increment;
41
- }
42
- function toMs(v) {
43
- const y = v.year, m = v.month, d = v.day;
44
- const y2 = m <= 2 ? y - 1 : y;
45
- const era = Math.floor((y2 >= 0 ? y2 : y2 - 399) / 400);
46
- const yoe = y2 - era * 400;
47
- const m2 = m > 2 ? m - 3 : m + 9;
48
- const doy = Math.floor((153 * m2 + 2) / 5) + d - 1;
49
- const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
50
- const days = era * 146097 + doe - 719468;
51
- return days * MS_PER_UNIT.day + (v.hour ?? 0) * MS_PER_UNIT.hour + (v.minute ?? 0) * MS_PER_UNIT.minute + (v.second ?? 0) * MS_PER_UNIT.second + (v.millisecond ?? 0);
52
- }
53
- function fromMs(ms, base) {
54
- const MS_PER_DAY = MS_PER_UNIT.day;
55
- const totalDays = Math.floor(ms / MS_PER_DAY);
56
- let withinDay = ms - totalDays * MS_PER_DAY;
57
- if (withinDay < 0) withinDay += MS_PER_DAY;
58
- const hour = Math.floor(withinDay / MS_PER_UNIT.hour);
59
- const minute = Math.floor(withinDay % MS_PER_UNIT.hour / MS_PER_UNIT.minute);
60
- const second = Math.floor(withinDay % MS_PER_UNIT.minute / MS_PER_UNIT.second);
61
- const millisecond = withinDay % MS_PER_UNIT.second;
62
- const z = totalDays + 719468;
63
- const era = Math.floor((z >= 0 ? z : z - 146096) / 146097);
64
- const doe = z - era * 146097;
65
- const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
66
- const y2 = yoe + era * 400;
67
- const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
68
- const mp = Math.floor((5 * doy + 2) / 153);
69
- const d = doy - Math.floor((153 * mp + 2) / 5) + 1;
70
- const m = mp < 10 ? mp + 3 : mp - 9;
71
- const year = m <= 2 ? y2 + 1 : y2;
72
- return { ...base, year, month: m, day: d, hour, minute, second, millisecond };
73
- }
74
- function round(value, options) {
75
- const base = asDateFieldView(value);
76
- const mode = options.mode ?? "nearest";
77
- const increment = (options.roundingIncrement ?? 1) * MS_PER_UNIT[options.unit];
78
- if (increment <= 0) {
79
- throw new Error(`temporal-fmt: round() requires a positive roundingIncrement (got ${options.roundingIncrement}).`);
80
- }
81
- const ms = toMs(base);
82
- const rounded = applyMode(ms, mode, increment);
83
- return fromMs(rounded, base);
84
- }
85
- function floor(value, unit, roundingIncrement = 1) {
86
- return round(value, { unit, mode: "floor", roundingIncrement });
87
- }
88
- function ceil(value, unit, roundingIncrement = 1) {
89
- return round(value, { unit, mode: "ceil", roundingIncrement });
90
- }
91
- function truncate(value, unit, roundingIncrement = 1) {
92
- return round(value, { unit, mode: "trunc", roundingIncrement });
93
- }
94
- var DURATION_UNIT_TO_NS = {
95
- years: 0n,
96
- // calendar-bound, can't convert without relativeTo
97
- months: 0n,
98
- // same
99
- weeks: 0n,
100
- // same
101
- days: 86400n * 1000000000n,
102
- hours: 3600n * 1000000000n,
103
- minutes: 60n * 1000000000n,
104
- seconds: 1000000000n,
105
- milliseconds: 1000000n,
106
- microseconds: 1000n,
107
- nanoseconds: 1n
108
- };
109
- function isCalendarBound(unit) {
110
- return unit === "years" || unit === "months" || unit === "weeks";
111
- }
112
- function roundDuration(duration, options) {
113
- const mode = options.mode ?? "nearest";
114
- const increment = BigInt(options.roundingIncrement ?? 1);
115
- if (increment <= 0n) {
116
- throw new Error(`temporal-fmt: roundDuration() requires a positive roundingIncrement (got ${options.roundingIncrement}).`);
117
- }
118
- if (isCalendarBound(options.unit)) {
119
- throw new Error(
120
- `temporal-fmt: roundDuration() to "${options.unit}" requires a Temporal.Duration with a relativeTo \u2014 this helper operates on plain field bags without calendar context. Use Temporal.Duration.prototype.round() directly.`
121
- );
122
- }
123
- let totalNs = 0n;
124
- for (const u of ["days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"]) {
125
- const v = duration[u] ?? 0;
126
- totalNs += BigInt(v) * DURATION_UNIT_TO_NS[u];
127
- }
128
- const stepNs = increment * DURATION_UNIT_TO_NS[options.unit];
129
- const sign = totalNs < 0n ? -1n : 1n;
130
- const abs = totalNs < 0n ? -totalNs : totalNs;
131
- const stepped = abs / stepNs;
132
- const remainder = abs % stepNs;
133
- let rounded;
134
- switch (mode) {
135
- case "nearest":
136
- rounded = remainder * 2n >= stepNs ? stepped + 1n : stepped;
137
- break;
138
- case "floor":
139
- rounded = stepped;
140
- break;
141
- case "ceil":
142
- rounded = remainder > 0n ? stepped + 1n : stepped;
143
- break;
144
- case "trunc":
145
- rounded = stepped;
146
- break;
147
- }
148
- const newTotalNs = sign * rounded * stepNs;
149
- const result = { ...duration };
150
- let remaining = newTotalNs;
151
- for (const u of ["days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"]) {
152
- if (u === options.unit || isLargerUnit(u, options.unit) || u === options.unit) {
153
- }
154
- result[u] = 0;
155
- }
156
- const unitsInOrder = ["days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"];
157
- const startIdx = unitsInOrder.indexOf(options.unit);
158
- for (let i = 0; i <= startIdx; i++) {
159
- const u = unitsInOrder[i];
160
- const unitNs = DURATION_UNIT_TO_NS[u];
161
- if (unitNs === 0n) continue;
162
- const count = remaining / unitNs;
163
- remaining -= count * unitNs;
164
- if (count !== 0n) {
165
- result[u] = Number(count);
166
- }
167
- }
168
- return result;
169
- }
170
- function isLargerUnit(_a, _b) {
171
- return false;
172
- }
173
-
174
- // src/formatDuration.ts
175
- var DURATION_UNITS = {
176
- y: { longSingular: "year", longPlural: "years", shortSingular: "yr", shortPlural: "yrs", field: "years", intlUnit: "year" },
177
- o: { longSingular: "month", longPlural: "months", shortSingular: "mo", shortPlural: "mos", field: "months", intlUnit: "month" },
178
- w: { longSingular: "week", longPlural: "weeks", shortSingular: "wk", shortPlural: "wks", field: "weeks", intlUnit: "week" },
179
- d: { longSingular: "day", longPlural: "days", shortSingular: "d", shortPlural: "d", field: "days", intlUnit: "day" },
180
- h: { longSingular: "hour", longPlural: "hours", shortSingular: "h", shortPlural: "h", field: "hours", intlUnit: "hour" },
181
- m: { longSingular: "minute", longPlural: "minutes", shortSingular: "m", shortPlural: "m", field: "minutes", intlUnit: "minute" },
182
- s: { longSingular: "second", longPlural: "seconds", shortSingular: "s", shortPlural: "s", field: "seconds", intlUnit: "second" },
183
- S: { longSingular: "millisecond", longPlural: "milliseconds", shortSingular: "ms", shortPlural: "ms", field: "milliseconds", intlUnit: "millisecond" }
184
- };
185
- var DURATION_TOKEN_STRINGS = Object.keys(DURATION_UNITS).flatMap((single) => [single + single + single, single + single, single]).sort((a, b) => b.length - a.length);
186
- function tokenizeDuration(format) {
187
- const pieces = [];
188
- let i = 0;
189
- while (i < format.length) {
190
- const ch = format[i];
191
- if (ch === "'") {
192
- if (format[i + 1] === "'") {
193
- appendLiteral(pieces, "'");
194
- i += 2;
195
- continue;
196
- }
197
- let j = i + 1;
198
- let literal = "";
199
- let closed = false;
200
- while (j < format.length) {
201
- if (format[j] === "'") {
202
- if (format[j + 1] === "'") {
203
- literal += "'";
204
- j += 2;
205
- continue;
206
- }
207
- closed = true;
208
- j += 1;
209
- break;
210
- }
211
- literal += format[j];
212
- j += 1;
213
- }
214
- if (!closed) {
215
- throw new Error(`temporal-fmt: unterminated quote in duration format string "${format}"`);
216
- }
217
- appendLiteral(pieces, literal);
218
- i = j;
219
- continue;
220
- }
221
- const match = DURATION_TOKEN_STRINGS.find((tok) => format.startsWith(tok, i));
222
- if (match) {
223
- const unit = match[0];
224
- const form = match.length === 1 ? "numeric" : match.length === 2 ? "short" : "long";
225
- pieces.push({ kind: "token", value: match, unit, form });
226
- i += match.length;
227
- continue;
228
- }
229
- appendLiteral(pieces, ch);
230
- i += 1;
231
- }
232
- return pieces;
233
- }
234
- function appendLiteral(pieces, value) {
235
- const last = pieces[pieces.length - 1];
236
- if (last && last.kind === "literal") {
237
- last.value += value;
238
- } else {
239
- pieces.push({ kind: "literal", value });
240
- }
241
- }
242
- function readUnit(duration, field) {
243
- const value = duration[field];
244
- if (value === void 0 || value === null) return 0;
245
- if (typeof value === "number" && Number.isFinite(value)) return value;
246
- const coerced = Number(value);
247
- if (!Number.isFinite(coerced)) {
248
- throw new Error(
249
- `temporal-fmt: duration field "${field}" is not a finite number (got ${String(value)}).`
250
- );
251
- }
252
- return coerced;
253
- }
254
- var unitFormatterCache = /* @__PURE__ */ new Map();
255
- var MAX_UNIT_FORMATTER_CACHE_SIZE = 200;
256
- function getUnitFormatter(locale, intlUnit, unitDisplay) {
257
- const key = `${canonicalCacheKey(locale)}|${intlUnit}|${unitDisplay}`;
258
- const cached = unitFormatterCache.get(key);
259
- if (cached) return cached;
260
- if (unitFormatterCache.size >= MAX_UNIT_FORMATTER_CACHE_SIZE) {
261
- const oldestKey = unitFormatterCache.keys().next().value;
262
- if (oldestKey !== void 0) unitFormatterCache.delete(oldestKey);
263
- }
264
- const formatter = new Intl.NumberFormat(locale, {
265
- style: "unit",
266
- unit: intlUnit,
267
- unitDisplay
268
- });
269
- unitFormatterCache.set(key, formatter);
270
- return formatter;
271
- }
272
- function formatDuration(duration, formatStr, options = {}) {
273
- if (formatStr.length > MAX_FORMAT_LENGTH) {
274
- throw new Error(
275
- `temporal-fmt: duration format string exceeds maximum length of ${MAX_FORMAT_LENGTH} characters (got ${formatStr.length}).`
276
- );
277
- }
278
- const showZeroes = options.showZeroValues === true;
279
- const locale = options.locale;
280
- const useIntl = locale !== void 0;
281
- const pieces = tokenizeDuration(formatStr);
282
- let result = "";
283
- for (const piece of pieces) {
284
- if (piece.kind === "literal") {
285
- result += piece.value;
286
- continue;
287
- }
288
- const unit = DURATION_UNITS[piece.unit];
289
- if (!unit) {
290
- throw new Error(`temporal-fmt: unknown duration token "${piece.value}"`);
291
- }
292
- const value = readUnit(duration, unit.field);
293
- if (value === 0 && !showZeroes) {
294
- continue;
295
- }
296
- if (piece.form === "numeric") {
297
- result += String(value);
298
- continue;
299
- }
300
- if (useIntl) {
301
- const unitDisplay = piece.form === "short" ? "short" : "long";
302
- const formatter = getUnitFormatter(locale, unit.intlUnit, unitDisplay);
303
- result += formatter.format(value);
304
- continue;
305
- }
306
- if (piece.form === "short") {
307
- result += value + (value === 1 || value === -1 ? unit.shortSingular : unit.shortPlural);
308
- } else {
309
- result += value + " " + (value === 1 || value === -1 ? unit.longSingular : unit.longPlural);
310
- }
311
- }
312
- return result;
313
- }
314
-
315
- // src/duration.ts
316
- function formatDurationToParts(duration, formatStr, options = {}) {
317
- const full = formatDuration(duration, formatStr, options);
318
- const tokens = tokenizeDurationFormat(formatStr);
319
- const parts = [];
320
- let pos = 0;
321
- for (const tok of tokens) {
322
- if (tok.kind === "literal") {
323
- const lit = tok.value;
324
- if (full.slice(pos, pos + lit.length) === lit) {
325
- parts.push({ type: "literal", value: lit });
326
- pos += lit.length;
327
- /* c8 ignore start @preserve -- unreachable given current formatDuration(): every
328
- literal is appended to `full` unconditionally regardless of adjacent
329
- zero-value tokens, and pos tracking never desyncs from the true
330
- literal position (verified via adversarial testing: duplicate
331
- literals, skipped tokens before/after, ambiguous text — all still
332
- found via indexOf as expected). Kept as a guard against a future
333
- formatDuration() change that conditionally drops literals. */
334
- } else {
335
- }
336
- /* c8 ignore stop @preserve */
337
- } else {
338
- const nextLiteralStart = findNextLiteralStart(tokens, tok, full, pos);
339
- const value = full.slice(pos, nextLiteralStart);
340
- parts.push({ type: "token", value, token: tok.value });
341
- pos = nextLiteralStart;
342
- }
343
- }
344
- return parts;
345
- }
346
- function tokenizeDurationFormat(formatStr) {
347
- const DURATION_TOKEN_STRINGS2 = ["y", "o", "w", "d", "h", "m", "s", "S"].flatMap((single) => [single + single + single, single + single, single]).sort((a, b) => b.length - a.length);
348
- const pieces = [];
349
- let i = 0;
350
- while (i < formatStr.length) {
351
- const ch = formatStr[i];
352
- if (ch === "'") {
353
- if (formatStr[i + 1] === "'") {
354
- appendLiteral2(pieces, "'");
355
- i += 2;
356
- continue;
357
- }
358
- let j = i + 1;
359
- let literal = "";
360
- let closed = false;
361
- while (j < formatStr.length) {
362
- if (formatStr[j] === "'") {
363
- if (formatStr[j + 1] === "'") {
364
- literal += "'";
365
- j += 2;
366
- continue;
367
- }
368
- closed = true;
369
- j += 1;
370
- break;
371
- }
372
- literal += formatStr[j];
373
- j += 1;
374
- }
375
- if (!closed) throw new FormatSyntaxError({ format: formatStr, reason: "unterminated quote" });
376
- appendLiteral2(pieces, literal);
377
- i = j;
378
- continue;
379
- }
380
- const match = DURATION_TOKEN_STRINGS2.find((tok) => formatStr.startsWith(tok, i));
381
- if (match) {
382
- pieces.push({ kind: "token", value: match, unit: match[0] });
383
- i += match.length;
384
- continue;
385
- }
386
- appendLiteral2(pieces, ch);
387
- i += 1;
388
- }
389
- return pieces;
390
- }
391
- function appendLiteral2(pieces, value) {
392
- const last = pieces[pieces.length - 1];
393
- if (last && last.kind === "literal") {
394
- last.value += value;
395
- } else {
396
- pieces.push({ kind: "literal", value });
397
- }
398
- }
399
- function findNextLiteralStart(tokens, _currentTok, full, pos) {
400
- const idx = tokens.indexOf(_currentTok);
401
- for (let j = idx + 1; j < tokens.length; j++) {
402
- const t = tokens[j];
403
- if (t.kind === "literal") {
404
- const found = full.indexOf(t.value, pos);
405
- if (found >= 0) return found;
406
- }
407
- }
408
- return full.length;
409
- }
410
- function parseISODuration(input) {
411
- const re = /^P(?:(?:(\d+(?:\.\d+)?)Y)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)W)?(?:(\d+(?:\.\d+)?)D)?)?(?:T(?:(\d+(?:\.\d+)?)H)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)S)?)?$/;
412
- const m = re.exec(input);
413
- if (!m) {
414
- throw new InvalidDurationError({ input, reason: "does not match ISO 8601 duration grammar (P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S)" });
415
- }
416
- if (m.slice(1).every((g) => g === void 0)) {
417
- throw new InvalidDurationError({ input, reason: 'duration has no fields (use "P0D" for zero duration)' });
418
- }
419
- const toNum = (s) => s === void 0 ? 0 : Number(s);
420
- return {
421
- years: toNum(m[1]),
422
- months: toNum(m[2]),
423
- weeks: toNum(m[3]),
424
- days: toNum(m[4]),
425
- hours: toNum(m[5]),
426
- minutes: toNum(m[6]),
427
- seconds: toNum(m[7])
428
- };
429
- }
430
- function formatISODuration(duration) {
431
- const parts = ["P"];
432
- const years = duration.years ?? 0;
433
- const months = duration.months ?? 0;
434
- const weeks = duration.weeks ?? 0;
435
- const days = duration.days ?? 0;
436
- const hours = duration.hours ?? 0;
437
- const minutes = duration.minutes ?? 0;
438
- const seconds = duration.seconds ?? 0;
439
- if (years) parts.push(`${years}Y`);
440
- if (months) parts.push(`${months}M`);
441
- if (weeks) parts.push(`${weeks}W`);
442
- if (days) parts.push(`${days}D`);
443
- if (hours || minutes || seconds) {
444
- parts.push("T");
445
- if (hours) parts.push(`${hours}H`);
446
- if (minutes) parts.push(`${minutes}M`);
447
- if (seconds) parts.push(`${seconds}S`);
448
- }
449
- if (parts.length === 1) parts.push("0D");
450
- return parts.join("");
451
- }
452
- function parseDuration(input, formatStr, options = {}) {
453
- const tokens = tokenizeDurationFormat(formatStr);
454
- const fields = {};
455
- const groupSpecs = [];
456
- let regex = "";
457
- for (const tok of tokens) {
458
- if (tok.kind === "literal") {
459
- regex += tok.value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
460
- } else {
461
- const unit = tok.unit;
462
- const form = tok.value.length === 1 ? "numeric" : tok.value.length === 2 ? "short" : "long";
463
- /* c8 ignore start @preserve -- the final `: null` branch, and the
464
- `if (key === null)` throw it feeds, are both unreachable: tok.unit
465
- is always one of the 8 letters in DURATION_TOKEN_STRINGS (the same
466
- source set this ternary checks against), so `null` can't be
467
- produced by any token the tokenizer emits. Kept as a guard against
468
- a future letter being added to one set without the other. */
469
- const key = unit === "y" ? "years" : unit === "o" ? "months" : unit === "w" ? "weeks" : unit === "d" ? "days" : unit === "h" ? "hours" : unit === "m" ? "minutes" : unit === "s" ? "seconds" : unit === "S" ? "milliseconds" : null;
470
- if (key === null) {
471
- throw new FormatSyntaxError({ format: formatStr, token: tok.value, reason: "unknown duration token" });
472
- }
473
- /* c8 ignore stop @preserve */
474
- if (form === "numeric") {
475
- regex += "(-?\\d+)";
476
- } else {
477
- regex += "(\\d+)\\s*\\w*";
478
- }
479
- groupSpecs.push({ unit: key, form });
480
- }
481
- }
482
- const re = new RegExp(`^${regex}$`, "u");
483
- const m = re.exec(input);
484
- if (!m) {
485
- throw new FormatSyntaxError({ input, format: formatStr, reason: "does not match duration format" });
486
- }
487
- groupSpecs.forEach((spec, i) => {
488
- const raw = m[i + 1];
489
- const value = Number(raw);
490
- if (!Number.isFinite(value)) {
491
- throw new InvalidDurationError({ input, reason: `token for ${spec.unit} matched "${raw}" but isn't a finite number` });
492
- }
493
- fields[spec.unit] = value;
494
- });
495
- return fields;
496
- }
497
- function balanceDuration(duration) {
498
- const NS_PER = [
499
- ["days", 86400n * 1000000000n],
500
- ["hours", 3600n * 1000000000n],
501
- ["minutes", 60n * 1000000000n],
502
- ["seconds", 1000000000n],
503
- ["milliseconds", 1000000n],
504
- ["microseconds", 1000n],
505
- ["nanoseconds", 1n]
506
- ];
507
- let totalNs = 0n;
508
- for (const [k, nsPer] of NS_PER) {
509
- const v = duration[k];
510
- if (typeof v === "number") totalNs += BigInt(v) * nsPer;
511
- }
512
- const result = { ...duration };
513
- let remaining = totalNs;
514
- for (const [k, nsPer] of NS_PER) {
515
- const count = remaining / nsPer;
516
- remaining -= count * nsPer;
517
- result[k] = Number(count);
518
- }
519
- return result;
520
- }
521
- function totalDuration(duration, unit) {
522
- const NS_PER = {
523
- days: 86400n * 1000000000n,
524
- hours: 3600n * 1000000000n,
525
- minutes: 60n * 1000000000n,
526
- seconds: 1000000000n,
527
- milliseconds: 1000000n,
528
- microseconds: 1000n,
529
- nanoseconds: 1n
530
- };
531
- let totalNs = 0n;
532
- for (const k of Object.keys(NS_PER)) {
533
- const v = duration[k];
534
- if (typeof v === "number") totalNs += BigInt(v) * NS_PER[k];
535
- }
536
- const divisor = NS_PER[unit];
537
- if (!divisor) {
538
- throw new InvalidDurationError({ reason: `totalDuration() does not support unit "${unit}" (calendar-bound units need a relativeTo)` });
539
- }
540
- return Number(totalNs) / Number(divisor);
541
- }
542
- function compareDuration(a, b) {
543
- const aNs = totalDurationNs(a);
544
- const bNs = totalDurationNs(b);
545
- if (aNs < bNs) return -1;
546
- if (aNs > bNs) return 1;
547
- return 0;
548
- }
549
- function totalDurationNs(d) {
550
- const NS_PER = {
551
- days: 86400n * 1000000000n,
552
- hours: 3600n * 1000000000n,
553
- minutes: 60n * 1000000000n,
554
- seconds: 1000000000n,
555
- milliseconds: 1000000n,
556
- microseconds: 1000n,
557
- nanoseconds: 1n
558
- };
559
- let total = 0n;
560
- for (const k of Object.keys(NS_PER)) {
561
- const v = d[k];
562
- if (typeof v === "number") total += BigInt(v) * NS_PER[k];
563
- }
564
- return total;
565
- }
566
- function addDuration(a, b) {
567
- const keys = ["years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"];
568
- const result = {};
569
- for (const k of keys) {
570
- result[k] = (a[k] ?? 0) + (b[k] ?? 0);
571
- }
572
- return result;
573
- }
574
- function subtractDuration(a, b) {
575
- return addDuration(a, negateDuration(b));
576
- }
577
- function negateDuration(d) {
578
- const result = {};
579
- for (const k of Object.keys(d)) {
580
- result[k] = -(d[k] ?? 0);
581
- }
582
- return result;
583
- }
584
-
585
- export {
586
- round,
587
- floor,
588
- ceil,
589
- truncate,
590
- roundDuration,
591
- formatDuration,
592
- formatDurationToParts,
593
- parseISODuration,
594
- formatISODuration,
595
- parseDuration,
596
- balanceDuration,
597
- totalDuration,
598
- compareDuration,
599
- addDuration,
600
- subtractDuration
601
- };
602
- //# sourceMappingURL=chunk-JCIMP43G.js.map