@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
46 lines (45 loc) • 2.44 kB
TypeScript
/**
* Shared token-pattern engine for J11's `parse*WithPattern` family.
*
* This is GMT's one named exception to `context/coding-standards.md`'s
* "manual string parsing" prohibition (Decision 4 in
* `context/roadmap/issues/J.md`). 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>;
/** 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,
* matching the shared never-throw / sentinel-return contract.
*/
export declare function parseValueWithPattern(value: string, pattern: string, locale: string | undefined, allowedFields: ReadonlySet<PatternField>): ParsedPatternFields | null;