temporal-fmt 0.9.80 → 0.9.82

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 (57) hide show
  1. package/README.md +302 -490
  2. package/dist/{chunk-Z245NHDT.js → chunk-4JMHI6E2.js} +6 -6
  3. package/dist/chunk-7XSIPXIR.js +129 -0
  4. package/dist/chunk-7XSIPXIR.js.map +1 -0
  5. package/dist/{chunk-DMJWJTFY.js → chunk-AF3LHBDQ.js} +2 -2
  6. package/dist/{chunk-WEVNKOQA.js → chunk-IEQCJPB5.js} +5 -5
  7. package/dist/{chunk-DFWTCRS3.js → chunk-JCJW2VOV.js} +10 -7
  8. package/dist/chunk-JCJW2VOV.js.map +1 -0
  9. package/dist/{chunk-HGRT37LT.js → chunk-KWOTFJNY.js} +3 -3
  10. package/dist/{chunk-64UB3WDT.js → chunk-NHNUFNQ7.js} +4 -4
  11. package/dist/{chunk-WP7TBCTT.js → chunk-TIMSK72I.js} +8 -8
  12. package/dist/{chunk-LHR5PCP4.js → chunk-UYKKGBZ3.js} +7 -7
  13. package/dist/{chunk-5KP5H7X2.js → chunk-W45J4E76.js} +6 -4
  14. package/dist/duration.js +3 -3
  15. package/dist/format.cjs +7 -4
  16. package/dist/format.cjs.map +1 -1
  17. package/dist/format.js +5 -5
  18. package/dist/index.cjs +147 -34
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -0
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +108 -87
  23. package/dist/index.js.map +1 -1
  24. package/dist/interval.cjs +7 -4
  25. package/dist/interval.cjs.map +1 -1
  26. package/dist/interval.js +7 -7
  27. package/dist/legacy/index.d.cts +23 -0
  28. package/dist/legacy/index.d.ts +23 -0
  29. package/dist/legacy/matchesFormat.d.cts +6 -0
  30. package/dist/legacy/matchesFormat.d.ts +6 -0
  31. package/dist/legacy.cjs +2156 -0
  32. package/dist/legacy.cjs.map +1 -0
  33. package/dist/legacy.d.cts +1 -0
  34. package/dist/legacy.d.ts +1 -0
  35. package/dist/legacy.js +31 -0
  36. package/dist/legacy.js.map +1 -0
  37. package/dist/parse.cjs +7 -4
  38. package/dist/parse.cjs.map +1 -1
  39. package/dist/parse.js +5 -5
  40. package/dist/relativeTime.cjs +6 -2
  41. package/dist/relativeTime.cjs.map +1 -1
  42. package/dist/relativeTime.js +4 -4
  43. package/dist/timezone.cjs +6 -2
  44. package/dist/timezone.cjs.map +1 -1
  45. package/dist/timezone.js +2 -2
  46. package/legacy.d.cts +1 -0
  47. package/legacy.d.ts +1 -0
  48. package/package.json +13 -1
  49. package/dist/chunk-DFWTCRS3.js.map +0 -1
  50. /package/dist/{chunk-Z245NHDT.js.map → chunk-4JMHI6E2.js.map} +0 -0
  51. /package/dist/{chunk-DMJWJTFY.js.map → chunk-AF3LHBDQ.js.map} +0 -0
  52. /package/dist/{chunk-WEVNKOQA.js.map → chunk-IEQCJPB5.js.map} +0 -0
  53. /package/dist/{chunk-HGRT37LT.js.map → chunk-KWOTFJNY.js.map} +0 -0
  54. /package/dist/{chunk-64UB3WDT.js.map → chunk-NHNUFNQ7.js.map} +0 -0
  55. /package/dist/{chunk-WP7TBCTT.js.map → chunk-TIMSK72I.js.map} +0 -0
  56. /package/dist/{chunk-LHR5PCP4.js.map → chunk-UYKKGBZ3.js.map} +0 -0
  57. /package/dist/{chunk-5KP5H7X2.js.map → chunk-W45J4E76.js.map} +0 -0
