@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.
112 lines • 6.05 kB
TypeScript
import { Interval } from "../types/interval.js";
import "../types/index.js";
/** 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[];
/**
* The overlap of two parsed intervals, as the interval it spans. Never throws.
*
* @param first the first parsed interval
* @param second the second parsed interval
* @returns the shared span, or null when the two share no instant
*
* @example intersectIntervalRecords({ start: 1n, end: 5n, startText: "a", endText: "e" }, { start: 3n, end: 9n, startText: "c", endText: "i" }) // { start: "c", end: "e" }
* @example intersectIntervalRecords({ start: 1n, end: 2n, startText: "a", endText: "b" }, { start: 2n, end: 3n, startText: "b", endText: "c" }) // null
*/
export declare function intersectIntervalRecords(first: IntervalNanoseconds, second: IntervalNanoseconds): Interval | null;
/**
* The core of `intersectIntervals`: the overlap of two intervals. Never throws.
*
* @returns what `intersectIntervals` returns, for the same arguments
*/
export declare function intersectIntervalPair(a: Interval, b: Interval): Interval | null;
/**
* The core of `mergeIntervals`: the union of a list of intervals as sorted, disjoint runs. Never throws.
*
* @returns what `mergeIntervals` returns, for the same arguments
*/
export declare function mergeIntervalList(intervals: Interval[]): Interval[];
/**
* The core of `intervalContains`: whether an interval holds an instant. Never throws.
*
* @returns what `intervalContains` returns, for the same arguments
*/
export declare function intervalContainsValue(interval: Interval, isoString: string): boolean;
/**
* An interval less a list of parsed removals, as the pieces left over. Never throws.
*
* @param source the parsed interval to cut from
* @param removals the parsed intervals to remove, in any order
* @returns the remaining pieces in order, empty when nothing is left
*
* @example subtractIntervalRecords({ start: 0n, end: 9n, startText: "a", endText: "i" }, [{ start: 3n, end: 5n, startText: "c", endText: "e" }]) // [{ start: "a", end: "c" }, { start: "e", end: "i" }]
* @example subtractIntervalRecords({ start: 0n, end: 9n, startText: "a", endText: "i" }, [{ start: 0n, end: 9n, startText: "a", endText: "i" }]) // []
*/
export declare function subtractIntervalRecords(source: IntervalNanoseconds, removals: readonly IntervalNanoseconds[]): Interval[];
/**
* The core of `subtractIntervals`: an interval less a list of removals. Never throws.
*
* @returns what `subtractIntervals` returns, for the same arguments
*/
export declare function subtractIntervalList(from: Interval, remove: Interval[]): Interval[];
/**
* Whether two parsed intervals share an instant.
*
* @param first the first parsed interval
* @param second the second parsed interval
* @returns true when the half-open spans overlap
*
* @example intervalRecordsOverlap({ start: 1n, end: 5n, startText: "a", endText: "e" }, { start: 3n, end: 9n, startText: "c", endText: "i" }) // true
* @example intervalRecordsOverlap({ start: 1n, end: 2n, startText: "a", endText: "b" }, { start: 2n, end: 3n, startText: "b", endText: "c" }) // false
*/
export declare function intervalRecordsOverlap(first: IntervalNanoseconds, second: IntervalNanoseconds): boolean;
/**
* The core of `intervalsOverlap`: whether two intervals share an instant. Never throws.
*
* @returns what `intervalsOverlap` returns, for the same arguments
*/
export declare function intervalPairOverlaps(a: Interval, b: Interval): boolean;