utilium 3.9.0 → 3.10.0
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.d.ts +2 -0
- package/dist/format.js +16 -5
- package/package.json +1 -1
package/dist/format.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
|
|
2
|
+
export declare function relativeToDate(date: Date | number, relativeTo?: Date | number): string;
|
|
1
3
|
export declare function dateRange(date: Date): string;
|
|
2
4
|
export declare function bytes(bytes: bigint | number): string;
|
|
3
5
|
export declare function ms(time: number): string;
|
package/dist/format.js
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
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
|
+
];
|
|
9
|
+
/** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
|
|
10
|
+
export function relativeToDate(date, relativeTo = Date.now()) {
|
|
11
|
+
const offset = Number(date) - Number(relativeTo);
|
|
12
|
+
const distance = Math.abs(offset);
|
|
13
|
+
const [unit, length] = rangeUnits.find(([, length]) => distance >= length) ?? rangeUnits.at(-1);
|
|
14
|
+
const count = Math.round(distance / length);
|
|
15
|
+
const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
|
|
16
|
+
return offset >= 0 ? `in ${text}` : `${text} ago`;
|
|
17
|
+
}
|
|
3
18
|
export function dateRange(date) {
|
|
4
|
-
|
|
5
|
-
const daysCount = Math.abs(rawDays);
|
|
6
|
-
const daysText = Math.round(daysCount);
|
|
7
|
-
const plural = daysCount == 1 ? '' : 's';
|
|
8
|
-
return `${date.toLocaleString()} (${rawDays >= 0 ? `in ${daysText} day${plural}` : `${daysText} day${plural} ago`})`;
|
|
19
|
+
return `${date.toLocaleString()} (${relativeToDate(date)})`;
|
|
9
20
|
}
|
|
10
21
|
export function bytes(bytes) {
|
|
11
22
|
if (typeof bytes == 'number') {
|