eachDayOfInterval.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. exports.eachDayOfInterval = eachDayOfInterval;
  3. var _index = require("./_lib/normalizeInterval.cjs");
  4. var _index2 = require("./constructFrom.cjs");
  5. /**
  6. * The {@link eachDayOfInterval} function options.
  7. */
  8. /**
  9. * The {@link eachDayOfInterval} function result type. It resolves the proper data type.
  10. * It uses the first argument date object type, starting from the date argument,
  11. * then the start interval date, and finally the end interval date. If
  12. * a context function is passed, it uses the context function return type.
  13. */
  14. /**
  15. * @name eachDayOfInterval
  16. * @category Interval Helpers
  17. * @summary Return the array of dates within the specified time interval.
  18. *
  19. * @description
  20. * Return the array of dates within the specified time interval.
  21. *
  22. * @typeParam IntervalType - Interval type.
  23. * @typeParam Options - Options type.
  24. *
  25. * @param interval - The interval.
  26. * @param options - An object with options.
  27. *
  28. * @returns The array with starts of days from the day of the interval start to the day of the interval end
  29. *
  30. * @example
  31. * // Each day between 6 October 2014 and 10 October 2014:
  32. * const result = eachDayOfInterval({
  33. * start: new Date(2014, 9, 6),
  34. * end: new Date(2014, 9, 10)
  35. * })
  36. * //=> [
  37. * // Mon Oct 06 2014 00:00:00,
  38. * // Tue Oct 07 2014 00:00:00,
  39. * // Wed Oct 08 2014 00:00:00,
  40. * // Thu Oct 09 2014 00:00:00,
  41. * // Fri Oct 10 2014 00:00:00
  42. * // ]
  43. */
  44. function eachDayOfInterval(interval, options) {
  45. const { start, end } = (0, _index.normalizeInterval)(options?.in, interval);
  46. let reversed = +start > +end;
  47. const endTime = reversed ? +start : +end;
  48. const date = reversed ? end : start;
  49. date.setHours(0, 0, 0, 0);
  50. let step = options?.step ?? 1;
  51. if (!step) return [];
  52. if (step < 0) {
  53. step = -step;
  54. reversed = !reversed;
  55. }
  56. const dates = [];
  57. while (+date <= endTime) {
  58. dates.push((0, _index2.constructFrom)(start, date));
  59. date.setDate(date.getDate() + step);
  60. date.setHours(0, 0, 0, 0);
  61. }
  62. return reversed ? dates.reverse() : dates;
  63. }