@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.
33 lines (31 loc) • 1.51 kB
JavaScript
import { isIntlOptionValue } from "./typedOptions.js";
// src/internal/plainOptionEntries.ts
/**
* The own enumerable options as `[name, value]` pairs, read through property descriptors so a
* getter is never run, or `null` when one is an accessor or a number `JSON` cannot key (not
* finite). A value of a type the option is not declared with (a boxed string, an object, a
* string for `hour12`, a string for a digit count) throws `TypeError`, which each caller turns
* into its sentinel: `Intl` would convert it. An `undefined` value is skipped, as `Intl` reads it
* as absent.
*
* @param options the options object to read
* @returns the pairs, or `null` when the options cannot be keyed safely
*
* @example plainOptionEntries({ year: "numeric", day: undefined }) // [["year", "numeric"]]
* @example plainOptionEntries({ get year() { return "numeric"; } }) // null
* @example plainOptionEntries({ hour12: "yes" }) // throws TypeError
*/
function plainOptionEntries(options) {
const entries = [];
for (const name of Object.keys(options)) {
const descriptor = Object.getOwnPropertyDescriptor(options, name);
if (descriptor === void 0 || !("value" in descriptor)) return null;
const value = descriptor.value;
if (value === void 0) continue;
if (!isIntlOptionValue(name, value)) throw new TypeError(`option ${name} has the wrong type`);
if (typeof value === "number" && !Number.isFinite(value)) return null;
entries.push([name, value]);
}
return entries;
}
export { plainOptionEntries };