MCPcopy
hub / github.com/date-fns/date-fns / eachYearOfInterval

Function eachYearOfInterval

pkgs/core/src/eachYearOfInterval/index.ts:59–89  ·  view source on GitHub ↗
(
  interval: IntervalType,
  options?: Options,
)

Source from the content-addressed store, hash-verified

57 * // ]
58 */
59export function eachYearOfInterval<
60 IntervalType extends Interval,
61 Options extends EachYearOfIntervalOptions | undefined = undefined,
62>(
63 interval: IntervalType,
64 options?: Options,
65): EachYearOfIntervalResult<IntervalType, Options> {
66 const { start, end } = normalizeInterval(options?.in, interval);
67
68 let reversed = +start > +end;
69 const endTime = reversed ? +start : +end;
70 const date = reversed ? end : start;
71 date.setHours(0, 0, 0, 0);
72 date.setMonth(0, 1);
73
74 let step = options?.step ?? 1;
75 if (!step) return [];
76 if (step < 0) {
77 step = -step;
78 reversed = !reversed;
79 }
80
81 const dates: EachYearOfIntervalResult<IntervalType, Options> = [];
82
83 while (+date <= endTime) {
84 dates.push(constructFrom(start, date));
85 date.setFullYear(date.getFullYear() + step);
86 }
87
88 return reversed ? dates.reverse() : dates;
89}

Callers 2

test.tsFile · 0.90
_testFunction · 0.90

Calls 2

normalizeIntervalFunction · 0.90
constructFromFunction · 0.90

Tested by

no test coverage detected