UNPKG

@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.

95 lines • 5.99 kB
import { Temporal } from "./temporal.js"; import { Overflow } from "../types/overflow.js"; import "../types/index.js"; /** The date fields `setDate` takes. */ export declare const DATE_FIELDS: readonly ["year", "month", "monthCode", "day"]; /** The time fields `setTime` takes. */ export declare const TIME_FIELDS: readonly ["hour", "minute", "second", "millisecond", "microsecond", "nanosecond"]; /** The fields `setDateTime`, `setUtc`, `setUnix` and `setZoned` take. */ export declare const DATE_TIME_FIELDS: readonly ["year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"]; /** The one field whose value is a string; every other field a `set*` function takes is a number. */ declare const STRING_FIELD = "monthCode"; /** A field a `set*` function may take. */ type SetField = (typeof DATE_TIME_FIELDS)[number]; /** The checked values of the fields in `Field`: `monthCode` a string, every other an integer. */ export type SetFields<Field extends SetField> = { [Name in Field]?: Name extends typeof STRING_FIELD ? string : number; }; /** * Read the `fields` argument of a `set*` function the way Temporal's `with` reads it, and return * the values as a fresh plain object that holds only the fields the function takes. * * The check and the read are one pass, so nothing reaches Temporal that was not checked: * * - `fields` must be an Object (`isObject`), a function included, and not a Temporal object that * holds a date or a time (TC39 IsPartialTemporalObject). * - An own enumerable string key outside `allowedFields` returns `null`, whatever its value: the guard * against a misspelt key. A symbol key is ignored. * - Each property `with` reads is read once with an ordinary property get, in the order `with` * reads them, so an inherited, non-enumerable or getter field applies. `undefined` means absent. * A defined property outside `allowedFields` (another type's field, an era, an offset, a * `calendar` or a `timeZone`) returns `null`. * - A value is not converted: `monthCode` must be a string, and every other field a number that is * an integer. `null`, a numeric string, a boolean and a fraction return `null`, where Temporal * would convert or truncate them. A bigint, `NaN` and an infinity return `null` too; Temporal * throws for them (ToIntegerWithTruncation). * * Range and overflow are left to Temporal. An empty result means nothing is set: the function * returns its value unchanged, where Temporal's `with` would throw. A getter that throws is not * caught here, nor is a Proxy's `getPrototypeOf` trap that throws; the calling function's own catch * returns its sentinel. * * @param fields the caller's `fields` argument * @param allowedFields the fields the calling function takes * @returns a new plain object of the fields set, empty when none is, or null on invalid input * * @example readSetFields({ year: 2025 }, DATE_FIELDS) // { year: 2025 } * @example readSetFields({}, DATE_FIELDS) // {} (nothing set) * @example readSetFields({ year: "2025" }, DATE_FIELDS) // null (not converted) * @example readSetFields({ year: 2025, yaer: 1 }, DATE_FIELDS) // null (misspelt key) * @example readSetFields({ year: 2025, hour: 5 }, DATE_FIELDS) // null (a field setDate does not take) */ export declare function readSetFields<Field extends SetField>(fields: unknown, allowedFields: readonly Field[]): SetFields<Field> | null; /** * The `setDate` core: a `PlainDate` with the checked `fields` set. * * Only values `readSetFields` checked reach Temporal. An empty set changes nothing, where * Temporal's `with()` would throw. May throw where Temporal does; each caller maps that to its * sentinel. * * @param date the date to change * @param fields the caller's `fields` argument * @param overflow Temporal `overflow`, or a function that reads it; the function runs only once `fields` is * valid and not empty, so an option that throws is never read for a value that does not need it * @returns the changed date, or `null` when `fields` is invalid * * @example withDateFields(Temporal.PlainDate.from("2024-03-10"), { day: 15 }, "constrain")?.toString() // "2024-03-15" */ export declare function withDateFields(date: Temporal.PlainDate, fields: unknown, overflow: Overflow | (() => Overflow)): Temporal.PlainDate | null; /** * The `setDateTime` core: a `PlainDateTime` with the checked `fields` set. * * May throw where Temporal does; each caller maps that to its sentinel. * * @param dateTime the date-time to change * @param fields the caller's `fields` argument * @param overflow Temporal `overflow`, or a function that reads it; the function runs only once `fields` is * valid and not empty, so an option that throws is never read for a value that does not need it * @returns the changed date-time, or `null` when `fields` is invalid * * @example withDateTimeFields(Temporal.PlainDateTime.from("2024-03-10T08:00"), { hour: 9 }, "constrain")?.toString() // "2024-03-10T09:00:00" */ export declare function withDateTimeFields(dateTime: Temporal.PlainDateTime, fields: unknown, overflow: Overflow | (() => Overflow)): Temporal.PlainDateTime | null; /** * The `setTime` core: a `PlainTime` with the checked `fields` set. * * May throw where Temporal does; each caller maps that to its sentinel. * * @param time the time to change * @param fields the caller's `fields` argument * @param overflow Temporal `overflow`, or a function that reads it; the function runs only once `fields` is * valid and not empty, so an option that throws is never read for a value that does not need it * @returns the changed time, or `null` when `fields` is invalid * * @example withTimeFields(Temporal.PlainTime.from("08:00"), { minute: 30 }, "constrain")?.toString() // "08:30:00" */ export declare function withTimeFields(time: Temporal.PlainTime, fields: unknown, overflow: Overflow | (() => Overflow)): Temporal.PlainTime | null; export {};