UNPKG

@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.

47 lines • 2.6 kB
/** An `Interval` parsed once: bigint epoch nanoseconds plus the exact strings they came from. */ export interface IntervalNanoseconds { start: bigint; end: bigint; startText: string; endText: string; } /** * 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 */ export declare function parseIntervalNanoseconds(value: unknown): IntervalNanoseconds | null; /** * 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 */ export declare function parseIntervalNanosecondsList(values: unknown): IntervalNanoseconds[] | null; /** * 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" }]) // [] */ export declare function coalesceIntervalNanoseconds(records: readonly IntervalNanoseconds[]): IntervalNanoseconds[];