@tb-dev/utils 7.3.3 → 7.3.5
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/index.js +96 -80
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,111 +1,127 @@
|
|
|
1
|
+
//#region src/array/to-array.ts
|
|
2
|
+
/** Converts the item to an array if it isn't already. */
|
|
1
3
|
function toArray(item) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
const array = item ?? [];
|
|
5
|
+
return Array.isArray(array) ? array : [array];
|
|
4
6
|
}
|
|
5
|
-
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/array/trim-array.ts
|
|
9
|
+
/** Trims each string in the array, removing any empty strings. */
|
|
6
10
|
function trimArray(array, options = {}) {
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
const _array = array.map((it) => it.trim());
|
|
12
|
+
return options.allowEmpty ? _array : _array.filter(Boolean);
|
|
9
13
|
}
|
|
10
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/array/upsert.ts
|
|
16
|
+
/** Pushes an item to the array if it doesn't exist, otherwise updates it. */
|
|
11
17
|
function upsert(array, item, predicate) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const noop = () => {
|
|
23
|
-
};
|
|
24
|
-
|
|
18
|
+
const index = array.findIndex(predicate ?? ((value) => value === item));
|
|
19
|
+
if (index === -1) array.push(item);
|
|
20
|
+
else array[index] = item;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/function/noop.ts
|
|
24
|
+
var noop = () => {};
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/nil/is-nil.ts
|
|
25
27
|
function isNil(value) {
|
|
26
|
-
|
|
28
|
+
return value === void 0 || value === null;
|
|
27
29
|
}
|
|
28
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/panic/index.ts
|
|
32
|
+
/** Throws an error with the given message. */
|
|
29
33
|
function panic(...args) {
|
|
30
|
-
|
|
34
|
+
throw new Error(args.join(" "));
|
|
31
35
|
}
|
|
32
36
|
function todo(...args) {
|
|
33
|
-
|
|
37
|
+
panic(format("not yet implemented", args));
|
|
34
38
|
}
|
|
35
39
|
function unimplemented(...args) {
|
|
36
|
-
|
|
40
|
+
panic(format("not implemented", args));
|
|
37
41
|
}
|
|
38
42
|
function unreachable(...args) {
|
|
39
|
-
|
|
43
|
+
panic(format("unreachable", args));
|
|
40
44
|
}
|
|
41
45
|
function format(base, args) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
return message;
|
|
46
|
+
let message = base;
|
|
47
|
+
if (args.length > 0) message = `${message}: ${args.join(" ")}`;
|
|
48
|
+
return message;
|
|
47
49
|
}
|
|
48
|
-
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/option/unwrap.ts
|
|
49
52
|
function unwrap(value, message = "`unwrap` called with a nil value") {
|
|
50
|
-
|
|
53
|
+
return isNil(value) ? panic(message) : value;
|
|
51
54
|
}
|
|
52
55
|
function unwrapOr(value, other) {
|
|
53
|
-
|
|
56
|
+
return isNil(value) ? other : value;
|
|
54
57
|
}
|
|
55
58
|
function unwrapOrElse(value, fn) {
|
|
56
|
-
|
|
59
|
+
return isNil(value) ? fn() : value;
|
|
57
60
|
}
|
|
58
|
-
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/promise/flush-promises.ts
|
|
63
|
+
/** Flushes pending promises. */
|
|
59
64
|
function flushPromises() {
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
65
|
+
return new Promise((resolve) => void setTimeout(resolve, 0));
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/promise/promise-set.ts
|
|
69
|
+
var PromiseSet = class PromiseSet {
|
|
70
|
+
promises = /* @__PURE__ */ new Set();
|
|
71
|
+
chain(promise) {
|
|
72
|
+
this.promises.add(promise);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
async join() {
|
|
76
|
+
await Promise.all(this.promises);
|
|
77
|
+
this.promises.clear();
|
|
78
|
+
}
|
|
79
|
+
async joinFlushed() {
|
|
80
|
+
await this.join();
|
|
81
|
+
await flushPromises();
|
|
82
|
+
}
|
|
83
|
+
static from(promises) {
|
|
84
|
+
const set = new PromiseSet();
|
|
85
|
+
for (const promise of promises) set.promises.add(promise);
|
|
86
|
+
return set;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/promise/sleep.ts
|
|
86
91
|
function sleep(ms) {
|
|
87
|
-
|
|
92
|
+
return new Promise((resolve) => void setTimeout(resolve, ms));
|
|
88
93
|
}
|
|
89
|
-
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/promise/timeout.ts
|
|
90
96
|
function timeout(f, ms) {
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
97
|
+
return Promise.race([f(), sleep(ms).then(() => null)]);
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/string/split-whitespace.ts
|
|
101
|
+
/**
|
|
102
|
+
* Splits a string or an array of strings (recursively) by whitespace.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* splitWhitespace('a b c'); // ['a', 'b', 'c']
|
|
107
|
+
* splitWhitespace(['a b', 'c']); // ['a', 'b', 'c']
|
|
108
|
+
* splitWhitespace(['a ', ['b', ['c d']]]); // ['a', 'b', 'c', 'd']
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
94
111
|
function splitWhitespace(value, options = {}) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
112
|
+
if (value) {
|
|
113
|
+
if (Array.isArray(value)) return value.map((it) => splitWhitespace(it, options)).flat(Number.POSITIVE_INFINITY);
|
|
114
|
+
value = value.trim().split(/\s/);
|
|
115
|
+
return trimArray(value, options);
|
|
116
|
+
}
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/index.ts
|
|
121
|
+
/** Adds the pixel unit to a value if it is a number. */
|
|
106
122
|
function toPixel(value) {
|
|
107
|
-
|
|
108
|
-
|
|
123
|
+
if (typeof value === "number") return `${value}px`;
|
|
124
|
+
return value;
|
|
109
125
|
}
|
|
110
|
-
|
|
126
|
+
//#endregion
|
|
111
127
|
export { PromiseSet, flushPromises, isNil, noop, panic, sleep, splitWhitespace, timeout, toArray, toPixel, todo, trimArray, unimplemented, unreachable, unwrap, unwrapOr, unwrapOrElse, upsert };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tb-dev/utils",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.5",
|
|
4
4
|
"description": "TypeScript utils",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,17 +24,17 @@
|
|
|
24
24
|
"typescript"
|
|
25
25
|
],
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@tb-dev/eslint-config": "^9.0.
|
|
28
|
-
"@types/node": "^25.
|
|
29
|
-
"@vitest/ui": "^4.0
|
|
30
|
-
"eslint": "^10.0.
|
|
27
|
+
"@tb-dev/eslint-config": "^9.0.3",
|
|
28
|
+
"@types/node": "^25.5.0",
|
|
29
|
+
"@vitest/ui": "^4.1.0",
|
|
30
|
+
"eslint": "^10.0.3",
|
|
31
31
|
"tslib": "^2.8.1",
|
|
32
32
|
"typedoc": "^0.28.17",
|
|
33
33
|
"typedoc-plugin-mdn-links": "^5.1.1",
|
|
34
34
|
"typescript": "^5.9.3",
|
|
35
|
-
"vite": "^
|
|
35
|
+
"vite": "^8.0.0",
|
|
36
36
|
"vite-plugin-dts": "^4.5.4",
|
|
37
|
-
"vitest": "^4.0
|
|
37
|
+
"vitest": "^4.1.0"
|
|
38
38
|
},
|
|
39
39
|
"files": [
|
|
40
40
|
"dist"
|