Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/library/dayjs/plugins/isoweek.js
12923 views
1
import { D, W, Y } from "./constant.js";
2
3
const isoWeekPrettyUnit = "isoweek";
4
5
export default (o, c, d) => {
6
const getYearFirstThursday = (year, isUtc) => {
7
const yearFirstDay = (isUtc ? d.utc : d)().year(year).startOf(Y);
8
let addDiffDays = 4 - yearFirstDay.isoWeekday();
9
if (yearFirstDay.isoWeekday() > 4) {
10
addDiffDays += 7;
11
}
12
return yearFirstDay.add(addDiffDays, D);
13
};
14
15
const getCurrentWeekThursday = (ins) => ins.add(4 - ins.isoWeekday(), D);
16
17
const proto = c.prototype;
18
19
proto.isoWeekYear = function () {
20
const nowWeekThursday = getCurrentWeekThursday(this);
21
return nowWeekThursday.year();
22
};
23
24
proto.isoWeek = function (week) {
25
if (!this.$utils().u(week)) {
26
return this.add((week - this.isoWeek()) * 7, D);
27
}
28
const nowWeekThursday = getCurrentWeekThursday(this);
29
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear(), this.$u);
30
return nowWeekThursday.diff(diffWeekThursday, W) + 1;
31
};
32
33
proto.isoWeekday = function (week) {
34
if (!this.$utils().u(week)) {
35
return this.day(this.day() % 7 ? week : week - 7);
36
}
37
return this.day() || 7;
38
};
39
40
const oldStartOf = proto.startOf;
41
proto.startOf = function (units, startOf) {
42
const utils = this.$utils();
43
const isStartOf = !utils.u(startOf) ? startOf : true;
44
const unit = utils.p(units);
45
if (unit === isoWeekPrettyUnit) {
46
return isStartOf
47
? this.date(this.date() - (this.isoWeekday() - 1)).startOf("day")
48
: this.date(this.date() - 1 - (this.isoWeekday() - 1) + 7).endOf("day");
49
}
50
return oldStartOf.bind(this)(units, startOf);
51
};
52
};
53
54