utilium 3.11.1 → 3.13.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/async-queue.d.ts +13 -0
- package/dist/async-queue.js +45 -0
- package/dist/format.d.ts +6 -0
- package/dist/format.js +21 -0
- package/package.json +3 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs operations in the order they are added, without waiting for them.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AsyncQueue extends EventTarget {
|
|
5
|
+
#private;
|
|
6
|
+
settled(): Promise<unknown>;
|
|
7
|
+
get size(): number;
|
|
8
|
+
/** Call `thunk` once every earlier operation has settled */
|
|
9
|
+
defer(thunk: () => Promise<unknown>): void;
|
|
10
|
+
/** Track an operation that has already started */
|
|
11
|
+
add(promise: Promise<unknown>): void;
|
|
12
|
+
throwIfError(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
/**
|
|
3
|
+
* Runs operations in the order they are added, without waiting for them.
|
|
4
|
+
*/
|
|
5
|
+
export class AsyncQueue extends EventTarget {
|
|
6
|
+
#promise = Promise.resolve();
|
|
7
|
+
settled() {
|
|
8
|
+
return this.#promise;
|
|
9
|
+
}
|
|
10
|
+
/** How many queued operations have not finished */
|
|
11
|
+
#pending = 0;
|
|
12
|
+
get size() {
|
|
13
|
+
return this.#pending;
|
|
14
|
+
}
|
|
15
|
+
#error;
|
|
16
|
+
async #settle(operation) {
|
|
17
|
+
try {
|
|
18
|
+
await operation();
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
this.#error ??= Error.isError(e) ? e : new Error(String(e));
|
|
22
|
+
this.dispatchEvent(new ErrorEvent('error', { error: e }));
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
this.#pending--;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Call `thunk` once every earlier operation has settled */
|
|
29
|
+
defer(thunk) {
|
|
30
|
+
this.#pending++;
|
|
31
|
+
this.#promise = this.#promise.finally(() => this.#settle(thunk));
|
|
32
|
+
}
|
|
33
|
+
/** Track an operation that has already started */
|
|
34
|
+
add(promise) {
|
|
35
|
+
this.#pending++;
|
|
36
|
+
this.#promise = Promise.all([this.#promise, this.#settle(() => promise)]);
|
|
37
|
+
}
|
|
38
|
+
throwIfError() {
|
|
39
|
+
if (!this.#error)
|
|
40
|
+
return;
|
|
41
|
+
const error = this.#error;
|
|
42
|
+
this.#error = undefined;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
package/dist/format.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
|
|
2
2
|
export declare function relativeToDate(date: Date | number, relativeTo?: Date | number): string;
|
|
3
|
+
/**
|
|
4
|
+
* Describes a length of time using its largest units, e.g. `1 hour 5 minutes`.
|
|
5
|
+
* @param milliseconds The length of time
|
|
6
|
+
* @param precision How many units to use at most
|
|
7
|
+
*/
|
|
8
|
+
export declare function timeSpan(milliseconds: number, precision?: number): string;
|
|
3
9
|
export declare function dateRange(date: Date): string;
|
|
4
10
|
export declare function bytes(bytes: bigint | number): string;
|
|
5
11
|
export declare function ms(time: number): string;
|
package/dist/format.js
CHANGED
|
@@ -15,6 +15,27 @@ export function relativeToDate(date, relativeTo = Date.now()) {
|
|
|
15
15
|
const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
|
|
16
16
|
return offset >= 0 ? `in ${text}` : `${text} ago`;
|
|
17
17
|
}
|
|
18
|
+
/** Lengths are in milliseconds */
|
|
19
|
+
const spanUnits = [...rangeUnits, ['second', 1000]];
|
|
20
|
+
/**
|
|
21
|
+
* Describes a length of time using its largest units, e.g. `1 hour 5 minutes`.
|
|
22
|
+
* @param milliseconds The length of time
|
|
23
|
+
* @param precision How many units to use at most
|
|
24
|
+
*/
|
|
25
|
+
export function timeSpan(milliseconds, precision = 2) {
|
|
26
|
+
const distance = Math.abs(milliseconds);
|
|
27
|
+
const largest = Math.max(spanUnits.findIndex(([, length]) => distance >= length), 0);
|
|
28
|
+
const smallest = Math.min(largest + precision - 1, spanUnits.length - 1);
|
|
29
|
+
let rest = Math.round(distance / spanUnits[smallest][1]) * spanUnits[smallest][1];
|
|
30
|
+
const parts = [];
|
|
31
|
+
for (const [unit, length] of spanUnits.slice(largest, smallest + 1)) {
|
|
32
|
+
const count = Math.floor(rest / length);
|
|
33
|
+
rest -= count * length;
|
|
34
|
+
if (count)
|
|
35
|
+
parts.push(`${count} ${unit}${count == 1 ? '' : 's'}`);
|
|
36
|
+
}
|
|
37
|
+
return parts.join(' ') || '0 seconds';
|
|
38
|
+
}
|
|
18
39
|
export function dateRange(date) {
|
|
19
40
|
return `${date.toLocaleString()} (${relativeToDate(date)})`;
|
|
20
41
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilium",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.13.0",
|
|
4
4
|
"description": "Typescript utilities",
|
|
5
5
|
"funding": {
|
|
6
6
|
"type": "individual",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"format:check": "prettier --check .",
|
|
27
27
|
"format": "prettier --write .",
|
|
28
28
|
"lint": "tsc --noEmit && eslint src",
|
|
29
|
-
"test": "
|
|
29
|
+
"test": "node --test test/**/*.test.ts",
|
|
30
30
|
"build": "tsc -b",
|
|
31
31
|
"prepublishOnly": "npx tsc"
|
|
32
32
|
},
|
|
@@ -46,7 +46,6 @@
|
|
|
46
46
|
"eslint": "^10.1.0",
|
|
47
47
|
"globals": "^17.8.0",
|
|
48
48
|
"prettier": "^3.2.5",
|
|
49
|
-
"tsx": "^4.19.1",
|
|
50
49
|
"typedoc": "^0.28.18",
|
|
51
50
|
"typescript": "^6.0.0",
|
|
52
51
|
"typescript-eslint": "^8.58.0"
|
|
@@ -55,7 +54,7 @@
|
|
|
55
54
|
"eventemitter3": "^5.0.1"
|
|
56
55
|
},
|
|
57
56
|
"engines": {
|
|
58
|
-
"node": ">=22
|
|
57
|
+
"node": ">=22"
|
|
59
58
|
},
|
|
60
59
|
"prettier": {
|
|
61
60
|
"singleQuote": true,
|