index.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = startOfWeekYear;
  7. var _index = _interopRequireDefault(require("../getWeekYear/index.js"));
  8. var _index2 = _interopRequireDefault(require("../startOfWeek/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. var _index5 = require("../_lib/defaultOptions/index.js");
  12. /**
  13. * @name startOfWeekYear
  14. * @category Week-Numbering Year Helpers
  15. * @summary Return the start of a local week-numbering year for the given date.
  16. *
  17. * @description
  18. * Return the start of a local week-numbering year.
  19. * The exact calculation depends on the values of
  20. * `options.weekStartsOn` (which is the index of the first day of the week)
  21. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  22. * the first week of the week-numbering year)
  23. *
  24. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  25. *
  26. * @param {Date|Number} date - the original date
  27. * @param {Object} [options] - an object with options.
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  30. * @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
  31. * @returns {Date} the start of a week-numbering year
  32. * @throws {TypeError} 1 argument required
  33. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  34. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  35. *
  36. * @example
  37. * // The start of an a week-numbering year for 2 July 2005 with default settings:
  38. * const result = startOfWeekYear(new Date(2005, 6, 2))
  39. * //=> Sun Dec 26 2004 00:00:00
  40. *
  41. * @example
  42. * // The start of a week-numbering year for 2 July 2005
  43. * // if Monday is the first day of week
  44. * // and 4 January is always in the first week of the year:
  45. * const result = startOfWeekYear(new Date(2005, 6, 2), {
  46. * weekStartsOn: 1,
  47. * firstWeekContainsDate: 4
  48. * })
  49. * //=> Mon Jan 03 2005 00:00:00
  50. */
  51. function startOfWeekYear(dirtyDate, options) {
  52. var _ref, _ref2, _ref3, _options$firstWeekCon, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  53. (0, _index4.default)(1, arguments);
  54. var defaultOptions = (0, _index5.getDefaultOptions)();
  55. var firstWeekContainsDate = (0, _index3.default)((_ref = (_ref2 = (_ref3 = (_options$firstWeekCon = options === null || options === void 0 ? void 0 : options.firstWeekContainsDate) !== null && _options$firstWeekCon !== void 0 ? _options$firstWeekCon : options === null || options === void 0 ? void 0 : (_options$locale = options.locale) === null || _options$locale === void 0 ? void 0 : (_options$locale$optio = _options$locale.options) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio.firstWeekContainsDate) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.firstWeekContainsDate) !== null && _ref2 !== void 0 ? _ref2 : (_defaultOptions$local = defaultOptions.locale) === null || _defaultOptions$local === void 0 ? void 0 : (_defaultOptions$local2 = _defaultOptions$local.options) === null || _defaultOptions$local2 === void 0 ? void 0 : _defaultOptions$local2.firstWeekContainsDate) !== null && _ref !== void 0 ? _ref : 1);
  56. var year = (0, _index.default)(dirtyDate, options);
  57. var firstWeek = new Date(0);
  58. firstWeek.setFullYear(year, 0, firstWeekContainsDate);
  59. firstWeek.setHours(0, 0, 0, 0);
  60. var date = (0, _index2.default)(firstWeek, options);
  61. return date;
  62. }
  63. module.exports = exports.default;