index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setDay;
  6. var _index = _interopRequireDefault(require("../addDays/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/toInteger/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 setDay
  13. * @category Weekday Helpers
  14. * @summary Set the day of the week to the given date.
  15. *
  16. * @description
  17. * Set the day of the week to the given date.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Date|Number} date - the date to be changed
  24. * @param {Number} day - the day of the week of the new date
  25. * @param {Object} [options] - an object with options.
  26. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  27. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  28. * @returns {Date} the new date with the day of the week set
  29. * @throws {TypeError} 2 arguments required
  30. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  31. *
  32. * @example
  33. * // Set week day to Sunday, with the default weekStartsOn of Sunday:
  34. * var result = setDay(new Date(2014, 8, 1), 0)
  35. * //=> Sun Aug 31 2014 00:00:00
  36. *
  37. * @example
  38. * // Set week day to Sunday, with a weekStartsOn of Monday:
  39. * var result = setDay(new Date(2014, 8, 1), 0, { weekStartsOn: 1 })
  40. * //=> Sun Sep 07 2014 00:00:00
  41. */
  42. function setDay(dirtyDate, dirtyDay, dirtyOptions) {
  43. (0, _index4.default)(2, arguments);
  44. var options = dirtyOptions || {};
  45. var locale = options.locale;
  46. var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
  47. var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : (0, _index3.default)(localeWeekStartsOn);
  48. var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : (0, _index3.default)(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  49. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  50. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  51. }
  52. var date = (0, _index2.default)(dirtyDate, options);
  53. var day = (0, _index3.default)(dirtyDay);
  54. var currentDay = date.getDay();
  55. var remainder = day % 7;
  56. var dayIndex = (remainder + 7) % 7;
  57. var delta = 7 - weekStartsOn;
  58. var diff = day < 0 || day > 6 ? day - (currentDay + delta) % 7 : (dayIndex + delta) % 7 - (currentDay + delta) % 7;
  59. return (0, _index.default)(date, diff, options);
  60. }
  61. module.exports = exports.default;