@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.
160 lines • 8.22 kB
TypeScript
/**
* 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
*/
export declare function isIntlOptionValue(name: string, value: unknown): boolean;
/**
* 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
*/
export declare function hasTypedTemporalOptions(options: object): boolean;
/**
* 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
*/
export declare function booleanOption(value: unknown, fallback: boolean): boolean;
/**
* 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
*/
export declare function stringOption<T extends string>(value: T | undefined): T | undefined;
/**
* {@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
*/
export declare function stringOptionOrDefault<T extends string>(value: T | undefined, fallback: T): T;
/**
* 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
*/
export declare function numberOption(value: number | undefined): number | undefined;
/**
* 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
*/
export declare function fractionalDigitsOption<T extends number | "auto">(value: T | undefined): T | undefined;
/**
* {@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
*/
export declare function fractionalDigitsOptionOrDefault<T extends number | "auto">(value: T | undefined, fallback: T): T;
/**
* 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
*/
export declare function roundingMembers<M extends string>(options: {
roundingIncrement?: number;
roundingMode?: M;
} | undefined): {
roundingIncrement: number | undefined;
roundingMode: M | undefined;
};
/**
* {@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
*/
export declare function roundingOptionsOf<U extends string, M extends string>(options: {
smallestUnit?: U;
roundingIncrement?: number;
roundingMode?: M;
} | undefined): {
smallestUnit: U | undefined;
roundingIncrement: number | undefined;
roundingMode: M | undefined;
};