index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import requiredArgs from "../_lib/requiredArgs/index.js";
  2. import getDay from "../getDay/index.js";
  3. import addDays from "../addDays/index.js";
  4. import toDate from "../toDate/index.js";
  5. var baseMap = [7, 6, 5, 4, 3, 2, 1];
  6. /**
  7. * @name nextDay
  8. * @category Weekday Helpers
  9. * @summary When is the next day of the week?
  10. *
  11. * @description
  12. * When is the next day of the week? 0-6 the day of the week, 0 represents Sunday.
  13. *
  14. * @param {Date | number} date - the date to check
  15. * @param {Day} day - day of the week
  16. * @returns {Date} - the date is the next day of week
  17. * @throws {TypeError} - 2 arguments required
  18. *
  19. * @example
  20. * // When is the next Monday after Mar, 20, 2020?
  21. * const result = nextDay(new Date(2020, 2, 20), 1)
  22. * //=> Mon Mar 23 2020 00:00:00
  23. *
  24. * @example
  25. * // When is the next Tuesday after Mar, 21, 2020?
  26. * const result = nextDay(new Date(2020, 2, 21), 2)
  27. * //=> Tue Mar 24 2020 00:00:00
  28. */
  29. export default function nextDay(date, day) {
  30. requiredArgs(2, arguments);
  31. var map = genMap(day);
  32. return addDays(toDate(date), map[getDay(toDate(date))]);
  33. }
  34. function genMap(daysToMove) {
  35. if (daysToMove === 0) {
  36. return baseMap;
  37. } else {
  38. var mapStart = baseMap.slice(-daysToMove);
  39. var mapEnd = baseMap.slice(0, baseMap.length - daysToMove);
  40. return mapStart.concat(mapEnd);
  41. }
  42. }