index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { D, W, Y } from '../../constant';
  2. var isoWeekPrettyUnit = 'isoweek';
  3. export default (function (o, c, d) {
  4. var getYearFirstThursday = function getYearFirstThursday(year, isUtc) {
  5. var yearFirstDay = (isUtc ? d.utc : d)().year(year).startOf(Y);
  6. var addDiffDays = 4 - yearFirstDay.isoWeekday();
  7. if (yearFirstDay.isoWeekday() > 4) {
  8. addDiffDays += 7;
  9. }
  10. return yearFirstDay.add(addDiffDays, D);
  11. };
  12. var getCurrentWeekThursday = function getCurrentWeekThursday(ins) {
  13. return ins.add(4 - ins.isoWeekday(), D);
  14. };
  15. var proto = c.prototype;
  16. proto.isoWeekYear = function () {
  17. var nowWeekThursday = getCurrentWeekThursday(this);
  18. return nowWeekThursday.year();
  19. };
  20. proto.isoWeek = function (week) {
  21. if (!this.$utils().u(week)) {
  22. return this.add((week - this.isoWeek()) * 7, D);
  23. }
  24. var nowWeekThursday = getCurrentWeekThursday(this);
  25. var diffWeekThursday = getYearFirstThursday(this.isoWeekYear(), this.$u);
  26. return nowWeekThursday.diff(diffWeekThursday, W) + 1;
  27. };
  28. proto.isoWeekday = function (week) {
  29. if (!this.$utils().u(week)) {
  30. return this.day(this.day() % 7 ? week : week - 7);
  31. }
  32. return this.day() || 7;
  33. };
  34. var oldStartOf = proto.startOf;
  35. proto.startOf = function (units, startOf) {
  36. var utils = this.$utils();
  37. var isStartOf = !utils.u(startOf) ? startOf : true;
  38. var unit = utils.p(units);
  39. if (unit === isoWeekPrettyUnit) {
  40. return isStartOf ? this.date(this.date() - (this.isoWeekday() - 1)).startOf('day') : this.date(this.date() - 1 - (this.isoWeekday() - 1) + 7).endOf('day');
  41. }
  42. return oldStartOf.bind(this)(units, startOf);
  43. };
  44. });