@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.
231 lines (230 loc) • 10.7 kB
JavaScript
// src/internal/typedOptions.ts
/** Intl options typed `boolean` (ECMA-402 GetOption type "boolean"). */
const BOOLEAN_INTL_OPTIONS = /* @__PURE__ */ new Set(["hour12"]);
/** Intl options typed `number` (ECMA-402 GetNumberOption). */
const NUMBER_INTL_OPTIONS = /* @__PURE__ */ new Set([
"fractionalSecondDigits",
"minimumIntegerDigits",
"minimumFractionDigits",
"maximumFractionDigits",
"minimumSignificantDigits",
"maximumSignificantDigits",
"roundingIncrement"
]);
/** Intl options typed `boolean | string` (`NumberFormat` `useGrouping`). */
const BOOLEAN_OR_STRING_INTL_OPTIONS = /* @__PURE__ */ new Set(["useGrouping"]);
/**
* Whether `value` has the type an `Intl` formatter option of this name is declared with: `boolean`
* for `hour12`, `number` for the digit counts and `roundingIncrement`, `boolean | string` for
* `useGrouping`, and `string` for every other option. `undefined` is an absent option.
*
* @param name the option's name
* @param value the caller's value
* @returns whether `Intl` would read `value` without converting it
*
* @example isIntlOptionValue("dateStyle", "long") // true
* @example isIntlOptionValue("dateStyle", new String("long")) // false
* @example isIntlOptionValue("hour12", "yes") // false
* @example isIntlOptionValue("fractionalSecondDigits", 3) // true
*/
function isIntlOptionValue(name, value) {
if (value === void 0) return true;
if (BOOLEAN_INTL_OPTIONS.has(name)) return typeof value === "boolean";
if (NUMBER_INTL_OPTIONS.has(name)) return typeof value === "number";
if (BOOLEAN_OR_STRING_INTL_OPTIONS.has(name)) return typeof value === "boolean" || typeof value === "string";
return typeof value === "string";
}
/** Temporal option names typed `string` (a unit, a mode, an overflow, a disambiguation, an offset). */
const STRING_TEMPORAL_OPTIONS = [
"overflow",
"disambiguation",
"offset",
"roundingMode",
"largestUnit",
"smallestUnit"
];
/**
* The data value of a property, found without running caller code: own or inherited, read from its
* descriptor. `ACCESSOR` marks a getter or a Proxy, which this gate leaves to the function that
* reads the member once (a second read here would give a getter two chances to answer).
*/
const ACCESSOR = Symbol("accessor");
function dataValue(options, name) {
let holder = options;
while (holder !== null) {
const descriptor = Object.getOwnPropertyDescriptor(holder, name);
if (descriptor !== void 0) return "value" in descriptor ? descriptor.value : ACCESSOR;
holder = Object.getPrototypeOf(holder);
}
}
/**
* Whether the Temporal options in a bag have their declared types: `overflow`, `disambiguation`,
* `offset`, `roundingMode`, `largestUnit` and `smallestUnit` are strings, `roundingIncrement` is a
* number, `fractionalSecondDigits` is a number or `"auto"`, and each may be `undefined`. Other members are not read. Members are inspected through
* their property descriptors, so a getter is never run by this check; a getter's one answer is
* left to the readers that take it once (`resolveOverflow`, `stringOption`, `numberOption`,
* `fractionalDigitsOption`).
*
* @param options the caller's options object
* @returns `false` when one of those members holds a value of another type; `true` when none does
* or when the object cannot be inspected without running caller code that throws
*
* @example hasTypedTemporalOptions({ overflow: "reject" }) // true
* @example hasTypedTemporalOptions({ overflow: new String("reject") }) // false
* @example hasTypedTemporalOptions({ roundingIncrement: "5" }) // false
*/
function hasTypedTemporalOptions(options) {
try {
for (const name of STRING_TEMPORAL_OPTIONS) {
const value = dataValue(options, name);
if (value !== void 0 && value !== ACCESSOR && typeof value !== "string") return false;
}
const increment = dataValue(options, "roundingIncrement");
const digits = dataValue(options, "fractionalSecondDigits");
return (increment === void 0 || increment === ACCESSOR || typeof increment === "number") && (digits === void 0 || digits === ACCESSOR || typeof digits === "number" || digits === "auto");
} catch {
return true;
}
}
/**
* A boolean option as the caller wrote it: `fallback` when it is `undefined`, the value when it is
* a boolean, and a `TypeError` for anything else. A flag is read by what it is, never by whether
* it is truthy (`"false"`, `1` and `{}` would all switch it on), so a function returns its
* sentinel for a value the type `boolean` does not name.
*
* @param value the option as read from the caller's object
* @param fallback the documented default
* @returns the flag
* @throws TypeError for a value that is neither `undefined` nor a boolean
*
* @example booleanOption(undefined, true) // true
* @example booleanOption(false, true) // false
* @example booleanOption("false", true) // throws TypeError
*/
function booleanOption(value, fallback) {
if (value === void 0) return fallback;
if (typeof value !== "boolean") throw new TypeError("a flag must be a boolean");
return value;
}
/**
* A string option as the caller wrote it, read once: the value when it is `undefined` or a
* primitive string, a `TypeError` for anything else. Temporal GetOption and ECMA-402 GetOption
* would run ToString on a boxed `String` or an object with a `toString`, so a value the type
* `string` does not name is refused here, before Temporal sees it. Pass the property as it was
* read (`stringOption(options?.roundingMode)`) and hand Temporal the returned local, never the
* caller's object: a getter then has one chance to answer, and it cannot answer differently to
* Temporal.
*
* @param value the option as read from the caller's object
* @returns `value`, typed as the string it is, or `undefined` when absent
* @throws TypeError for a value that is neither `undefined` nor a primitive string
*
* @example stringOption(undefined) // undefined
* @example stringOption("floor") // "floor"
* @example stringOption(new String("floor")) // throws TypeError
*/
function stringOption(value) {
if (value !== void 0 && typeof value !== "string") throw new TypeError("an option typed as a string must be a string");
return value;
}
/**
* {@link stringOption} with the documented default for an absent option.
*
* @param value the option as read from the caller's object
* @param fallback the documented default
* @returns `fallback` when `value` is `undefined`, otherwise the string
* @throws TypeError for a value that is neither `undefined` nor a primitive string
*
* @example stringOptionOrDefault(undefined, "compatible") // "compatible"
* @example stringOptionOrDefault("later", "compatible") // "later"
* @example stringOptionOrDefault(null, "compatible") // throws TypeError
*/
function stringOptionOrDefault(value, fallback) {
return stringOption(value) ?? fallback;
}
/**
* A number option as the caller wrote it, read once: the value when it is `undefined` or a
* primitive number, a `TypeError` for anything else (`"5"`, a `Number` object and `{ valueOf }`
* would all be converted by Temporal's ToNumber).
*
* @param value the option as read from the caller's object
* @returns `value`, typed as the number it is, or `undefined` when absent
* @throws TypeError for a value that is neither `undefined` nor a primitive number
*
* @example numberOption(undefined) // undefined
* @example numberOption(5) // 5
* @example numberOption("5") // throws TypeError
*/
function numberOption(value) {
if (value !== void 0 && typeof value !== "number") throw new TypeError("an option typed as a number must be a number");
return value;
}
/**
* A `fractionalSecondDigits` option as the caller wrote it, read once: `undefined`, a primitive
* number or the primitive string `"auto"` (the members of `FractionalDigit`), and a `TypeError`
* for anything else. Temporal GetOption reads a non-number through ToString, so `"auto"` written
* as a boxed `String` would be accepted and `"3"` would be refused only by luck.
*
* @param value the option as read from the caller's object
* @returns `value`, or `undefined` when absent
* @throws TypeError for a value that is not `undefined`, a primitive number or `"auto"`
*
* @example fractionalDigitsOption(undefined) // undefined
* @example fractionalDigitsOption(3) // 3
* @example fractionalDigitsOption("auto") // "auto"
* @example fractionalDigitsOption(new String("auto")) // throws TypeError
*/
function fractionalDigitsOption(value) {
if (value !== void 0 && typeof value !== "number" && value !== "auto") throw new TypeError("fractionalSecondDigits must be a number or the string auto");
return value;
}
/**
* {@link fractionalDigitsOption} with the documented default for an absent option.
*
* @param value the option as read from the caller's object
* @param fallback the documented default
* @returns `fallback` when `value` is `undefined`, otherwise the digits
* @throws TypeError for a value that is not `undefined`, a primitive number or `"auto"`
*
* @example fractionalDigitsOptionOrDefault(undefined, 9) // 9
* @example fractionalDigitsOptionOrDefault("auto", 9) // "auto"
* @example fractionalDigitsOptionOrDefault("3", 9) // throws TypeError
*/
function fractionalDigitsOptionOrDefault(value, fallback) {
return fractionalDigitsOption(value) ?? fallback;
}
/**
* The rounding members of an options object, each read once and typed: `roundingIncrement` is a
* number and `roundingMode` a string, or `undefined`. Hand Temporal the returned values, never the
* caller's object.
*
* @param options the caller's options object, or `undefined`
* @returns the two members as primitives
* @throws TypeError for a member of another type
*
* @example roundingMembers({ roundingMode: "floor" }) // { roundingIncrement: undefined, roundingMode: "floor" }
* @example roundingMembers({ roundingIncrement: "5" }) // throws TypeError
*/
function roundingMembers(options) {
return {
roundingIncrement: numberOption(options?.roundingIncrement),
roundingMode: stringOption(options?.roundingMode)
};
}
/**
* {@link roundingMembers} plus `smallestUnit`, for a difference or a duration rounding.
*
* @param options the caller's options object, or `undefined`
* @returns the three members as primitives
* @throws TypeError for a member of another type
*
* @example roundingOptionsOf({ smallestUnit: "hours" }) // { smallestUnit: "hours", ... }
* @example roundingOptionsOf({ smallestUnit: new String("hours") }) // throws TypeError
*/
function roundingOptionsOf(options) {
return {
smallestUnit: stringOption(options?.smallestUnit),
...roundingMembers(options)
};
}
export { booleanOption, fractionalDigitsOption, fractionalDigitsOptionOrDefault, hasTypedTemporalOptions, isIntlOptionValue, numberOption, roundingMembers, roundingOptionsOf, stringOption, stringOptionOrDefault };