@vielzeug/tempo 2.1.2 → 2.2.0
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/dist/_tz.cjs +1 -1
- package/dist/_tz.cjs.map +1 -1
- package/dist/_tz.d.ts.map +1 -1
- package/dist/_tz.js +2 -2
- package/dist/_tz.js.map +1 -1
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +25 -25
- package/dist/core.js.map +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +2 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -2
- package/dist/errors.js.map +1 -1
- package/dist/format.cjs +1 -1
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +67 -67
- package/dist/format.js.map +1 -1
- package/dist/tempo.cjs +1 -1
- package/dist/tempo.cjs.map +1 -1
- package/dist/tempo.iife.js +1 -1
- package/dist/tempo.iife.js.map +1 -1
- package/dist/tempo.js +1 -1
- package/dist/tempo.js.map +1 -1
- package/package.json +1 -1
package/dist/format.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { inferTimeZone as
|
|
3
|
-
import { toInstant as
|
|
4
|
-
import { Temporal as
|
|
1
|
+
import { TempoInvalidInputError as e, fail as t } from "./errors.js";
|
|
2
|
+
import { inferTimeZone as n } from "./_tz.js";
|
|
3
|
+
import { toInstant as r, toZoned as i } from "./_convert.js";
|
|
4
|
+
import { Temporal as a } from "@js-temporal/polyfill";
|
|
5
5
|
//#region src/format.ts
|
|
6
|
-
var
|
|
7
|
-
function
|
|
6
|
+
var o = 128;
|
|
7
|
+
function s(e, t, n) {
|
|
8
8
|
let r = e.get(t);
|
|
9
9
|
if (r !== void 0) return r;
|
|
10
|
-
if (e.size >=
|
|
10
|
+
if (e.size >= o) {
|
|
11
11
|
let t = e.keys().next().value;
|
|
12
12
|
t !== void 0 && e.delete(t);
|
|
13
13
|
}
|
|
14
14
|
let i = n();
|
|
15
15
|
return e.set(t, i), i;
|
|
16
16
|
}
|
|
17
|
-
var
|
|
17
|
+
var c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), d = {
|
|
18
18
|
"date-only": { dateStyle: "short" },
|
|
19
19
|
long: {
|
|
20
20
|
dateStyle: "full",
|
|
@@ -30,12 +30,12 @@ var s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l = /* @__PURE
|
|
|
30
30
|
},
|
|
31
31
|
"time-only": { timeStyle: "short" }
|
|
32
32
|
};
|
|
33
|
-
function
|
|
33
|
+
function f(e) {
|
|
34
34
|
return JSON.stringify(Object.entries(e).filter(([, e]) => e !== void 0).sort(([e], [t]) => e.localeCompare(t)).map(([e, t]) => [e, String(t)]));
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function p(e, t) {
|
|
37
37
|
let n = e.timeZone ?? t, r = e.locale;
|
|
38
|
-
if (e.intl !== void 0) return
|
|
38
|
+
if (e.intl !== void 0) return s(c, `${String(r ?? "")}|intl|${n ?? ""}|${f(e.intl)}`, () => {
|
|
39
39
|
let t = n === void 0 ? e.intl : {
|
|
40
40
|
...e.intl,
|
|
41
41
|
timeZone: n
|
|
@@ -43,62 +43,62 @@ function f(e, t) {
|
|
|
43
43
|
return new Intl.DateTimeFormat(r, t);
|
|
44
44
|
});
|
|
45
45
|
let i = e.pattern ?? "medium";
|
|
46
|
-
return
|
|
47
|
-
...
|
|
46
|
+
return s(c, `${String(r ?? "")}|${i}|${n ?? ""}`, () => new Intl.DateTimeFormat(r, {
|
|
47
|
+
...d[i],
|
|
48
48
|
timeZone: n
|
|
49
49
|
}));
|
|
50
50
|
}
|
|
51
|
-
function
|
|
52
|
-
return
|
|
51
|
+
function m(e) {
|
|
52
|
+
return s(l, `${String(e.locale ?? "")}|${e.numeric ?? "auto"}|${e.style ?? "long"}`, () => new Intl.RelativeTimeFormat(e.locale, {
|
|
53
53
|
numeric: e.numeric ?? "auto",
|
|
54
54
|
style: e.style ?? "long"
|
|
55
55
|
}));
|
|
56
56
|
}
|
|
57
|
-
function
|
|
57
|
+
function h(e) {
|
|
58
58
|
let t = Intl;
|
|
59
|
-
return t.DurationFormat ?
|
|
59
|
+
return t.DurationFormat ? s(u, `${String(e.locale ?? "")}|${e.style ?? ""}`, () => new t.DurationFormat(e.locale, { style: e.style })) : null;
|
|
60
60
|
}
|
|
61
|
-
var
|
|
61
|
+
var g = 60, _ = 3600, v = 86400, y = 604800, b = 2629800, x = 31557600, S = [
|
|
62
62
|
{
|
|
63
63
|
scale: 1,
|
|
64
|
-
thresholdToPromote:
|
|
64
|
+
thresholdToPromote: g,
|
|
65
65
|
unit: "second"
|
|
66
66
|
},
|
|
67
|
-
{
|
|
68
|
-
scale: h,
|
|
69
|
-
thresholdToPromote: g / h,
|
|
70
|
-
unit: "minute"
|
|
71
|
-
},
|
|
72
67
|
{
|
|
73
68
|
scale: g,
|
|
74
69
|
thresholdToPromote: _ / g,
|
|
75
|
-
unit: "
|
|
70
|
+
unit: "minute"
|
|
76
71
|
},
|
|
77
72
|
{
|
|
78
73
|
scale: _,
|
|
79
74
|
thresholdToPromote: v / _,
|
|
80
|
-
unit: "
|
|
75
|
+
unit: "hour"
|
|
81
76
|
},
|
|
82
77
|
{
|
|
83
78
|
scale: v,
|
|
84
79
|
thresholdToPromote: y / v,
|
|
85
|
-
unit: "
|
|
80
|
+
unit: "day"
|
|
86
81
|
},
|
|
87
82
|
{
|
|
88
83
|
scale: y,
|
|
84
|
+
thresholdToPromote: b / y,
|
|
85
|
+
unit: "week"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
scale: b,
|
|
89
89
|
thresholdToPromote: 12,
|
|
90
90
|
unit: "month"
|
|
91
91
|
},
|
|
92
92
|
{
|
|
93
|
-
scale:
|
|
93
|
+
scale: x,
|
|
94
94
|
thresholdToPromote: Infinity,
|
|
95
95
|
unit: "year"
|
|
96
96
|
}
|
|
97
97
|
];
|
|
98
|
-
function
|
|
99
|
-
Number.isFinite(
|
|
100
|
-
let n = Math.round(
|
|
101
|
-
for (let { scale: e, thresholdToPromote: t, unit: r } of
|
|
98
|
+
function C(e) {
|
|
99
|
+
Number.isFinite(e) || t("formatRelative received a non-finite time difference.");
|
|
100
|
+
let n = Math.round(e);
|
|
101
|
+
for (let { scale: e, thresholdToPromote: t, unit: r } of S) {
|
|
102
102
|
let i = Math.round(n / e);
|
|
103
103
|
if (Math.abs(i) < t) return {
|
|
104
104
|
unit: r,
|
|
@@ -107,10 +107,10 @@ function S(t) {
|
|
|
107
107
|
}
|
|
108
108
|
return {
|
|
109
109
|
unit: "year",
|
|
110
|
-
value: Math.round(n /
|
|
110
|
+
value: Math.round(n / x)
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
|
-
var
|
|
113
|
+
var w = [
|
|
114
114
|
"years",
|
|
115
115
|
"months",
|
|
116
116
|
"weeks",
|
|
@@ -122,62 +122,62 @@ var C = [
|
|
|
122
122
|
"microseconds",
|
|
123
123
|
"nanoseconds"
|
|
124
124
|
];
|
|
125
|
-
function
|
|
125
|
+
function T(e) {
|
|
126
126
|
let t = [];
|
|
127
|
-
for (let n of
|
|
127
|
+
for (let n of w) {
|
|
128
128
|
let r = Math.abs(e[n]);
|
|
129
129
|
r !== 0 && t.push(`${r} ${r === 1 ? n.slice(0, -1) : n}`);
|
|
130
130
|
}
|
|
131
131
|
return t.length === 0 ? "0 seconds" : t.join(", ");
|
|
132
132
|
}
|
|
133
|
-
function
|
|
133
|
+
function E(e, n, r, i) {
|
|
134
134
|
if (r.timeZone) return r.timeZone;
|
|
135
|
-
let o =
|
|
136
|
-
return o && s && o !== s &&
|
|
137
|
-
}
|
|
138
|
-
function E(e, t = {}) {
|
|
139
|
-
let r = t.timeZone ?? (e instanceof i.ZonedDateTime ? e.timeZoneId : void 0);
|
|
140
|
-
return f(t, r).format(new Date(n(e, { timeZone: r }).epochMilliseconds));
|
|
135
|
+
let o = e instanceof a.ZonedDateTime ? e.timeZoneId : void 0, s = n instanceof a.ZonedDateTime ? n.timeZoneId : void 0;
|
|
136
|
+
return o && s && o !== s && t(`${i} received ZonedDateTime inputs with different time zones. Pass options.timeZone explicitly.`), o ?? s;
|
|
141
137
|
}
|
|
142
|
-
function D(e, t
|
|
143
|
-
let
|
|
144
|
-
return
|
|
138
|
+
function D(e, t = {}) {
|
|
139
|
+
let n = t.timeZone ?? (e instanceof a.ZonedDateTime ? e.timeZoneId : void 0);
|
|
140
|
+
return p(t, n).format(new Date(r(e, { timeZone: n }).epochMilliseconds));
|
|
145
141
|
}
|
|
146
|
-
function O(e, t,
|
|
147
|
-
let i =
|
|
148
|
-
return
|
|
142
|
+
function O(e, t, n = {}) {
|
|
143
|
+
let i = E(e, t, n, "formatRange");
|
|
144
|
+
return p(n, i).formatRange(new Date(r(e, { timeZone: i }).epochMilliseconds), new Date(r(t, { timeZone: i }).epochMilliseconds));
|
|
149
145
|
}
|
|
150
|
-
function k(e, t = {}) {
|
|
151
|
-
|
|
146
|
+
function k(e, t, n = {}) {
|
|
147
|
+
let i = E(e, t, n, "formatRangeParts");
|
|
148
|
+
return p(n, i).formatRangeToParts(new Date(r(e, { timeZone: i }).epochMilliseconds), new Date(r(t, { timeZone: i }).epochMilliseconds));
|
|
152
149
|
}
|
|
153
|
-
function A(e,
|
|
154
|
-
|
|
155
|
-
return r(e, { timeZone: i }).toString();
|
|
150
|
+
function A(e, t = {}) {
|
|
151
|
+
return r(e, t).toString();
|
|
156
152
|
}
|
|
157
153
|
function j(e, t = {}) {
|
|
158
|
-
let
|
|
159
|
-
return
|
|
154
|
+
let r = n(e, t);
|
|
155
|
+
return i(e, { timeZone: r }).toString();
|
|
156
|
+
}
|
|
157
|
+
function M(e, t = {}) {
|
|
158
|
+
let n = e instanceof a.Instant ? e : e.toInstant(), r = t.base ? t.base instanceof a.Instant ? t.base : t.base.toInstant() : a.Now.instant(), { unit: i, value: o } = C((n.epochMilliseconds - r.epochMilliseconds) / 1e3);
|
|
159
|
+
return m(t).format(o, i);
|
|
160
160
|
}
|
|
161
|
-
function
|
|
161
|
+
function N(n) {
|
|
162
162
|
try {
|
|
163
|
-
return
|
|
164
|
-
} catch {
|
|
165
|
-
|
|
163
|
+
return a.Duration.from(n);
|
|
164
|
+
} catch (r) {
|
|
165
|
+
t(`Invalid duration input: "${String(n)}". Expected an ISO 8601 duration string or Temporal.DurationLike.`, e, { cause: r });
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
-
function N(e, t = {}) {
|
|
169
|
-
let n = M(e), r = m(t);
|
|
170
|
-
return r ? r.format(n) : w(n);
|
|
171
|
-
}
|
|
172
168
|
function P(e, t = {}) {
|
|
173
|
-
let
|
|
174
|
-
return
|
|
169
|
+
let n = N(e), r = h(t);
|
|
170
|
+
return r ? r.format(n) : T(n);
|
|
175
171
|
}
|
|
176
172
|
function F(e, t = {}) {
|
|
173
|
+
let n = t.timeZone ?? (e instanceof a.ZonedDateTime ? e.timeZoneId : void 0);
|
|
174
|
+
return p(t, n).formatToParts(new Date(r(e, { timeZone: n }).epochMilliseconds));
|
|
175
|
+
}
|
|
176
|
+
function I(e, t = {}) {
|
|
177
177
|
let { unit: n, value: r } = e;
|
|
178
178
|
return `${t.locale ? new Intl.NumberFormat(t.locale).format(r) : String(r)} ${r === 1 ? n : `${n}s`}`;
|
|
179
179
|
}
|
|
180
180
|
//#endregion
|
|
181
|
-
export {
|
|
181
|
+
export { D as format, P as formatDuration, A as formatInstant, F as formatParts, O as formatRange, k as formatRangeParts, M as formatRelative, j as formatZoned, I as humanize, N as parseDuration };
|
|
182
182
|
|
|
183
183
|
//# sourceMappingURL=format.js.map
|
package/dist/format.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","names":[],"sources":["../src/format.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant, toZoned } from './_convert';\nimport { inferTimeZone } from './_tz';\nimport { fail } from './errors';\nimport type {\n DurationFormatOptions,\n FormatOptions,\n FormatPattern,\n RelativeFormatOptions,\n RelativeTimeInput,\n TimeDiffResult,\n TimeInput,\n TimeZoneOptions,\n} from './types';\n\n// ─── Formatter types ──────────────────────────────────────────────────────────\n\ntype DurationFormatter = { format(value: Temporal.Duration): string };\ntype DurationFormatterConstructor = new (\n locales?: Intl.LocalesArgument,\n options?: { style?: 'digital' | 'long' | 'narrow' | 'short' },\n) => DurationFormatter;\n\n// ─── Formatter caches ─────────────────────────────────────────────────────────\n\nconst FORMATTER_CACHE_MAX = 128;\n\nfunction cappedGetOrCreate<V>(cache: Map<string, V>, key: string, factory: () => V): V {\n const cached = cache.get(key);\n\n if (cached !== undefined) return cached;\n\n if (cache.size >= FORMATTER_CACHE_MAX) {\n const oldest = cache.keys().next().value;\n\n if (oldest !== undefined) cache.delete(oldest);\n }\n\n const value = factory();\n\n cache.set(key, value);\n\n return value;\n}\n\nconst DATE_TIME_FORMATTER_CACHE = new Map<string, Intl.DateTimeFormat>();\nconst RELATIVE_TIME_FORMATTER_CACHE = new Map<string, Intl.RelativeTimeFormat>();\nconst DURATION_FORMATTER_CACHE = new Map<string, DurationFormatter>();\n\n// ─── Format presets ───────────────────────────────────────────────────────────\n\nconst FORMAT_PRESETS: Record<FormatPattern, Intl.DateTimeFormatOptions> = {\n 'date-only': { dateStyle: 'short' },\n long: { dateStyle: 'full', timeStyle: 'long' },\n medium: { dateStyle: 'medium', timeStyle: 'short' },\n short: { dateStyle: 'short', timeStyle: 'short' },\n 'time-only': { timeStyle: 'short' },\n};\n\n// ─── Formatter factory helpers ────────────────────────────────────────────────\n\nfunction serializeIntlOptions(options: Intl.DateTimeFormatOptions): string {\n return JSON.stringify(\n Object.entries(options)\n .filter(([, value]) => value !== undefined)\n .sort(([l], [r]) => l.localeCompare(r))\n .map(([key, value]) => [key, String(value)]),\n );\n}\n\nfunction makeFormatter(options: FormatOptions, fallbackTz?: string): Intl.DateTimeFormat {\n const timeZone = options.timeZone ?? fallbackTz;\n const locale = options.locale;\n\n if (options.intl !== undefined) {\n const cacheKey = `${String(locale ?? '')}|intl|${timeZone ?? ''}|${serializeIntlOptions(options.intl)}`;\n\n return cappedGetOrCreate(DATE_TIME_FORMATTER_CACHE, cacheKey, () => {\n const intlOptions = timeZone !== undefined ? { ...options.intl, timeZone: timeZone } : options.intl;\n\n return new Intl.DateTimeFormat(locale, intlOptions);\n });\n }\n\n const pattern = options.pattern ?? 'medium';\n const cacheKey = `${String(locale ?? '')}|${pattern}|${timeZone ?? ''}`;\n\n return cappedGetOrCreate(\n DATE_TIME_FORMATTER_CACHE,\n cacheKey,\n () => new Intl.DateTimeFormat(locale, { ...FORMAT_PRESETS[pattern], timeZone: timeZone }),\n );\n}\n\nfunction getRelativeFormatter(options: {\n locale?: Intl.LocalesArgument;\n numeric?: Intl.RelativeTimeFormatNumeric;\n style?: Intl.RelativeTimeFormatStyle;\n}): Intl.RelativeTimeFormat {\n const cacheKey = `${String(options.locale ?? '')}|${options.numeric ?? 'auto'}|${options.style ?? 'long'}`;\n\n return cappedGetOrCreate(\n RELATIVE_TIME_FORMATTER_CACHE,\n cacheKey,\n () =>\n new Intl.RelativeTimeFormat(options.locale, {\n numeric: options.numeric ?? 'auto',\n style: options.style ?? 'long',\n }),\n );\n}\n\nfunction getDurationFormatter(options: {\n locale?: Intl.LocalesArgument;\n style?: 'digital' | 'long' | 'narrow' | 'short';\n}): DurationFormatter | null {\n const IntlWithDurationFormat = Intl as typeof Intl & { DurationFormat?: DurationFormatterConstructor };\n\n if (!IntlWithDurationFormat.DurationFormat) return null;\n\n const cacheKey = `${String(options.locale ?? '')}|${options.style ?? ''}`;\n\n return cappedGetOrCreate(\n DURATION_FORMATTER_CACHE,\n cacheKey,\n () => new IntlWithDurationFormat.DurationFormat!(options.locale, { style: options.style }),\n );\n}\n\n// ─── Time scale constants ─────────────────────────────────────────────────────\n\nconst SECONDS_PER_MINUTE = 60;\nconst SECONDS_PER_HOUR = 3_600;\nconst SECONDS_PER_DAY = 86_400;\nconst SECONDS_PER_WEEK = 604_800;\nconst SECONDS_PER_MONTH = 2_629_800; // ≈ 30.4375 days × 86400\nconst SECONDS_PER_YEAR = 31_557_600; // 365.25 days × 86400\n\n// ─── Relative time helpers ────────────────────────────────────────────────────\n\nconst RELATIVE_UNITS: ReadonlyArray<{ scale: number; thresholdToPromote: number; unit: Intl.RelativeTimeFormatUnit }> =\n [\n { scale: 1, thresholdToPromote: SECONDS_PER_MINUTE, unit: 'second' },\n { scale: SECONDS_PER_MINUTE, thresholdToPromote: SECONDS_PER_HOUR / SECONDS_PER_MINUTE, unit: 'minute' },\n { scale: SECONDS_PER_HOUR, thresholdToPromote: SECONDS_PER_DAY / SECONDS_PER_HOUR, unit: 'hour' },\n { scale: SECONDS_PER_DAY, thresholdToPromote: SECONDS_PER_WEEK / SECONDS_PER_DAY, unit: 'day' },\n { scale: SECONDS_PER_WEEK, thresholdToPromote: SECONDS_PER_MONTH / SECONDS_PER_WEEK, unit: 'week' },\n { scale: SECONDS_PER_MONTH, thresholdToPromote: 12, unit: 'month' },\n { scale: SECONDS_PER_YEAR, thresholdToPromote: Number.POSITIVE_INFINITY, unit: 'year' },\n ];\n\nfunction toRelativeUnit(seconds: number): { unit: Intl.RelativeTimeFormatUnit; value: number } {\n if (!Number.isFinite(seconds)) fail('formatRelative received a non-finite time difference.');\n\n const roundedSeconds = Math.round(seconds);\n\n for (const { scale, thresholdToPromote, unit } of RELATIVE_UNITS) {\n const value = Math.round(roundedSeconds / scale);\n\n if (Math.abs(value) < thresholdToPromote) return { unit, value };\n }\n\n return { unit: 'year', value: Math.round(roundedSeconds / SECONDS_PER_YEAR) };\n}\n\n// ─── Duration fallback renderer ───────────────────────────────────────────────\n\n// All English duration unit names follow the same pluralization rule: singular = plural.slice(0, -1)\nconst DURATION_UNITS = [\n 'years',\n 'months',\n 'weeks',\n 'days',\n 'hours',\n 'minutes',\n 'seconds',\n 'milliseconds',\n 'microseconds',\n 'nanoseconds',\n] as const satisfies ReadonlyArray<keyof Temporal.Duration>;\n\n// English-only fallback; runs only when Intl.DurationFormat is unavailable in the runtime.\nfunction buildDurationFallback(duration: Temporal.Duration): string {\n const parts: string[] = [];\n\n for (const unit of DURATION_UNITS) {\n const value = Math.abs(duration[unit] as number);\n\n if (value !== 0) parts.push(`${value} ${value === 1 ? unit.slice(0, -1) : unit}`);\n }\n\n return parts.length === 0 ? '0 seconds' : parts.join(', ');\n}\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/**\n * Resolves a shared display timezone for two-input range functions.\n * Throws when both inputs are `ZonedDateTime` with different zones and no `options.timeZone` override.\n */\nfunction resolveRangeTz(start: TimeInput, end: TimeInput, options: FormatOptions, caller: string): string | undefined {\n if (options.timeZone) return options.timeZone;\n\n const startTz = start instanceof Temporal.ZonedDateTime ? start.timeZoneId : undefined;\n const endTz = end instanceof Temporal.ZonedDateTime ? end.timeZoneId : undefined;\n\n if (startTz && endTz && startTz !== endTz) {\n fail(`${caller} received ZonedDateTime inputs with different time zones. Pass options.timeZone explicitly.`);\n }\n\n return startTz ?? endTz;\n}\n\n// ─── Public API ───────────────────────────────────────────────────────────────\n\n/**\n * Formats `input` using `Intl.DateTimeFormat`. Defaults to `pattern: 'medium'`.\n *\n * Pass `intl` for full `Intl.DateTimeFormatOptions` control (mutually exclusive with `pattern`).\n * The timezone is inferred from a `ZonedDateTime` input or from `options.timeZone`.\n *\n * @example\n * ```ts\n * format(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { locale: 'en-GB', pattern: 'short', timeZone: 'UTC' })\n * // '21/03/2026, 10:15'\n * ```\n */\nexport function format(input: TimeInput, options: FormatOptions = {}): string {\n const timeZone = options.timeZone ?? (input instanceof Temporal.ZonedDateTime ? input.timeZoneId : undefined);\n\n return makeFormatter(options, timeZone).format(new Date(toInstant(input, { timeZone }).epochMilliseconds));\n}\n\n/**\n * Formats a time span between `start` and `end` using `Intl.DateTimeFormat.formatRange`.\n *\n * @example\n * ```ts\n * formatRange(start, end, { locale: 'en-GB', pattern: 'short', timeZone: 'UTC' })\n * // '21/03/2026, 10:00 – 12:00'\n * ```\n */\nexport function formatRange(start: TimeInput, end: TimeInput, options: FormatOptions = {}): string {\n const timeZone = resolveRangeTz(start, end, options, 'formatRange');\n const formatter = makeFormatter(options, timeZone);\n\n return formatter.formatRange(\n new Date(toInstant(start, { timeZone }).epochMilliseconds),\n new Date(toInstant(end, { timeZone }).epochMilliseconds),\n );\n}\n\n/**\n * Returns the raw `Intl.DateTimeRangeFormatPart[]` array for a time span, enabling\n * fine-grained rendering of range start, end, and shared parts separately.\n *\n * @example\n * ```ts\n * formatRangeParts(start, end, { locale: 'en-US', pattern: 'short', timeZone: 'UTC' })\n * // [{ type: 'month', value: '3', source: 'startRange' }, ...]\n * ```\n */\nexport function formatRangeParts(\n start: TimeInput,\n end: TimeInput,\n options: FormatOptions = {},\n): ReturnType<Intl.DateTimeFormat['formatRangeToParts']> {\n const timeZone = resolveRangeTz(start, end, options, 'formatRangeParts');\n const formatter = makeFormatter(options, timeZone);\n\n return formatter.formatRangeToParts(\n new Date(toInstant(start, { timeZone }).epochMilliseconds),\n new Date(toInstant(end, { timeZone }).epochMilliseconds),\n );\n}\n\n/**\n * Serializes `input` to a UTC ISO 8601 instant string (`2026-03-21T10:15:30Z`).\n *\n * Pass `options.timeZone` when `input` is a `PlainDate` or `PlainDateTime`.\n * Inferred from `ZonedDateTime`; ignored for `Instant`.\n *\n * @throws {TempoMissingTzError} When `input` is a `PlainDate` or `PlainDateTime` and `options.timeZone` is omitted.\n * @throws {TempoInvalidTzError} When `options.timeZone` is not a valid IANA timezone or UTC offset.\n *\n * @example\n * ```ts\n * formatInstant(parse('2026-03-21T11:15:30+01:00[Europe/Berlin]', { as: 'zonedDateTime' }))\n * // '2026-03-21T10:15:30Z'\n * ```\n */\nexport function formatInstant(input: TimeInput, options: TimeZoneOptions = {}): string {\n return toInstant(input, options).toString();\n}\n\n/**\n * Serializes `input` to a zoned ISO 8601 string (`2026-03-21T11:15:30+01:00[Europe/Berlin]`).\n *\n * @param options.timeZone - Required when `input` is a `PlainDate`, `PlainDateTime`, or `Instant`.\n * Inferred automatically from a `ZonedDateTime` input.\n *\n * @throws {TempoMissingTzError} When `input` is not a `ZonedDateTime` and `options.timeZone` is omitted.\n * @throws {TempoInvalidTzError} When `options.timeZone` is not a valid IANA timezone or UTC offset.\n *\n * @example\n * ```ts\n * formatZoned(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { timeZone: 'Europe/Berlin' })\n * // '2026-03-21T11:15:30+01:00[Europe/Berlin]'\n *\n * formatZoned(parse('2026-03-21T11:15:30+01:00[Europe/Berlin]', { as: 'zonedDateTime' }))\n * // '2026-03-21T11:15:30+01:00[Europe/Berlin]' (timeZone inferred)\n * ```\n */\nexport function formatZoned(input: TimeInput, options: TimeZoneOptions = {}): string {\n const timeZone = inferTimeZone(input, options);\n\n return toZoned(input, { timeZone }).toString();\n}\n\n/**\n * Formats `input` relative to `options.base` (defaults to now) using `Intl.RelativeTimeFormat`.\n *\n * @example\n * ```ts\n * formatRelative(parse('2026-03-21T12:00:00Z', { as: 'instant' }), {\n * base: parse('2026-03-21T10:00:00Z', { as: 'instant' }),\n * locale: 'en-US',\n * numeric: 'always',\n * })\n * // 'in 2 hours'\n * ```\n */\nexport function formatRelative(input: RelativeTimeInput, options: RelativeFormatOptions = {}): string {\n const target = input instanceof Temporal.Instant ? input : input.toInstant();\n const base = options.base\n ? options.base instanceof Temporal.Instant\n ? options.base\n : options.base.toInstant()\n : Temporal.Now.instant();\n const differenceInSeconds = (target.epochMilliseconds - base.epochMilliseconds) / 1000;\n const { unit, value } = toRelativeUnit(differenceInSeconds);\n\n return getRelativeFormatter(options).format(value, unit);\n}\n\n/**\n * Parses an ISO duration string or `Temporal.DurationLike` into a `Temporal.Duration`.\n *\n * @example\n * ```ts\n * parseDuration('PT2H30M').toString() // 'PT2H30M'\n * parseDuration({ hours: 2, minutes: 30 }).toString() // 'PT2H30M'\n * ```\n */\nexport function parseDuration(input: string | Temporal.DurationLike): Temporal.Duration {\n try {\n return Temporal.Duration.from(input);\n } catch {\n fail(`Invalid duration input: \"${String(input)}\". Expected an ISO 8601 duration string or Temporal.DurationLike.`);\n }\n}\n\n/**\n * Formats a duration using `Intl.DurationFormat` when available, falling back to\n * a human-readable plain-English string.\n *\n * @example\n * ```ts\n * formatDuration('PT2H30M', { locale: 'en-US', style: 'long' })\n * // '2 hours, 30 minutes'\n * ```\n */\nexport function formatDuration(input: string | Temporal.DurationLike, options: DurationFormatOptions = {}): string {\n const duration = parseDuration(input);\n const formatter = getDurationFormatter(options);\n\n if (formatter) return formatter.format(duration);\n\n return buildDurationFallback(duration);\n}\n\n/**\n * Returns the raw `Intl.DateTimeFormatPart[]` array for `input`, enabling\n * custom rendering where individual parts (year, month, day, etc.) need\n * to be styled or composed differently.\n *\n * @example\n * ```ts\n * formatParts(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { pattern: 'medium', timeZone: 'UTC' })\n * // [{ type: 'month', value: 'Mar' }, { type: 'literal', value: ' ' }, ...]\n * ```\n */\nexport function formatParts(input: TimeInput, options: FormatOptions = {}): Intl.DateTimeFormatPart[] {\n const timeZone = options.timeZone ?? (input instanceof Temporal.ZonedDateTime ? input.timeZoneId : undefined);\n\n return makeFormatter(options, timeZone).formatToParts(new Date(toInstant(input, { timeZone }).epochMilliseconds));\n}\n\n/**\n * Converts a `TimeDiffResult` to a human-readable string.\n * Uses the singular unit name when value is 1, plural (unit + 's') otherwise.\n *\n * Pass `options.locale` to localize the numeric part via `Intl.NumberFormat`.\n * Unit names remain English — for fully localized output use {@link formatRelative}\n * or {@link formatDuration} instead.\n *\n * @example\n * ```ts\n * humanize({ unit: 'day', value: 1 }) // '1 day'\n * humanize({ unit: 'day', value: 3 }) // '3 days'\n * humanize({ unit: 'day', value: 3 }, { locale: 'ar' }) // '٣ days'\n * humanize({ unit: 'millisecond', value: 0 }) // '0 milliseconds'\n * ```\n */\nexport function humanize(diff: TimeDiffResult, options: { locale?: Intl.LocalesArgument } = {}): string {\n const { unit, value } = diff;\n const formatted = options.locale ? new Intl.NumberFormat(options.locale).format(value) : String(value);\n\n return `${formatted} ${value === 1 ? unit : `${unit}s`}`;\n}\n"],"mappings":";;;;;AAyBA,IAAM,IAAsB;AAE5B,SAAS,EAAqB,GAAuB,GAAa,GAAqB;CACrF,IAAM,IAAS,EAAM,IAAI,CAAG;CAE5B,IAAI,MAAW,KAAA,GAAW,OAAO;CAEjC,IAAI,EAAM,QAAQ,GAAqB;EACrC,IAAM,IAAS,EAAM,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;EAEnC,AAAI,MAAW,KAAA,KAAW,EAAM,OAAO,CAAM;CAC/C;CAEA,IAAM,IAAQ,EAAQ;CAItB,OAFA,EAAM,IAAI,GAAK,CAAK,GAEb;AACT;AAEA,IAAM,oBAA4B,IAAI,IAAiC,GACjE,oBAAgC,IAAI,IAAqC,GACzE,oBAA2B,IAAI,IAA+B,GAI9D,IAAoE;CACxE,aAAa,EAAE,WAAW,QAAQ;CAClC,MAAM;EAAE,WAAW;EAAQ,WAAW;CAAO;CAC7C,QAAQ;EAAE,WAAW;EAAU,WAAW;CAAQ;CAClD,OAAO;EAAE,WAAW;EAAS,WAAW;CAAQ;CAChD,aAAa,EAAE,WAAW,QAAQ;AACpC;AAIA,SAAS,EAAqB,GAA6C;CACzE,OAAO,KAAK,UACV,OAAO,QAAQ,CAAO,CAAC,CACpB,QAAQ,GAAG,OAAW,MAAU,KAAA,CAAS,CAAC,CAC1C,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,CACtC,KAAK,CAAC,GAAK,OAAW,CAAC,GAAK,OAAO,CAAK,CAAC,CAAC,CAC/C;AACF;AAEA,SAAS,EAAc,GAAwB,GAA0C;CACvF,IAAM,IAAW,EAAQ,YAAY,GAC/B,IAAS,EAAQ;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,gBAAgB,EAAS,UAC/B,EAAQ,OACR,EAAQ,KAAK,UAAU,IACzB,EAAS,IAAI,QAAQ,GAEnB,EAAE,SAAM,aAAU,GADK,EAAO,oBAAoB,EAAK,qBAAqB,GACxB;CAE1D,OAAO,EAAqB,CAAO,CAAC,CAAC,OAAO,GAAO,CAAI;AACzD;AAWA,SAAgB,EAAc,GAA0D;CACtF,IAAI;EACF,OAAO,EAAS,SAAS,KAAK,CAAK;CACrC,QAAQ;EACN,EAAK,4BAA4B,OAAO,CAAK,EAAE,kEAAkE;CACnH;AACF;AAYA,SAAgB,EAAe,GAAuC,IAAiC,CAAC,GAAW;CACjH,IAAM,IAAW,EAAc,CAAK,GAC9B,IAAY,EAAqB,CAAO;CAI9C,OAFI,IAAkB,EAAU,OAAO,CAAQ,IAExC,EAAsB,CAAQ;AACvC;AAaA,SAAgB,EAAY,GAAkB,IAAyB,CAAC,GAA8B;CACpG,IAAM,IAAW,EAAQ,aAAa,aAAiB,EAAS,gBAAgB,EAAM,aAAa,KAAA;CAEnG,OAAO,EAAc,GAAS,CAAQ,CAAC,CAAC,cAAc,IAAI,KAAK,EAAU,GAAO,EAAE,YAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC;AAClH;AAkBA,SAAgB,EAAS,GAAsB,IAA6C,CAAC,GAAW;CACtG,IAAM,EAAE,SAAM,aAAU;CAGxB,OAAO,GAFW,EAAQ,SAAS,IAAI,KAAK,aAAa,EAAQ,MAAM,CAAC,CAAC,OAAO,CAAK,IAAI,OAAO,CAAK,EAEjF,GAAG,MAAU,IAAI,IAAO,GAAG,EAAK;AACtD"}
|
|
1
|
+
{"version":3,"file":"format.js","names":[],"sources":["../src/format.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant, toZoned } from './_convert';\nimport { inferTimeZone } from './_tz';\nimport { fail, TempoInvalidInputError } from './errors';\nimport type {\n DurationFormatOptions,\n FormatOptions,\n FormatPattern,\n RelativeFormatOptions,\n RelativeTimeInput,\n TimeDiffResult,\n TimeInput,\n TimeZoneOptions,\n} from './types';\n\n// ─── Formatter types ──────────────────────────────────────────────────────────\n\ntype DurationFormatter = { format(value: Temporal.Duration): string };\ntype DurationFormatterConstructor = new (\n locales?: Intl.LocalesArgument,\n options?: { style?: 'digital' | 'long' | 'narrow' | 'short' },\n) => DurationFormatter;\n\n// ─── Formatter caches ─────────────────────────────────────────────────────────\n\nconst FORMATTER_CACHE_MAX = 128;\n\nfunction cappedGetOrCreate<V>(cache: Map<string, V>, key: string, factory: () => V): V {\n const cached = cache.get(key);\n\n if (cached !== undefined) return cached;\n\n if (cache.size >= FORMATTER_CACHE_MAX) {\n const oldest = cache.keys().next().value;\n\n if (oldest !== undefined) cache.delete(oldest);\n }\n\n const value = factory();\n\n cache.set(key, value);\n\n return value;\n}\n\nconst DATE_TIME_FORMATTER_CACHE = new Map<string, Intl.DateTimeFormat>();\nconst RELATIVE_TIME_FORMATTER_CACHE = new Map<string, Intl.RelativeTimeFormat>();\nconst DURATION_FORMATTER_CACHE = new Map<string, DurationFormatter>();\n\n// ─── Format presets ───────────────────────────────────────────────────────────\n\nconst FORMAT_PRESETS: Record<FormatPattern, Intl.DateTimeFormatOptions> = {\n 'date-only': { dateStyle: 'short' },\n long: { dateStyle: 'full', timeStyle: 'long' },\n medium: { dateStyle: 'medium', timeStyle: 'short' },\n short: { dateStyle: 'short', timeStyle: 'short' },\n 'time-only': { timeStyle: 'short' },\n};\n\n// ─── Formatter factory helpers ────────────────────────────────────────────────\n\nfunction serializeIntlOptions(options: Intl.DateTimeFormatOptions): string {\n return JSON.stringify(\n Object.entries(options)\n .filter(([, value]) => value !== undefined)\n .sort(([l], [r]) => l.localeCompare(r))\n .map(([key, value]) => [key, String(value)]),\n );\n}\n\nfunction makeFormatter(options: FormatOptions, fallbackTz?: string): Intl.DateTimeFormat {\n const timeZone = options.timeZone ?? fallbackTz;\n const locale = options.locale;\n\n if (options.intl !== undefined) {\n const cacheKey = `${String(locale ?? '')}|intl|${timeZone ?? ''}|${serializeIntlOptions(options.intl)}`;\n\n return cappedGetOrCreate(DATE_TIME_FORMATTER_CACHE, cacheKey, () => {\n const intlOptions = timeZone !== undefined ? { ...options.intl, timeZone: timeZone } : options.intl;\n\n return new Intl.DateTimeFormat(locale, intlOptions);\n });\n }\n\n const pattern = options.pattern ?? 'medium';\n const cacheKey = `${String(locale ?? '')}|${pattern}|${timeZone ?? ''}`;\n\n return cappedGetOrCreate(\n DATE_TIME_FORMATTER_CACHE,\n cacheKey,\n () => new Intl.DateTimeFormat(locale, { ...FORMAT_PRESETS[pattern], timeZone: timeZone }),\n );\n}\n\nfunction getRelativeFormatter(options: {\n locale?: Intl.LocalesArgument;\n numeric?: Intl.RelativeTimeFormatNumeric;\n style?: Intl.RelativeTimeFormatStyle;\n}): Intl.RelativeTimeFormat {\n const cacheKey = `${String(options.locale ?? '')}|${options.numeric ?? 'auto'}|${options.style ?? 'long'}`;\n\n return cappedGetOrCreate(\n RELATIVE_TIME_FORMATTER_CACHE,\n cacheKey,\n () =>\n new Intl.RelativeTimeFormat(options.locale, {\n numeric: options.numeric ?? 'auto',\n style: options.style ?? 'long',\n }),\n );\n}\n\nfunction getDurationFormatter(options: {\n locale?: Intl.LocalesArgument;\n style?: 'digital' | 'long' | 'narrow' | 'short';\n}): DurationFormatter | null {\n const IntlWithDurationFormat = Intl as typeof Intl & { DurationFormat?: DurationFormatterConstructor };\n\n if (!IntlWithDurationFormat.DurationFormat) return null;\n\n const cacheKey = `${String(options.locale ?? '')}|${options.style ?? ''}`;\n\n return cappedGetOrCreate(\n DURATION_FORMATTER_CACHE,\n cacheKey,\n () => new IntlWithDurationFormat.DurationFormat!(options.locale, { style: options.style }),\n );\n}\n\n// ─── Time scale constants ─────────────────────────────────────────────────────\n\nconst SECONDS_PER_MINUTE = 60;\nconst SECONDS_PER_HOUR = 3_600;\nconst SECONDS_PER_DAY = 86_400;\nconst SECONDS_PER_WEEK = 604_800;\nconst SECONDS_PER_MONTH = 2_629_800; // ≈ 30.4375 days × 86400\nconst SECONDS_PER_YEAR = 31_557_600; // 365.25 days × 86400\n\n// ─── Relative time helpers ────────────────────────────────────────────────────\n\nconst RELATIVE_UNITS: ReadonlyArray<{ scale: number; thresholdToPromote: number; unit: Intl.RelativeTimeFormatUnit }> =\n [\n { scale: 1, thresholdToPromote: SECONDS_PER_MINUTE, unit: 'second' },\n { scale: SECONDS_PER_MINUTE, thresholdToPromote: SECONDS_PER_HOUR / SECONDS_PER_MINUTE, unit: 'minute' },\n { scale: SECONDS_PER_HOUR, thresholdToPromote: SECONDS_PER_DAY / SECONDS_PER_HOUR, unit: 'hour' },\n { scale: SECONDS_PER_DAY, thresholdToPromote: SECONDS_PER_WEEK / SECONDS_PER_DAY, unit: 'day' },\n { scale: SECONDS_PER_WEEK, thresholdToPromote: SECONDS_PER_MONTH / SECONDS_PER_WEEK, unit: 'week' },\n { scale: SECONDS_PER_MONTH, thresholdToPromote: 12, unit: 'month' },\n { scale: SECONDS_PER_YEAR, thresholdToPromote: Number.POSITIVE_INFINITY, unit: 'year' },\n ];\n\nfunction toRelativeUnit(seconds: number): { unit: Intl.RelativeTimeFormatUnit; value: number } {\n if (!Number.isFinite(seconds)) fail('formatRelative received a non-finite time difference.');\n\n const roundedSeconds = Math.round(seconds);\n\n for (const { scale, thresholdToPromote, unit } of RELATIVE_UNITS) {\n const value = Math.round(roundedSeconds / scale);\n\n if (Math.abs(value) < thresholdToPromote) return { unit, value };\n }\n\n return { unit: 'year', value: Math.round(roundedSeconds / SECONDS_PER_YEAR) };\n}\n\n// ─── Duration fallback renderer ───────────────────────────────────────────────\n\n// All English duration unit names follow the same pluralization rule: singular = plural.slice(0, -1)\nconst DURATION_UNITS = [\n 'years',\n 'months',\n 'weeks',\n 'days',\n 'hours',\n 'minutes',\n 'seconds',\n 'milliseconds',\n 'microseconds',\n 'nanoseconds',\n] as const satisfies ReadonlyArray<keyof Temporal.Duration>;\n\n// English-only fallback; runs only when Intl.DurationFormat is unavailable in the runtime.\nfunction buildDurationFallback(duration: Temporal.Duration): string {\n const parts: string[] = [];\n\n for (const unit of DURATION_UNITS) {\n const value = Math.abs(duration[unit] as number);\n\n if (value !== 0) parts.push(`${value} ${value === 1 ? unit.slice(0, -1) : unit}`);\n }\n\n return parts.length === 0 ? '0 seconds' : parts.join(', ');\n}\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/**\n * Resolves a shared display timezone for two-input range functions.\n * Throws when both inputs are `ZonedDateTime` with different zones and no `options.timeZone` override.\n */\nfunction resolveRangeTz(start: TimeInput, end: TimeInput, options: FormatOptions, caller: string): string | undefined {\n if (options.timeZone) return options.timeZone;\n\n const startTz = start instanceof Temporal.ZonedDateTime ? start.timeZoneId : undefined;\n const endTz = end instanceof Temporal.ZonedDateTime ? end.timeZoneId : undefined;\n\n if (startTz && endTz && startTz !== endTz) {\n fail(`${caller} received ZonedDateTime inputs with different time zones. Pass options.timeZone explicitly.`);\n }\n\n return startTz ?? endTz;\n}\n\n// ─── Public API ───────────────────────────────────────────────────────────────\n\n/**\n * Formats `input` using `Intl.DateTimeFormat`. Defaults to `pattern: 'medium'`.\n *\n * Pass `intl` for full `Intl.DateTimeFormatOptions` control (mutually exclusive with `pattern`).\n * The timezone is inferred from a `ZonedDateTime` input or from `options.timeZone`.\n *\n * @example\n * ```ts\n * format(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { locale: 'en-GB', pattern: 'short', timeZone: 'UTC' })\n * // '21/03/2026, 10:15'\n * ```\n */\nexport function format(input: TimeInput, options: FormatOptions = {}): string {\n const timeZone = options.timeZone ?? (input instanceof Temporal.ZonedDateTime ? input.timeZoneId : undefined);\n\n return makeFormatter(options, timeZone).format(new Date(toInstant(input, { timeZone }).epochMilliseconds));\n}\n\n/**\n * Formats a time span between `start` and `end` using `Intl.DateTimeFormat.formatRange`.\n *\n * @example\n * ```ts\n * formatRange(start, end, { locale: 'en-GB', pattern: 'short', timeZone: 'UTC' })\n * // '21/03/2026, 10:00 – 12:00'\n * ```\n */\nexport function formatRange(start: TimeInput, end: TimeInput, options: FormatOptions = {}): string {\n const timeZone = resolveRangeTz(start, end, options, 'formatRange');\n const formatter = makeFormatter(options, timeZone);\n\n return formatter.formatRange(\n new Date(toInstant(start, { timeZone }).epochMilliseconds),\n new Date(toInstant(end, { timeZone }).epochMilliseconds),\n );\n}\n\n/**\n * Returns the raw `Intl.DateTimeRangeFormatPart[]` array for a time span, enabling\n * fine-grained rendering of range start, end, and shared parts separately.\n *\n * @example\n * ```ts\n * formatRangeParts(start, end, { locale: 'en-US', pattern: 'short', timeZone: 'UTC' })\n * // [{ type: 'month', value: '3', source: 'startRange' }, ...]\n * ```\n */\nexport function formatRangeParts(\n start: TimeInput,\n end: TimeInput,\n options: FormatOptions = {},\n): ReturnType<Intl.DateTimeFormat['formatRangeToParts']> {\n const timeZone = resolveRangeTz(start, end, options, 'formatRangeParts');\n const formatter = makeFormatter(options, timeZone);\n\n return formatter.formatRangeToParts(\n new Date(toInstant(start, { timeZone }).epochMilliseconds),\n new Date(toInstant(end, { timeZone }).epochMilliseconds),\n );\n}\n\n/**\n * Serializes `input` to a UTC ISO 8601 instant string (`2026-03-21T10:15:30Z`).\n *\n * Pass `options.timeZone` when `input` is a `PlainDate` or `PlainDateTime`.\n * Inferred from `ZonedDateTime`; ignored for `Instant`.\n *\n * @throws {TempoMissingTzError} When `input` is a `PlainDate` or `PlainDateTime` and `options.timeZone` is omitted.\n * @throws {TempoInvalidTzError} When `options.timeZone` is not a valid IANA timezone or UTC offset.\n *\n * @example\n * ```ts\n * formatInstant(parse('2026-03-21T11:15:30+01:00[Europe/Berlin]', { as: 'zonedDateTime' }))\n * // '2026-03-21T10:15:30Z'\n * ```\n */\nexport function formatInstant(input: TimeInput, options: TimeZoneOptions = {}): string {\n return toInstant(input, options).toString();\n}\n\n/**\n * Serializes `input` to a zoned ISO 8601 string (`2026-03-21T11:15:30+01:00[Europe/Berlin]`).\n *\n * @param options.timeZone - Required when `input` is a `PlainDate`, `PlainDateTime`, or `Instant`.\n * Inferred automatically from a `ZonedDateTime` input.\n *\n * @throws {TempoMissingTzError} When `input` is not a `ZonedDateTime` and `options.timeZone` is omitted.\n * @throws {TempoInvalidTzError} When `options.timeZone` is not a valid IANA timezone or UTC offset.\n *\n * @example\n * ```ts\n * formatZoned(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { timeZone: 'Europe/Berlin' })\n * // '2026-03-21T11:15:30+01:00[Europe/Berlin]'\n *\n * formatZoned(parse('2026-03-21T11:15:30+01:00[Europe/Berlin]', { as: 'zonedDateTime' }))\n * // '2026-03-21T11:15:30+01:00[Europe/Berlin]' (timeZone inferred)\n * ```\n */\nexport function formatZoned(input: TimeInput, options: TimeZoneOptions = {}): string {\n const timeZone = inferTimeZone(input, options);\n\n return toZoned(input, { timeZone }).toString();\n}\n\n/**\n * Formats `input` relative to `options.base` (defaults to now) using `Intl.RelativeTimeFormat`.\n *\n * @example\n * ```ts\n * formatRelative(parse('2026-03-21T12:00:00Z', { as: 'instant' }), {\n * base: parse('2026-03-21T10:00:00Z', { as: 'instant' }),\n * locale: 'en-US',\n * numeric: 'always',\n * })\n * // 'in 2 hours'\n * ```\n */\nexport function formatRelative(input: RelativeTimeInput, options: RelativeFormatOptions = {}): string {\n const target = input instanceof Temporal.Instant ? input : input.toInstant();\n const base = options.base\n ? options.base instanceof Temporal.Instant\n ? options.base\n : options.base.toInstant()\n : Temporal.Now.instant();\n const differenceInSeconds = (target.epochMilliseconds - base.epochMilliseconds) / 1000;\n const { unit, value } = toRelativeUnit(differenceInSeconds);\n\n return getRelativeFormatter(options).format(value, unit);\n}\n\n/**\n * Parses an ISO duration string or `Temporal.DurationLike` into a `Temporal.Duration`.\n *\n * @example\n * ```ts\n * parseDuration('PT2H30M').toString() // 'PT2H30M'\n * parseDuration({ hours: 2, minutes: 30 }).toString() // 'PT2H30M'\n * ```\n */\nexport function parseDuration(input: string | Temporal.DurationLike): Temporal.Duration {\n try {\n return Temporal.Duration.from(input);\n } catch (error) {\n fail(\n `Invalid duration input: \"${String(input)}\". Expected an ISO 8601 duration string or Temporal.DurationLike.`,\n TempoInvalidInputError,\n { cause: error },\n );\n }\n}\n\n/**\n * Formats a duration using `Intl.DurationFormat` when available, falling back to\n * a human-readable plain-English string.\n *\n * @example\n * ```ts\n * formatDuration('PT2H30M', { locale: 'en-US', style: 'long' })\n * // '2 hours, 30 minutes'\n * ```\n */\nexport function formatDuration(input: string | Temporal.DurationLike, options: DurationFormatOptions = {}): string {\n const duration = parseDuration(input);\n const formatter = getDurationFormatter(options);\n\n if (formatter) return formatter.format(duration);\n\n return buildDurationFallback(duration);\n}\n\n/**\n * Returns the raw `Intl.DateTimeFormatPart[]` array for `input`, enabling\n * custom rendering where individual parts (year, month, day, etc.) need\n * to be styled or composed differently.\n *\n * @example\n * ```ts\n * formatParts(parse('2026-03-21T10:15:30Z', { as: 'instant' }), { pattern: 'medium', timeZone: 'UTC' })\n * // [{ type: 'month', value: 'Mar' }, { type: 'literal', value: ' ' }, ...]\n * ```\n */\nexport function formatParts(input: TimeInput, options: FormatOptions = {}): Intl.DateTimeFormatPart[] {\n const timeZone = options.timeZone ?? (input instanceof Temporal.ZonedDateTime ? input.timeZoneId : undefined);\n\n return makeFormatter(options, timeZone).formatToParts(new Date(toInstant(input, { timeZone }).epochMilliseconds));\n}\n\n/**\n * Converts a `TimeDiffResult` to a human-readable string.\n * Uses the singular unit name when value is 1, plural (unit + 's') otherwise.\n *\n * Pass `options.locale` to localize the numeric part via `Intl.NumberFormat`.\n * Unit names remain English — for fully localized output use {@link formatRelative}\n * or {@link formatDuration} instead.\n *\n * @example\n * ```ts\n * humanize({ unit: 'day', value: 1 }) // '1 day'\n * humanize({ unit: 'day', value: 3 }) // '3 days'\n * humanize({ unit: 'day', value: 3 }, { locale: 'ar' }) // '٣ days'\n * humanize({ unit: 'millisecond', value: 0 }) // '0 milliseconds'\n * ```\n */\nexport function humanize(diff: TimeDiffResult, options: { locale?: Intl.LocalesArgument } = {}): string {\n const { unit, value } = diff;\n const formatted = options.locale ? new Intl.NumberFormat(options.locale).format(value) : String(value);\n\n return `${formatted} ${value === 1 ? unit : `${unit}s`}`;\n}\n"],"mappings":";;;;;AAyBA,IAAM,IAAsB;AAE5B,SAAS,EAAqB,GAAuB,GAAa,GAAqB;CACrF,IAAM,IAAS,EAAM,IAAI,CAAG;CAE5B,IAAI,MAAW,KAAA,GAAW,OAAO;CAEjC,IAAI,EAAM,QAAQ,GAAqB;EACrC,IAAM,IAAS,EAAM,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;EAEnC,AAAI,MAAW,KAAA,KAAW,EAAM,OAAO,CAAM;CAC/C;CAEA,IAAM,IAAQ,EAAQ;CAItB,OAFA,EAAM,IAAI,GAAK,CAAK,GAEb;AACT;AAEA,IAAM,oBAA4B,IAAI,IAAiC,GACjE,oBAAgC,IAAI,IAAqC,GACzE,oBAA2B,IAAI,IAA+B,GAI9D,IAAoE;CACxE,aAAa,EAAE,WAAW,QAAQ;CAClC,MAAM;EAAE,WAAW;EAAQ,WAAW;CAAO;CAC7C,QAAQ;EAAE,WAAW;EAAU,WAAW;CAAQ;CAClD,OAAO;EAAE,WAAW;EAAS,WAAW;CAAQ;CAChD,aAAa,EAAE,WAAW,QAAQ;AACpC;AAIA,SAAS,EAAqB,GAA6C;CACzE,OAAO,KAAK,UACV,OAAO,QAAQ,CAAO,CAAC,CACpB,QAAQ,GAAG,OAAW,MAAU,KAAA,CAAS,CAAC,CAC1C,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,CACtC,KAAK,CAAC,GAAK,OAAW,CAAC,GAAK,OAAO,CAAK,CAAC,CAAC,CAC/C;AACF;AAEA,SAAS,EAAc,GAAwB,GAA0C;CACvF,IAAM,IAAW,EAAQ,YAAY,GAC/B,IAAS,EAAQ;CAEvB,IAAI,EAAQ,SAAS,KAAA,GAGnB,OAAO,EAAkB,GAA2B,GAFhC,OAAO,KAAU,EAAE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nB,EAAE,SAAM,aAAU,GADK,EAAO,oBAAoB,EAAK,qBAAqB,GACxB;CAE1D,OAAO,EAAqB,CAAO,CAAC,CAAC,OAAO,GAAO,CAAI;AACzD;AAWA,SAAgB,EAAc,GAA0D;CACtF,IAAI;EACF,OAAO,EAAS,SAAS,KAAK,CAAK;CACrC,SAAS,GAAO;EACd,EACE,4BAA4B,OAAO,CAAK,EAAE,oEAC1C,GACA,EAAE,OAAO,EAAM,CACjB;CACF;AACF;AAYA,SAAgB,EAAe,GAAuC,IAAiC,CAAC,GAAW;CACjH,IAAM,IAAW,EAAc,CAAK,GAC9B,IAAY,EAAqB,CAAO;CAI9C,OAFI,IAAkB,EAAU,OAAO,CAAQ,IAExC,EAAsB,CAAQ;AACvC;AAaA,SAAgB,EAAY,GAAkB,IAAyB,CAAC,GAA8B;CACpG,IAAM,IAAW,EAAQ,aAAa,aAAiB,EAAS,gBAAgB,EAAM,aAAa,KAAA;CAEnG,OAAO,EAAc,GAAS,CAAQ,CAAC,CAAC,cAAc,IAAI,KAAK,EAAU,GAAO,EAAE,YAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC;AAClH;AAkBA,SAAgB,EAAS,GAAsB,IAA6C,CAAC,GAAW;CACtG,IAAM,EAAE,SAAM,aAAU;CAGxB,OAAO,GAFW,EAAQ,SAAS,IAAI,KAAK,aAAa,EAAQ,MAAM,CAAC,CAAC,OAAO,CAAK,IAAI,OAAO,CAAK,EAEjF,GAAG,MAAU,IAAI,IAAO,GAAG,EAAK;AACtD"}
|
package/dist/tempo.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@js-temporal/polyfill");var t=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},n=class extends t{},r=class extends t{},i=class extends t{},a=class extends t{};function o(e,t=n){throw new t(e)}function s(t){try{e.Temporal.Instant.fromEpochMilliseconds(0).toZonedDateTimeISO(t)}catch{o(`Unknown or invalid timezone: "${t}". Expected an IANA timezone name or UTC offset.`,r)}return t}function c(t,n){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return r||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i),s(r)}function l(t,n){if(n.timeZone)return s(n.timeZone);let r;for(let n of t)n instanceof e.Temporal.ZonedDateTime&&(r?r!==n.timeZoneId&&o(`Inputs use different time zones. Pass options.timeZone explicitly.`):r=n.timeZoneId);return r||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i),r}var u=new Set([`day`,`month`,`week`,`year`]);function d(t,n={}){if(t instanceof e.Temporal.Instant)return t;if(t instanceof e.Temporal.ZonedDateTime)return t.toInstant();n.timeZone||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i);let r=s(n.timeZone);if(t instanceof e.Temporal.PlainDateTime)return t.toZonedDateTime(r,{disambiguation:n.disambiguation}).toInstant();if(t instanceof e.Temporal.PlainDate)return t.toZonedDateTime({timeZone:r}).toInstant();o(`Unsupported time input type: ${String(t)}`,a)}function f(t,n){let r=s(n.timeZone);if(t instanceof e.Temporal.ZonedDateTime)return t.withTimeZone(r);if(t instanceof e.Temporal.Instant)return t.toZonedDateTimeISO(r);if(t instanceof e.Temporal.PlainDateTime)return t.toZonedDateTime(r,{disambiguation:n.disambiguation});if(t instanceof e.Temporal.PlainDate)return t.toZonedDateTime({timeZone:r});o(`Unsupported time input type: ${String(t)}`,a)}function p(e,t){return f(e,{timeZone:t})}var m={hour:0,microsecond:0,millisecond:0,minute:0,nanosecond:0,second:0},h={day:m,hour:{microsecond:0,millisecond:0,minute:0,nanosecond:0,second:0},minute:{microsecond:0,millisecond:0,nanosecond:0,second:0},month:{...m,day:1},year:{...m,day:1,month:1}};function g(e,t,n){let r=f(e,n);if(t===`week`){let e=(r.dayOfWeek-(n.weekStartsOn??1)+7)%7;return r.subtract({days:e}).with(m).toInstant()}return r.with(h[t]).toInstant()}var _={day:{days:1},hour:{hours:1},minute:{minutes:1},month:{months:1},week:{weeks:1},year:{years:1}};function v(e,t,n={}){let r=c(e,n);return g(e,t,{timeZone:r,weekStartsOn:n.weekStartsOn}).toZonedDateTimeISO(r)}function ee(e,t,n={}){let r=c(e,n);return g(e,t,{timeZone:r,weekStartsOn:n.weekStartsOn}).toZonedDateTimeISO(r).add(_[t]).subtract({nanoseconds:1})}var y={day:864e5,hour:36e5,microsecond:1/1e3,millisecond:1,minute:6e4,nanosecond:1/1e6,second:1e3,week:6048e5},b=[{field:`years`,unit:`year`},{field:`months`,unit:`month`},{field:`weeks`,unit:`week`},{field:`days`,unit:`day`},{field:`hours`,unit:`hour`},{field:`minutes`,unit:`minute`},{field:`seconds`,unit:`second`},{field:`milliseconds`,unit:`millisecond`}];function x(t){let r=e.Temporal.Duration.from(t);if(r.years||r.months)throw new n(`classifyExpiry thresholds cannot contain months or years. Use fixed elapsed-time units.`);return r.weeks*y.week+r.days*y.day+r.hours*y.hour+r.minutes*y.minute+r.seconds*y.second+r.milliseconds+r.microseconds*y.microsecond+r.nanoseconds*y.nanosecond}function S(t){let n=t.relativeTo??e.Temporal.Now.instant(),r=d(t.value,t).epochMilliseconds-n.epochMilliseconds;return Object.entries(t.thresholds).map(([e,t])=>({key:e,max:x(t)})).sort((e,t)=>e.max-t.max).find(({max:e})=>r<=e)?.key??null}function C(e){for(let{field:t,unit:n}of b){let r=Math.abs(e[t]);if(r>0)return{unit:n,value:r}}return{unit:`millisecond`,value:0}}function w(t,n=e.Temporal.Now.instant(),r={}){if(!r.timeZone&&t instanceof e.Temporal.Instant&&n instanceof e.Temporal.Instant){let r=t.toZonedDateTimeISO(`UTC`),i=n.toZonedDateTimeISO(`UTC`);return C(e.Temporal.ZonedDateTime.compare(r,i)<=0?i.since(r,{largestUnit:`year`}):r.since(i,{largestUnit:`year`}))}let i=l([t,n],r),a=f(t,{timeZone:i}),o=f(n,{timeZone:i});return C(e.Temporal.ZonedDateTime.compare(a,o)<=0?o.since(a,{largestUnit:`year`}):a.since(o,{largestUnit:`year`}))}function T(t,n){return e.Temporal.Instant.compare(t,n)<=0?[t,n]:[n,t]}function E(t,n,r){if(!r.unit)return e.Temporal.Instant.compare(d(t,r),d(n,r));let i={timeZone:l([t,n],r),weekStartsOn:r.weekStartsOn};return e.Temporal.Instant.compare(g(t,r.unit,i),g(n,r.unit,i))}function D({end:e,start:t,value:n,...r}){if(!r.unit){let i=d(n,r),[a,o]=T(d(t,r),d(e,r));return{lower:a,target:i,upper:o}}let i={timeZone:l([n,t,e],r),weekStartsOn:r.weekStartsOn},a=g(n,r.unit,i),[o,s]=T(g(t,r.unit,i),g(e,r.unit,i));return{lower:o,target:a,upper:s}}function te(e,t,n={}){return E(e,t,n)<0}function ne(e,t,n={}){return E(e,t,n)>0}function re(e,t,n={}){return E(e,t,n)===0}function ie(t){let{lower:n,target:r,upper:i}=D(t);return e.Temporal.Instant.compare(n,r)<=0&&e.Temporal.Instant.compare(r,i)<=0}function O(t){let{lower:n,target:r,upper:i}=D(t),a=e.Temporal.Instant.compare(r,n)<0?n:e.Temporal.Instant.compare(r,i)>0?i:r;return t.value instanceof e.Temporal.ZonedDateTime?a.toZonedDateTimeISO(t.timeZone??t.value.timeZoneId):a}function k(t,n){try{return n.as===`zonedDateTime`?e.Temporal.ZonedDateTime.from(t):n.as===`instant`?e.Temporal.Instant.from(t):n.as===`plainDateTime`?e.Temporal.PlainDateTime.from(t):e.Temporal.PlainDate.from(t)}catch{o(`Invalid ${n.as} ISO 8601 string: "${t}".`)}}function A(t){return e.Temporal.Now.zonedDateTimeISO(s(t.timeZone))}function j(){return e.Temporal.Now.instant()}function M(e,t,n={}){let r=c(e,n);return f(e,{disambiguation:n.disambiguation,timeZone:r}).add(t)}function N(t){let{end:n,largestUnit:r,roundingIncrement:i,roundingMode:a,smallestUnit:o,start:s}=t,c={largestUnit:r,roundingIncrement:i,roundingMode:a,smallestUnit:o};if(!(r!==void 0&&u.has(r)||o!==void 0&&u.has(o))&&s instanceof e.Temporal.Instant&&n instanceof e.Temporal.Instant)return n.since(s,c);let d=l([s,n],t),p={disambiguation:t.disambiguation,timeZone:d};return f(n,p).since(f(s,p),c)}function P(t){return t instanceof e.Temporal.Instant||t instanceof e.Temporal.ZonedDateTime||t instanceof e.Temporal.PlainDateTime||t instanceof e.Temporal.PlainDate}var F=128;function I(e,t,n){let r=e.get(t);if(r!==void 0)return r;if(e.size>=F){let t=e.keys().next().value;t!==void 0&&e.delete(t)}let i=n();return e.set(t,i),i}var L=new Map,R=new Map,z=new Map,B={"date-only":{dateStyle:`short`},long:{dateStyle:`full`,timeStyle:`long`},medium:{dateStyle:`medium`,timeStyle:`short`},short:{dateStyle:`short`,timeStyle:`short`},"time-only":{timeStyle:`short`}};function V(e){return JSON.stringify(Object.entries(e).filter(([,e])=>e!==void 0).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,String(t)]))}function H(e,t){let n=e.timeZone??t,r=e.locale;if(e.intl!==void 0)return I(L,`${String(r??``)}|intl|${n??``}|${V(e.intl)}`,()=>{let t=n===void 0?e.intl:{...e.intl,timeZone:n};return new Intl.DateTimeFormat(r,t)});let i=e.pattern??`medium`;return I(L,`${String(r??``)}|${i}|${n??``}`,()=>new Intl.DateTimeFormat(r,{...B[i],timeZone:n}))}function U(e){return I(R,`${String(e.locale??``)}|${e.numeric??`auto`}|${e.style??`long`}`,()=>new Intl.RelativeTimeFormat(e.locale,{numeric:e.numeric??`auto`,style:e.style??`long`}))}function W(e){let t=Intl;return t.DurationFormat?I(z,`${String(e.locale??``)}|${e.style??``}`,()=>new t.DurationFormat(e.locale,{style:e.style})):null}var G=60,K=3600,q=86400,J=604800,Y=2629800,X=31557600,ae=[{scale:1,thresholdToPromote:G,unit:`second`},{scale:G,thresholdToPromote:K/G,unit:`minute`},{scale:K,thresholdToPromote:q/K,unit:`hour`},{scale:q,thresholdToPromote:J/q,unit:`day`},{scale:J,thresholdToPromote:Y/J,unit:`week`},{scale:Y,thresholdToPromote:12,unit:`month`},{scale:X,thresholdToPromote:1/0,unit:`year`}];function oe(e){Number.isFinite(e)||o(`formatRelative received a non-finite time difference.`);let t=Math.round(e);for(let{scale:e,thresholdToPromote:n,unit:r}of ae){let i=Math.round(t/e);if(Math.abs(i)<n)return{unit:r,value:i}}return{unit:`year`,value:Math.round(t/X)}}var se=[`years`,`months`,`weeks`,`days`,`hours`,`minutes`,`seconds`,`milliseconds`,`microseconds`,`nanoseconds`];function ce(e){let t=[];for(let n of se){let r=Math.abs(e[n]);r!==0&&t.push(`${r} ${r===1?n.slice(0,-1):n}`)}return t.length===0?`0 seconds`:t.join(`, `)}function Z(t,n,r,i){if(r.timeZone)return r.timeZone;let a=t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0,s=n instanceof e.Temporal.ZonedDateTime?n.timeZoneId:void 0;return a&&s&&a!==s&&o(`${i} received ZonedDateTime inputs with different time zones. Pass options.timeZone explicitly.`),a??s}function le(t,n={}){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return H(n,r).format(new Date(d(t,{timeZone:r}).epochMilliseconds))}function ue(e,t,n={}){let r=Z(e,t,n,`formatRange`);return H(n,r).formatRange(new Date(d(e,{timeZone:r}).epochMilliseconds),new Date(d(t,{timeZone:r}).epochMilliseconds))}function de(e,t,n={}){let r=Z(e,t,n,`formatRangeParts`);return H(n,r).formatRangeToParts(new Date(d(e,{timeZone:r}).epochMilliseconds),new Date(d(t,{timeZone:r}).epochMilliseconds))}function fe(e,t={}){return d(e,t).toString()}function pe(e,t={}){return f(e,{timeZone:c(e,t)}).toString()}function me(t,n={}){let r=t instanceof e.Temporal.Instant?t:t.toInstant(),i=n.base?n.base instanceof e.Temporal.Instant?n.base:n.base.toInstant():e.Temporal.Now.instant(),{unit:a,value:o}=oe((r.epochMilliseconds-i.epochMilliseconds)/1e3);return U(n).format(o,a)}function Q(t){try{return e.Temporal.Duration.from(t)}catch{o(`Invalid duration input: "${String(t)}". Expected an ISO 8601 duration string or Temporal.DurationLike.`)}}function he(e,t={}){let n=Q(e),r=W(t);return r?r.format(n):ce(n)}function ge(t,n={}){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return H(n,r).formatToParts(new Date(d(t,{timeZone:r}).epochMilliseconds))}function $(e,t={}){let{unit:n,value:r}=e;return`${t.locale?new Intl.NumberFormat(t.locale).format(r):String(r)} ${r===1?n:`${n}s`}`}function _e(t,r,i,a={}){let o=c(t,a),s=f(t,{timeZone:o}),l=f(r,{timeZone:o});if(e.Temporal.ZonedDateTime.compare(s.add(i),s)<=0)throw new n(`dateRange: step must advance time forward.`);return ve(s,l,i)}function*ve(t,n,r){for(let i=t;e.Temporal.ZonedDateTime.compare(i,n)<=0;i=i.add(r))yield i}function ye(e,t,n={}){let r=c(e,n),i=t.frequency===`daily`?{days:t.interval??1}:t.frequency===`weekly`?{weeks:t.interval??1}:t.frequency===`monthly`?{months:t.interval??1}:{years:t.interval??1},a=t.until?d(t.until,{timeZone:r}):void 0;return be(f(e,{timeZone:r}),i,t.count,a)}function*be(t,n,r,i){for(let a=t,o=0;r===void 0||o<r;a=a.add(n),o++){if(i&&e.Temporal.Instant.compare(a.toInstant(),i)>0)return;yield a}}exports.TempoError=t,exports.TempoInvalidInputError=n,exports.TempoInvalidTzError=r,exports.TempoMissingTzError=i,exports.TempoUnsupportedInputError=a,Object.defineProperty(exports,"Temporal",{enumerable:!0,get:function(){return e.Temporal}}),exports.clamp=O,exports.classifyExpiry=S,exports.contains=ie,exports.dateRange=_e,exports.difference=N,exports.endOf=ee,exports.format=le,exports.formatDuration=he,exports.formatInstant=fe,exports.formatParts=ge,exports.formatRange=ue,exports.formatRangeParts=de,exports.formatRelative=me,exports.formatZoned=pe,exports.humanize=$,exports.inTimeZone=p,exports.isAfter=ne,exports.isBefore=te,exports.isSame=re,exports.isValid=P,exports.now=A,exports.nowInstant=j,exports.parse=k,exports.parseDuration=Q,exports.recurrence=ye,exports.shift=M,exports.startOf=v,exports.timeDiff=w,exports.toInstant=d;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@js-temporal/polyfill");var t=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},n=class extends t{},r=class extends t{},i=class extends t{},a=class extends t{};function o(e,t=n,r){throw new t(e,r)}function s(t){try{e.Temporal.Instant.fromEpochMilliseconds(0).toZonedDateTimeISO(t)}catch(e){o(`Unknown or invalid timezone: "${t}". Expected an IANA timezone name or UTC offset.`,r,{cause:e})}return t}function c(t,n){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return r||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i),s(r)}function l(t,n){if(n.timeZone)return s(n.timeZone);let r;for(let n of t)n instanceof e.Temporal.ZonedDateTime&&(r?r!==n.timeZoneId&&o(`Inputs use different time zones. Pass options.timeZone explicitly.`):r=n.timeZoneId);return r||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i),r}var u=new Set([`day`,`month`,`week`,`year`]);function d(t,n={}){if(t instanceof e.Temporal.Instant)return t;if(t instanceof e.Temporal.ZonedDateTime)return t.toInstant();n.timeZone||o(`This operation requires a timeZone. Pass options.timeZone or use a ZonedDateTime input.`,i);let r=s(n.timeZone);if(t instanceof e.Temporal.PlainDateTime)return t.toZonedDateTime(r,{disambiguation:n.disambiguation}).toInstant();if(t instanceof e.Temporal.PlainDate)return t.toZonedDateTime({timeZone:r}).toInstant();o(`Unsupported time input type: ${String(t)}`,a)}function f(t,n){let r=s(n.timeZone);if(t instanceof e.Temporal.ZonedDateTime)return t.withTimeZone(r);if(t instanceof e.Temporal.Instant)return t.toZonedDateTimeISO(r);if(t instanceof e.Temporal.PlainDateTime)return t.toZonedDateTime(r,{disambiguation:n.disambiguation});if(t instanceof e.Temporal.PlainDate)return t.toZonedDateTime({timeZone:r});o(`Unsupported time input type: ${String(t)}`,a)}function p(e,t){return f(e,{timeZone:t})}var m={hour:0,microsecond:0,millisecond:0,minute:0,nanosecond:0,second:0},h={day:m,hour:{microsecond:0,millisecond:0,minute:0,nanosecond:0,second:0},minute:{microsecond:0,millisecond:0,nanosecond:0,second:0},month:{...m,day:1},year:{...m,day:1,month:1}};function g(e,t,n){let r=f(e,n);if(t===`week`){let e=(r.dayOfWeek-(n.weekStartsOn??1)+7)%7;return r.subtract({days:e}).with(m).toInstant()}return r.with(h[t]).toInstant()}var _={day:{days:1},hour:{hours:1},minute:{minutes:1},month:{months:1},week:{weeks:1},year:{years:1}};function v(e,t,n={}){let r=c(e,n);return g(e,t,{timeZone:r,weekStartsOn:n.weekStartsOn}).toZonedDateTimeISO(r)}function ee(e,t,n={}){let r=c(e,n);return g(e,t,{timeZone:r,weekStartsOn:n.weekStartsOn}).toZonedDateTimeISO(r).add(_[t]).subtract({nanoseconds:1})}var y={day:864e5,hour:36e5,microsecond:1/1e3,millisecond:1,minute:6e4,nanosecond:1/1e6,second:1e3,week:6048e5},b=[{field:`years`,unit:`year`},{field:`months`,unit:`month`},{field:`weeks`,unit:`week`},{field:`days`,unit:`day`},{field:`hours`,unit:`hour`},{field:`minutes`,unit:`minute`},{field:`seconds`,unit:`second`},{field:`milliseconds`,unit:`millisecond`}];function x(t){let r=e.Temporal.Duration.from(t);if(r.years||r.months)throw new n(`classifyExpiry thresholds cannot contain months or years. Use fixed elapsed-time units.`);return r.weeks*y.week+r.days*y.day+r.hours*y.hour+r.minutes*y.minute+r.seconds*y.second+r.milliseconds+r.microseconds*y.microsecond+r.nanoseconds*y.nanosecond}function S(t){let n=t.relativeTo??e.Temporal.Now.instant(),r=d(t.value,t).epochMilliseconds-n.epochMilliseconds;return Object.entries(t.thresholds).map(([e,t])=>({key:e,max:x(t)})).sort((e,t)=>e.max-t.max).find(({max:e})=>r<=e)?.key??null}function C(e){for(let{field:t,unit:n}of b){let r=Math.abs(e[t]);if(r>0)return{unit:n,value:r}}return{unit:`millisecond`,value:0}}function w(t,n=e.Temporal.Now.instant(),r={}){if(!r.timeZone&&t instanceof e.Temporal.Instant&&n instanceof e.Temporal.Instant){let r=t.toZonedDateTimeISO(`UTC`),i=n.toZonedDateTimeISO(`UTC`);return C(e.Temporal.ZonedDateTime.compare(r,i)<=0?i.since(r,{largestUnit:`year`}):r.since(i,{largestUnit:`year`}))}let i=l([t,n],r),a=f(t,{timeZone:i}),o=f(n,{timeZone:i});return C(e.Temporal.ZonedDateTime.compare(a,o)<=0?o.since(a,{largestUnit:`year`}):a.since(o,{largestUnit:`year`}))}function T(t,n){return e.Temporal.Instant.compare(t,n)<=0?[t,n]:[n,t]}function E(t,n,r){if(!r.unit)return e.Temporal.Instant.compare(d(t,r),d(n,r));let i={timeZone:l([t,n],r),weekStartsOn:r.weekStartsOn};return e.Temporal.Instant.compare(g(t,r.unit,i),g(n,r.unit,i))}function D({end:e,start:t,value:n,...r}){if(!r.unit){let i=d(n,r),[a,o]=T(d(t,r),d(e,r));return{lower:a,target:i,upper:o}}let i={timeZone:l([n,t,e],r),weekStartsOn:r.weekStartsOn},a=g(n,r.unit,i),[o,s]=T(g(t,r.unit,i),g(e,r.unit,i));return{lower:o,target:a,upper:s}}function te(e,t,n={}){return E(e,t,n)<0}function ne(e,t,n={}){return E(e,t,n)>0}function re(e,t,n={}){return E(e,t,n)===0}function ie(t){let{lower:n,target:r,upper:i}=D(t);return e.Temporal.Instant.compare(n,r)<=0&&e.Temporal.Instant.compare(r,i)<=0}function O(t){let{lower:n,target:r,upper:i}=D(t),a=e.Temporal.Instant.compare(r,n)<0?n:e.Temporal.Instant.compare(r,i)>0?i:r;return t.value instanceof e.Temporal.ZonedDateTime?a.toZonedDateTimeISO(t.timeZone??t.value.timeZoneId):a}function k(t,r){try{return r.as===`zonedDateTime`?e.Temporal.ZonedDateTime.from(t):r.as===`instant`?e.Temporal.Instant.from(t):r.as===`plainDateTime`?e.Temporal.PlainDateTime.from(t):e.Temporal.PlainDate.from(t)}catch(e){o(`Invalid ${r.as} ISO 8601 string: "${t}".`,n,{cause:e})}}function A(t){return e.Temporal.Now.zonedDateTimeISO(s(t.timeZone))}function j(){return e.Temporal.Now.instant()}function M(e,t,n={}){let r=c(e,n);return f(e,{disambiguation:n.disambiguation,timeZone:r}).add(t)}function N(t){let{end:n,largestUnit:r,roundingIncrement:i,roundingMode:a,smallestUnit:o,start:s}=t,c={largestUnit:r,roundingIncrement:i,roundingMode:a,smallestUnit:o};if(!(r!==void 0&&u.has(r)||o!==void 0&&u.has(o))&&s instanceof e.Temporal.Instant&&n instanceof e.Temporal.Instant)return n.since(s,c);let d=l([s,n],t),p={disambiguation:t.disambiguation,timeZone:d};return f(n,p).since(f(s,p),c)}function P(t){return t instanceof e.Temporal.Instant||t instanceof e.Temporal.ZonedDateTime||t instanceof e.Temporal.PlainDateTime||t instanceof e.Temporal.PlainDate}var F=128;function I(e,t,n){let r=e.get(t);if(r!==void 0)return r;if(e.size>=F){let t=e.keys().next().value;t!==void 0&&e.delete(t)}let i=n();return e.set(t,i),i}var L=new Map,R=new Map,z=new Map,B={"date-only":{dateStyle:`short`},long:{dateStyle:`full`,timeStyle:`long`},medium:{dateStyle:`medium`,timeStyle:`short`},short:{dateStyle:`short`,timeStyle:`short`},"time-only":{timeStyle:`short`}};function V(e){return JSON.stringify(Object.entries(e).filter(([,e])=>e!==void 0).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,String(t)]))}function H(e,t){let n=e.timeZone??t,r=e.locale;if(e.intl!==void 0)return I(L,`${String(r??``)}|intl|${n??``}|${V(e.intl)}`,()=>{let t=n===void 0?e.intl:{...e.intl,timeZone:n};return new Intl.DateTimeFormat(r,t)});let i=e.pattern??`medium`;return I(L,`${String(r??``)}|${i}|${n??``}`,()=>new Intl.DateTimeFormat(r,{...B[i],timeZone:n}))}function U(e){return I(R,`${String(e.locale??``)}|${e.numeric??`auto`}|${e.style??`long`}`,()=>new Intl.RelativeTimeFormat(e.locale,{numeric:e.numeric??`auto`,style:e.style??`long`}))}function W(e){let t=Intl;return t.DurationFormat?I(z,`${String(e.locale??``)}|${e.style??``}`,()=>new t.DurationFormat(e.locale,{style:e.style})):null}var G=60,K=3600,q=86400,J=604800,Y=2629800,X=31557600,ae=[{scale:1,thresholdToPromote:G,unit:`second`},{scale:G,thresholdToPromote:K/G,unit:`minute`},{scale:K,thresholdToPromote:q/K,unit:`hour`},{scale:q,thresholdToPromote:J/q,unit:`day`},{scale:J,thresholdToPromote:Y/J,unit:`week`},{scale:Y,thresholdToPromote:12,unit:`month`},{scale:X,thresholdToPromote:1/0,unit:`year`}];function oe(e){Number.isFinite(e)||o(`formatRelative received a non-finite time difference.`);let t=Math.round(e);for(let{scale:e,thresholdToPromote:n,unit:r}of ae){let i=Math.round(t/e);if(Math.abs(i)<n)return{unit:r,value:i}}return{unit:`year`,value:Math.round(t/X)}}var se=[`years`,`months`,`weeks`,`days`,`hours`,`minutes`,`seconds`,`milliseconds`,`microseconds`,`nanoseconds`];function ce(e){let t=[];for(let n of se){let r=Math.abs(e[n]);r!==0&&t.push(`${r} ${r===1?n.slice(0,-1):n}`)}return t.length===0?`0 seconds`:t.join(`, `)}function Z(t,n,r,i){if(r.timeZone)return r.timeZone;let a=t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0,s=n instanceof e.Temporal.ZonedDateTime?n.timeZoneId:void 0;return a&&s&&a!==s&&o(`${i} received ZonedDateTime inputs with different time zones. Pass options.timeZone explicitly.`),a??s}function le(t,n={}){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return H(n,r).format(new Date(d(t,{timeZone:r}).epochMilliseconds))}function ue(e,t,n={}){let r=Z(e,t,n,`formatRange`);return H(n,r).formatRange(new Date(d(e,{timeZone:r}).epochMilliseconds),new Date(d(t,{timeZone:r}).epochMilliseconds))}function de(e,t,n={}){let r=Z(e,t,n,`formatRangeParts`);return H(n,r).formatRangeToParts(new Date(d(e,{timeZone:r}).epochMilliseconds),new Date(d(t,{timeZone:r}).epochMilliseconds))}function fe(e,t={}){return d(e,t).toString()}function pe(e,t={}){return f(e,{timeZone:c(e,t)}).toString()}function me(t,n={}){let r=t instanceof e.Temporal.Instant?t:t.toInstant(),i=n.base?n.base instanceof e.Temporal.Instant?n.base:n.base.toInstant():e.Temporal.Now.instant(),{unit:a,value:o}=oe((r.epochMilliseconds-i.epochMilliseconds)/1e3);return U(n).format(o,a)}function Q(t){try{return e.Temporal.Duration.from(t)}catch(e){o(`Invalid duration input: "${String(t)}". Expected an ISO 8601 duration string or Temporal.DurationLike.`,n,{cause:e})}}function he(e,t={}){let n=Q(e),r=W(t);return r?r.format(n):ce(n)}function ge(t,n={}){let r=n.timeZone??(t instanceof e.Temporal.ZonedDateTime?t.timeZoneId:void 0);return H(n,r).formatToParts(new Date(d(t,{timeZone:r}).epochMilliseconds))}function $(e,t={}){let{unit:n,value:r}=e;return`${t.locale?new Intl.NumberFormat(t.locale).format(r):String(r)} ${r===1?n:`${n}s`}`}function _e(t,r,i,a={}){let o=c(t,a),s=f(t,{timeZone:o}),l=f(r,{timeZone:o});if(e.Temporal.ZonedDateTime.compare(s.add(i),s)<=0)throw new n(`dateRange: step must advance time forward.`);return ve(s,l,i)}function*ve(t,n,r){for(let i=t;e.Temporal.ZonedDateTime.compare(i,n)<=0;i=i.add(r))yield i}function ye(e,t,n={}){let r=c(e,n),i=t.frequency===`daily`?{days:t.interval??1}:t.frequency===`weekly`?{weeks:t.interval??1}:t.frequency===`monthly`?{months:t.interval??1}:{years:t.interval??1},a=t.until?d(t.until,{timeZone:r}):void 0;return be(f(e,{timeZone:r}),i,t.count,a)}function*be(t,n,r,i){for(let a=t,o=0;r===void 0||o<r;a=a.add(n),o++){if(i&&e.Temporal.Instant.compare(a.toInstant(),i)>0)return;yield a}}exports.TempoError=t,exports.TempoInvalidInputError=n,exports.TempoInvalidTzError=r,exports.TempoMissingTzError=i,exports.TempoUnsupportedInputError=a,Object.defineProperty(exports,"Temporal",{enumerable:!0,get:function(){return e.Temporal}}),exports.clamp=O,exports.classifyExpiry=S,exports.contains=ie,exports.dateRange=_e,exports.difference=N,exports.endOf=ee,exports.format=le,exports.formatDuration=he,exports.formatInstant=fe,exports.formatParts=ge,exports.formatRange=ue,exports.formatRangeParts=de,exports.formatRelative=me,exports.formatZoned=pe,exports.humanize=$,exports.inTimeZone=p,exports.isAfter=ne,exports.isBefore=te,exports.isSame=re,exports.isValid=P,exports.now=A,exports.nowInstant=j,exports.parse=k,exports.parseDuration=Q,exports.recurrence=ye,exports.shift=M,exports.startOf=v,exports.timeDiff=w,exports.toInstant=d;
|
|
2
2
|
//# sourceMappingURL=tempo.cjs.map
|