eachQuarterOfInterval.cjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. exports.eachQuarterOfInterval = eachQuarterOfInterval;
  3. var _index = require("./_lib/normalizeInterval.cjs");
  4. var _index2 = require("./addQuarters.cjs");
  5. var _index3 = require("./constructFrom.cjs");
  6. var _index4 = require("./startOfQuarter.cjs");
  7. /**
  8. * The {@link eachQuarterOfInterval} function options.
  9. */
  10. /**
  11. * The {@link eachQuarterOfInterval} function result type. It resolves the proper data type.
  12. * It uses the first argument date object type, starting from the date argument,
  13. * then the start interval date, and finally the end interval date. If
  14. * a context function is passed, it uses the context function return type.
  15. */
  16. /**
  17. * @name eachQuarterOfInterval
  18. * @category Interval Helpers
  19. * @summary Return the array of quarters within the specified time interval.
  20. *
  21. * @description
  22. * Return the array of quarters within the specified time interval.
  23. *
  24. * @typeParam IntervalType - Interval type.
  25. * @typeParam Options - Options type.
  26. *
  27. * @param interval - The interval
  28. * @param options - An object with options
  29. *
  30. * @returns The array with starts of quarters from the quarter of the interval start to the quarter of the interval end
  31. *
  32. * @example
  33. * // Each quarter within interval 6 February 2014 - 10 August 2014:
  34. * const result = eachQuarterOfInterval({
  35. * start: new Date(2014, 1, 6),
  36. * end: new Date(2014, 7, 10),
  37. * })
  38. * //=> [
  39. * // Wed Jan 01 2014 00:00:00,
  40. * // Tue Apr 01 2014 00:00:00,
  41. * // Tue Jul 01 2014 00:00:00,
  42. * // ]
  43. */
  44. function eachQuarterOfInterval(interval, options) {
  45. const { start, end } = (0, _index.normalizeInterval)(options?.in, interval);
  46. let reversed = +start > +end;
  47. const endTime = reversed
  48. ? +(0, _index4.startOfQuarter)(start)
  49. : +(0, _index4.startOfQuarter)(end);
  50. let date = reversed
  51. ? (0, _index4.startOfQuarter)(end)
  52. : (0, _index4.startOfQuarter)(start);
  53. let step = options?.step ?? 1;
  54. if (!step) return [];
  55. if (step < 0) {
  56. step = -step;
  57. reversed = !reversed;
  58. }
  59. const dates = [];
  60. while (+date <= endTime) {
  61. dates.push((0, _index3.constructFrom)(start, date));
  62. date = (0, _index2.addQuarters)(date, step);
  63. }
  64. return reversed ? dates.reverse() : dates;
  65. }