UNPKG

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