@vielzeug/tempo 1.0.6 → 2.1.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 +5 -9
- package/dist/_floor.d.ts.map +1 -1
- package/dist/_floor.js +1 -5
- 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 -13
- package/dist/_tz.d.ts.map +1 -1
- package/dist/_tz.js +9 -20
- 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 +1 -22
- package/dist/boundary.d.ts.map +1 -1
- package/dist/boundary.js +2 -3
- 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 +38 -49
- 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/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +0 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -4
- 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 +24 -18
- 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 +6 -6
- 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 +35 -37
- package/dist/types.d.ts.map +1 -1
- package/package.json +23 -18
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';\
|
|
1
|
+
{"version":3,"file":"classify.cjs","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant, toZoned } from './_convert';\nimport { inferSharedTimeZone } from './_tz';\nimport { TempoInvalidInputError } from './errors';\nimport type {\n ClassifyExpiryInput,\n FixedDuration,\n TimeDiffResult,\n TimeDiffUnit,\n TimeInput,\n TimeZoneOptions,\n} from './types';\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":"0HAaA,IAAM,EAAS,CACb,IAAK,MACL,KAAM,KACN,YAAa,EAAI,IACjB,YAAa,EACb,OAAQ,IACR,WAAY,EAAI,IAChB,OAAQ,IACR,KAAM,MACR,EACM,EAAoF,CACxF,CAAE,MAAO,QAAS,KAAM,MAAO,EAC/B,CAAE,MAAO,SAAU,KAAM,OAAQ,EACjC,CAAE,MAAO,QAAS,KAAM,MAAO,EAC/B,CAAE,MAAO,OAAQ,KAAM,KAAM,EAC7B,CAAE,MAAO,QAAS,KAAM,MAAO,EAC/B,CAAE,MAAO,UAAW,KAAM,QAAS,EACnC,CAAE,MAAO,UAAW,KAAM,QAAS,EACnC,CAAE,MAAO,eAAgB,KAAM,aAAc,CAC/C,EAGA,SAAS,EAA4B,EAA8B,CACjE,IAAM,EAAW,EAAA,SAAS,SAAS,KAAK,CAAK,EAE7C,GAAI,EAAS,OAAS,EAAS,OAC7B,MAAM,IAAI,EAAA,uBACR,yFACF,EAGF,OACE,EAAS,MAAQ,EAAO,KACxB,EAAS,KAAO,EAAO,IACvB,EAAS,MAAQ,EAAO,KACxB,EAAS,QAAU,EAAO,OAC1B,EAAS,QAAU,EAAO,OAC1B,EAAS,aACT,EAAS,aAAe,EAAO,YAC/B,EAAS,YAAc,EAAO,UAElC,CAEA,SAAgB,EAAiC,EAAyC,CACxF,IAAM,EAAY,EAAM,YAAc,EAAA,SAAS,IAAI,QAAQ,EAErD,EADQ,EAAA,UAAU,EAAM,MAAO,CACrB,CAAA,CAAM,kBAAoB,EAAU,kBAKpD,OAJoB,OAAO,QAAQ,EAAM,UAAU,CAAC,CACjD,KAAK,CAAC,EAAK,MAAe,CAAE,MAAK,IAAK,EAA4B,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|
|
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":"AAIA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EAEd,SAAS,EACT,eAAe,EAChB,MAAM,SAAS,CAAC;AA6CjB,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';\
|
|
1
|
+
{"version":3,"file":"classify.js","names":[],"sources":["../src/classify.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant, toZoned } from './_convert';\nimport { inferSharedTimeZone } from './_tz';\nimport { TempoInvalidInputError } from './errors';\nimport type {\n ClassifyExpiryInput,\n FixedDuration,\n TimeDiffResult,\n TimeDiffUnit,\n TimeInput,\n TimeZoneOptions,\n} from './types';\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":";;;;;AAaA,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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AW,EAAoB,CAAC,GAAG,CAAC,GAAG,CAAO,GAC9C,IAAO,EAAQ,GAAG,EAAE,YAAS,CAAC,GAC9B,IAAQ,EAAQ,GAAG,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(t
|
|
1
|
+
const e=require("./_tz.cjs"),t=require("./_convert.cjs"),n=require("./_floor.cjs");let r=require("@js-temporal/polyfill");function i(e,t){return r.Temporal.Instant.compare(e,t)<=0?[e,t]:[t,e]}function a(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 o({end:r,start:a,value:o,...s}){if(!s.unit){let e=t.toInstant(o,s),[n,c]=i(t.toInstant(a,s),t.toInstant(r,s));return{lower:n,target:e,upper:c}}let c={timeZone:e.inferSharedTimeZone([o,a,r],s),weekStartsOn:s.weekStartsOn},l=n.floorToUnit(o,s.unit,c),[u,d]=i(n.floorToUnit(a,s.unit,c),n.floorToUnit(r,s.unit,c));return{lower:u,target:l,upper:d}}function s(e,t,n={}){return a(e,t,n)<0}function c(e,t,n={}){return a(e,t,n)>0}function l(e,t,n={}){return a(e,t,n)===0}function u(e){let{lower:t,target:n,upper:i}=o(e);return r.Temporal.Instant.compare(t,n)<=0&&r.Temporal.Instant.compare(n,i)<=0}function d(e){let{lower:t,target:n,upper:i}=o(e),a=r.Temporal.Instant.compare(n,t)<0?t:r.Temporal.Instant.compare(n,i)>0?i:n;return e.value instanceof r.Temporal.ZonedDateTime?a.toZonedDateTimeISO(e.timeZone??e.value.timeZoneId):a}exports.clamp=d,exports.contains=u,exports.isAfter=c,exports.isBefore=s,exports.isSame=l;
|
|
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';\
|
|
1
|
+
{"version":3,"file":"compare.cjs","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant } from './_convert';\nimport { floorToUnit } from './_floor';\nimport { inferSharedTimeZone } from './_tz';\nimport type { ClampInput, CompareOptions, ContainsInput, TimeInput } from './types';\n\nfunction normalizeRange(start: Temporal.Instant, end: Temporal.Instant): [Temporal.Instant, Temporal.Instant] {\n return Temporal.Instant.compare(start, end) <= 0 ? [start, end] : [end, start];\n}\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":"0HAMA,SAAS,EAAe,EAAyB,EAA6D,CAC5G,OAAO,EAAA,SAAS,QAAQ,QAAQ,EAAO,CAAG,GAAK,EAAI,CAAC,EAAO,CAAG,EAAI,CAAC,EAAK,CAAK,CAC/E,CAEA,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,EAA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J,EAAA,SAAS,QAAQ,QAAQ,EAAQ,CAAK,EAAI,EAAI,EAAQ,EAAA,SAAS,QAAQ,QAAQ,EAAQ,CAAK,EAAI,EAAI,EAAQ,EAE9G,OAAO,EAAM,iBAAiB,EAAA,SAAS,cACnC,EAAQ,mBAAmB,EAAM,UAAY,EAAM,MAAM,UAAU,EACnE,CACN"}
|
package/dist/compare.d.ts
CHANGED
|
@@ -1,75 +1,11 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
-
import type { CompareOptions, TimeInput } from './types';
|
|
3
|
-
/**
|
|
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, CompareOptions, ContainsInput, 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;
|
|
1
|
+
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAIjD,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,aAAa,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
|
@@ -1,71 +1,60 @@
|
|
|
1
|
-
import { inferSharedTimeZone as e
|
|
2
|
-
import { toInstant as
|
|
3
|
-
import { floorToUnit as
|
|
4
|
-
import { Temporal as
|
|
1
|
+
import { inferSharedTimeZone as e } from "./_tz.js";
|
|
2
|
+
import { toInstant as t } from "./_convert.js";
|
|
3
|
+
import { floorToUnit as n } from "./_floor.js";
|
|
4
|
+
import { Temporal as r } from "@js-temporal/polyfill";
|
|
5
5
|
//#region src/compare.ts
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
function i(e, t) {
|
|
7
|
+
return r.Instant.compare(e, t) <= 0 ? [e, t] : [t, e];
|
|
8
|
+
}
|
|
9
|
+
function a(i, a, o) {
|
|
10
|
+
if (!o.unit) return r.Instant.compare(t(i, o), t(a, o));
|
|
11
|
+
let s = {
|
|
12
|
+
timeZone: e([i, a], o),
|
|
13
|
+
weekStartsOn: o.weekStartsOn
|
|
14
14
|
};
|
|
15
|
+
return r.Instant.compare(n(i, o.unit, s), n(a, o.unit, s));
|
|
15
16
|
}
|
|
16
|
-
function o(
|
|
17
|
+
function o({ end: r, start: a, value: o, ...s }) {
|
|
18
|
+
if (!s.unit) {
|
|
19
|
+
let e = t(o, s), [n, c] = i(t(a, s), t(r, s));
|
|
20
|
+
return {
|
|
21
|
+
lower: n,
|
|
22
|
+
target: e,
|
|
23
|
+
upper: c
|
|
24
|
+
};
|
|
25
|
+
}
|
|
17
26
|
let c = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
timeZone: e([
|
|
28
|
+
o,
|
|
29
|
+
a,
|
|
30
|
+
r
|
|
22
31
|
], s),
|
|
23
32
|
weekStartsOn: s.weekStartsOn
|
|
24
|
-
}, l =
|
|
33
|
+
}, l = n(o, s.unit, c), [u, d] = i(n(a, s.unit, c), n(r, s.unit, c));
|
|
25
34
|
return {
|
|
26
35
|
lower: u,
|
|
27
36
|
target: l,
|
|
28
37
|
upper: d
|
|
29
38
|
};
|
|
30
39
|
}
|
|
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);
|
|
40
|
+
function s(e, t, n = {}) {
|
|
41
|
+
return a(e, t, n) < 0;
|
|
35
42
|
}
|
|
36
43
|
function c(e, t, n = {}) {
|
|
37
|
-
return
|
|
44
|
+
return a(e, t, n) > 0;
|
|
38
45
|
}
|
|
39
46
|
function l(e, t, n = {}) {
|
|
40
|
-
return
|
|
47
|
+
return a(e, t, n) === 0;
|
|
41
48
|
}
|
|
42
|
-
function u(e
|
|
43
|
-
|
|
49
|
+
function u(e) {
|
|
50
|
+
let { lower: t, target: n, upper: i } = o(e);
|
|
51
|
+
return r.Instant.compare(t, n) <= 0 && r.Instant.compare(n, i) <= 0;
|
|
44
52
|
}
|
|
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;
|
|
53
|
+
function d(e) {
|
|
54
|
+
let { lower: t, target: n, upper: i } = o(e), a = r.Instant.compare(n, t) < 0 ? t : r.Instant.compare(n, i) > 0 ? i : n;
|
|
55
|
+
return e.value instanceof r.ZonedDateTime ? a.toZonedDateTimeISO(e.timeZone ?? e.value.timeZoneId) : a;
|
|
67
56
|
}
|
|
68
57
|
//#endregion
|
|
69
|
-
export {
|
|
58
|
+
export { d as clamp, u as contains, c as isAfter, s as isBefore, l as isSame };
|
|
70
59
|
|
|
71
60
|
//# 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';\
|
|
1
|
+
{"version":3,"file":"compare.js","names":[],"sources":["../src/compare.ts"],"sourcesContent":["import { Temporal } from '@js-temporal/polyfill';\nimport { toInstant } from './_convert';\nimport { floorToUnit } from './_floor';\nimport { inferSharedTimeZone } from './_tz';\nimport type { ClampInput, CompareOptions, ContainsInput, TimeInput } from './types';\n\nfunction normalizeRange(start: Temporal.Instant, end: Temporal.Instant): [Temporal.Instant, Temporal.Instant] {\n return Temporal.Instant.compare(start, end) <= 0 ? [start, end] : [end, start];\n}\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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AAI;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
|