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.

30 lines • 2.17 kB
/** * 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 converted once as well, right after its `Get`, as GetOption converts it: * ToString for a string option, ToNumber for `fractionalSecondDigits`, ToBoolean for `hour12`. * An option given as an object would otherwise be asked for its value by every * `Intl.DateTimeFormat` built from the copy, and could answer each one differently. The range * and the allowed values are still checked by `Intl.DateTimeFormat`. `timeZone` alone is copied * unconverted, 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, converted to its primitive; throws * TypeError for `null`, or for a value that cannot be converted, for the caller's sentinel * * @example readDateTimeFormatOptions({ dateStyle: "long", extra: 1 }) // { dateStyle: "long" } * @example readDateTimeFormatOptions({ month: { toString: () => "long" } }) // { month: "long" } * @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;