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.

60 lines (58 loc) • 3.39 kB
import { Temporal } from "../../internal/temporal.js"; import { isValidDate } from "../validate/isValidDate.js"; import { exceedsPieceLimit, resolveMaxPieces } from "../../internal/maxPieces.js"; // src/plain/map/mapDatesInRange.ts /** * Return an array of ISO PlainDate strings between `startDate` and `endDate` inclusive. * * - Generates dates with optional step (default 1 day). * - Returns [] if start > end, invalid inputs, or step <= 0. * - An explicit `undefined` `stepDays` is the default step, so `(start, end, undefined, options)` * reaches `options`. **Compatibility:** before 1.16.0 it returned `[]`. * - **End-inclusive, where the `interval*` functions are half-open.** This enumerates the dates a * range covers, so `endDate` is one of them; an interval bounds a span as `[start, end)`, so its * `end` is not. The same pair therefore yields one more date here than `intervalCountDate` * counts. To read one set of bounds both ways, pass `addDate(endDate, { days: 1 })` to the * `interval*` call, or `subtractDate(endDate, { days: 1 })` to this one. * * @param startDate ISO PlainDate string for the first date * @param endDate ISO PlainDate string for the last date (inclusive) * @param stepDays optional number of days to step between results * @param options The limit on the size of the result * @returns array of ISO PlainDate strings, or [] on invalid input * * @example mapDatesInRange("2024-03-01", "2024-03-05") // ["2024-03-01", "2024-03-02", "2024-03-03", "2024-03-04", "2024-03-05"] * @example mapDatesInRange("2024-03-01", "2024-03-05", 2) // ["2024-03-01", "2024-03-03", "2024-03-05"] * @example mapDatesInRange("+275760-09-11", "+275760-09-13") // ["+275760-09-11", "+275760-09-12", "+275760-09-13"] (a range ending on the last representable date) * @example mapDatesInRange("2024-03-05", "2024-03-01") // [] * @example mapDatesInRange("invalid", "2024-03-05") // [] * @example mapDatesInRange("2024-03-01", "invalid") // [] * @example mapDatesInRange("2024-03-01", "2024-03-03", undefined) // ["2024-03-01", "2024-03-02", "2024-03-03"] (undefined is the default step) * @example mapDatesInRange("2024-03-01", "2024-03-05", 0) // [] * @example mapDatesInRange("2024-03-01", "2024-03-05", 1, { maxPieces: 3 }) // [] (more dates than the limit) * @example mapDatesInRange("2024-03-01", "2024-03-05", 1, { maxPieces: 10 }) // ["2024-03-01", "2024-03-02", "2024-03-03", "2024-03-04", "2024-03-05"] */ function mapDatesInRange(startDate, endDate, stepDays, options) { try { const resolvedStepDays = stepDays === void 0 ? 1 : stepDays; if (typeof resolvedStepDays !== "number" || !Number.isInteger(resolvedStepDays) || resolvedStepDays <= 0) return []; const maxPieces = resolveMaxPieces(options); if (maxPieces === null) return []; if (!isValidDate(startDate) || !isValidDate(endDate)) return []; try { const start = Temporal.PlainDate.from(startDate); const end = Temporal.PlainDate.from(endDate); if (Temporal.PlainDate.compare(start, end) === 1) return []; const count = Math.floor(start.until(end).days / resolvedStepDays) + 1; if (exceedsPieceLimit(count, maxPieces)) return []; const result = []; for (let index = 0; index < count; index++) result.push(start.add({ days: index * resolvedStepDays }).toString()); return result; } catch { return []; } } catch { return []; } } export { mapDatesInRange };