@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.
70 lines • 3.77 kB
TypeScript
import { LocalesArgument } from "./resolveLocale.js";
/**
* Shared token-pattern engine for J11's `parse*WithPattern` family.
*
* This is GMT's one named exception to `context/coding-standards/string-grammars.md`'s
* "manual string parsing" prohibition (its section "Scoped exception:
* manual string parsing"). The exception is scoped to this module
* and the three `parse*WithPattern` public functions, and is bound by
* three rules, enforced throughout this file:
* 1. The regex is always built *from the pattern string itself* at call
* time (see `compilePattern`) — never hand-rolled per-format string
* slicing.
* 2. Extracted fields are handed to the caller for `Temporal.*.from(...,
* { overflow: "reject" })` — a regex match alone only proves *shape*,
* never validity (see each `parse*WithPattern` wrapper).
* 3. Never throws; malformed pattern, no match, or invalid input all
* resolve to `null` here (the public wrappers translate that to
* `""`).
*/
/** Field categories a pattern token can produce. Each may appear at most once per pattern. */
export type PatternField = "year" | "month" | "day" | "hour" | "minute" | "second" | "millisecond" | "weekday" | "meridiem" | "era";
/** Fields accepted by `parseDateWithPattern` — date-shaped tokens only. */
export declare const DATE_PATTERN_FIELDS: ReadonlySet<PatternField>;
/** Fields accepted by `parseTimeWithPattern` — time-shaped tokens only. */
export declare const TIME_PATTERN_FIELDS: ReadonlySet<PatternField>;
/** Fields accepted by `parseDateTimeWithPattern` — the full combined set. */
export declare const DATE_TIME_PATTERN_FIELDS: ReadonlySet<PatternField>;
/**
* Fields accepted by `parseMonthDayWithPattern`: the month and the day.
*
* There is no year, weekday or era. A weekday and an era have no meaning without a year
* (Temporal `PlainMonthDay` keeps only a month, a day and a calendar), and a time field has
* no place in a month-day.
*/
export declare const MONTH_DAY_PATTERN_FIELDS: ReadonlySet<PatternField>;
/**
* Fields accepted by `parseYearMonthWithPattern`: the year, the month and the era.
*
* No day, weekday or time: Temporal `PlainYearMonth` keeps a year, a month and a calendar. An era
* is kept because it qualifies the year (`GG` with BC gives year `1 - y`, as the date parser does).
*/
export declare const YEAR_MONTH_PATTERN_FIELDS: ReadonlySet<PatternField>;
/** Resolved output of a successful parse — only fields the pattern actually contained are set. */
export interface ParsedPatternFields {
year?: number;
month?: number;
day?: number;
hour?: number;
minute?: number;
second?: number;
millisecond?: number;
}
/**
* Parse `value` against `pattern` and return the resolved candidate
* fields, or `null` on malformed pattern / no match / invalid input.
*
* `null` here always means "return the sentinel" to the caller — it
* never distinguishes malformed-pattern from no-match from invalid-input
* from a `yy` token with no window, matching the shared never-throw /
* sentinel-return contract.
*
* `options` is the caller's `TwoDigitYearOptions` bag, already accepted
* by `isOptionsArgument`. `yearWindow` is read once, once the value matches, and a value
* the types reject (not `undefined`, `"rolling"` or a number) returns
* `null` even when the pattern has no `yy`. Only a matched `yy` token
* resolves it, so a pattern without `yy` never reads the clock. A bag that
* throws when read makes this function throw; every caller runs it inside
* `try`.
*/
export declare function parseValueWithPattern(value: string, pattern: string, locale: LocalesArgument | undefined, allowedFields: ReadonlySet<PatternField>, options?: unknown): ParsedPatternFields | null;