@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.
199 lines • 5.2 kB
TypeScript
import { Temporal } from "./temporal.js";
import { DutySeverity } from "../types/duty.js";
import "../types/index.js";
import { CalendarUnit } from "./dutyPeriods.js";
/**
* The most local periods one count may name: 10,000, the bound `bucketRange` puts on a walk.
* 10,000 days is 27 years; a larger count is refused rather than walked.
*/
export declare const MAX_PERIOD_COUNT = 10000;
/** A status list read once, as the set a run is tested against. */
export type StatusSet = ReadonlySet<string>;
/** A `Period`, resolved. */
export type ResolvedPeriod = {
kind: "elapsed";
length: bigint;
} | {
kind: "units";
unit: "operatorDay" | CalendarUnit;
count: number;
};
/** A `Reset`, resolved. */
export type ResolvedReset = {
statuses: StatusSet;
consecutive: bigint;
};
/** A `PairSpec`, resolved. */
export type ResolvedPair = {
each: bigint;
one: bigint;
combined: bigint;
};
/** A `LocalWindow`, resolved: canonical wall times and whether it wraps past midnight. */
export type ResolvedLocalWindow = {
from: Temporal.PlainTime;
to: Temporal.PlainTime;
wraps: boolean;
};
type Common = {
label: string;
severity: DutySeverity;
};
/** The kind-specific part of a resolved rule. */
type ResolvedRuleBody = {
kind: "cumulativeCap";
statuses: StatusSet;
cap: bigint;
since: ResolvedReset;
} | {
kind: "elapsedWindow";
startStatuses: StatusSet;
length: bigint;
restrictedStatuses: StatusSet;
since: ResolvedReset;
exclude: ResolvedPair | null;
extension: {
by: bigint;
from: bigint;
} | null;
} | {
kind: "requiredInterruption";
countedStatuses: StatusSet;
after: bigint;
minimum: bigint;
outsideStatuses: StatusSet | null;
orderedSplit: bigint[] | null;
} | {
kind: "rollingCap";
statuses: StatusSet;
cap: bigint;
window: ResolvedPeriod;
reset: ResolvedReset | null;
precondition: {
restStatuses: StatusSet;
minimum: bigint;
beforeHours: bigint;
} | null;
} | {
kind: "fixedPeriodCap";
statuses: StatusSet;
cap: bigint;
period: Extract<ResolvedPeriod, {
kind: "units";
}>;
adjacent: number;
average: boolean;
} | {
kind: "restRequirement";
restStatuses: StatusSet;
per: ResolvedPeriod;
minimum: bigint;
composition: {
maxPeriods: number;
longestAtLeast: bigint;
othersAtLeast: bigint;
} | null;
composite: {
consecutive: bigint;
additional: bigint;
blockMinimum: bigint;
} | null;
maxGap: bigint | null;
} | {
kind: "freePeriod";
restStatuses: StatusSet;
minimum: bigint;
within: ResolvedPeriod;
containing: {
window: ResolvedLocalWindow;
count: number;
} | null;
atLeast: number;
} | {
kind: "restBeforeDuty";
restStatuses: StatusSet;
minimum: bigint;
within: bigint;
orFactor: {
k: DecimalRatio;
floor: bigint;
} | null;
} | {
kind: "quota";
statuses: StatusSet;
minimum: bigint;
maximum: bigint;
count: number;
reset: ResolvedReset | {
every: ResolvedPeriod;
};
} | {
kind: "dueBy";
statuses: StatusSet;
minimum: bigint;
periods: number;
period: ResolvedPeriod;
reset: ResolvedReset | null;
} | {
kind: "pattern";
period: ResolvedPeriod;
lookback: number;
classes: {
label: string;
statuses: StatusSet;
minimum: bigint;
}[];
require: {
label: string;
atLeast: number;
}[];
} | {
kind: "occurrenceCap";
statuses: StatusSet;
per: Extract<ResolvedPeriod, {
kind: "units";
}>;
lookback: number;
maximum: number;
byInitiationDate: boolean;
} | {
kind: "usageLookback";
/** Ascending. */
uses: bigint[];
notWithin: ResolvedPeriod;
reset: ResolvedReset;
};
/** A `DutyRule`, validated once and reduced to what evaluation reads. */
export type ResolvedRule = Common & ResolvedRuleBody;
/** A positive number as the exact fraction its decimal digits name. */
export type DecimalRatio = {
numerator: bigint;
denominator: bigint;
};
/**
* Read a `PairSpec`: three positive exact durations.
*
* @param value candidate `{ eachAtLeast, oneAtLeast, combinedAtLeast }`
* @returns the floors, or null when `value` is not a valid `PairSpec`
*/
export declare function parsePairSpec(value: unknown): ResolvedPair | null;
/**
* Whether a resolved rule counts operator days anywhere, and so needs `dayStartTime`.
*/
export declare function usesOperatorDays(rule: ResolvedRule): boolean;
/**
* Validate and resolve a list of `DutyRule`s, all-or-nothing.
*
* - Every field is read once, with an ordinary property get. Labels must be strings and unique.
* - Durations are exact (`exactDurationNanoseconds`) and positive; counts of periods are integers
* from 1 to `MAX_PERIOD_COUNT`; other counts are non-negative integers.
* - A status list is a non-empty array of strings.
*
* @param value candidate array of rules
* @returns the resolved rules in order, or null when any is malformed
*
* @example parseDutyRules([]) // []
* @example parseDutyRules([{ label: "x", kind: "cumulativeCap", statuses: [], cap: "PT11H", since: { statuses: ["off"], consecutive: "PT10H" } }]) // null — no status to count
*/
export declare function parseDutyRules(value: unknown): ResolvedRule[] | null;
export {};