index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setUTCDay;
  6. var _index = _interopRequireDefault(require("../toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // This function will be a part of public API when UTC function will be implemented.
  11. // See issue: https://github.com/date-fns/date-fns/issues/376
  12. function setUTCDay(dirtyDate, dirtyDay, dirtyOptions) {
  13. (0, _index3.default)(2, arguments);
  14. var options = dirtyOptions || {};
  15. var locale = options.locale;
  16. var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
  17. var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : (0, _index.default)(localeWeekStartsOn);
  18. var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : (0, _index.default)(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  19. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  20. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  21. }
  22. var date = (0, _index2.default)(dirtyDate);
  23. var day = (0, _index.default)(dirtyDay);
  24. var currentDay = date.getUTCDay();
  25. var remainder = day % 7;
  26. var dayIndex = (remainder + 7) % 7;
  27. var diff = (dayIndex < weekStartsOn ? 7 : 0) + day - currentDay;
  28. date.setUTCDate(date.getUTCDate() + diff);
  29. return date;
  30. }
  31. module.exports = exports.default;