index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = eachQuarterOfInterval;
  6. var _index = _interopRequireDefault(require("../addQuarters/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfQuarter/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/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 eachQuarterOfInterval
  13. * @category Interval Helpers
  14. * @summary Return the array of quarters within the specified time interval.
  15. *
  16. * @description
  17. * Return the array of quarters within the specified time interval.
  18. *
  19. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  20. * @returns {Date[]} the array with starts of quarters from the quarter of the interval start to the quarter of the interval end
  21. * @throws {TypeError} 1 argument required
  22. * @throws {RangeError} The start of an interval cannot be after its end
  23. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  24. *
  25. * @example
  26. * // Each quarter within interval 6 February 2014 - 10 August 2014:
  27. * var result = eachQuarterOfInterval({
  28. * start: new Date(2014, 1, 6),
  29. * end: new Date(2014, 7, 10)
  30. * })
  31. * //=> [
  32. * // Wed Jan 01 2014 00:00:00,
  33. * // Tue Apr 01 2014 00:00:00,
  34. * // Tue Jul 01 2014 00:00:00,
  35. * // ]
  36. */
  37. function eachQuarterOfInterval(dirtyInterval) {
  38. (0, _index4.default)(1, arguments);
  39. var interval = dirtyInterval || {};
  40. var startDate = (0, _index3.default)(interval.start);
  41. var endDate = (0, _index3.default)(interval.end);
  42. var endTime = endDate.getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  43. if (!(startDate.getTime() <= endTime)) {
  44. throw new RangeError('Invalid interval');
  45. }
  46. var startDateQuarter = (0, _index2.default)(startDate);
  47. var endDateQuarter = (0, _index2.default)(endDate);
  48. endTime = endDateQuarter.getTime();
  49. var quarters = [];
  50. var currentQuarter = startDateQuarter;
  51. while (currentQuarter.getTime() <= endTime) {
  52. quarters.push((0, _index3.default)(currentQuarter));
  53. currentQuarter = (0, _index.default)(currentQuarter, 1);
  54. }
  55. return quarters;
  56. }
  57. module.exports = exports.default;