index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = eachDayOfInterval;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * - The function was renamed from `eachDay` to `eachDayOfInterval`.
  22. * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
  23. *
  24. * ```
  25. * 2.1.3
  26. * time interval
  27. * part of the time axis limited by two instants
  28. * ```
  29. *
  30. * Also, this function now accepts an object with `start` and `end` properties
  31. * instead of two arguments as an interval.
  32. * This function now throws `RangeError` if the start of the interval is after its end
  33. * or if any date in the interval is `Invalid Date`.
  34. *
  35. * ```javascript
  36. * // Before v2.0.0
  37. *
  38. * eachDay(new Date(2014, 0, 10), new Date(2014, 0, 20))
  39. *
  40. * // v2.0.0 onward
  41. *
  42. * eachDayOfInterval(
  43. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }
  44. * )
  45. * ```
  46. *
  47. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  48. * @param {Object} [options] - an object with options.
  49. * @param {Number} [options.step=1] - the step to increment by. The value should be more than 1.
  50. * @returns {Date[]} the array with starts of days from the day of the interval start to the day of the interval end
  51. * @throws {TypeError} 1 argument required
  52. * @throws {RangeError} `options.step` must be a number greater than 1
  53. * @throws {RangeError} The start of an interval cannot be after its end
  54. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  55. *
  56. * @example
  57. * // Each day between 6 October 2014 and 10 October 2014:
  58. * const result = eachDayOfInterval({
  59. * start: new Date(2014, 9, 6),
  60. * end: new Date(2014, 9, 10)
  61. * })
  62. * //=> [
  63. * // Mon Oct 06 2014 00:00:00,
  64. * // Tue Oct 07 2014 00:00:00,
  65. * // Wed Oct 08 2014 00:00:00,
  66. * // Thu Oct 09 2014 00:00:00,
  67. * // Fri Oct 10 2014 00:00:00
  68. * // ]
  69. */
  70. function eachDayOfInterval(dirtyInterval, options) {
  71. (0, _index2.default)(1, arguments);
  72. var interval = dirtyInterval || {};
  73. var startDate = (0, _index.default)(interval.start);
  74. var endDate = (0, _index.default)(interval.end);
  75. var endTime = endDate.getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  76. if (!(startDate.getTime() <= endTime)) {
  77. throw new RangeError('Invalid interval');
  78. }
  79. var dates = [];
  80. var currentDate = startDate;
  81. currentDate.setHours(0, 0, 0, 0);
  82. var step = options && 'step' in options ? Number(options.step) : 1;
  83. if (step < 1 || isNaN(step)) throw new RangeError('`options.step` must be a number greater than 1');
  84. while (currentDate.getTime() <= endTime) {
  85. dates.push((0, _index.default)(currentDate));
  86. currentDate.setDate(currentDate.getDate() + step);
  87. currentDate.setHours(0, 0, 0, 0);
  88. }
  89. return dates;
  90. }
  91. module.exports = exports.default;