utilium 3.7.1 → 3.8.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 +5 -0
- package/dist/format.js +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +3 -2
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function dateRange(date: Date): string;
|
|
2
|
+
export declare function bytes(bytes: bigint | number): string;
|
|
3
|
+
export declare function ms(time: number): string;
|
|
4
|
+
/** Formats a short duration into colon-separated values */
|
|
5
|
+
export declare function duration(seconds: number): string;
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 James Prevett
|
|
3
|
+
export function dateRange(date) {
|
|
4
|
+
const rawDays = (date.getTime() - Date.now()) / (24 * 3600_000);
|
|
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`})`;
|
|
9
|
+
}
|
|
10
|
+
export function bytes(bytes) {
|
|
11
|
+
if (typeof bytes == 'number') {
|
|
12
|
+
bytes = BigInt(bytes);
|
|
13
|
+
}
|
|
14
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
15
|
+
const i = bytes == 0n ? 0 : Math.floor((bytes.toString(10).length - 1) / 3);
|
|
16
|
+
const value = bytes == 0n ? 0 : Number(bytes) / Math.pow(1000, i);
|
|
17
|
+
return `${Number.isInteger(value) ? value : value.toFixed(2)} ${units[i]}`;
|
|
18
|
+
}
|
|
19
|
+
export function ms(time) {
|
|
20
|
+
return time > 5000 ? (time / 1000).toFixed(2) + 's' : time + 'ms';
|
|
21
|
+
}
|
|
22
|
+
/** Formats a short duration into colon-separated values */
|
|
23
|
+
export function duration(seconds) {
|
|
24
|
+
if (!Number.isFinite(seconds))
|
|
25
|
+
return '--:--';
|
|
26
|
+
seconds = Math.floor(seconds);
|
|
27
|
+
let minutes = Math.floor(seconds / 60);
|
|
28
|
+
seconds %= 60;
|
|
29
|
+
const hours = Math.floor(minutes / 60);
|
|
30
|
+
minutes %= 60;
|
|
31
|
+
const secondsStr = ':' + seconds.toString().padStart(2, '0');
|
|
32
|
+
return hours ? `${hours}:${minutes.toString().padStart(2, '0')}${secondsStr}` : minutes + secondsStr;
|
|
33
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export * as cache from './cache.js';
|
|
|
7
7
|
export * from './checksum.js';
|
|
8
8
|
export * from './color.js';
|
|
9
9
|
export * as diff from './diff.js';
|
|
10
|
+
export * as format from './format.js';
|
|
10
11
|
export * from './list.js';
|
|
11
12
|
export * from './misc.js';
|
|
12
13
|
export * from './numbers.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilium",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
4
4
|
"description": "Typescript utilities",
|
|
5
5
|
"funding": {
|
|
6
6
|
"type": "individual",
|
|
@@ -68,5 +68,6 @@
|
|
|
68
68
|
},
|
|
69
69
|
"allowScripts": {
|
|
70
70
|
"esbuild": true
|
|
71
|
-
}
|
|
71
|
+
},
|
|
72
|
+
"sideEffects": false
|
|
72
73
|
}
|