@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.
80 lines (78 loc) • 3.42 kB
JavaScript
import { isObject } from "./isObject.js";
import { parseInstantNanoseconds } from "./instantNanoseconds.js";
// src/internal/intervalNanoseconds.ts
/**
* Parse a candidate `Interval` to epoch nanoseconds, or `null` when it is not one.
*
* - Any non-null object with ISO 8601 instant strings `start` ≤ `end` (by instant) is accepted;
* extra keys are ignored (GMT rule: the type is structural).
* - Each endpoint is read exactly once, so the validated text is the text callers echo.
*
* @param value candidate `{ start, end }` record
* @returns the parsed record, or null when `value` is not a valid `Interval`
*
* @example parseIntervalNanoseconds({ start: "1970-01-01T00:00:00Z", end: "1970-01-01T00:00:00.000000001Z" }) // { start: 0n, end: 1n, startText: "1970-01-01T00:00:00Z", endText: "1970-01-01T00:00:00.000000001Z" }
* @example parseIntervalNanoseconds({ start: "1970-01-01T00:00:01Z", end: "1970-01-01T00:00:00Z" }) // null — inverted
*/
function parseIntervalNanoseconds(value) {
if (!isObject(value)) return null;
const { start: startText, end: endText } = value;
const start = parseInstantNanoseconds(startText);
const end = parseInstantNanoseconds(endText);
if (start === null || end === null || start > end) return null;
return {
start,
end,
startText,
endText
};
}
/**
* Parse a list of candidate `Interval`s, all-or-nothing.
*
* @param values candidate array of `{ start, end }` records
* @returns the parsed records in input order, or null when `values` is not an array or any element is invalid
*
* @example parseIntervalNanosecondsList([]) // []
* @example parseIntervalNanosecondsList([null]) // null
*/
function parseIntervalNanosecondsList(values) {
if (!Array.isArray(values)) return null;
const records = [];
for (let index = 0; index < values.length; index++) {
const record = parseIntervalNanoseconds(values[index]);
if (record === null) return null;
records.push(record);
}
return records;
}
/**
* Sweep parsed intervals into sorted, disjoint, non-touching, non-empty runs.
*
* - Touching records coalesce (`start <= run.end`); a 1 ns gap does not.
* - Equal starts keep input order (stable sort), so a run keeps its first start text; the first
* record to reach the run's latest end keeps its end text (GMT rule: first wins ties).
* - Empty records that touch or fall inside a run are absorbed; a stranded one is dropped.
* - Runs are copies; the input records are never mutated.
*
* @param records parsed intervals, in any order
* @returns coalesced runs sorted by start
*
* @example coalesceIntervalNanoseconds([{ start: 3n, end: 5n, startText: "c", endText: "e" }, { start: 1n, end: 3n, startText: "a", endText: "c" }]) // [{ start: 1n, end: 5n, startText: "a", endText: "e" }]
* @example coalesceIntervalNanoseconds([{ start: 4n, end: 4n, startText: "d", endText: "d" }]) // []
*/
function coalesceIntervalNanoseconds(records) {
const sorted = [...records].sort((x, y) => x.start < y.start ? -1 : x.start > y.start ? 1 : 0);
const runs = [];
for (const record of sorted) {
const last = runs[runs.length - 1];
if (last !== void 0 && record.start <= last.end) {
if (record.end > last.end) {
last.end = record.end;
last.endText = record.endText;
}
} else runs.push({ ...record });
}
return runs.filter((run) => run.start < run.end);
}
export { coalesceIntervalNanoseconds, parseIntervalNanoseconds, parseIntervalNanosecondsList };