@vielzeug/tempo 1.0.6 → 2.0.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/README.md +18 -43
- package/dist/_convert.cjs +1 -1
- package/dist/_convert.cjs.map +1 -1
- package/dist/_convert.d.ts +9 -43
- package/dist/_convert.d.ts.map +1 -1
- package/dist/_convert.js +12 -10
- package/dist/_convert.js.map +1 -1
- package/dist/_floor.cjs +1 -1
- package/dist/_floor.cjs.map +1 -1
- package/dist/_floor.d.ts +3 -7
- package/dist/_floor.d.ts.map +1 -1
- package/dist/_floor.js +1 -4
- package/dist/_floor.js.map +1 -1
- package/dist/_tz.cjs +1 -1
- package/dist/_tz.cjs.map +1 -1
- package/dist/_tz.d.ts +4 -11
- package/dist/_tz.d.ts.map +1 -1
- package/dist/_tz.js +8 -16
- package/dist/_tz.js.map +1 -1
- package/dist/boundary.cjs +1 -1
- package/dist/boundary.cjs.map +1 -1
- package/dist/boundary.d.ts +0 -21
- package/dist/boundary.d.ts.map +1 -1
- package/dist/boundary.js +2 -2
- package/dist/boundary.js.map +1 -1
- package/dist/classify.cjs +1 -1
- package/dist/classify.cjs.map +1 -1
- package/dist/classify.d.ts +3 -45
- package/dist/classify.d.ts.map +1 -1
- package/dist/classify.js +33 -31
- package/dist/classify.js.map +1 -1
- package/dist/compare.cjs +1 -1
- package/dist/compare.cjs.map +1 -1
- package/dist/compare.d.ts +6 -70
- package/dist/compare.d.ts.map +1 -1
- package/dist/compare.js +31 -45
- package/dist/compare.js.map +1 -1
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +21 -131
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +30 -73
- package/dist/core.js.map +1 -1
- package/dist/format.cjs +1 -1
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.ts +17 -17
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +10 -10
- package/dist/format.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/range.cjs +1 -1
- package/dist/range.cjs.map +1 -1
- package/dist/range.d.ts +3 -57
- package/dist/range.d.ts.map +1 -1
- package/dist/range.js +9 -20
- package/dist/range.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/dist/types.d.ts +34 -36
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/classify.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classify.cjs","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"classify.cjs","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {\n ClassifyExpiryInput,\n FixedDuration,\n TimeDiffResult,\n TimeDiffUnit,\n TimeInput,\n TimeZoneOptions,\n} from './types';\n\nimport { toInstant, toZoned } from './_convert';\nimport { inferSharedTimeZone } from './_tz';\nimport { TempoInvalidInputError } from './errors';\n\nconst MILLIS = {\n day: 86_400_000,\n hour: 3_600_000,\n microsecond: 1 / 1_000,\n millisecond: 1,\n minute: 60_000,\n nanosecond: 1 / 1_000_000,\n second: 1_000,\n week: 604_800_000,\n} as const;\nconst UNIT_ORDER: ReadonlyArray<{ field: keyof Temporal.Duration; unit: TimeDiffUnit }> = [\n { field: 'years', unit: 'year' },\n { field: 'months', unit: 'month' },\n { field: 'weeks', unit: 'week' },\n { field: 'days', unit: 'day' },\n { field: 'hours', unit: 'hour' },\n { field: 'minutes', unit: 'minute' },\n { field: 'seconds', unit: 'second' },\n { field: 'milliseconds', unit: 'millisecond' },\n];\n\n// Expiry classification is elapsed-time math. Calendar months and years are rejected rather than approximated.\nfunction fixedDurationToMilliseconds(input: FixedDuration): number {\n const duration = Temporal.Duration.from(input);\n\n if (duration.years || duration.months) {\n throw new TempoInvalidInputError(\n 'classifyExpiry thresholds cannot contain months or years. Use fixed elapsed-time units.',\n );\n }\n\n return (\n duration.weeks * MILLIS.week +\n duration.days * MILLIS.day +\n duration.hours * MILLIS.hour +\n duration.minutes * MILLIS.minute +\n duration.seconds * MILLIS.second +\n duration.milliseconds +\n duration.microseconds * MILLIS.microsecond +\n duration.nanoseconds * MILLIS.nanosecond\n );\n}\n\nexport function classifyExpiry<K extends string>(input: ClassifyExpiryInput<K>): K | null {\n const reference = input.relativeTo ?? Temporal.Now.instant();\n const value = toInstant(input.value, input);\n const elapsed = value.epochMilliseconds - reference.epochMilliseconds;\n const thresholds = (Object.entries(input.thresholds) as Array<[K, FixedDuration]>)\n .map(([key, duration]) => ({ key, max: fixedDurationToMilliseconds(duration) }))\n .sort((left, right) => left.max - right.max);\n\n return thresholds.find(({ max }) => elapsed <= max)?.key ?? null;\n}\n\nfunction pickLargestUnit(duration: Temporal.Duration): TimeDiffResult {\n for (const { field, unit } of UNIT_ORDER) {\n const value = Math.abs(duration[field] as number);\n\n if (value > 0) return { unit, value };\n }\n\n return { unit: 'millisecond', value: 0 };\n}\n\nexport function timeDiff(\n a: TimeInput,\n b: TimeInput = Temporal.Now.instant(),\n options: TimeZoneOptions = {},\n): TimeDiffResult {\n if (!options.timeZone && a instanceof Temporal.Instant && b instanceof Temporal.Instant) {\n const left = a.toZonedDateTimeISO('UTC');\n const right = b.toZonedDateTimeISO('UTC');\n\n return pickLargestUnit(\n Temporal.ZonedDateTime.compare(left, right) <= 0\n ? right.since(left, { largestUnit: 'year' })\n : left.since(right, { largestUnit: 'year' }),\n );\n }\n\n const timeZone = inferSharedTimeZone([a, b], options);\n const left = toZoned(a, { timeZone });\n const right = toZoned(b, { timeZone });\n\n return pickLargestUnit(\n Temporal.ZonedDateTime.compare(left, right) <= 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C,EAAE,KAAO,IAC9D,CAEA,SAAS,EAAgB,EAA6C,CACpE,IAAK,GAAM,CAAE,QAAO,UAAU,EAAY,CACxC,IAAM,EAAQ,KAAK,IAAI,EAAS,EAAgB,EAEhD,GAAI,EAAQ,EAAG,MAAO,CAAE,OAAM,OAAM,CACtC,CAEA,MAAO,CAAE,KAAM,cAAe,MAAO,CAAE,CACzC,CAEA,SAAgB,EACd,EACA,EAAe,EAAA,SAAS,IAAI,QAAQ,EACpC,EAA2B,CAAC,EACZ,CAChB,GAAI,CAAC,EAAQ,UAAY,aAAa,EAAA,SAAS,SAAW,aAAa,EAAA,SAAS,QAAS,CACvF,IAAM,EAAO,EAAE,mBAAmB,KAAK,EACjC,EAAQ,EAAE,mBAAmB,KAAK,EAExC,OAAO,EACL,EAAA,SAAS,cAAc,QAAQ,EAAM,CAAK,GAAK,EAC3C,EAAM,MAAM,EAAM,CAAE,YAAa,MAAO,CAAC,EACzC,EAAK,MAAM,EAAO,CAAE,YAAa,MAAO,CAAC,CAC/C,CACF,CAEA,IAAM,EAAW,EAAA,oBAAoB,CAAC,EAAG,CAAC,EAAG,CAAO,EAC9C,EAAO,EAAA,QAAQ,EAAG,CAAE,UAAS,CAAC,EAC9B,EAAQ,EAAA,QAAQ,EAAG,CAAE,UAAS,CAAC,EAErC,OAAO,EACL,EAAA,SAAS,cAAc,QAAQ,EAAM,CAAK,GAAK,EAC3C,EAAM,MAAM,EAAM,CAAE,YAAa,MAAO,CAAC,EACzC,EAAK,MAAM,EAAO,CAAE,YAAa,MAAO,CAAC,CAC/C,CACF"}
|
package/dist/classify.d.ts
CHANGED
|
@@ -1,46 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Classifies a date into a user-defined bucket by comparing diff = date − now
|
|
5
|
-
* against the provided thresholds (sorted ascending). Returns the key of the
|
|
6
|
-
* first threshold the diff falls within, or `null` if no threshold matches.
|
|
7
|
-
*
|
|
8
|
-
* Thresholds accept negative durations to classify past dates. The function
|
|
9
|
-
* requires `options.tz` when input is a `PlainDate` or `PlainDateTime`.
|
|
10
|
-
*
|
|
11
|
-
* **Performance:** threshold objects are cached by reference in a `WeakMap`. Define
|
|
12
|
-
* the threshold record at module scope (not inline) so sorting is performed only once
|
|
13
|
-
* per unique object.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* expires(expiresAt, {
|
|
18
|
-
* longExpired: { days: -30 }, // more than 30 days in the past
|
|
19
|
-
* expired: { days: 0 }, // any past date
|
|
20
|
-
* critical: { days: 3 }, // within 3 days
|
|
21
|
-
* warning: { days: 14 }, // within 14 days
|
|
22
|
-
* safe: { years: 100 }, // catch-all for far future
|
|
23
|
-
* })
|
|
24
|
-
* // → 'longExpired' | 'expired' | 'critical' | 'warning' | 'safe' | null
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare function expires<K extends string>(date: TimeInput, thresholds: Record<K, Temporal.DurationLike>, options?: TimeOptions, now?: Temporal.Instant): K | null;
|
|
28
|
-
/**
|
|
29
|
-
* Returns the absolute calendar-accurate difference between two dates as a
|
|
30
|
-
* structured `{ unit, value }` in the largest meaningful unit.
|
|
31
|
-
*
|
|
32
|
-
* When `b` is omitted, the current instant is used.
|
|
33
|
-
* Requires `options.tz` when inputs are `PlainDate`, `PlainDateTime`, or plain `Instant` with
|
|
34
|
-
* calendar-unit precision. Throws when timezone cannot be inferred from inputs.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```ts
|
|
38
|
-
* timeDiff(
|
|
39
|
-
* parseInstant('2026-01-01T00:00:00Z'),
|
|
40
|
-
* parseInstant('2027-03-15T00:00:00Z'),
|
|
41
|
-
* )
|
|
42
|
-
* // { unit: 'year', value: 1 }
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export declare function timeDiff(a: TimeInput, b?: TimeInput, options?: TimeOptions): TimeDiffResult;
|
|
1
|
+
import type { ClassifyExpiryInput, TimeDiffResult, TimeInput, TimeZoneOptions } from './types';
|
|
2
|
+
export declare function classifyExpiry<K extends string>(input: ClassifyExpiryInput<K>): K | null;
|
|
3
|
+
export declare function timeDiff(a: TimeInput, b?: TimeInput, options?: TimeZoneOptions): TimeDiffResult;
|
|
46
4
|
//# sourceMappingURL=classify.d.ts.map
|
package/dist/classify.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EAEd,SAAS,EACT,eAAe,EAChB,MAAM,SAAS,CAAC;AAiDjB,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CASxF;AAYD,wBAAgB,QAAQ,CACtB,CAAC,EAAE,SAAS,EACZ,CAAC,GAAE,SAAkC,EACrC,OAAO,GAAE,eAAoB,GAC5B,cAAc,CAqBhB"}
|
package/dist/classify.js
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TempoInvalidInputError as e } from "./errors.js";
|
|
2
|
+
import { inferSharedTimeZone as t } from "./_tz.js";
|
|
2
3
|
import { toInstant as n, toZoned as r } from "./_convert.js";
|
|
3
4
|
import { Temporal as i } from "@js-temporal/polyfill";
|
|
4
5
|
//#region src/classify.ts
|
|
5
|
-
var a =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
function s(e, t, r = {}, a = i.Now.instant()) {
|
|
16
|
-
let s = n(e, r).epochMilliseconds - a.epochMilliseconds;
|
|
17
|
-
for (let { key: e, ms: n } of o(t)) if (s <= n) return e;
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
function c(t) {
|
|
21
|
-
let n = i.Duration.from(t);
|
|
22
|
-
return (n.years ?? 0) * 12 * e + (n.months ?? 0) * e + (n.weeks ?? 0) * 7 * 864e5 + (n.days ?? 0) * 864e5 + (n.hours ?? 0) * 36e5 + (n.minutes ?? 0) * 6e4 + (n.seconds ?? 0) * 1e3 + (n.milliseconds ?? 0) + (n.microseconds ?? 0) / 1e3 + (n.nanoseconds ?? 0) / 1e6;
|
|
23
|
-
}
|
|
24
|
-
var l = [
|
|
6
|
+
var a = {
|
|
7
|
+
day: 864e5,
|
|
8
|
+
hour: 36e5,
|
|
9
|
+
microsecond: 1 / 1e3,
|
|
10
|
+
millisecond: 1,
|
|
11
|
+
minute: 6e4,
|
|
12
|
+
nanosecond: 1 / 1e6,
|
|
13
|
+
second: 1e3,
|
|
14
|
+
week: 6048e5
|
|
15
|
+
}, o = [
|
|
25
16
|
{
|
|
26
17
|
field: "years",
|
|
27
18
|
unit: "year"
|
|
@@ -55,11 +46,20 @@ var l = [
|
|
|
55
46
|
unit: "millisecond"
|
|
56
47
|
}
|
|
57
48
|
];
|
|
58
|
-
function
|
|
59
|
-
|
|
49
|
+
function s(t) {
|
|
50
|
+
let n = i.Duration.from(t);
|
|
51
|
+
if (n.years || n.months) throw new e("classifyExpiry thresholds cannot contain months or years. Use fixed elapsed-time units.");
|
|
52
|
+
return n.weeks * a.week + n.days * a.day + n.hours * a.hour + n.minutes * a.minute + n.seconds * a.second + n.milliseconds + n.microseconds * a.microsecond + n.nanoseconds * a.nanosecond;
|
|
53
|
+
}
|
|
54
|
+
function c(e) {
|
|
55
|
+
let t = e.relativeTo ?? i.Now.instant(), r = n(e.value, e).epochMilliseconds - t.epochMilliseconds;
|
|
56
|
+
return Object.entries(e.thresholds).map(([e, t]) => ({
|
|
57
|
+
key: e,
|
|
58
|
+
max: s(t)
|
|
59
|
+
})).sort((e, t) => e.max - t.max).find(({ max: e }) => r <= e)?.key ?? null;
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
for (let { field: t, unit: n } of
|
|
61
|
+
function l(e) {
|
|
62
|
+
for (let { field: t, unit: n } of o) {
|
|
63
63
|
let r = Math.abs(e[t]);
|
|
64
64
|
if (r > 0) return {
|
|
65
65
|
unit: n,
|
|
@@ -71,13 +71,15 @@ function d(e) {
|
|
|
71
71
|
value: 0
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
function u(e, n = i.Now.instant(), a = {}) {
|
|
75
|
+
if (!a.timeZone && e instanceof i.Instant && n instanceof i.Instant) {
|
|
76
|
+
let t = e.toZonedDateTimeISO("UTC"), r = n.toZonedDateTimeISO("UTC");
|
|
77
|
+
return l(i.ZonedDateTime.compare(t, r) <= 0 ? r.since(t, { largestUnit: "year" }) : t.since(r, { largestUnit: "year" }));
|
|
78
|
+
}
|
|
79
|
+
let o = t([e, n], a), s = r(e, { timeZone: o }), c = r(n, { timeZone: o });
|
|
80
|
+
return l(i.ZonedDateTime.compare(s, c) <= 0 ? c.since(s, { largestUnit: "year" }) : s.since(c, { largestUnit: "year" }));
|
|
79
81
|
}
|
|
80
82
|
//#endregion
|
|
81
|
-
export {
|
|
83
|
+
export { c as classifyExpiry, u as timeDiff };
|
|
82
84
|
|
|
83
85
|
//# sourceMappingURL=classify.js.map
|
package/dist/classify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classify.js","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"classify.js","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {\n ClassifyExpiryInput,\n FixedDuration,\n TimeDiffResult,\n TimeDiffUnit,\n TimeInput,\n TimeZoneOptions,\n} from './types';\n\nimport { toInstant, toZoned } from './_convert';\nimport { inferSharedTimeZone } from './_tz';\nimport { TempoInvalidInputError } from './errors';\n\nconst MILLIS = {\n day: 86_400_000,\n hour: 3_600_000,\n microsecond: 1 / 1_000,\n millisecond: 1,\n minute: 60_000,\n nanosecond: 1 / 1_000_000,\n second: 1_000,\n week: 604_800_000,\n} as const;\nconst UNIT_ORDER: ReadonlyArray<{ field: keyof Temporal.Duration; unit: TimeDiffUnit }> = [\n { field: 'years', unit: 'year' },\n { field: 'months', unit: 'month' },\n { field: 'weeks', unit: 'week' },\n { field: 'days', unit: 'day' },\n { field: 'hours', unit: 'hour' },\n { field: 'minutes', unit: 'minute' },\n { field: 'seconds', unit: 'second' },\n { field: 'milliseconds', unit: 'millisecond' },\n];\n\n// Expiry classification is elapsed-time math. Calendar months and years are rejected rather than approximated.\nfunction fixedDurationToMilliseconds(input: FixedDuration): number {\n const duration = Temporal.Duration.from(input);\n\n if (duration.years || duration.months) {\n throw new TempoInvalidInputError(\n 'classifyExpiry thresholds cannot contain months or years. Use fixed elapsed-time units.',\n );\n }\n\n return (\n duration.weeks * MILLIS.week +\n duration.days * MILLIS.day +\n duration.hours * MILLIS.hour +\n duration.minutes * MILLIS.minute +\n duration.seconds * MILLIS.second +\n duration.milliseconds +\n duration.microseconds * MILLIS.microsecond +\n duration.nanoseconds * MILLIS.nanosecond\n );\n}\n\nexport function classifyExpiry<K extends string>(input: ClassifyExpiryInput<K>): K | null {\n const reference = input.relativeTo ?? Temporal.Now.instant();\n const value = toInstant(input.value, input);\n const elapsed = value.epochMilliseconds - reference.epochMilliseconds;\n const thresholds = (Object.entries(input.thresholds) as Array<[K, FixedDuration]>)\n .map(([key, duration]) => ({ key, max: fixedDurationToMilliseconds(duration) }))\n .sort((left, right) => left.max - right.max);\n\n return thresholds.find(({ max }) => elapsed <= max)?.key ?? null;\n}\n\nfunction pickLargestUnit(duration: Temporal.Duration): TimeDiffResult {\n for (const { field, unit } of UNIT_ORDER) {\n const value = Math.abs(duration[field] as number);\n\n if (value > 0) return { unit, value };\n }\n\n return { unit: 'millisecond', value: 0 };\n}\n\nexport function timeDiff(\n a: TimeInput,\n b: TimeInput = Temporal.Now.instant(),\n options: TimeZoneOptions = {},\n): TimeDiffResult {\n if (!options.timeZone && a instanceof Temporal.Instant && b instanceof Temporal.Instant) {\n const left = a.toZonedDateTimeISO('UTC');\n const right = b.toZonedDateTimeISO('UTC');\n\n return pickLargestUnit(\n Temporal.ZonedDateTime.compare(left, right) <= 0\n ? right.since(left, { largestUnit: 'year' })\n : left.since(right, { largestUnit: 'year' }),\n );\n }\n\n const timeZone = inferSharedTimeZone([a, b], options);\n const left = toZoned(a, { timeZone });\n const right = toZoned(b, { timeZone });\n\n return pickLargestUnit(\n Temporal.ZonedDateTime.compare(left, right) <= 0\n ? right.since(left, { largestUnit: 'year' })\n : left.since(right, { largestUnit: 'year' }),\n );\n}\n"],"mappings":";;;;;AAeA,IAAM,IAAS;CACb,KAAK;CACL,MAAM;CACN,aAAa,IAAI;CACjB,aAAa;CACb,QAAQ;CACR,YAAY,IAAI;CAChB,QAAQ;CACR,MAAM;AACR,GACM,IAAoF;CACxF;EAAE,OAAO;EAAS,MAAM;CAAO;CAC/B;EAAE,OAAO;EAAU,MAAM;CAAQ;CACjC;EAAE,OAAO;EAAS,MAAM;CAAO;CAC/B;EAAE,OAAO;EAAQ,MAAM;CAAM;CAC7B;EAAE,OAAO;EAAS,MAAM;CAAO;CAC/B;EAAE,OAAO;EAAW,MAAM;CAAS;CACnC;EAAE,OAAO;EAAW,MAAM;CAAS;CACnC;EAAE,OAAO;EAAgB,MAAM;CAAc;AAC/C;AAGA,SAAS,EAA4B,GAA8B;CACjE,IAAM,IAAW,EAAS,SAAS,KAAK,CAAK;CAE7C,IAAI,EAAS,SAAS,EAAS,QAC7B,MAAM,IAAI,EACR,yFACF;CAGF,OACE,EAAS,QAAQ,EAAO,OACxB,EAAS,OAAO,EAAO,MACvB,EAAS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AAG,EAAE,YAAS,CAAC;CAErC,OAAO,EACL,EAAS,cAAc,QAAQ,GAAM,CAAK,KAAK,IAC3C,EAAM,MAAM,GAAM,EAAE,aAAa,OAAO,CAAC,IACzC,EAAK,MAAM,GAAO,EAAE,aAAa,OAAO,CAAC,CAC/C;AACF"}
|
package/dist/compare.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./_tz.cjs"),t=require("./_convert.cjs"),n=require("./_floor.cjs");let r=require("@js-temporal/polyfill");function i(
|
|
1
|
+
const e=require("./_tz.cjs"),t=require("./_convert.cjs"),n=require("./_floor.cjs");let r=require("@js-temporal/polyfill");function i(i,a,o){if(!o.unit)return r.Temporal.Instant.compare(t.toInstant(i,o),t.toInstant(a,o));let s={timeZone:e.inferSharedTimeZone([i,a],o),weekStartsOn:o.weekStartsOn};return r.Temporal.Instant.compare(n.floorToUnit(i,o.unit,s),n.floorToUnit(a,o.unit,s))}function a({end:r,start:i,value:a,...o}){if(!o.unit){let n=t.toInstant(a,o),[s,c]=e.normalizeRange(t.toInstant(i,o),t.toInstant(r,o));return{lower:s,target:n,upper:c}}let s={timeZone:e.inferSharedTimeZone([a,i,r],o),weekStartsOn:o.weekStartsOn},c=n.floorToUnit(a,o.unit,s),[l,u]=e.normalizeRange(n.floorToUnit(i,o.unit,s),n.floorToUnit(r,o.unit,s));return{lower:l,target:c,upper:u}}function o(e,t,n={}){return i(e,t,n)<0}function s(e,t,n={}){return i(e,t,n)>0}function c(e,t,n={}){return i(e,t,n)===0}function l(e){let{lower:t,target:n,upper:i}=a(e);return r.Temporal.Instant.compare(t,n)<=0&&r.Temporal.Instant.compare(n,i)<=0}function u(e){let{lower:t,target:n,upper:i}=a(e),o=r.Temporal.Instant.compare(n,t)<0?t:r.Temporal.Instant.compare(n,i)>0?i:n;return e.value instanceof r.Temporal.ZonedDateTime?o.toZonedDateTimeISO(e.timeZone??e.value.timeZoneId):o}exports.clamp=u,exports.contains=l,exports.isAfter=s,exports.isBefore=o,exports.isSame=c;
|
|
2
2
|
//# sourceMappingURL=compare.cjs.map
|
package/dist/compare.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.cjs","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"compare.cjs","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type { ClampInput, ContainsInput, CompareOptions, TimeInput } from './types';\n\nimport { toInstant } from './_convert';\nimport { floorToUnit } from './_floor';\nimport { inferSharedTimeZone, normalizeRange } from './_tz';\n\nfunction compareByUnit(a: TimeInput, b: TimeInput, options: CompareOptions): number {\n if (!options.unit) return Temporal.Instant.compare(toInstant(a, options), toInstant(b, options));\n\n const timeZone = inferSharedTimeZone([a, b], options);\n const unitOptions = { timeZone, weekStartsOn: options.weekStartsOn };\n\n return Temporal.Instant.compare(floorToUnit(a, options.unit, unitOptions), floorToUnit(b, options.unit, unitOptions));\n}\n\nfunction resolveRange({ end, start, value, ...options }: ContainsInput | ClampInput): {\n lower: Temporal.Instant;\n target: Temporal.Instant;\n upper: Temporal.Instant;\n} {\n if (!options.unit) {\n const target = toInstant(value, options);\n const [lower, upper] = normalizeRange(toInstant(start, options), toInstant(end, options));\n\n return { lower, target, upper };\n }\n\n const timeZone = inferSharedTimeZone([value, start, end], options);\n const unitOptions = { timeZone, weekStartsOn: options.weekStartsOn };\n const target = floorToUnit(value, options.unit, unitOptions);\n const [lower, upper] = normalizeRange(\n floorToUnit(start, options.unit, unitOptions),\n floorToUnit(end, options.unit, unitOptions),\n );\n\n return { lower, target, upper };\n}\n\nexport function isBefore(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) < 0;\n}\n\nexport function isAfter(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) > 0;\n}\n\nexport function isSame(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) === 0;\n}\n\nexport function contains(input: ContainsInput): boolean {\n const { lower, target, upper } = resolveRange(input);\n\n return Temporal.Instant.compare(lower, target) <= 0 && Temporal.Instant.compare(target, upper) <= 0;\n}\n\nexport function clamp(input: ClampInput & { value: Temporal.ZonedDateTime }): Temporal.ZonedDateTime;\nexport function clamp(input: ClampInput): Temporal.Instant;\nexport function clamp(input: ClampInput): Temporal.Instant | Temporal.ZonedDateTime {\n const { lower, target, upper } = resolveRange(input);\n const clamped =\n Temporal.Instant.compare(target, lower) < 0 ? lower : Temporal.Instant.compare(target, upper) > 0 ? upper : target;\n\n return input.value instanceof Temporal.ZonedDateTime\n ? clamped.toZonedDateTimeISO(input.timeZone ?? input.value.timeZoneId)\n : clamped;\n}\n"],"mappings":"0HAQA,SAAS,EAAc,EAAc,EAAc,EAAiC,CAClF,GAAI,CAAC,EAAQ,KAAM,OAAO,EAAA,SAAS,QAAQ,QAAQ,EAAA,UAAU,EAAG,CAAO,EAAG,EAAA,UAAU,EAAG,CAAO,CAAC,EAG/F,IAAM,EAAc,CAAE,SADL,EAAA,oBAAoB,CAAC,EAAG,CAAC,EAAG,CACvB,EAAU,aAAc,EAAQ,YAAa,EAEnE,OAAO,EAAA,SAAS,QAAQ,QAAQ,EAAA,YAAY,EAAG,EAAQ,KAAM,CAAW,EAAG,EAAA,YAAY,EAAG,EAAQ,KAAM,CAAW,CAAC,CACtH,CAEA,SAAS,EAAa,CAAE,MAAK,QAAO,QAAO,GAAG,GAI5C,CACA,GAAI,CAAC,EAAQ,KAAM,CACjB,IAAM,EAAS,EAAA,UAAU,EAAO,CAAO,EACjC,CAAC,EAAO,GAAS,EAAA,eAAe,EAAA,UAAU,EAAO,CAAO,EAAG,EAAA,UAAU,EAAK,CAAO,CAAC,EAExF,MAAO,CAAE,QAAO,SAAQ,OAAM,CAChC,CAGA,IAAM,EAAc,CAAE,SADL,EAAA,oBA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
4
|
-
* Returns `true` when `a` is strictly before `b` on the timeline.
|
|
5
|
-
* Pass `options.unit` to compare by calendar boundary (e.g. same day).
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* isBefore(parseInstant('2026-03-21T10:00:00Z'), parseInstant('2026-03-21T11:00:00Z'))
|
|
10
|
-
* // true
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
2
|
+
import type { ClampInput, ContainsInput, CompareOptions, TimeInput } from './types';
|
|
13
3
|
export declare function isBefore(a: TimeInput, b: TimeInput, options?: CompareOptions): boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Returns `true` when `a` is strictly after `b` on the timeline.
|
|
16
|
-
* Pass `options.unit` to compare by calendar boundary (e.g. same day).
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* isAfter(parseInstant('2026-03-21T11:00:00Z'), parseInstant('2026-03-21T10:00:00Z'))
|
|
21
|
-
* // true
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
4
|
export declare function isAfter(a: TimeInput, b: TimeInput, options?: CompareOptions): boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Returns `true` when `a` and `b` represent the same point (or boundary unit) in time.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* isSame(a, b, { tz: 'America/New_York', unit: 'day' })
|
|
31
|
-
* // true when a and b fall on the same calendar day in New York
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
5
|
export declare function isSame(a: TimeInput, b: TimeInput, options?: CompareOptions): boolean;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
* ```ts
|
|
41
|
-
* within(
|
|
42
|
-
* parseInstant('2026-03-21T11:00:00Z'),
|
|
43
|
-
* parseInstant('2026-03-21T10:00:00Z'),
|
|
44
|
-
* parseInstant('2026-03-21T12:00:00Z'),
|
|
45
|
-
* ) // true
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export declare function within(value: TimeInput, start: TimeInput, end: TimeInput, options?: CompareOptions): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Clamps `value` to within `[start, end]` (bounds normalized).
|
|
51
|
-
*
|
|
52
|
-
* When `value` is a `ZonedDateTime`, returns a `ZonedDateTime` in the same timezone.
|
|
53
|
-
* Otherwise returns an `Instant`.
|
|
54
|
-
*
|
|
55
|
-
* When `options.unit` is set, all three inputs are floored to that calendar boundary before
|
|
56
|
-
* clamping — the result is at the start of the boundary unit, not the original time-of-day.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* clamp(
|
|
61
|
-
* parseInstant('2026-03-21T13:00:00Z'),
|
|
62
|
-
* parseInstant('2026-03-21T10:00:00Z'),
|
|
63
|
-
* parseInstant('2026-03-21T12:00:00Z'),
|
|
64
|
-
* ).toString() // '2026-03-21T12:00:00Z'
|
|
65
|
-
*
|
|
66
|
-
* clamp(
|
|
67
|
-
* parseZoned('2026-03-21T13:00:00+00:00[UTC]'),
|
|
68
|
-
* parseZoned('2026-03-21T10:00:00+00:00[UTC]'),
|
|
69
|
-
* parseZoned('2026-03-21T12:00:00+00:00[UTC]'),
|
|
70
|
-
* ).toString() // '2026-03-21T12:00:00+00:00[UTC]'
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export declare function clamp(value: Temporal.ZonedDateTime, start: TimeInput, end: TimeInput, options?: CompareOptions): Temporal.ZonedDateTime;
|
|
74
|
-
export declare function clamp(value: TimeInput, start: TimeInput, end: TimeInput, options?: CompareOptions): Temporal.Instant;
|
|
6
|
+
export declare function contains(input: ContainsInput): boolean;
|
|
7
|
+
export declare function clamp(input: ClampInput & {
|
|
8
|
+
value: Temporal.ZonedDateTime;
|
|
9
|
+
}): Temporal.ZonedDateTime;
|
|
10
|
+
export declare function clamp(input: ClampInput): Temporal.Instant;
|
|
75
11
|
//# sourceMappingURL=compare.d.ts.map
|
package/dist/compare.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAsCpF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAE1F;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAEzF;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAExF;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAItD;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG;IAAE,KAAK,EAAE,QAAQ,CAAC,aAAa,CAAA;CAAE,GAAG,QAAQ,CAAC,aAAa,CAAC;AACrG,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ,CAAC,OAAO,CAAC"}
|
package/dist/compare.js
CHANGED
|
@@ -3,69 +3,55 @@ import { toInstant as n } from "./_convert.js";
|
|
|
3
3
|
import { floorToUnit as r } from "./_floor.js";
|
|
4
4
|
import { Temporal as i } from "@js-temporal/polyfill";
|
|
5
5
|
//#region src/compare.ts
|
|
6
|
-
function a(t,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
left: r(t, i, o),
|
|
13
|
-
right: r(n, i, o)
|
|
6
|
+
function a(t, a, o) {
|
|
7
|
+
if (!o.unit) return i.Instant.compare(n(t, o), n(a, o));
|
|
8
|
+
let s = {
|
|
9
|
+
timeZone: e([t, a], o),
|
|
10
|
+
weekStartsOn: o.weekStartsOn
|
|
14
11
|
};
|
|
12
|
+
return i.Instant.compare(r(t, o.unit, s), r(a, o.unit, s));
|
|
15
13
|
}
|
|
16
|
-
function o(
|
|
14
|
+
function o({ end: i, start: a, value: o, ...s }) {
|
|
15
|
+
if (!s.unit) {
|
|
16
|
+
let e = n(o, s), [r, c] = t(n(a, s), n(i, s));
|
|
17
|
+
return {
|
|
18
|
+
lower: r,
|
|
19
|
+
target: e,
|
|
20
|
+
upper: c
|
|
21
|
+
};
|
|
22
|
+
}
|
|
17
23
|
let c = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
timeZone: e([
|
|
25
|
+
o,
|
|
26
|
+
a,
|
|
27
|
+
i
|
|
22
28
|
], s),
|
|
23
29
|
weekStartsOn: s.weekStartsOn
|
|
24
|
-
}, l = r(
|
|
30
|
+
}, l = r(o, s.unit, c), [u, d] = t(r(a, s.unit, c), r(i, s.unit, c));
|
|
25
31
|
return {
|
|
26
32
|
lower: u,
|
|
27
33
|
target: l,
|
|
28
34
|
upper: d
|
|
29
35
|
};
|
|
30
36
|
}
|
|
31
|
-
function s(e, t,
|
|
32
|
-
|
|
33
|
-
let { left: o, right: s } = a(e, t, r.unit, r);
|
|
34
|
-
return i.Instant.compare(o, s);
|
|
37
|
+
function s(e, t, n = {}) {
|
|
38
|
+
return a(e, t, n) < 0;
|
|
35
39
|
}
|
|
36
40
|
function c(e, t, n = {}) {
|
|
37
|
-
return
|
|
41
|
+
return a(e, t, n) > 0;
|
|
38
42
|
}
|
|
39
43
|
function l(e, t, n = {}) {
|
|
40
|
-
return
|
|
44
|
+
return a(e, t, n) === 0;
|
|
41
45
|
}
|
|
42
|
-
function u(e
|
|
43
|
-
|
|
46
|
+
function u(e) {
|
|
47
|
+
let { lower: t, target: n, upper: r } = o(e);
|
|
48
|
+
return i.Instant.compare(t, n) <= 0 && i.Instant.compare(n, r) <= 0;
|
|
44
49
|
}
|
|
45
|
-
function d(e
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return i.Instant.compare(c, o) <= 0 && i.Instant.compare(o, l) <= 0;
|
|
49
|
-
}
|
|
50
|
-
let { lower: c, target: l, upper: u } = o(e, r, a, s.unit, s);
|
|
51
|
-
return i.Instant.compare(c, l) <= 0 && i.Instant.compare(l, u) <= 0;
|
|
52
|
-
}
|
|
53
|
-
function f(r, a, s, c = {}) {
|
|
54
|
-
let l = r instanceof i.ZonedDateTime, u = l ? r.timeZoneId : void 0;
|
|
55
|
-
if (!c.unit) {
|
|
56
|
-
let e = n(r, c), [o, d] = t(n(a, c), n(s, c)), f;
|
|
57
|
-
return f = i.Instant.compare(e, o) < 0 ? o : i.Instant.compare(e, d) > 0 ? d : e, l && u ? f.toZonedDateTimeISO(u) : f;
|
|
58
|
-
}
|
|
59
|
-
let { lower: d, target: f, upper: p } = o(r, a, s, c.unit, c), m;
|
|
60
|
-
m = i.Instant.compare(f, d) < 0 ? d : i.Instant.compare(f, p) > 0 ? p : f;
|
|
61
|
-
let h = c.tz ?? u ?? e([
|
|
62
|
-
r,
|
|
63
|
-
a,
|
|
64
|
-
s
|
|
65
|
-
], c);
|
|
66
|
-
return l ? m.toZonedDateTimeISO(h) : m;
|
|
50
|
+
function d(e) {
|
|
51
|
+
let { lower: t, target: n, upper: r } = o(e), a = i.Instant.compare(n, t) < 0 ? t : i.Instant.compare(n, r) > 0 ? r : n;
|
|
52
|
+
return e.value instanceof i.ZonedDateTime ? a.toZonedDateTimeISO(e.timeZone ?? e.value.timeZoneId) : a;
|
|
67
53
|
}
|
|
68
54
|
//#endregion
|
|
69
|
-
export {
|
|
55
|
+
export { d as clamp, u as contains, c as isAfter, s as isBefore, l as isSame };
|
|
70
56
|
|
|
71
57
|
//# sourceMappingURL=compare.js.map
|
package/dist/compare.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.js","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"compare.js","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type { ClampInput, ContainsInput, CompareOptions, TimeInput } from './types';\n\nimport { toInstant } from './_convert';\nimport { floorToUnit } from './_floor';\nimport { inferSharedTimeZone, normalizeRange } from './_tz';\n\nfunction compareByUnit(a: TimeInput, b: TimeInput, options: CompareOptions): number {\n if (!options.unit) return Temporal.Instant.compare(toInstant(a, options), toInstant(b, options));\n\n const timeZone = inferSharedTimeZone([a, b], options);\n const unitOptions = { timeZone, weekStartsOn: options.weekStartsOn };\n\n return Temporal.Instant.compare(floorToUnit(a, options.unit, unitOptions), floorToUnit(b, options.unit, unitOptions));\n}\n\nfunction resolveRange({ end, start, value, ...options }: ContainsInput | ClampInput): {\n lower: Temporal.Instant;\n target: Temporal.Instant;\n upper: Temporal.Instant;\n} {\n if (!options.unit) {\n const target = toInstant(value, options);\n const [lower, upper] = normalizeRange(toInstant(start, options), toInstant(end, options));\n\n return { lower, target, upper };\n }\n\n const timeZone = inferSharedTimeZone([value, start, end], options);\n const unitOptions = { timeZone, weekStartsOn: options.weekStartsOn };\n const target = floorToUnit(value, options.unit, unitOptions);\n const [lower, upper] = normalizeRange(\n floorToUnit(start, options.unit, unitOptions),\n floorToUnit(end, options.unit, unitOptions),\n );\n\n return { lower, target, upper };\n}\n\nexport function isBefore(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) < 0;\n}\n\nexport function isAfter(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) > 0;\n}\n\nexport function isSame(a: TimeInput, b: TimeInput, options: CompareOptions = {}): boolean {\n return compareByUnit(a, b, options) === 0;\n}\n\nexport function contains(input: ContainsInput): boolean {\n const { lower, target, upper } = resolveRange(input);\n\n return Temporal.Instant.compare(lower, target) <= 0 && Temporal.Instant.compare(target, upper) <= 0;\n}\n\nexport function clamp(input: ClampInput & { value: Temporal.ZonedDateTime }): Temporal.ZonedDateTime;\nexport function clamp(input: ClampInput): Temporal.Instant;\nexport function clamp(input: ClampInput): Temporal.Instant | Temporal.ZonedDateTime {\n const { lower, target, upper } = resolveRange(input);\n const clamped =\n Temporal.Instant.compare(target, lower) < 0 ? lower : Temporal.Instant.compare(target, upper) > 0 ? upper : target;\n\n return input.value instanceof Temporal.ZonedDateTime\n ? clamped.toZonedDateTimeISO(input.timeZone ?? input.value.timeZoneId)\n : clamped;\n}\n"],"mappings":";;;;;AAQA,SAAS,EAAc,GAAc,GAAc,GAAiC;CAClF,IAAI,CAAC,EAAQ,MAAM,OAAO,EAAS,QAAQ,QAAQ,EAAU,GAAG,CAAO,GAAG,EAAU,GAAG,CAAO,CAAC;CAG/F,IAAM,IAAc;EAAE,UADL,EAAoB,CAAC,GAAG,CAAC,GAAG,CACvB;EAAU,cAAc,EAAQ;CAAa;CAEnE,OAAO,EAAS,QAAQ,QAAQ,EAAY,GAAG,EAAQ,MAAM,CAAW,GAAG,EAAY,GAAG,EAAQ,MAAM,CAAW,CAAC;AACtH;AAEA,SAAS,EAAa,EAAE,QAAK,UAAO,UAAO,GAAG,KAI5C;CACA,IAAI,CAAC,EAAQ,MAAM;EACjB,IAAM,IAAS,EAAU,GAAO,CAAO,GACjC,CAAC,GAAO,KAAS,EAAe,EAAU,GAAO,CAAO,GAAG,EAAU,GAAK,CAAO,CAAC;EAExF,OAAO;GAAE;GAAO;GAAQ;EAAM;CAChC;CAGA,IAAM,IAAc;EAAE,UADL,EAAoB;GAAC;GAAO;GAAO;EAAG,GAAG,CACpC;EAAU,cAAc,EAAQ;CAAa,GAC7D,IAAS,EAAY,GAAO,EAAQ,MAAM,CAAW,GACrD,CAAC,GAAO,KAAS,EACrB,EAAY,GAAO,EAAQ,MAAM,CAAW,GAC5C,EAAY,GAAK,EAAQ,MAAM,CAAW,CAC5C;CAEA,OAAO;EAAE;EAAO;EAAQ;CAAM;AAChC;AAEA,SAAgB,EAAS,GAAc,GAAc,IAA0B,CAAC,GAAY;CAC1F,OAAO,EAAc,GAAG,GAAG,CAAO,IAAI;AACxC;AAEA,SAAgB,EAAQ,GAAc,GAAc,IAA0B,CAAC,GAAY;CACzF,OAAO,EAAc,GAAG,GAAG,CAAO,IAAI;AACxC;AAEA,SAAgB,EAAO,GAAc,GAAc,IAA0B,CAAC,GAAY;CACxF,OAAO,EAAc,GAAG,GAAG,CAAO,MAAM;AAC1C;AAEA,SAAgB,EAAS,GAA+B;CACtD,IAAM,EAAE,UAAO,WAAQ,aAAU,EAAa,CAAK;CAEnD,OAAO,EAAS,QAAQ,QAAQ,GAAO,CAAM,KAAK,KAAK,EAAS,QAAQ,QAAQ,GAAQ,CAAK,KAAK;AACpG;AAIA,SAAgB,EAAM,GAA8D;CAClF,IAAM,EAAE,UAAO,WAAQ,aAAU,EAAa,CAAK,GAC7C,IACJ,EAAS,QAAQ,QAAQ,GAAQ,CAAK,IAAI,IAAI,IAAQ,EAAS,QAAQ,QAAQ,GAAQ,CAAK,IAAI,IAAI,IAAQ;CAE9G,OAAO,EAAM,iBAAiB,EAAS,gBACnC,EAAQ,mBAAmB,EAAM,YAAY,EAAM,MAAM,UAAU,IACnE;AACN"}
|
package/dist/core.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./errors.cjs"),t=require("./_tz.cjs"),n=require("./_convert.cjs");let r=require("@js-temporal/polyfill");function i(
|
|
1
|
+
const e=require("./errors.cjs"),t=require("./_tz.cjs"),n=require("./_convert.cjs");let r=require("@js-temporal/polyfill");function i(t,n){try{return n.as===`zonedDateTime`?r.Temporal.ZonedDateTime.from(t):n.as===`instant`?r.Temporal.Instant.from(t):n.as===`plainDateTime`?r.Temporal.PlainDateTime.from(t):r.Temporal.PlainDate.from(t)}catch{e.fail(`Invalid ${n.as} ISO 8601 string: "${t}".`)}}function a(e){return r.Temporal.Now.zonedDateTimeISO(t.validateTimeZone(e.timeZone))}function o(){return r.Temporal.Now.instant()}function s(e,r,i={}){let a=t.inferTimeZone(e,i);return n.toZoned(e,{disambiguation:i.disambiguation,timeZone:a}).add(r)}function c(e){let{end:i,largestUnit:a,roundingIncrement:o,roundingMode:s,smallestUnit:c,start:l}=e,u={largestUnit:a,roundingIncrement:o,roundingMode:s,smallestUnit:c};if(!(a!==void 0&&t.CALENDAR_UNITS.has(a)||c!==void 0&&t.CALENDAR_UNITS.has(c))&&l instanceof r.Temporal.Instant&&i instanceof r.Temporal.Instant)return i.since(l,u);let d=t.inferSharedTimeZone([l,i],e),f={disambiguation:e.disambiguation,timeZone:d};return n.toZoned(i,f).since(n.toZoned(l,f),u)}function l(e){return e instanceof r.Temporal.Instant||e instanceof r.Temporal.ZonedDateTime||e instanceof r.Temporal.PlainDateTime||e instanceof r.Temporal.PlainDate}exports.difference=c,exports.isValid=l,exports.now=a,exports.nowInstant=o,exports.parse=i,exports.shift=s;
|
|
2
2
|
//# sourceMappingURL=core.cjs.map
|
package/dist/core.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.cjs","names":[],"sources":["../src/core.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type { CalendarUnit, DifferenceOptions, ParseAs, ShiftOptions, TimeInput } from './types';\n\nimport { toInstant, toZoned } from './_convert';\nimport { CALENDAR_UNITS, inferSharedTimeZone, inferTimeZone } from './_tz';\nimport { fail } from './errors';\n\ntype TimeOptionsWithTz = { tz: string };\n\n/**\n * Returns the current date and time in the given timezone.\n *\n * @example\n * ```ts\n * now('America/New_York').hour; // current hour in New York\n * ```\n */\nexport function now(tz: string): Temporal.ZonedDateTime {\n return Temporal.Now.zonedDateTimeISO(tz);\n}\n\n/**\n * Returns the current absolute instant (UTC point in time).\n * Use this instead of `Temporal.Now.instant()` to avoid importing Temporal directly.\n *\n * @example\n * ```ts\n * timeDiff(nowInstant()) // { unit: 'millisecond', value: 0 } (compared to now)\n * expires(nowInstant(), { expired: { days: 0 }, safe: { years: 100 } }) // 'safe'\n * ```\n */\nexport function nowInstant(): Temporal.Instant {\n return Temporal.Now.instant();\n}\n\n/**\n * Parses a full ISO 8601 zoned date-time string into a `ZonedDateTime`.\n * Use this instead of `Temporal.ZonedDateTime.from()` to avoid importing Temporal directly.\n *\n * @example\n * ```ts\n * parseZoned('2026-03-21T11:00:00+01:00[Europe/Berlin]')\n * parseZoned('2026-03-21T00:00:00[UTC]')\n * ```\n */\nexport function parseZoned(input: string): Temporal.ZonedDateTime {\n try {\n return Temporal.ZonedDateTime.from(input);\n } catch {\n fail(\n `Invalid zoned date-time string: \"${input}\". Expected an ISO 8601 string with offset and timezone (e.g. 2026-03-21T10:00:00+01:00[Europe/Berlin]).`,\n );\n }\n}\n\n/**\n * Parses an ISO 8601 date-only string into a timezone-free `PlainDate`.\n * Use this instead of `Temporal.PlainDate.from()` to avoid importing Temporal directly.\n *\n * @example\n * ```ts\n * parsePlainDate('2026-03-21') // 2026-03-21\n * ```\n */\nexport function parsePlainDate(input: string): Temporal.PlainDate {\n try {\n return Temporal.PlainDate.from(input);\n } catch {\n fail(`Invalid plain date string: \"${input}\". Expected an ISO 8601 date string (e.g. YYYY-MM-DD).`);\n }\n}\n\n/**\n * Parses an ISO 8601 string into a timezone-free `PlainDateTime` (wall-clock time).\n * Use {@link toInstant} or {@link inTz} to attach a timezone when needed.\n *\n * @example\n * ```ts\n * parsePlainDateTime('2026-03-21') // 2026-03-21T00:00:00\n * parsePlainDateTime('2026-03-21T10:15:30') // 2026-03-21T10:15:30\n * ```\n */\nexport function parsePlainDateTime(input: string): Temporal.PlainDateTime {\n try {\n return Temporal.PlainDateTime.from(input);\n } catch {\n fail(\n `Invalid date/time string: \"${input}\". Expected an ISO 8601 date or date-time string (e.g. YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss).`,\n );\n }\n}\n\n/**\n * Parses an ISO 8601 UTC string into an absolute `Instant`.\n *\n * @example\n * ```ts\n * parseInstant('2026-03-21T10:15:30Z')\n * ```\n */\nexport function parseInstant(input: string): Temporal.Instant {\n try {\n return Temporal.Instant.from(input);\n } catch {\n fail(`Invalid instant string: \"${input}\". Expected an ISO 8601 UTC string (e.g. YYYY-MM-DDTHH:mm:ssZ).`);\n }\n}\n\n/**\n * DST-safe date arithmetic. Adds `duration` to `input` and returns the result as a\n * `ZonedDateTime`. Handles spring-forward and fall-back correctly.\n *\n * **Always returns a `ZonedDateTime`** — even when the input is an `Instant`.\n * Call `.toInstant()` on the result if you need an `Instant` back.\n * Requires `options.tz` when input is an `Instant`, `PlainDate`, or `PlainDateTime`.\n *\n * @example\n * ```ts\n * shift(parseZoned('2026-03-08T01:30:00-05:00[America/New_York]'), { hours: 1 })\n * // 2026-03-08T03:30:00-04:00[America/New_York] (skipped the missing hour)\n *\n * // Instant input — tz required, result is ZonedDateTime\n * shift(parseInstant('2026-03-21T10:00:00Z'), { hours: 2 }, { tz: 'UTC' }).toInstant()\n * ```\n */\nexport function shift(\n input: Temporal.ZonedDateTime,\n duration: Temporal.DurationLike,\n options?: ShiftOptions,\n): Temporal.ZonedDateTime;\nexport function shift(\n input: Temporal.Instant | Temporal.PlainDate | Temporal.PlainDateTime,\n duration: Temporal.DurationLike,\n options: ShiftOptions & TimeOptionsWithTz,\n): Temporal.ZonedDateTime;\nexport function shift(\n input: TimeInput,\n duration: Temporal.DurationLike,\n options: ShiftOptions = {},\n): Temporal.ZonedDateTime {\n const tz = inferTimeZone(input, options);\n\n return toZoned(input, { prefer: options.prefer, tz }).add(duration);\n}\n\n/**\n * Returns the calendar-aware duration between `start` and `end`.\n *\n * When both inputs are `Instant` and no calendar unit is requested, the fast\n * path skips timezone conversion. Calendar units (`day`, `week`, `month`, `year`)\n * always require a timezone — pass `options.tz` or use `ZonedDateTime` inputs.\n * `options.prefer` (DST disambiguation) is only meaningful for `PlainDateTime` inputs.\n *\n * @example\n * ```ts\n * difference(\n * parseZoned('2026-03-08T00:00:00-05:00[America/New_York]'),\n * parseZoned('2026-03-09T00:00:00-04:00[America/New_York]'),\n * { largestUnit: 'hour' },\n * ).hours // 23 (DST spring-forward day)\n * ```\n */\nexport function difference(start: TimeInput, end: TimeInput, options: DifferenceOptions = {}): Temporal.Duration {\n const { largestUnit, prefer, roundingIncrement, roundingMode, smallestUnit } = options;\n const roundingOptions = { largestUnit, roundingIncrement, roundingMode, smallestUnit };\n\n const needsCalendar =\n (largestUnit !== undefined && CALENDAR_UNITS.has(largestUnit as CalendarUnit)) ||\n (smallestUnit !== undefined && CALENDAR_UNITS.has(smallestUnit as CalendarUnit));\n\n if (!needsCalendar && start instanceof Temporal.Instant && end instanceof Temporal.Instant) {\n return end.since(start, roundingOptions as Temporal.DifferenceOptions<Temporal.TimeUnit>);\n }\n\n const tz = inferSharedTimeZone([start, end], options);\n\n return toZoned(end, { prefer, tz }).since(toZoned(start, { prefer, tz }), roundingOptions);\n}\n\n/**\n * Type guard that checks whether `value` is a valid `TimeInput`.\n *\n * @example\n * ```ts\n * isValid(parseInstant('2026-03-21T10:00:00Z')) // true\n * isValid('2026-03-21') // false\n * ```\n */\nexport function isValid(value: unknown): value is TimeInput {\n return (\n value instanceof Temporal.Instant ||\n value instanceof Temporal.ZonedDateTime ||\n value instanceof Temporal.PlainDateTime ||\n value instanceof Temporal.PlainDate\n );\n}\n\n/**\n * Parses any ISO 8601 string into the most specific `TimeInput` type possible.\n * Tries ZonedDateTime → Instant → PlainDateTime → PlainDate in order.\n * Throws a descriptive `TypeError` if none match.\n *\n * Pass `as` to request a specific return type (throws if the string cannot be parsed as that type):\n *\n * @example\n * ```ts\n * parse('2026-03-21T11:00:00+01:00[Europe/Berlin]') // TimeInput (auto-detect)\n * parse('2026-03-21T11:00:00+01:00[Europe/Berlin]', 'zoned') // Temporal.ZonedDateTime\n * parse('2026-03-21T10:00:00Z', 'instant') // Temporal.Instant\n * parse('2026-03-21T10:00:00', 'plain-datetime') // Temporal.PlainDateTime\n * parse('2026-03-21', 'plain-date') // Temporal.PlainDate\n * ```\n */\nexport function parse(input: string, as: 'zoned'): Temporal.ZonedDateTime;\nexport function parse(input: string, as: 'instant'): Temporal.Instant;\nexport function parse(input: string, as: 'plain-datetime'): Temporal.PlainDateTime;\nexport function parse(input: string, as: 'plain-date'): Temporal.PlainDate;\nexport function parse(input: string, as?: ParseAs): TimeInput;\nexport function parse(input: string, as?: ParseAs): TimeInput {\n if (as === 'zoned') return parseZoned(input);\n\n if (as === 'instant') return parseInstant(input);\n\n if (as === 'plain-datetime') return parsePlainDateTime(input);\n\n if (as === 'plain-date') return parsePlainDate(input);\n\n try {\n return Temporal.ZonedDateTime.from(input);\n } catch {\n /* try next format */\n }\n\n try {\n return Temporal.Instant.from(input);\n } catch {\n /* try next format */\n }\n\n // Try PlainDateTime before PlainDate — a date-only string (no 'T') will also\n // be accepted by PlainDateTime.from(), producing midnight, so we check the\n // string to pick the most specific type.\n if (input.includes('T')) {\n try {\n return Temporal.PlainDateTime.from(input);\n } catch {\n /* fall through to error */\n }\n } else {\n try {\n return Temporal.PlainDate.from(input);\n } catch {\n /* fall through to error */\n }\n }\n\n fail(\n `Unable to parse date/time string: \"${input}\". Expected ISO 8601 ZonedDateTime, Instant, PlainDateTime, or PlainDate.`,\n );\n}\n"],"mappings":"0HAkBA,SAAgB,EAAI,EAAoC,CACtD,OAAO,EAAA,SAAS,IAAI,iBAAiB,CAAE,CACzC,CAYA,SAAgB,GAA+B,CAC7C,OAAO,EAAA,SAAS,IAAI,QAAQ,CAC9B,CAYA,SAAgB,EAAW,EAAuC,CAChE,GAAI,CACF,OAAO,EAAA,SAAS,cAAc,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AG,CAAC,EAAG,CAAe,CAC3F,CAWA,SAAgB,EAAQ,EAAoC,CAC1D,OACE,aAAiB,EAAA,SAAS,SAC1B,aAAiB,EAAA,SAAS,eAC1B,aAAiB,EAAA,SAAS,eAC1B,aAAiB,EAAA,SAAS,SAE9B,CAuBA,SAAgB,EAAM,EAAe,EAAyB,CAC5D,GAAI,IAAO,QAAS,OAAO,EAAW,CAAK,EAE3C,GAAI,IAAO,UAAW,OAAO,EAAa,CAAK,EAE/C,GAAI,IAAO,iBAAkB,OAAO,EAAmB,CAAK,EAE5D,GAAI,IAAO,aAAc,OAAO,EAAe,CAAK,EAEpD,GAAI,CACF,OAAO,EAAA,SAAS,cAAc,KAAK,CAAK,CAC1C,MAAQ,CAER,CAEA,GAAI,CACF,OAAO,EAAA,SAAS,QAAQ,KAAK,CAAK,CACpC,MAAQ,CAER,CAKA,GAAI,EAAM,SAAS,GAAG,EACpB,GAAI,CACF,OAAO,EAAA,SAAS,cAAc,KAAK,CAAK,CAC1C,MAAQ,CAER,MAEA,GAAI,CACF,OAAO,EAAA,SAAS,UAAU,KAAK,CAAK,CACtC,MAAQ,CAER,CAGF,EAAA,KACE,sCAAsC,EAAM,0EAC9C,CACF"}
|
|
1
|
+
{"version":3,"file":"core.cjs","names":[],"sources":["../src/core.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\n\nimport type { CalendarUnit, DifferenceInput, ParseAs, ShiftOptions, TimeInput } from './types';\n\nimport { toZoned } from './_convert';\nimport { CALENDAR_UNITS, inferSharedTimeZone, inferTimeZone, validateTimeZone } from './_tz';\nimport { fail } from './errors';\n\n/** Parse intentionally requires an explicit result kind: time strings are ambiguous at system boundaries. */\nexport function parse(input: string, options: { as: 'zonedDateTime' }): Temporal.ZonedDateTime;\nexport function parse(input: string, options: { as: 'instant' }): Temporal.Instant;\nexport function parse(input: string, options: { as: 'plainDateTime' }): Temporal.PlainDateTime;\nexport function parse(input: string, options: { as: 'plainDate' }): Temporal.PlainDate;\nexport function parse(input: string, options: { as: ParseAs }): TimeInput {\n try {\n if (options.as === 'zonedDateTime') return Temporal.ZonedDateTime.from(input);\n\n if (options.as === 'instant') return Temporal.Instant.from(input);\n\n if (options.as === 'plainDateTime') return Temporal.PlainDateTime.from(input);\n\n return Temporal.PlainDate.from(input);\n } catch {\n fail(`Invalid ${options.as} ISO 8601 string: \"${input}\".`);\n }\n}\n\nexport function now(options: { timeZone: string }): Temporal.ZonedDateTime {\n return Temporal.Now.zonedDateTimeISO(validateTimeZone(options.timeZone));\n}\n\nexport function nowInstant(): Temporal.Instant {\n return Temporal.Now.instant();\n}\n\nexport function shift(\n input: Temporal.ZonedDateTime,\n duration: Temporal.DurationLike,\n options?: ShiftOptions,\n): Temporal.ZonedDateTime;\nexport function shift(\n input: Exclude<TimeInput, Temporal.ZonedDateTime>,\n duration: Temporal.DurationLike,\n options: ShiftOptions & { timeZone: string },\n): Temporal.ZonedDateTime;\nexport function shift(\n input: TimeInput,\n duration: Temporal.DurationLike,\n options: ShiftOptions = {},\n): Temporal.ZonedDateTime {\n const timeZone = inferTimeZone(input, options);\n\n return toZoned(input, { disambiguation: options.disambiguation, timeZone }).add(duration);\n}\n\nexport function difference(input: DifferenceInput): Temporal.Duration {\n const { end, largestUnit, roundingIncrement, roundingMode, smallestUnit, start } = input;\n const rounding = { largestUnit, roundingIncrement, roundingMode, smallestUnit };\n const needsCalendar =\n (largestUnit !== undefined && CALENDAR_UNITS.has(largestUnit as CalendarUnit)) ||\n (smallestUnit !== undefined && CALENDAR_UNITS.has(smallestUnit as CalendarUnit));\n\n if (!needsCalendar && start instanceof Temporal.Instant && end instanceof Temporal.Instant) {\n return end.since(start, rounding as Temporal.DifferenceOptions<Temporal.TimeUnit>);\n }\n\n const timeZone = inferSharedTimeZone([start, end], input);\n const options = { disambiguation: input.disambiguation, timeZone };\n\n return toZoned(end, options).since(toZoned(start, options), rounding);\n}\n\nexport function isValid(value: unknown): value is TimeInput {\n return (\n value instanceof Temporal.Instant ||\n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