@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
78 lines (77 loc) • 3.43 kB
JavaScript
// src/internal/differenceRecord.ts
/** Temporal's duration fields, largest first (Temporal Table 21, the unit ladder). */
const DURATION_FIELDS = [
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds"
];
/** True when a field strictly between `larger` and `smaller` on the ladder has an amount. */
function hasAmountBetween(duration, larger, smaller) {
const from = DURATION_FIELDS.indexOf(larger) + 1;
const to = DURATION_FIELDS.indexOf(smaller);
return DURATION_FIELDS.slice(from, to).some((field) => duration[field] !== 0);
}
/**
* True when `unit` has to be measured again from the moved start: an unlisted unit above it (and
* below the previous listed unit) has an amount, or `unit` is weeks in a duration whose largest
* unit is larger than weeks. Temporal balances weeks only when `largestUnit` is weeks (TC39
* Temporal DifferenceISODate: weeks are computed only for largestUnit "week"; for "month" and
* "year" the remainder is days), so such a duration holds its weeks in `days`.
*/
function needsCarry(duration, largest, previous, unit) {
if (hasAmountBetween(duration, previous, unit)) return true;
return unit === "weeks" && largest !== "weeks" && duration.days !== 0;
}
/**
* Express a difference in exactly the listed units, so the record is the whole difference.
*
* `duration` is `start.until(end)` with `largestUnit` = the largest listed unit and the caller's
* rounding options. Its fields are kept while the listed units are contiguous or every unlisted
* unit between them is zero, so a single unit or adjacent units return Temporal's own fields.
* When an unlisted unit between two listed units has an amount, that amount is carried into the
* next smaller listed unit (and a listed `weeks` below months or years is measured afresh, because
* Temporal leaves weeks in `days` unless `largestUnit` is weeks): the start is moved by the listed amounts found so far (one Temporal
* `add`, which adds years and months before days), and the rest is measured from there to the
* rounded end (`start.add(duration)`) with `largestUnit` = that listed unit. The record then
* satisfies `start.add(record)` = `start.add(duration)`. Units smaller than the smallest listed
* unit are dropped, as for a single unit.
*
* @param start the start value
* @param duration the (rounded) difference from `start` with the largest listed unit
* @param units the listed plural units, any order, non-empty
* @param operations the type's `add` and `until`
* @returns the amount of each listed unit, keyed by plural unit name in the order of `units`
*/
function differenceRecord(start, duration, units, operations) {
const ordered = DURATION_FIELDS.filter((field) => units.includes(field));
const amounts = {};
let origin = start;
let current = duration;
let moved = {};
let largest = ordered[0];
let end;
ordered.forEach((unit, index) => {
const previous = ordered[index - 1];
if (previous !== void 0 && largest !== void 0 && needsCarry(current, largest, previous, unit)) {
end ??= operations.add(start, duration);
origin = operations.add(origin, moved);
current = operations.until(origin, end, unit);
largest = unit;
moved = {};
}
amounts[unit] = current[unit];
moved[unit] = current[unit];
});
return units.reduce((record, unit) => {
record[unit] = amounts[unit];
return record;
}, {});
}
export { differenceRecord };