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.

52 lines (50 loc) • 2.13 kB
import { isObject } from "./isObject.js"; // src/internal/maxPieces.ts /** * Default `maxPieces` for functions that return one array element per generated piece * (`splitIntervalByUnit*`, `intervalDivideEqually*`, `mapDatesInRange`, `mapZonedDatesInRange`). * * Owner decision A2 (CORE-8): an output that would be larger returns the sentinel `[]` rather * than exhausting the heap, which aborts the process where no caller can catch it. */ const DEFAULT_MAX_PIECES = 1e6; /** ECMA-262 §10.4.2 (Array exotic objects): an array length is at most 2^32 - 1. */ const MAX_ARRAY_LENGTH = 2 ** 32 - 1; /** * Read the `maxPieces` option. * * - Omitted options, or an omitted/`undefined` `maxPieces`, give `DEFAULT_MAX_PIECES` * (Temporal GetOption reads `undefined` as absent). * - A non-object options argument returns `null` (Temporal GetOptionsObject throws `TypeError`). * - `maxPieces` must be a positive safe integer, otherwise `null`. * * @param options the caller's options argument * @returns the piece limit, or `null` when the option is invalid * * @example resolveMaxPieces(undefined) // 1000000 * @example resolveMaxPieces({ maxPieces: 10 }) // 10 * @example resolveMaxPieces({ maxPieces: 0 }) // null */ function resolveMaxPieces(options) { if (options === void 0) return DEFAULT_MAX_PIECES; if (!isObject(options)) return null; const { maxPieces } = options; if (maxPieces === void 0) return DEFAULT_MAX_PIECES; return Number.isSafeInteger(maxPieces) && maxPieces > 0 ? maxPieces : null; } /** * Whether an output of `count` pieces is over the limit. * * - Over `maxPieces`, or over the longest array a JavaScript engine can hold. * * @param count number of pieces the output would hold * @param maxPieces resolved limit from `resolveMaxPieces` * @returns `true` when the function must return its sentinel instead * * @example exceedsPieceLimit(11, 10) // true * @example exceedsPieceLimit(2 ** 32, 2 ** 40) // true */ function exceedsPieceLimit(count, maxPieces) { return count > maxPieces || count > MAX_ARRAY_LENGTH; } export { DEFAULT_MAX_PIECES, MAX_ARRAY_LENGTH, exceedsPieceLimit, resolveMaxPieces };