index.js 1.7 KB

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