index.js 2.5 KB

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