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.

48 lines • 3.34 kB
/** 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; export {};