@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.
23 lines (22 loc) • 1.09 kB
JavaScript
// src/internal/optionOrDefault.ts
/**
* The value of an option that was read once, or its default when the option is absent.
*
* - ECMA-402 `GetOption` (the Temporal specification defines the same operation) reads the
* property with one `Get` and returns the default only when that value is `undefined`. Pass
* the property read as the argument, so the option is read exactly once: a getter cannot
* answer one value to the check and another to the use.
* - `null` is a value, not an omission, so it is returned as it is for the caller to validate.
*
* @param value the option as read from the caller's object, `undefined` when absent
* @param fallback the documented default
* @returns `fallback` when `value` is `undefined`, otherwise `value`
*
* @example optionOrDefault(undefined, "compatible") // "compatible"
* @example optionOrDefault("later", "compatible") // "later"
* @example optionOrDefault(null, true) // null (a value for the caller to validate)
*/
function optionOrDefault(value, fallback) {
return value === void 0 ? fallback : value;
}
export { optionOrDefault };