index.js 2.5 KB

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