index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = getWeekOfMonth;
  7. var _index = require("../_lib/defaultOptions/index.js");
  8. var _index2 = _interopRequireDefault(require("../getDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../getDay/index.js"));
  10. var _index4 = _interopRequireDefault(require("../startOfMonth/index.js"));
  11. var _index5 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  12. var _index6 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  13. /**
  14. * @name getWeekOfMonth
  15. * @category Week Helpers
  16. * @summary Get the week of the month of the given date.
  17. *
  18. * @description
  19. * Get the week of the month of the given date.
  20. *
  21. * @param {Date|Number} date - the given date
  22. * @param {Object} [options] - an object with options.
  23. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  24. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  25. * @returns {Number} the week of month
  26. * @throws {TypeError} 1 argument required
  27. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 inclusively
  28. *
  29. * @example
  30. * // Which week of the month is 9 November 2017?
  31. * const result = getWeekOfMonth(new Date(2017, 10, 9))
  32. * //=> 2
  33. */
  34. function getWeekOfMonth(date, options) {
  35. var _ref, _ref2, _ref3, _options$weekStartsOn, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  36. (0, _index5.default)(1, arguments);
  37. var defaultOptions = (0, _index.getDefaultOptions)();
  38. var weekStartsOn = (0, _index6.default)((_ref = (_ref2 = (_ref3 = (_options$weekStartsOn = options === null || options === void 0 ? void 0 : options.weekStartsOn) !== null && _options$weekStartsOn !== void 0 ? _options$weekStartsOn : 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.weekStartsOn) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.weekStartsOn) !== 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.weekStartsOn) !== null && _ref !== void 0 ? _ref : 0);
  39. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  40. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  41. }
  42. var currentDayOfMonth = (0, _index2.default)(date);
  43. if (isNaN(currentDayOfMonth)) return NaN;
  44. var startWeekDay = (0, _index3.default)((0, _index4.default)(date));
  45. var lastDayOfFirstWeek = weekStartsOn - startWeekDay;
  46. if (lastDayOfFirstWeek <= 0) lastDayOfFirstWeek += 7;
  47. var remainingDaysAfterFirstWeek = currentDayOfMonth - lastDayOfFirstWeek;
  48. return Math.ceil(remainingDaysAfterFirstWeek / 7) + 1;
  49. }
  50. module.exports = exports.default;