UNPKG

1.68 kBJavaScriptView Raw
1import { D, W, Y } from '../../constant';
2var isoWeekPrettyUnit = 'isoweek';
3export 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
8 if (yearFirstDay.isoWeekday() > 4) {
9 addDiffDays += 7;
10 }
11
12 return yearFirstDay.add(addDiffDays, D);
13 };
14
15 var getCurrentWeekThursday = function getCurrentWeekThursday(ins) {
16 return ins.add(4 - ins.isoWeekday(), D);
17 };
18
19 var proto = c.prototype;
20
21 proto.isoWeekYear = function () {
22 var nowWeekThursday = getCurrentWeekThursday(this);
23 return nowWeekThursday.year();
24 };
25
26 proto.isoWeek = function (week) {
27 if (!this.$utils().u(week)) {
28 return this.add((week - this.isoWeek()) * 7, D);
29 }
30
31 var nowWeekThursday = getCurrentWeekThursday(this);
32 var diffWeekThursday = getYearFirstThursday(this.isoWeekYear(), this.$u);
33 return nowWeekThursday.diff(diffWeekThursday, W) + 1;
34 };
35
36 proto.isoWeekday = function (week) {
37 if (!this.$utils().u(week)) {
38 return this.day(this.day() % 7 ? week : week - 7);
39 }
40
41 return this.day() || 7;
42 };
43
44 var oldStartOf = proto.startOf;
45
46 proto.startOf = function (units, startOf) {
47 var utils = this.$utils();
48 var isStartOf = !utils.u(startOf) ? startOf : true;
49 var unit = utils.p(units);
50
51 if (unit === isoWeekPrettyUnit) {
52 return isStartOf ? this.date(this.date() - (this.isoWeekday() - 1)).startOf('day') : this.date(this.date() - 1 - (this.isoWeekday() - 1) + 7).endOf('day');
53 }
54
55 return oldStartOf.bind(this)(units, startOf);
56 };
57});
\No newline at end of file