eachMinuteOfInterval.cjs 2.1 KB

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