Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 78d52b3

Browse files
committed
Refactor units into a module
1 parent 857fe3a commit 78d52b3

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/lib/time-diff.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import units from "./units.js";
2+
13
//#region Types
24

35
/**
@@ -13,28 +15,6 @@
1315

1416
//#endregion
1517

16-
//#region Constants
17-
18-
/** One minute in seconds. */
19-
const ONE_MIN = 60;
20-
21-
/** One hour in seconds. */
22-
const ONE_HOUR = 60 * ONE_MIN;
23-
24-
/** One day in seconds. */
25-
const ONE_DAY = 24 * ONE_HOUR;
26-
27-
/** One week in seconds. */
28-
const ONE_WEEK = 7 * ONE_DAY;
29-
30-
/** One month in seconds. */
31-
const ONE_MONTH = 4 * ONE_WEEK;
32-
33-
/** One year in seconds. */
34-
const ONE_YEAR = 52 * ONE_WEEK;
35-
36-
//#endregion
37-
3818
//#region Classes
3919

4020
/** Represents the difference between two times. */
@@ -47,12 +27,12 @@ export class TimeDiff {
4727

4828
/** @type {Map<string, number>} */
4929
#units = new Map([
50-
["year", ONE_YEAR],
51-
["month", ONE_MONTH],
52-
["week", ONE_WEEK],
53-
["day", ONE_DAY],
54-
["hour", ONE_HOUR],
55-
["minute", ONE_MIN],
30+
["year", units.year],
31+
["month", units.month],
32+
["week", units.week],
33+
["day", units.day],
34+
["hour", units.hour],
35+
["minute", units.minute],
5636
]);
5737

5838
//#region Public API

src/lib/units.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default {
2+
/** One minute in seconds. */
3+
get minute() {
4+
return 60;
5+
},
6+
/** One hour in seconds. */
7+
get hour() {
8+
return this.minute * 60;
9+
},
10+
/** One day in seconds. */
11+
get day() {
12+
return this.hour * 24;
13+
},
14+
/** One week in seconds. */
15+
get week() {
16+
return this.day * 7;
17+
},
18+
/** One month in seconds. */
19+
get month() {
20+
return this.week * 4;
21+
},
22+
/** One year in seconds. */
23+
get year() {
24+
return this.day * 365;
25+
},
26+
};

0 commit comments

Comments
 (0)