/* eslint-disable eslint-comments/no-unlimited-disable */ /* eslint-disable */ // @ts-nocheck import { normalizeInterval } from '../_lib/normalizeInterval/index.ts'; import { constructFrom } from '../constructFrom/index.ts'; import type { ContextOptions, Interval, StepOptions } from '../types.ts'; /** * The {@link eachDayOfInterval} function options. */ export interface EachDayOfIntervalOptions extends StepOptions, ContextOptions {} /** * The {@link eachDayOfInterval} function result type. It resolves the proper data type. * It uses the first argument date object type, starting from the date argument, * then the start interval date, and finally the end interval date. If * a context function is passed, it uses the context function return type. */ export type EachDayOfIntervalResult< IntervalType extends Interval, Options extends EachDayOfIntervalOptions | undefined, > = Array< Options extends EachDayOfIntervalOptions ? DateType : IntervalType['start'] extends Date ? IntervalType['start'] : IntervalType['end'] extends Date ? IntervalType['end'] : Date >; /** * @name eachDayOfInterval * @category Interval Helpers * @summary Return the array of dates within the specified time interval. * * @description * Return the array of dates within the specified time interval. * * @typeParam IntervalType - Interval type. * @typeParam Options - Options type. * * @param interval - The interval. * @param options - An object with options. * * @returns The array with starts of days from the day of the interval start to the day of the interval end * * @example * // Each day between 6 October 2014 and 10 October 2014: * const result = eachDayOfInterval({ * start: new Date(2014, 9, 6), * end: new Date(2014, 9, 10) * }) * //=> [ * // Mon Oct 06 2014 00:00:00, * // Tue Oct 07 2014 00:00:00, * // Wed Oct 08 2014 00:00:00, * // Thu Oct 09 2014 00:00:00, * // Fri Oct 10 2014 00:00:00 * // ] */ export function eachDayOfInterval< IntervalType extends Interval, Options extends EachDayOfIntervalOptions | undefined = undefined, >(interval: IntervalType, options?: Options): EachDayOfIntervalResult { const { start, end } = normalizeInterval(options?.in, interval); let reversed = +start > +end; const endTime = reversed ? +start : +end; const date = reversed ? end : start; date.setHours(0, 0, 0, 0); let step = options?.step ?? 1; if (!step) return []; if (step < 0) { step = -step; reversed = !reversed; } const dates: EachDayOfIntervalResult = []; while (+date <= endTime) { dates.push(constructFrom(start, date)); date.setDate(date.getDate() + step); date.setHours(0, 0, 0, 0); } return reversed ? dates.reverse() : dates; } /* eslint-enable */