utilium 3.9.0 → 3.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/format.js +12 -5
- package/package.json +1 -1
package/dist/format.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
2
|
// Copyright (c) 2026 James Prevett
|
|
3
|
+
/** Lengths are in milliseconds */
|
|
4
|
+
const rangeUnits = [
|
|
5
|
+
['day', 24 * 3600_000],
|
|
6
|
+
['hour', 3600_000],
|
|
7
|
+
['minute', 60_000],
|
|
8
|
+
];
|
|
3
9
|
export function dateRange(date) {
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
10
|
+
const offset = date.getTime() - Date.now();
|
|
11
|
+
const distance = Math.abs(offset);
|
|
12
|
+
const [unit, length] = rangeUnits.find(([, length]) => distance >= length) ?? rangeUnits.at(-1);
|
|
13
|
+
const count = Math.round(distance / length);
|
|
14
|
+
const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
|
|
15
|
+
return `${date.toLocaleString()} (${offset >= 0 ? `in ${text}` : `${text} ago`})`;
|
|
9
16
|
}
|
|
10
17
|
export function bytes(bytes) {
|
|
11
18
|
if (typeof bytes == 'number') {
|