@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.
28 lines • 2.01 kB
TypeScript
/**
* Read a caller's `Intl.DateTimeFormat` options once, into a plain object of primitives.
*
* - A formatter that inspects the options before it hands them to `Intl.DateTimeFormat` would
* otherwise read each one several times, and a getter could answer one value to the check and
* another to the formatter. ECMA-402 CreateDateTimeFormat reads each option with one `Get`
* (GetOption); this does the same, in the same order, and everything after works on the copy.
* - Each value is checked once, right after its `Get`, and never converted: a string option must be a string, `fractionalSecondDigits` a number and `hour12` a boolean; any
* other value throws `TypeError` and nothing is converted. The range and the allowed values are
* still checked by `Intl.DateTimeFormat`. `timeZone` alone is copied unchecked, for the
* caller's own zone rule.
* - The object is coerced as ECMA-402 CoerceOptionsToObject coerces it: `undefined` is no options,
* `null` throws TypeError, and a string or a number has none of these properties. Inherited
* properties are read, as `Get` reads them.
* - Only the options `Intl.DateTimeFormat` reads are copied, and only those that are not
* `undefined`. Any other key was never read by the formatter, so dropping it changes nothing.
*
* @param options the caller's options argument
* @returns a plain object holding each option that was set; throws TypeError for `null`, or for
* a value of the wrong type, for the caller's sentinel
*
* @example readDateTimeFormatOptions({ dateStyle: "long", extra: 1 }) // { dateStyle: "long" }
* @example readDateTimeFormatOptions({ month: { toString: () => "long" } }) // throws TypeError
* @example readDateTimeFormatOptions(undefined) // {}
* @example readDateTimeFormatOptions("x") // {} (a string has no date-time options)
* @example readDateTimeFormatOptions(null) // throws TypeError
*/
export declare function readDateTimeFormatOptions(options: unknown): Intl.DateTimeFormatOptions;