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.

26 lines (24 loc) • 1.06 kB
import { withZonedFields } from "./zonedWallClockOperations.js"; import { DATE_TIME_FIELDS, readSetFields } from "./readSetFields.js"; // src/internal/setZonedFields.ts /** * The `setZoned` core: a `ZonedDateTime` with the checked `fields` set, correct at the range * limits on both paths. * * An empty set changes nothing, where Temporal's `with()` would throw. May throw where Temporal * does; each caller maps that to its sentinel. * * @param zoned the value to change * @param fields the caller's `fields` argument * @param options the resolved `overflow`, `disambiguation` and `offset` * @returns the changed value, or `null` when `fields` is invalid * * @example setZonedFields(zoned, { hour: 9 }, { overflow: "constrain", disambiguation: "compatible", offset: "prefer" })?.toString() */ function setZonedFields(zoned, fields, options) { const set = readSetFields(fields, DATE_TIME_FIELDS); if (set === null) return null; if (Object.keys(set).length === 0) return zoned; return withZonedFields(zoned, set, options); } export { setZonedFields };