@c9up/chronos 0.1.15 → 0.1.17
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 +136 -16
- package/dist/DateTime.d.ts +209 -8
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +815 -116
- package/dist/DateTime.js.map +1 -1
- package/dist/Duration.d.ts +37 -20
- package/dist/Duration.d.ts.map +1 -1
- package/dist/Duration.js +287 -120
- package/dist/Duration.js.map +1 -1
- package/dist/Interval.d.ts +50 -7
- package/dist/Interval.d.ts.map +1 -1
- package/dist/Interval.js +142 -29
- package/dist/Interval.js.map +1 -1
- package/dist/atlas.d.ts +11 -15
- package/dist/atlas.d.ts.map +1 -1
- package/dist/atlas.js +22 -17
- package/dist/atlas.js.map +1 -1
- package/dist/format.d.ts +47 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +387 -0
- package/dist/format.js.map +1 -0
- package/dist/holidays.d.ts +43 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +102 -0
- package/dist/holidays.js.map +1 -0
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/native/generated.d.ts +11 -2
- package/dist/native/generated.d.ts.map +1 -1
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +5 -2
- package/dist/native.js.map +1 -1
- package/dist/parse.d.ts +59 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +404 -0
- package/dist/parse.js.map +1 -0
- package/dist/rrule.d.ts +34 -2
- package/dist/rrule.d.ts.map +1 -1
- package/dist/rrule.js +156 -32
- package/dist/rrule.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +2 -3
- package/src/DateTime.ts +1089 -124
- package/src/Duration.ts +318 -139
- package/src/Interval.ts +160 -53
- package/src/atlas.ts +23 -17
- package/src/format.ts +449 -0
- package/src/holidays.ts +161 -0
- package/src/index.ts +35 -7
- package/src/native/generated.ts +27 -2
- package/src/native.ts +7 -3
- package/src/parse.ts +559 -0
- package/src/rrule.ts +229 -38
- package/wasm/chronos_engine_wasm.d.ts +13 -4
- package/wasm/chronos_engine_wasm.js +87 -16
- package/wasm/chronos_engine_wasm_bg.wasm +0 -0
- package/wasm/chronos_engine_wasm_bg.wasm.d.ts +5 -2
- package/dist/utils.d.ts +0 -15
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -17
- package/dist/utils.js.map +0 -1
- package/scripts/build-napi-types.mjs +0 -69
- package/scripts/clean-wasm-pack-meta.mjs +0 -32
- package/scripts/copy-napi.mjs +0 -56
- package/scripts/generate-napi-types.mjs +0 -156
- package/scripts/verify-napi.mjs +0 -46
- package/scripts/verify-wasm.mjs +0 -55
- package/src/utils.ts +0 -17
package/src/Duration.ts
CHANGED
|
@@ -21,6 +21,17 @@ export interface DurationObject {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export type DurationUnit = keyof DurationObject;
|
|
24
|
+
/** A unit name as Luxon accepts it: plural or singular. */
|
|
25
|
+
export type DurationUnitLike =
|
|
26
|
+
| DurationUnit
|
|
27
|
+
| "year"
|
|
28
|
+
| "month"
|
|
29
|
+
| "week"
|
|
30
|
+
| "day"
|
|
31
|
+
| "hour"
|
|
32
|
+
| "minute"
|
|
33
|
+
| "second"
|
|
34
|
+
| "millisecond";
|
|
24
35
|
|
|
25
36
|
const ORDERED_UNITS: DurationUnit[] = [
|
|
26
37
|
"years",
|
|
@@ -33,12 +44,11 @@ const ORDERED_UNITS: DurationUnit[] = [
|
|
|
33
44
|
"milliseconds",
|
|
34
45
|
];
|
|
35
46
|
|
|
36
|
-
/** Millisecond equivalents
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* document it. Luxon does the same. */
|
|
47
|
+
/** Millisecond equivalents. Calendar units (years/months) have no fixed length
|
|
48
|
+
* — they depend on the anchor date — so conversions use Luxon's casual matrix:
|
|
49
|
+
* a 30-day month and a 365-day year. */
|
|
40
50
|
const MS_PER: Record<DurationUnit, number> = {
|
|
41
|
-
years: 365
|
|
51
|
+
years: 365 * 24 * 60 * 60 * 1000,
|
|
42
52
|
months: 30 * 24 * 60 * 60 * 1000,
|
|
43
53
|
weeks: 7 * 24 * 60 * 60 * 1000,
|
|
44
54
|
days: 24 * 60 * 60 * 1000,
|
|
@@ -48,6 +58,156 @@ const MS_PER: Record<DurationUnit, number> = {
|
|
|
48
58
|
milliseconds: 1,
|
|
49
59
|
};
|
|
50
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Luxon's casual conversion matrix: how many of the lower unit one higher unit
|
|
63
|
+
* holds. It is not self-consistent (a month is 30 days but 4 weeks) — that is
|
|
64
|
+
* Luxon's, and `shiftTo` follows it so the same call gives the same units.
|
|
65
|
+
*/
|
|
66
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
67
|
+
const MATRIX: Readonly<
|
|
68
|
+
Record<DurationUnit, Partial<Record<DurationUnit, number>>>
|
|
69
|
+
> = {
|
|
70
|
+
years: {
|
|
71
|
+
months: 12,
|
|
72
|
+
weeks: 52,
|
|
73
|
+
days: 365,
|
|
74
|
+
hours: 365 * 24,
|
|
75
|
+
minutes: 365 * 24 * 60,
|
|
76
|
+
seconds: 365 * 24 * 60 * 60,
|
|
77
|
+
milliseconds: 365 * DAY_MS,
|
|
78
|
+
},
|
|
79
|
+
months: {
|
|
80
|
+
weeks: 4,
|
|
81
|
+
days: 30,
|
|
82
|
+
hours: 30 * 24,
|
|
83
|
+
minutes: 30 * 24 * 60,
|
|
84
|
+
seconds: 30 * 24 * 60 * 60,
|
|
85
|
+
milliseconds: 30 * DAY_MS,
|
|
86
|
+
},
|
|
87
|
+
weeks: {
|
|
88
|
+
days: 7,
|
|
89
|
+
hours: 7 * 24,
|
|
90
|
+
minutes: 7 * 24 * 60,
|
|
91
|
+
seconds: 7 * 24 * 60 * 60,
|
|
92
|
+
milliseconds: 7 * DAY_MS,
|
|
93
|
+
},
|
|
94
|
+
days: {
|
|
95
|
+
hours: 24,
|
|
96
|
+
minutes: 24 * 60,
|
|
97
|
+
seconds: 24 * 60 * 60,
|
|
98
|
+
milliseconds: DAY_MS,
|
|
99
|
+
},
|
|
100
|
+
hours: { minutes: 60, seconds: 60 * 60, milliseconds: 60 * 60 * 1000 },
|
|
101
|
+
minutes: { seconds: 60, milliseconds: 60 * 1000 },
|
|
102
|
+
seconds: { milliseconds: 1000 },
|
|
103
|
+
milliseconds: {},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** How many `lower` one `higher` holds; both are units of the matrix. */
|
|
107
|
+
function conv(higher: DurationUnit, lower: DurationUnit): number {
|
|
108
|
+
return MATRIX[higher][lower] ?? 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Luxon's `normalizeValues` over `units` (in descending order): carry each
|
|
113
|
+
* unit into the next one up against the sign of the whole, then push
|
|
114
|
+
* fractions down. Mutates `vals`.
|
|
115
|
+
*/
|
|
116
|
+
function normalizeValues(
|
|
117
|
+
vals: Record<DurationUnit, number>,
|
|
118
|
+
units: readonly DurationUnit[],
|
|
119
|
+
): void {
|
|
120
|
+
let total = 0;
|
|
121
|
+
for (const u of ORDERED_UNITS) total += vals[u] * MS_PER[u];
|
|
122
|
+
const factor = total < 0 ? -1 : 1;
|
|
123
|
+
for (let i = units.length - 1; i > 0; i--) {
|
|
124
|
+
const current = units[i - 1];
|
|
125
|
+
const previous = units[i];
|
|
126
|
+
if (current === undefined || previous === undefined) continue;
|
|
127
|
+
const c = conv(current, previous);
|
|
128
|
+
const rollUp = Math.floor((vals[previous] * factor) / c);
|
|
129
|
+
vals[current] += rollUp * factor;
|
|
130
|
+
vals[previous] -= rollUp * c * factor;
|
|
131
|
+
}
|
|
132
|
+
for (let i = 1; i < units.length; i++) {
|
|
133
|
+
const previous = units[i - 1];
|
|
134
|
+
const current = units[i];
|
|
135
|
+
if (current === undefined || previous === undefined) continue;
|
|
136
|
+
const fraction = vals[previous] % 1;
|
|
137
|
+
vals[previous] -= fraction;
|
|
138
|
+
vals[current] += fraction * conv(previous, current);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const UNIT_NAMES: Readonly<Record<string, DurationUnit>> = {
|
|
143
|
+
year: "years",
|
|
144
|
+
years: "years",
|
|
145
|
+
month: "months",
|
|
146
|
+
months: "months",
|
|
147
|
+
week: "weeks",
|
|
148
|
+
weeks: "weeks",
|
|
149
|
+
day: "days",
|
|
150
|
+
days: "days",
|
|
151
|
+
hour: "hours",
|
|
152
|
+
hours: "hours",
|
|
153
|
+
minute: "minutes",
|
|
154
|
+
minutes: "minutes",
|
|
155
|
+
second: "seconds",
|
|
156
|
+
seconds: "seconds",
|
|
157
|
+
millisecond: "milliseconds",
|
|
158
|
+
milliseconds: "milliseconds",
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** A unit name, singular or plural, any case (Luxon's `normalizeUnit`). */
|
|
162
|
+
function normalizeUnit(unit: string): DurationUnit {
|
|
163
|
+
const key = typeof unit === "string" ? unit.toLowerCase() : "";
|
|
164
|
+
const normalized = Object.hasOwn(UNIT_NAMES, key)
|
|
165
|
+
? UNIT_NAMES[key]
|
|
166
|
+
: undefined;
|
|
167
|
+
if (normalized === undefined) {
|
|
168
|
+
throw new RangeError(`Invalid duration unit: ${String(unit)}`);
|
|
169
|
+
}
|
|
170
|
+
return normalized;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A unit value as Luxon's `asNumber` reads it: a numeric string is coerced, and
|
|
175
|
+
* anything that is not a finite number (NaN, ±Infinity, a boolean, "") is
|
|
176
|
+
* refused rather than stored — stored, NaN printed as `PT0S` and a string
|
|
177
|
+
* concatenated in `plus`.
|
|
178
|
+
*/
|
|
179
|
+
function asNumber(value: unknown): number {
|
|
180
|
+
const numeric = Number(value);
|
|
181
|
+
if (typeof value === "boolean" || value === "" || !Number.isFinite(numeric)) {
|
|
182
|
+
throw new RangeError(`Invalid unit value ${String(value)}`);
|
|
183
|
+
}
|
|
184
|
+
return numeric;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Validate a duration-like object into canonical unit names. */
|
|
188
|
+
function normalizeObject(obj: unknown): DurationObject {
|
|
189
|
+
if (typeof obj !== "object" || obj === null) {
|
|
190
|
+
throw new TypeError(
|
|
191
|
+
`Invalid duration: expected an object of units, got ${typeof obj}`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const out: DurationObject = {};
|
|
195
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
196
|
+
if (value === undefined || value === null) continue;
|
|
197
|
+
const unit = normalizeUnit(key);
|
|
198
|
+
out[unit] = (out[unit] ?? 0) + asNumber(value);
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* ISO 8601 durations as Luxon reads them: a sign on the whole and on each
|
|
205
|
+
* component, a fraction on any of them, weeks mixable with the rest, and at
|
|
206
|
+
* most 20 digits per number so a value cannot overflow to Infinity.
|
|
207
|
+
*/
|
|
208
|
+
const ISO_DURATION =
|
|
209
|
+
/^-?P(?:(?:(-?\d{1,20}(?:\.\d{1,20})?)Y)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20}(?:\.\d{1,20})?)W)?(?:(-?\d{1,20}(?:\.\d{1,20})?)D)?(?:T(?:(-?\d{1,20}(?:\.\d{1,20})?)H)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20})(?:[.,](-?\d{1,20}))?S)?)?)$/;
|
|
210
|
+
|
|
51
211
|
export class Duration {
|
|
52
212
|
readonly #values: Readonly<Required<DurationObject>>;
|
|
53
213
|
|
|
@@ -72,66 +232,65 @@ export class Duration {
|
|
|
72
232
|
|
|
73
233
|
// ─── Factories ──────────────────────────────────────────
|
|
74
234
|
|
|
75
|
-
/**
|
|
235
|
+
/**
|
|
236
|
+
* Build from a plain object of unit values. Unit names may be singular or
|
|
237
|
+
* plural; every value must be a finite number (a numeric string is coerced).
|
|
238
|
+
*
|
|
239
|
+
* @throws RangeError on an unknown unit or a non-finite value.
|
|
240
|
+
*/
|
|
76
241
|
static fromObject(obj: DurationObject): Duration {
|
|
77
|
-
return new Duration(obj);
|
|
242
|
+
return new Duration(normalizeObject(obj));
|
|
78
243
|
}
|
|
79
244
|
|
|
80
245
|
/** Build from a total number of milliseconds (approximates calendar units). */
|
|
81
246
|
static fromMillis(ms: number): Duration {
|
|
82
|
-
return new Duration({ milliseconds: ms });
|
|
247
|
+
return new Duration({ milliseconds: asNumber(ms) });
|
|
83
248
|
}
|
|
84
249
|
|
|
85
250
|
/**
|
|
86
|
-
* Parse an ISO 8601 duration string (`P1Y2M3DT4H5M6.789S`
|
|
87
|
-
*
|
|
88
|
-
* Supports the full `PnYnMnDTnHnMnS` form. Fractional seconds are
|
|
89
|
-
* rounded to the nearest millisecond. Weeks (`PnW`) are a separate form
|
|
90
|
-
* that cannot be mixed with other designators per the spec.
|
|
251
|
+
* Parse an ISO 8601 duration string (`P1Y2M3DT4H5M6.789S`, `P2W`, `P1.5D`,
|
|
252
|
+
* `PT-5M`, `-P1D`), with Luxon's grammar.
|
|
91
253
|
*/
|
|
92
254
|
static fromISO(iso: string): Duration {
|
|
93
|
-
const
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
throw new Error(`Invalid ISO 8601 duration: ${iso}`);
|
|
99
|
-
const apply = (d: Duration): Duration => (negative ? d.negate() : d);
|
|
100
|
-
|
|
101
|
-
// Week form: P3W
|
|
102
|
-
const weekMatch = /^P(\d+(?:\.\d+)?)W$/.exec(s);
|
|
103
|
-
if (weekMatch) {
|
|
104
|
-
const [, weeks] = weekMatch;
|
|
105
|
-
return apply(new Duration({ weeks: Number.parseFloat(weeks ?? "0") }));
|
|
255
|
+
const m = typeof iso === "string" ? ISO_DURATION.exec(iso) : null;
|
|
256
|
+
// The all-optional grammar also matches a bare "P"/"PT": a duration
|
|
257
|
+
// needs at least one component. An explicit zero ("PT0S") still parses.
|
|
258
|
+
if (!m || m.slice(1).every((part) => part === undefined)) {
|
|
259
|
+
throw new RangeError(`Invalid ISO 8601 duration: ${String(iso)}`);
|
|
106
260
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// least one component. An explicit zero ("PT0S", "P0D") still parses.
|
|
117
|
-
if (!y && !mo && !d && !h && !mi && !sec) {
|
|
118
|
-
throw new Error(`Invalid ISO 8601 duration: ${iso}`);
|
|
261
|
+
const [, y, mo, w, d, h, mi, sec, frac] = m;
|
|
262
|
+
// ISO 8601 requires a time component after "T"; Luxon lets "P1DT" through.
|
|
263
|
+
if (
|
|
264
|
+
iso.includes("T") &&
|
|
265
|
+
h === undefined &&
|
|
266
|
+
mi === undefined &&
|
|
267
|
+
sec === undefined
|
|
268
|
+
) {
|
|
269
|
+
throw new RangeError(`Invalid ISO 8601 duration: ${iso}`);
|
|
119
270
|
}
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
271
|
+
const negativePrefix = iso.startsWith("-");
|
|
272
|
+
const read = (part: string | undefined): number | undefined => {
|
|
273
|
+
if (part === undefined) return undefined;
|
|
274
|
+
const value = Number.parseFloat(part);
|
|
275
|
+
return negativePrefix && value !== 0 ? -value : value;
|
|
276
|
+
};
|
|
277
|
+
let milliseconds: number | undefined;
|
|
278
|
+
if (frac !== undefined) {
|
|
279
|
+
const ms = Math.floor(Number.parseFloat(`0.${frac}`) * 1000);
|
|
280
|
+
const negativeSeconds = sec?.startsWith("-") === true;
|
|
281
|
+
milliseconds = negativeSeconds !== negativePrefix ? -ms : ms;
|
|
282
|
+
}
|
|
283
|
+
const values: DurationObject = {
|
|
284
|
+
years: read(y),
|
|
285
|
+
months: read(mo),
|
|
286
|
+
weeks: read(w),
|
|
287
|
+
days: read(d),
|
|
288
|
+
hours: read(h),
|
|
289
|
+
minutes: read(mi),
|
|
290
|
+
seconds: sec === "-0" && !negativePrefix ? -0 : read(sec),
|
|
291
|
+
milliseconds,
|
|
292
|
+
};
|
|
293
|
+
return new Duration(normalizeObject(values));
|
|
135
294
|
}
|
|
136
295
|
|
|
137
296
|
// ─── Accessors ──────────────────────────────────────────
|
|
@@ -161,9 +320,9 @@ export class Duration {
|
|
|
161
320
|
return this.#values.milliseconds;
|
|
162
321
|
}
|
|
163
322
|
|
|
164
|
-
/** Get a specific unit. */
|
|
165
|
-
get(unit:
|
|
166
|
-
return this.#values[unit];
|
|
323
|
+
/** Get a specific unit (singular or plural name). */
|
|
324
|
+
get(unit: DurationUnitLike): number {
|
|
325
|
+
return this.#values[normalizeUnit(unit)];
|
|
167
326
|
}
|
|
168
327
|
|
|
169
328
|
/** Return the internal bag as a plain object. */
|
|
@@ -173,9 +332,9 @@ export class Duration {
|
|
|
173
332
|
|
|
174
333
|
// ─── Arithmetic ─────────────────────────────────────────
|
|
175
334
|
|
|
176
|
-
/** Add another duration
|
|
177
|
-
plus(other: Duration | DurationObject): Duration {
|
|
178
|
-
const o =
|
|
335
|
+
/** Add another duration (a Duration, an object of units, or milliseconds). */
|
|
336
|
+
plus(other: Duration | DurationObject | number): Duration {
|
|
337
|
+
const o = Duration.#valuesOf(other);
|
|
179
338
|
return new Duration({
|
|
180
339
|
years: this.#values.years + (o.years ?? 0),
|
|
181
340
|
months: this.#values.months + (o.months ?? 0),
|
|
@@ -189,8 +348,8 @@ export class Duration {
|
|
|
189
348
|
}
|
|
190
349
|
|
|
191
350
|
/** Subtract another duration from this one. */
|
|
192
|
-
minus(other: Duration | DurationObject): Duration {
|
|
193
|
-
const o =
|
|
351
|
+
minus(other: Duration | DurationObject | number): Duration {
|
|
352
|
+
const o = Duration.#valuesOf(other);
|
|
194
353
|
return new Duration({
|
|
195
354
|
years: this.#values.years - (o.years ?? 0),
|
|
196
355
|
months: this.#values.months - (o.months ?? 0),
|
|
@@ -203,67 +362,69 @@ export class Duration {
|
|
|
203
362
|
});
|
|
204
363
|
}
|
|
205
364
|
|
|
206
|
-
|
|
365
|
+
static #valuesOf(other: Duration | DurationObject | number): DurationObject {
|
|
366
|
+
if (other instanceof Duration) return other.#values;
|
|
367
|
+
if (typeof other === "number") return { milliseconds: asNumber(other) };
|
|
368
|
+
return normalizeObject(other);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Total length in milliseconds, on the casual matrix (Luxon's `toMillis`). */
|
|
372
|
+
toMillis(): number {
|
|
373
|
+
return this.as("milliseconds");
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Milliseconds, so durations compare and subtract like numbers. */
|
|
377
|
+
valueOf(): number {
|
|
378
|
+
return this.toMillis();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Spread over every unit, years down to milliseconds (Luxon's `shiftToAll`). */
|
|
382
|
+
shiftToAll(): Duration {
|
|
383
|
+
return this.shiftTo(...ORDERED_UNITS);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Re-express in the largest units that fit: 90 000 ms becomes 1 minute
|
|
388
|
+
* 30 seconds (Luxon's `rescale`).
|
|
389
|
+
*/
|
|
390
|
+
rescale(): Duration {
|
|
391
|
+
return this.normalize().shiftToAll();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** A Duration with `fn` applied to every unit's value (Luxon's `mapUnits`). */
|
|
395
|
+
mapUnits(fn: (value: number, unit: DurationUnit) => number): Duration {
|
|
396
|
+
const out: DurationObject = {};
|
|
397
|
+
for (const unit of ORDERED_UNITS) {
|
|
398
|
+
out[unit] = asNumber(fn(this.#values[unit], unit));
|
|
399
|
+
}
|
|
400
|
+
return new Duration(out);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Flip the sign on every unit (a zero stays 0, never -0, as in Luxon). */
|
|
207
404
|
negate(): Duration {
|
|
208
|
-
return
|
|
209
|
-
years: -this.#values.years,
|
|
210
|
-
months: -this.#values.months,
|
|
211
|
-
weeks: -this.#values.weeks,
|
|
212
|
-
days: -this.#values.days,
|
|
213
|
-
hours: -this.#values.hours,
|
|
214
|
-
minutes: -this.#values.minutes,
|
|
215
|
-
seconds: -this.#values.seconds,
|
|
216
|
-
milliseconds: -this.#values.milliseconds,
|
|
217
|
-
});
|
|
405
|
+
return this.mapUnits((value) => (value === 0 ? 0 : -value));
|
|
218
406
|
}
|
|
219
407
|
|
|
220
408
|
// ─── Conversion ─────────────────────────────────────────
|
|
221
409
|
|
|
222
410
|
/**
|
|
223
|
-
* Normalize
|
|
224
|
-
* becomes 1 minute 30 seconds
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
411
|
+
* Normalize days and the clock units with Luxon's `normalizeValues`: carry
|
|
412
|
+
* overflows upward (90 seconds becomes 1 minute 30 seconds), rebalance mixed
|
|
413
|
+
* signs against the sign of the whole (`{ hours: -1, minutes: 30 }` is
|
|
414
|
+
* thirty minutes back, `{ minutes: -30 }`), and push fractions down into
|
|
415
|
+
* the next unit. Years, months and weeks are left as they are: rolling days
|
|
416
|
+
* into them would need an anchor date.
|
|
229
417
|
*/
|
|
230
418
|
normalize(): Duration {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
// relative to its parent unit, matching the mathematical convention.
|
|
241
|
-
const carry = (value: number, divisor: number): [number, number] => {
|
|
242
|
-
const quot = Math.trunc(value / divisor);
|
|
243
|
-
const rem = value - quot * divisor;
|
|
244
|
-
return [quot, rem];
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
let c: number;
|
|
248
|
-
[c, ms] = carry(ms, 1000);
|
|
249
|
-
sec += c;
|
|
250
|
-
[c, sec] = carry(sec, 60);
|
|
251
|
-
min += c;
|
|
252
|
-
[c, min] = carry(min, 60);
|
|
253
|
-
hrs += c;
|
|
254
|
-
[c, hrs] = carry(hrs, 24);
|
|
255
|
-
days += c;
|
|
256
|
-
|
|
257
|
-
return new Duration({
|
|
258
|
-
years: this.#values.years,
|
|
259
|
-
months: this.#values.months,
|
|
260
|
-
weeks: this.#values.weeks,
|
|
261
|
-
days,
|
|
262
|
-
hours: hrs,
|
|
263
|
-
minutes: min,
|
|
264
|
-
seconds: sec,
|
|
265
|
-
milliseconds: ms,
|
|
266
|
-
});
|
|
419
|
+
const vals = { ...this.#values };
|
|
420
|
+
normalizeValues(vals, [
|
|
421
|
+
"days",
|
|
422
|
+
"hours",
|
|
423
|
+
"minutes",
|
|
424
|
+
"seconds",
|
|
425
|
+
"milliseconds",
|
|
426
|
+
]);
|
|
427
|
+
return new Duration(vals);
|
|
267
428
|
}
|
|
268
429
|
|
|
269
430
|
/**
|
|
@@ -274,43 +435,61 @@ export class Duration {
|
|
|
274
435
|
* Duration.fromObject({ hours: 25 }).shiftTo('days', 'hours')
|
|
275
436
|
* // → { days: 1, hours: 1 }
|
|
276
437
|
*/
|
|
277
|
-
shiftTo(...
|
|
278
|
-
if (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
438
|
+
shiftTo(...requested: DurationUnitLike[]): Duration {
|
|
439
|
+
if (requested.length === 0) return this;
|
|
440
|
+
const units = requested.map(normalizeUnit);
|
|
441
|
+
const ordered = ORDERED_UNITS.filter((u) => units.includes(u));
|
|
442
|
+
const built: Record<DurationUnit, number> = {
|
|
443
|
+
years: 0,
|
|
444
|
+
months: 0,
|
|
445
|
+
weeks: 0,
|
|
446
|
+
days: 0,
|
|
447
|
+
hours: 0,
|
|
448
|
+
minutes: 0,
|
|
449
|
+
seconds: 0,
|
|
450
|
+
milliseconds: 0,
|
|
451
|
+
};
|
|
452
|
+
const accumulated = new Map<DurationUnit, number>();
|
|
453
|
+
let last: DurationUnit | undefined;
|
|
454
|
+
// Luxon's shiftTo: higher units not asked for flow down into the next
|
|
455
|
+
// requested one, each requested unit keeps its integer part, and what is
|
|
456
|
+
// left over becomes the fraction of the last one.
|
|
457
|
+
for (const k of ORDERED_UNITS) {
|
|
458
|
+
if (ordered.includes(k)) {
|
|
459
|
+
last = k;
|
|
460
|
+
let own = 0;
|
|
461
|
+
for (const [ak, value] of accumulated) {
|
|
462
|
+
own += conv(ak, k) * value;
|
|
463
|
+
accumulated.set(ak, 0);
|
|
464
|
+
}
|
|
465
|
+
own += this.#values[k];
|
|
466
|
+
const whole = Math.trunc(own);
|
|
467
|
+
built[k] = whole;
|
|
468
|
+
accumulated.set(k, (own * 1000 - whole * 1000) / 1000);
|
|
469
|
+
} else {
|
|
470
|
+
accumulated.set(k, this.#values[k]);
|
|
471
|
+
}
|
|
295
472
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
473
|
+
if (last !== undefined) {
|
|
474
|
+
for (const [key, value] of accumulated) {
|
|
475
|
+
if (value === 0) continue;
|
|
476
|
+
built[last] += key === last ? value : value / conv(last, key);
|
|
477
|
+
}
|
|
300
478
|
}
|
|
301
|
-
|
|
479
|
+
normalizeValues(built, ordered);
|
|
480
|
+
return new Duration(built);
|
|
302
481
|
}
|
|
303
482
|
|
|
304
483
|
/**
|
|
305
484
|
* Express the total duration in a single unit (approximate for calendar
|
|
306
485
|
* units — same caveat as `shiftTo`).
|
|
307
486
|
*/
|
|
308
|
-
as(unit:
|
|
487
|
+
as(unit: DurationUnitLike): number {
|
|
309
488
|
let totalMs = 0;
|
|
310
489
|
for (const u of ORDERED_UNITS) {
|
|
311
490
|
totalMs += this.#values[u] * MS_PER[u];
|
|
312
491
|
}
|
|
313
|
-
return totalMs / MS_PER[unit];
|
|
492
|
+
return totalMs / MS_PER[normalizeUnit(unit)];
|
|
314
493
|
}
|
|
315
494
|
|
|
316
495
|
// ─── Formatting ─────────────────────────────────────────
|