Skip to content

Commit 9292175

Browse files
committed
Replace moment with date-fns
1 parent 3ccefcf commit 9292175

6 files changed

Lines changed: 57 additions & 29 deletions

File tree

package-lock.json

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"@stripe/stripe-js": "^1.25.0",
1414
"chart.js": "^3.7.1",
1515
"clsx": "^1.1.1",
16+
"date-fns": "^4.1.0",
1617
"firebase": "^9.6.9",
17-
"moment": "^2.29.1",
18+
"format-duration": "^3.0.2",
1819
"obscenity": "^0.4.4",
1920
"project-name-generator": "^2.1.9",
2021
"react": "^17.0.2",

src/components/ElapsedTime.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import formatDistanceStrict from "date-fns/formatDistanceStrict";
2+
13
import useMoment from "../hooks/useMoment";
24

35
// Wrapper around useMoment, since state hooks cause rerender of component
46
function ElapsedTime({ value }) {
5-
const time = useMoment();
6-
return <>{time.to(value)}</>;
7+
const time = useMoment(5000);
8+
const opts = { addSuffix: true };
9+
return <>{formatDistanceStrict(value, time, opts)}</>;
710
}
811

912
export default ElapsedTime;

src/hooks/useMoment.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import moment from "moment";
21
import { useEffect, useState } from "react";
32

43
import useFirebaseRef from "./useFirebaseRef";
54

65
function useMoment(delay = 1000) {
7-
const [time, setTime] = useState(moment()); // Estimated firebase server time
6+
const [time, setTime] = useState(Date.now()); // Estimated firebase server time
87
const [offset] = useFirebaseRef(".info/serverTimeOffset");
98

109
useEffect(() => {
1110
if (!delay) return;
12-
13-
const id = setInterval(() => {
14-
setTime(moment(Date.now() + offset));
15-
}, delay);
11+
const id = setInterval(() => setTime(Date.now() + offset), delay);
1612
return () => clearInterval(id);
1713
}, [offset, delay]);
1814

src/util.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import moment from "moment";
1+
import formatDuration from "format-duration";
22
import {
33
RegExpMatcher,
44
TextCensor,
@@ -196,11 +196,11 @@ export function generateName() {
196196
}
197197

198198
export function formatTime(t, hideSubsecond) {
199-
t = Math.max(t, 0);
200-
const hours = Math.floor(t / (3600 * 1000));
201-
const rest = t % (3600 * 1000);
202-
const format = hideSubsecond ? "mm:ss" : "mm:ss.SS";
203-
return (hours ? `${hours}:` : "") + moment.utc(rest).format(format);
199+
const res = formatDuration(Math.max(t, 0), {
200+
leading: true,
201+
ms: !hideSubsecond,
202+
});
203+
return hideSubsecond ? res : res.slice(0, -1);
204204
}
205205

206206
export function formatCount(count, singular, plural = null) {

src/util.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { badWords, formatANoun, parseDuration } from "./util";
1+
import { badWords, formatANoun, formatTime, parseDuration } from "./util";
22

33
describe("bad words filter", () => {
44
it("sort of works", () => {
@@ -18,7 +18,7 @@ describe("bad words filter", () => {
1818
});
1919
});
2020

21-
it("parseDuration works", () => {
21+
it("formatANoun works", () => {
2222
expect(formatANoun("Set")).toBe("a Set");
2323
expect(formatANoun("UltraSet")).toBe("an UltraSet");
2424
expect(formatANoun("GhostSet")).toBe("a GhostSet");
@@ -37,3 +37,23 @@ it("parseDuration works", () => {
3737
expect(parseDuration("300")).toBe(null);
3838
expect(parseDuration("")).toBe(null);
3939
});
40+
41+
it("formatTime works", () => {
42+
const check = (ms, expected) => {
43+
expect(formatTime(ms)).toBe(expected);
44+
expect(formatTime(ms, true)).toBe(expected.slice(0, -3));
45+
};
46+
check(-12345, "00:00.00");
47+
check(0, "00:00.00");
48+
check(999, "00:00.99");
49+
check(12349, "00:12.34");
50+
check(59999, "00:59.99");
51+
check(123000, "02:03.00");
52+
check(123459, "02:03.45");
53+
check(1234599, "20:34.59");
54+
check(3599999, "59:59.99");
55+
check(3600000, "01:00:00.00");
56+
check(3725669, "01:02:05.66");
57+
check(37256699, "10:20:56.69");
58+
check(372566999, "4:07:29:26.99");
59+
});

0 commit comments

Comments
 (0)