Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (107 loc) · 4.03 KB
/
Copy pathapp.js
File metadata and controls
130 lines (107 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(function initGitClock(global) {
const SECOND = 1000;
const TWO_DIGITS = Array.from({ length: 61 }, (_, value) =>
value < 10 ? `0${value}` : `${value}`
);
function formatCount(value) {
return TWO_DIGITS[value];
}
function formatClockTime(date) {
return `${formatCount(date.getHours())}:${formatCount(date.getMinutes())}:${formatCount(date.getSeconds())}`;
}
function getHeaderStats(date) {
const day = date.getDate();
const daysInMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
return {
added: `+${formatCount(day)}`,
removed: `-${formatCount(daysInMonth - day)}`,
};
}
function getDiffRows(date) {
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const hourText = formatCount(hours);
const minuteText = formatCount(minutes);
const secondText = formatCount(seconds);
return [
{ file: "clock.now", value: `${hourText}:${minuteText}:${secondText}` },
{ file: "hours.js", added: `+${hourText}`, removed: `-${formatCount(24 - hours)}` },
{ file: "minutes.js", added: `+${minuteText}`, removed: `-${formatCount(60 - minutes)}` },
{ file: "seconds.js", added: `+${secondText}`, removed: `-${formatCount(60 - seconds)}` },
];
}
function updateClock(elements, date = new Date(), state = elements.state || (elements.state = {})) {
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const day = date.getDate();
const hourText = TWO_DIGITS[hours];
const minuteText = TWO_DIGITS[minutes];
const secondText = TWO_DIGITS[seconds];
const clockText = `${hourText}:${minuteText}:${secondText}`;
if (state.clock !== clockText) {
elements.clockValue.textContent = clockText;
elements.clockValue.dateTime = clockText;
state.clock = clockText;
}
if (state.day !== day) {
const daysInMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
elements.headerAdded.textContent = `+${TWO_DIGITS[day]}`;
elements.headerRemoved.textContent = `-${TWO_DIGITS[daysInMonth - day]}`;
state.day = day;
}
if (state.hours !== hours) {
const removedHours = `-${TWO_DIGITS[24 - hours]}`;
elements.hoursAdded.textContent = `+${hourText}`;
elements.hoursRemoved.textContent = removedHours;
state.hours = hours;
}
if (state.minutes !== minutes) {
elements.minutesAdded.textContent = `+${minuteText}`;
elements.minutesRemoved.textContent = `-${TWO_DIGITS[60 - minutes]}`;
state.minutes = minutes;
}
if (state.seconds !== seconds) {
elements.secondsAdded.textContent = `+${secondText}`;
elements.secondsRemoved.textContent = `-${TWO_DIGITS[60 - seconds]}`;
state.seconds = seconds;
}
}
function scheduleNextTick(tick, now = Date.now()) {
setTimeout(tick, SECOND - (now % SECOND));
}
function startClock() {
const elements = {
clockValue: document.querySelector("#clock-value"),
headerAdded: document.querySelector("#header-added"),
headerRemoved: document.querySelector("#header-removed"),
hoursAdded: document.querySelector("#hours-added"),
hoursRemoved: document.querySelector("#hours-removed"),
minutesAdded: document.querySelector("#minutes-added"),
minutesRemoved: document.querySelector("#minutes-removed"),
secondsAdded: document.querySelector("#seconds-added"),
secondsRemoved: document.querySelector("#seconds-removed"),
state: {},
};
if (Object.values(elements).some((element) => !element)) return;
const now = new Date();
function tick() {
const timestamp = Date.now();
now.setTime(timestamp);
updateClock(elements, now);
scheduleNextTick(tick, timestamp);
}
tick();
}
global.GitClock = {
formatClockTime,
getHeaderStats,
getDiffRows,
updateClock,
scheduleNextTick,
};
if (typeof document !== "undefined") {
startClock();
}
})(globalThis);