utilium 3.7.1 → 3.9.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/dist/numbers.d.ts +4 -0
- package/dist/numbers.js +20 -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/dist/numbers.d.ts
CHANGED
|
@@ -9,3 +9,7 @@ export declare const formatCompact: {
|
|
|
9
9
|
};
|
|
10
10
|
export type ToNumber<T extends CoercibleToString> = `${T}` extends `${infer U extends number}` ? U : never;
|
|
11
11
|
export type ToBigInt<T extends CoercibleToString> = `${T}` extends `${infer U extends bigint}` ? U : never;
|
|
12
|
+
/**
|
|
13
|
+
* Parse a string containing some number of bytes, e.g. `1 GiB` or `99.4kb`
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseBytes(input: string): bigint | null;
|
package/dist/numbers.js
CHANGED
|
@@ -15,3 +15,23 @@ export function toRadians(degrees) {
|
|
|
15
15
|
}
|
|
16
16
|
const __formatter = Intl.NumberFormat('en', { notation: 'compact' });
|
|
17
17
|
export const formatCompact = __formatter.format.bind(__formatter);
|
|
18
|
+
const byteSizePattern = /^(?<size>\d+(?:\.\d+)?)\s*(?<unit>\w*)$/i;
|
|
19
|
+
const units = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
|
|
20
|
+
/**
|
|
21
|
+
* Parse a string containing some number of bytes, e.g. `1 GiB` or `99.4kb`
|
|
22
|
+
*/
|
|
23
|
+
export function parseBytes(input) {
|
|
24
|
+
const match = byteSizePattern.exec(input.trim());
|
|
25
|
+
if (!match)
|
|
26
|
+
return null;
|
|
27
|
+
const size = BigInt(match.groups.size);
|
|
28
|
+
let unit = match.groups.unit.toUpperCase();
|
|
29
|
+
if (unit.at(-1) == 'B')
|
|
30
|
+
unit = unit.slice(0, -1);
|
|
31
|
+
if (unit.at(-1) == 'I')
|
|
32
|
+
unit = unit.slice(0, -1);
|
|
33
|
+
const unitIndex = BigInt(units.indexOf(unit));
|
|
34
|
+
if (unitIndex === -1n)
|
|
35
|
+
return null;
|
|
36
|
+
return size * 1024n ** unitIndex;
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilium",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.9.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
|
}
|