@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.
42 lines (40 loc) • 1.77 kB
JavaScript
import { isObject } from "./isObject.js";
import { parseInstantNanoseconds } from "./instantNanoseconds.js";
// src/internal/dutyLog.ts
/**
* Parse a candidate duty log, all-or-nothing.
*
* - An entry is any object whose `status` is a string and whose `start` and `end` are ISO 8601
* instant strings with `start` before `end`; extra keys are ignored (GMT rule: the type is
* structural). Each field is read once.
* - Entries must be ordered by `start` and must not overlap: one may start where the previous
* ends (half-open), or later.
*
* @param value candidate array of `{ status, start, end }` records
* @returns the parsed entries in order, or null when `value` is not a valid duty log
*
* @example parseDutyLog([{ status: "off", start: "1970-01-01T00:00:00Z", end: "1970-01-01T00:00:00.000000002Z" }]) // [{ status: "off", start: 0n, end: 2n }]
* @example parseDutyLog([{ status: "off", start: "1970-01-01T00:00:00Z", end: "1970-01-01T00:00:00Z" }]) // null — zero length
*/
function parseDutyLog(value) {
if (!Array.isArray(value)) return null;
const entries = [];
const length = value.length;
for (let index = 0; index < length; index++) {
const candidate = value[index];
if (!isObject(candidate)) return null;
const { status, start, end } = candidate;
if (typeof status !== "string" || typeof start !== "string" || typeof end !== "string") return null;
const startNs = parseInstantNanoseconds(start);
const endNs = parseInstantNanoseconds(end);
const previous = entries[entries.length - 1];
if (startNs === null || endNs === null || endNs <= startNs || previous !== void 0 && startNs < previous.end) return null;
entries.push({
status,
start: startNs,
end: endNs
});
}
return entries;
}
export { parseDutyLog };