package/dist/format.js CHANGED
@@ -4,13 +4,13 @@ import {
4
4
  compileFormat,
5
5
  format,
6
6
  formatToParts
7
- } from "./chunk-HGRT37LT.js";
8
- import "./chunk-DMJWJTFY.js";
9
- import "./chunk-Z245NHDT.js";
10
- import "./chunk-DFWTCRS3.js";
7
+ } from "./chunk-KWOTFJNY.js";
8
+ import "./chunk-AF3LHBDQ.js";
9
+ import "./chunk-4JMHI6E2.js";
10
+ import "./chunk-JCJW2VOV.js";
11
11
  import "./chunk-SOPTTXUI.js";
12
- import "./chunk-QRLQ7RR7.js";
13
12
  import "./chunk-BLZDBPSV.js";
13
+ import "./chunk-QRLQ7RR7.js";
14
14
  export {
15
15
  _getPieces,
16
16
  _handlerFor,
package/dist/index.cjs CHANGED
@@ -167,8 +167,18 @@ __export(index_exports, {
167
167
  isWeekend: () => isWeekend,
168
168
  isYesterday: () => isYesterday,
169
169
  isZonedDateTime: () => isZonedDateTime,
170
+ legacyCompileFormat: () => compileFormat2,
171
+ legacyCompileParser: () => compileParser2,
172
+ legacyFormat: () => format2,
173
+ legacyFormatToParts: () => formatToParts2,
174
+ legacyMatchesFormat: () => matchesFormat,
175
+ legacyParse: () => parse2,
176
+ legacyParseToParts: () => parseToParts2,
177
+ legacySafeParse: () => safeParse2,
178
+ legacyTryParse: () => tryParse2,
170
179
  listRegisteredGrammars: () => listRegisteredGrammars,
171
180
  listTokens: () => listTokens,
181
+ matchesFormat: () => matchesFormat,
172
182
  max: () => max,
173
183
  mergeIntervals: () => mergeIntervals,
174
184
  mergeWithConfig: () => mergeWithConfig,
@@ -236,17 +246,20 @@ __export(index_exports, {
236
246
  module.exports = __toCommonJS(index_exports);
237
247
 
238
248
  // src/temporalProvider.ts
239
- var injectedTemporal;
240
- var onTemporalChanged = [];
249
+ var TEMPORAL_PROVIDER_STATE = /* @__PURE__ */ Symbol.for("temporal-fmt.temporal-provider");
250
+ var globalState = globalThis;
251
+ var providerState = globalState[TEMPORAL_PROVIDER_STATE] ??= {
252
+ onTemporalChanged: []
253
+ };
241
254
  function subscribeToTemporalChanges(listener) {
242
- onTemporalChanged.push(listener);
255
+ providerState.onTemporalChanged.push(listener);
243
256
  }
244
257
  function setTemporal(temporal) {
245
- injectedTemporal = temporal;
246
- for (const listener of onTemporalChanged) listener();
258
+ providerState.injectedTemporal = temporal;
259
+ for (const listener of providerState.onTemporalChanged) listener();
247
260
  }
248
261
  function resolveTemporal() {
249
- return injectedTemporal ?? globalThis.Temporal;
262
+ return providerState.injectedTemporal ?? globalThis.Temporal;
250
263
  }
251
264
  function getTemporal() {
252
265
  const temporal = resolveTemporal();
@@ -1075,13 +1088,13 @@ var TOKENS = [
1075
1088
 
1076
1089
  // src/tokenize.ts
1077
1090
  var SORTED_TOKEN_STRINGS = TOKENS.map(([tok]) => tok).sort((a, b) => b.length - a.length);
1078
- function tokenizeWithSpans(format2) {
1091
+ function tokenizeWithSpans(format3) {
1079
1092
  const pieces = [];
1080
1093
  let i = 0;
1081
- while (i < format2.length) {
1082
- const ch = format2[i];
1094
+ while (i < format3.length) {
1095
+ const ch = format3[i];
1083
1096
  if (ch === "'") {
1084
- if (format2[i + 1] === "'") {
1097
+ if (format3[i + 1] === "'") {
1085
1098
  appendLiteral(pieces, "'", i, i + 2);
1086
1099
  i += 2;
1087
1100
  continue;
@@ -1089,9 +1102,9 @@ function tokenizeWithSpans(format2) {
1089
1102
  let j = i + 1;
1090
1103
  let literal = "";
1091
1104
  let closed = false;
1092
- while (j < format2.length) {
1093
- if (format2[j] === "'") {
1094
- if (format2[j + 1] === "'") {
1105
+ while (j < format3.length) {
1106
+ if (format3[j] === "'") {
1107
+ if (format3[j + 1] === "'") {
1095
1108
  literal += "'";
1096
1109
  j += 2;
1097
1110
  continue;
@@ -1100,29 +1113,29 @@ function tokenizeWithSpans(format2) {
1100
1113
  j += 1;
1101
1114
  break;
1102
1115
  }
1103
- literal += format2[j];
1116
+ literal += format3[j];
1104
1117
  j += 1;
1105
1118
  }
1106
1119
  if (!closed) {
1107
1120
  throw new FormatSyntaxError({
1108
- format: format2,
1109
- message: `temporal-fmt: unterminated quote in format string "${format2}"`
1121
+ format: format3,
1122
+ message: `temporal-fmt: unterminated quote in format string "${format3}"`
1110
1123
  });
1111
1124
  }
1112
1125
  appendLiteral(pieces, literal, i, j);
1113
1126
  i = j;
1114
1127
  continue;
1115
1128
  }
1116
- const match = SORTED_TOKEN_STRINGS.find((tok) => format2.startsWith(tok, i));
1129
+ const match = SORTED_TOKEN_STRINGS.find((tok) => format3.startsWith(tok, i));
1117
1130
  if (match) {
1118
1131
  const runChar = match[match.length - 1];
1119
- if (format2[i + match.length] === runChar) {
1132
+ if (format3[i + match.length] === runChar) {
1120
1133
  let end = i + match.length;
1121
- while (format2[end] === runChar) end += 1;
1134
+ while (format3[end] === runChar) end += 1;
1122
1135
  throw new UnknownTokenError({
1123
- format: format2,
1124
- token: format2.slice(i, end),
1125
- message: `temporal-fmt: "${format2.slice(i, end)}" in format string "${format2}" isn't a recognized token \u2014 did you mean "${match}"?`
1136
+ format: format3,
1137
+ token: format3.slice(i, end),
1138
+ message: `temporal-fmt: "${format3.slice(i, end)}" in format string "${format3}" isn't a recognized token \u2014 did you mean "${match}"?`
1126
1139
  });
1127
1140
  }
1128
1141
  pieces.push({ kind: "token", value: match, start: i, end: i + match.length });
@@ -1134,8 +1147,8 @@ function tokenizeWithSpans(format2) {
1134
1147
  }
1135
1148
  return pieces;
1136
1149
  }
1137
- function tokenize(format2) {
1138
- return tokenizeWithSpans(format2).map(({ kind, value }) => ({ kind, value }));
1150
+ function tokenize(format3) {
1151
+ return tokenizeWithSpans(format3).map(({ kind, value }) => ({ kind, value }));
1139
1152
  }
1140
1153
  function appendLiteral(pieces, value, start, end) {
1141
1154
  const last = pieces[pieces.length - 1];
@@ -2366,6 +2379,96 @@ function compileParser(formatStr, options = {}) {
2366
2379
  };
2367
2380
  }
2368
2381
 
2382
+ // src/legacy/matchesFormat.ts
2383
+ var patternCache2 = /* @__PURE__ */ new Map();
2384
+ var MAX_CACHE_SIZE3 = 500;
2385
+ function getPattern2(formatStr, locale) {
2386
+ const key = locale + "\0" + formatStr;
2387
+ const cached = patternCache2.get(key);
2388
+ if (cached) return cached;
2389
+ if (patternCache2.size >= MAX_CACHE_SIZE3) {
2390
+ const oldestKey = patternCache2.keys().next().value;
2391
+ if (oldestKey !== void 0) patternCache2.delete(oldestKey);
2392
+ }
2393
+ const pattern = buildCapturingPattern(tokenize(formatStr), locale);
2394
+ patternCache2.set(key, pattern);
2395
+ return pattern;
2396
+ }
2397
+ function matchesFormat(formatStr, input, options = {}) {
2398
+ try {
2399
+ if (formatStr.length > MAX_FORMAT_LENGTH) {
2400
+ throw new Error(
2401
+ `temporal-fmt: format string exceeds maximum length of ${MAX_FORMAT_LENGTH} characters (got ${formatStr.length}).`
2402
+ );
2403
+ }
2404
+ const locale = options.locale ?? DEFAULT_LOCALE;
2405
+ const pattern = getPattern2(formatStr, locale);
2406
+ const match = pattern.regex.exec(input);
2407
+ if (!match) return false;
2408
+ for (const run of pattern.ambiguousRuns) {
2409
+ const range = match.indices?.groups?.[run.groupName];
2410
+ if (!range || enumerateValidSplits(input.slice(range[0], range[1]), run.tokens).length === 0) {
2411
+ return false;
2412
+ }
2413
+ }
2414
+ return true;
2415
+ } catch (error) {
2416
+ if (error instanceof TemporalFmtError) throw new Error(error.message);
2417
+ throw error;
2418
+ }
2419
+ }
2420
+
2421
+ // src/legacy/index.ts
2422
+ function withLegacyErrors(operation) {
2423
+ try {
2424
+ return operation();
2425
+ } catch (error) {
2426
+ if (error instanceof TemporalFmtError) {
2427
+ throw new Error(error.message);
2428
+ }
2429
+ throw error;
2430
+ }
2431
+ }
2432
+ function toLegacySafeParseResult(result) {
2433
+ return result.ok ? result : { ok: false, error: new Error(result.error.message) };
2434
+ }
2435
+ function format2(temporal, formatStr, options = {}) {
2436
+ return withLegacyErrors(() => format(temporal, formatStr, options));
2437
+ }
2438
+ function formatToParts2(temporal, formatStr, options = {}) {
2439
+ return withLegacyErrors(() => formatToParts(temporal, formatStr, options));
2440
+ }
2441
+ function compileFormat2(formatStr) {
2442
+ const compiled = withLegacyErrors(() => compileFormat(formatStr));
2443
+ return {
2444
+ ...compiled,
2445
+ format: (temporal, options) => withLegacyErrors(() => compiled.format(temporal, options)),
2446
+ formatToParts: (temporal, options) => withLegacyErrors(() => compiled.formatToParts(temporal, options))
2447
+ };
2448
+ }
2449
+ function parse2(formatStr, input, options = {}) {
2450
+ return withLegacyErrors(() => parse(formatStr, input, options));
2451
+ }
2452
+ function safeParse2(formatStr, input, options = {}) {
2453
+ return toLegacySafeParseResult(safeParse(formatStr, input, options));
2454
+ }
2455
+ function tryParse2(formatStr, input, options = {}) {
2456
+ return tryParse(formatStr, input, options);
2457
+ }
2458
+ function parseToParts2(formatStr, input, options = {}) {
2459
+ return withLegacyErrors(() => parseToParts(formatStr, input, options));
2460
+ }
2461
+ function compileParser2(formatStr, options = {}) {
2462
+ const compiled = withLegacyErrors(() => compileParser(formatStr, options));
2463
+ return {
2464
+ ...compiled,
2465
+ parse: (input, callOptions) => withLegacyErrors(() => compiled.parse(input, callOptions)),
2466
+ safeParse: (input, callOptions) => toLegacySafeParseResult(compiled.safeParse(input, callOptions)),
2467
+ tryParse: (input, callOptions) => compiled.tryParse(input, callOptions),
2468
+ parseToParts: (input, callOptions) => withLegacyErrors(() => compiled.parseToParts(input, callOptions))
2469
+ };
2470
+ }
2471
+
2369
2472
  // src/formatDuration.ts
2370
2473
  var DURATION_UNITS = {
2371
2474
  y: { longSingular: "year", longPlural: "years", shortSingular: "yr", shortPlural: "yrs", field: "years", intlUnit: "year" },
@@ -2378,13 +2481,13 @@ var DURATION_UNITS = {
2378
2481
  S: { longSingular: "millisecond", longPlural: "milliseconds", shortSingular: "ms", shortPlural: "ms", field: "milliseconds", intlUnit: "millisecond" }
2379
2482
  };
2380
2483
  var DURATION_TOKEN_STRINGS = Object.keys(DURATION_UNITS).flatMap((single) => [single + single + single, single + single, single]).sort((a, b) => b.length - a.length);
2381
- function tokenizeDuration(format2) {
2484
+ function tokenizeDuration(format3) {
2382
2485
  const pieces = [];
2383
2486
  let i = 0;
2384
- while (i < format2.length) {
2385
- const ch = format2[i];
2487
+ while (i < format3.length) {
2488
+ const ch = format3[i];
2386
2489
  if (ch === "'") {
2387
- if (format2[i + 1] === "'") {
2490
+ if (format3[i + 1] === "'") {
2388
2491
  appendLiteral2(pieces, "'");
2389
2492
  i += 2;
2390
2493
  continue;
@@ -2392,9 +2495,9 @@ function tokenizeDuration(format2) {
2392
2495
  let j = i + 1;
2393
2496
  let literal = "";
2394
2497
  let closed = false;
2395
- while (j < format2.length) {
2396
- if (format2[j] === "'") {
2397
- if (format2[j + 1] === "'") {
2498
+ while (j < format3.length) {
2499
+ if (format3[j] === "'") {
2500
+ if (format3[j + 1] === "'") {
2398
2501
  literal += "'";
2399
2502
  j += 2;
2400
2503
  continue;
@@ -2403,17 +2506,17 @@ function tokenizeDuration(format2) {
2403
2506
  j += 1;
2404
2507
  break;
2405
2508
  }
2406
- literal += format2[j];
2509
+ literal += format3[j];
2407
2510
  j += 1;
2408
2511
  }
2409
2512
  if (!closed) {
2410
- throw new Error(`temporal-fmt: unterminated quote in duration format string "${format2}"`);
2513
+ throw new Error(`temporal-fmt: unterminated quote in duration format string "${format3}"`);
2411
2514
  }
2412
2515
  appendLiteral2(pieces, literal);
2413
2516
  i = j;
2414
2517
  continue;
2415
2518
  }
2416
- const match = DURATION_TOKEN_STRINGS.find((tok) => format2.startsWith(tok, i));
2519
+ const match = DURATION_TOKEN_STRINGS.find((tok) => format3.startsWith(tok, i));
2417
2520
  if (match) {
2418
2521
  const unit = match[0];
2419
2522
  const form = match.length === 1 ? "numeric" : match.length === 2 ? "short" : "long";
@@ -7222,8 +7325,18 @@ function translateDateFnsFormatString(formatStr) {
7222
7325
  isWeekend,
7223
7326
  isYesterday,
7224
7327
  isZonedDateTime,
7328
+ legacyCompileFormat,
7329
+ legacyCompileParser,
7330
+ legacyFormat,
7331
+ legacyFormatToParts,
7332
+ legacyMatchesFormat,
7333
+ legacyParse,
7334
+ legacyParseToParts,
7335
+ legacySafeParse,
7336
+ legacyTryParse,
7225
7337
  listRegisteredGrammars,
7226
7338
  listTokens,
7339
+ matchesFormat,
7227
7340
  max,
7228
7341
  mergeIntervals,
7229
7342
  mergeWithConfig,