index.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = startOfWeekYear;
  6. var _index = _interopRequireDefault(require("../getWeekYear/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfWeek/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 startOfWeekYear
  13. * @category Week-Numbering Year Helpers
  14. * @summary Return the start of a local week-numbering year for the given date.
  15. *
  16. * @description
  17. * Return the start of a local week-numbering year.
  18. * The exact calculation depends on the values of
  19. * `options.weekStartsOn` (which is the index of the first day of the week)
  20. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  21. * the first week of the week-numbering year)
  22. *
  23. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  24. *
  25. * ### v2.0.0 breaking changes:
  26. *
  27. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  28. *
  29. * @param {Date|Number} date - the original date
  30. * @param {Object} [options] - an object with options.
  31. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  32. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  33. * @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
  34. * @returns {Date} the start of a week-numbering year
  35. * @throws {TypeError} 1 argument required
  36. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  37. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  38. *
  39. * @example
  40. * // The start of an a week-numbering year for 2 July 2005 with default settings:
  41. * var result = startOfWeekYear(new Date(2005, 6, 2))
  42. * //=> Sun Dec 26 2004 00:00:00
  43. *
  44. * @example
  45. * // The start of a week-numbering year for 2 July 2005
  46. * // if Monday is the first day of week
  47. * // and 4 January is always in the first week of the year:
  48. * var result = startOfWeekYear(new Date(2005, 6, 2), {
  49. * weekStartsOn: 1,
  50. * firstWeekContainsDate: 4
  51. * })
  52. * //=> Mon Jan 03 2005 00:00:00
  53. */
  54. function startOfWeekYear(dirtyDate, dirtyOptions) {
  55. (0, _index4.default)(1, arguments);
  56. var options = dirtyOptions || {};
  57. var locale = options.locale;
  58. var localeFirstWeekContainsDate = locale && locale.options && locale.options.firstWeekContainsDate;
  59. var defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : (0, _index3.default)(localeFirstWeekContainsDate);
  60. var firstWeekContainsDate = options.firstWeekContainsDate == null ? defaultFirstWeekContainsDate : (0, _index3.default)(options.firstWeekContainsDate);
  61. var year = (0, _index.default)(dirtyDate, dirtyOptions);
  62. var firstWeek = new Date(0);
  63. firstWeek.setFullYear(year, 0, firstWeekContainsDate);
  64. firstWeek.setHours(0, 0, 0, 0);
  65. var date = (0, _index2.default)(firstWeek, dirtyOptions);
  66. return date;
  67. }
  68. module.exports = exports.default;