index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = eachDayOfInterval;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. /**
  10. * @name eachDayOfInterval
  11. * @category Interval Helpers
  12. * @summary Return the array of dates within the specified time interval.
  13. *
  14. * @description
  15. * Return the array of dates within the specified time interval.
  16. *
  17. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  18. * @param {Object} [options] - an object with options.
  19. * @param {Number} [options.step=1] - the step to increment by. The value should be more than 1.
  20. * @returns {Date[]} the array with starts of days from the day of the interval start to the day of the interval end
  21. * @throws {TypeError} 1 argument required
  22. * @throws {RangeError} `options.step` must be a number greater than 1
  23. * @throws {RangeError} The start of an interval cannot be after its end
  24. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  25. *
  26. * @example
  27. * // Each day between 6 October 2014 and 10 October 2014:
  28. * const result = eachDayOfInterval({
  29. * start: new Date(2014, 9, 6),
  30. * end: new Date(2014, 9, 10)
  31. * })
  32. * //=> [
  33. * // Mon Oct 06 2014 00:00:00,
  34. * // Tue Oct 07 2014 00:00:00,
  35. * // Wed Oct 08 2014 00:00:00,
  36. * // Thu Oct 09 2014 00:00:00,
  37. * // Fri Oct 10 2014 00:00:00
  38. * // ]
  39. */
  40. function eachDayOfInterval(dirtyInterval, options) {
  41. var _options$step;
  42. (0, _index2.default)(1, arguments);
  43. var interval = dirtyInterval || {};
  44. var startDate = (0, _index.default)(interval.start);
  45. var endDate = (0, _index.default)(interval.end);
  46. var endTime = endDate.getTime();
  47. // Throw an exception if start date is after end date or if any date is `Invalid Date`
  48. if (!(startDate.getTime() <= endTime)) {
  49. throw new RangeError('Invalid interval');
  50. }
  51. var dates = [];
  52. var currentDate = startDate;
  53. currentDate.setHours(0, 0, 0, 0);
  54. var step = Number((_options$step = options === null || options === void 0 ? void 0 : options.step) !== null && _options$step !== void 0 ? _options$step : 1);
  55. if (step < 1 || isNaN(step)) throw new RangeError('`options.step` must be a number greater than 1');
  56. while (currentDate.getTime() <= endTime) {
  57. dates.push((0, _index.default)(currentDate));
  58. currentDate.setDate(currentDate.getDate() + step);
  59. currentDate.setHours(0, 0, 0, 0);
  60. }
  61. return dates;
  62. }
  63. module.exports = exports.default;