Skip to content

Commit e0b4ee7

Browse files
committed
Refactor away jQuery and Moment.js dependencies (#752)
1 parent c2900ce commit e0b4ee7

File tree

5 files changed

+71
-54
lines changed

5 files changed

+71
-54
lines changed

Diff for: _layouts/default.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
<link href="/css/style.css" rel="stylesheet" type="text/css">
1212
<link href="/images/favicon.png" rel="icon" type="image/png">
1313
{% feed_meta %}
14-
<script src="/js/jquery.js" type="text/javascript"></script>
15-
<script src="/js/moment.min.js" type="text/javascript"></script>
16-
<script src="/js/behavior.js" type="text/javascript"></script>
14+
<script src="/js/callout.js" type="text/javascript"></script>
1715
</head>
1816
<body>
1917
<div id="page">

Diff for: js/behavior.js

-39
This file was deleted.

Diff for: js/callout.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function getCalloutText(dateString, phrase) {
2+
if (!dateString) return phrase
3+
4+
const date = new Date(dateString)
5+
const now = new Date()
6+
const tomorrow = new Date(now)
7+
tomorrow.setDate(now.getDate() + 1)
8+
9+
if (isSameDay(date, now)) {
10+
return `${phrase} today`
11+
}
12+
13+
if (isSameDay(date, tomorrow)) {
14+
return `${phrase} tomorrow`
15+
}
16+
17+
if (date > now) {
18+
return `${phrase} ${getRelativeTimePhrase(date)}`
19+
}
20+
21+
return phrase
22+
}
23+
24+
function isSameDay(date1, date2) {
25+
return (
26+
date1.getFullYear() === date2.getFullYear() &&
27+
date1.getMonth() === date2.getMonth() &&
28+
date1.getDate() === date2.getDate()
29+
)
30+
}
31+
32+
function getRelativeTimePhrase(date) {
33+
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
34+
const diffDays = Math.ceil((date - new Date()) / (1000 * 60 * 60 * 24))
35+
36+
if (Math.abs(diffDays) < 7) {
37+
return rtf.format(diffDays, 'day')
38+
}
39+
40+
if (Math.abs(diffDays) < 30) {
41+
return rtf.format(Math.floor(diffDays / 7), 'week')
42+
}
43+
44+
if (Math.abs(diffDays) < 365) {
45+
return rtf.format(Math.floor(diffDays / 30), 'month')
46+
}
47+
48+
return rtf.format(Math.floor(diffDays / 365), 'year')
49+
}
50+
51+
function updateCallouts() {
52+
document.querySelectorAll('[data-phrase]').forEach(element => {
53+
const { date, phrase } = element.dataset
54+
55+
if (!date) {
56+
element.textContent = phrase
57+
return
58+
}
59+
60+
const calloutText = getCalloutText(date, phrase)
61+
62+
if (calloutText !== phrase) {
63+
element.textContent = calloutText
64+
} else {
65+
element.remove()
66+
}
67+
})
68+
}
69+
70+
document.addEventListener('DOMContentLoaded', updateCallouts)

Diff for: js/jquery.js

-6
This file was deleted.

Diff for: js/moment.min.js

-6
This file was deleted.

0 commit comments

Comments
 (0)