index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = startOfUTCWeek;
  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 startOfUTCWeek(dirtyDate, dirtyOptions) {
  13. (0, _index3.default)(1, 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 = date.getUTCDay();
  24. var diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn;
  25. date.setUTCDate(date.getUTCDate() - diff);
  26. date.setUTCHours(0, 0, 0, 0);
  27. return date;
  28. }
  29. module.exports = exports.default;