@terrygonguet/utils 0.7.0 → 0.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/index.d.ts +3 -1
- package/dist/index.js +13 -0
- package/dist/result.d.ts +2 -0
- package/dist/result.js +19 -0
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -8,8 +8,10 @@ declare function exhaustive(_: never): never;
|
|
|
8
8
|
declare function hash(message: string): Promise<ArrayBuffer>;
|
|
9
9
|
declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
10
10
|
declare function yesno(value?: string): boolean;
|
|
11
|
+
declare function mapListPush<Key, T>(map: Map<Key, T[]>, key: Key, value: T): Map<Key, T[]>;
|
|
12
|
+
declare function recordListPush<Key extends string, T>(record: Record<Key, T[]>, key: Key, value: T): Record<Key, T[]>;
|
|
11
13
|
type ExecResult<T, Err = Error> = [null, T] | [Err, null];
|
|
12
14
|
declare function tryCatch<F extends (...args: any) => any>(f: F, ...args: Parameters<F>): ReturnType<F> extends Promise<any> ? Promise<ExecResult<Awaited<ReturnType<F>>>> : ExecResult<ReturnType<F>>;
|
|
13
15
|
declare function tryCatch<Err, F extends (...args: any) => any>(f: F, ...args: Parameters<F>): ReturnType<F> extends Promise<any> ? Promise<ExecResult<Awaited<ReturnType<F>>, Err>> : ExecResult<ReturnType<F>, Err>;
|
|
14
16
|
|
|
15
|
-
export { type ExecResult, clamp, createNoopProxy, exhaustive, hash, noop, range, tryCatch, yesno };
|
|
17
|
+
export { type ExecResult, clamp, createNoopProxy, exhaustive, hash, mapListPush, noop, range, recordListPush, tryCatch, yesno };
|
package/dist/index.js
CHANGED
|
@@ -47,6 +47,17 @@ function yesno(value) {
|
|
|
47
47
|
return false;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
+
function mapListPush(map, key, value) {
|
|
51
|
+
const arr = map.get(key) ?? [];
|
|
52
|
+
arr.push(value);
|
|
53
|
+
return map.set(key, arr);
|
|
54
|
+
}
|
|
55
|
+
function recordListPush(record, key, value) {
|
|
56
|
+
const arr = record[key] ?? [];
|
|
57
|
+
arr.push(value);
|
|
58
|
+
record[key] = arr;
|
|
59
|
+
return record;
|
|
60
|
+
}
|
|
50
61
|
function tryCatch(f, ...args) {
|
|
51
62
|
try {
|
|
52
63
|
const promiseOrResult = f(...args);
|
|
@@ -65,8 +76,10 @@ export {
|
|
|
65
76
|
createNoopProxy,
|
|
66
77
|
exhaustive,
|
|
67
78
|
hash,
|
|
79
|
+
mapListPush,
|
|
68
80
|
noop,
|
|
69
81
|
range,
|
|
82
|
+
recordListPush,
|
|
70
83
|
tryCatch,
|
|
71
84
|
yesno
|
|
72
85
|
};
|
package/dist/result.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ declare class Result<Err extends Error, T> {
|
|
|
11
11
|
error: null;
|
|
12
12
|
};
|
|
13
13
|
andThen<Err2 extends Error, F extends (value: T) => any>(f: F): ReturnType<F> extends Promise<any> ? AsyncResult<Err | Err2, Awaited<ReturnType<F>>> : Result<Err | Err2, ReturnType<F>>;
|
|
14
|
+
recover<Err2 extends Error, F extends (error: Err) => T | Promise<T>>(f: F): ReturnType<F> extends Promise<any> ? AsyncResult<Err2, T> : Result<Err2, T>;
|
|
14
15
|
unwrap(): T;
|
|
15
16
|
unwrapErr(): Err;
|
|
16
17
|
}
|
|
@@ -27,6 +28,7 @@ declare class AsyncResult<Err extends Error, T> {
|
|
|
27
28
|
}>;
|
|
28
29
|
then<U>(cb: (value: Result<Err, T>) => U | Promise<U>): Promise<U>;
|
|
29
30
|
andThen<Err2 extends Error, F extends (value: T) => any>(f: F): AsyncResult<Err | Err2, Awaited<ReturnType<F>>>;
|
|
31
|
+
recover<Err2 extends Error, F extends (error: Err) => T | Promise<T>>(f: F): AsyncResult<Err2, T>;
|
|
30
32
|
unwrap(): Promise<T>;
|
|
31
33
|
unwrapErr(): Promise<Err>;
|
|
32
34
|
}
|
package/dist/result.js
CHANGED
|
@@ -23,6 +23,17 @@ var Result = class _Result {
|
|
|
23
23
|
return new _Result(null, RawError.wrap(error));
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
+
recover(f) {
|
|
27
|
+
if (!this.error) return this;
|
|
28
|
+
try {
|
|
29
|
+
const newValue = f(this.error);
|
|
30
|
+
if (newValue instanceof Promise)
|
|
31
|
+
return new AsyncResult(newValue);
|
|
32
|
+
else return new _Result(newValue, null);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return new _Result(null, RawError.wrap(error));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
26
37
|
unwrap() {
|
|
27
38
|
if (this.value && !this.error) return this.value;
|
|
28
39
|
else throw new Error("Tried to unwrap a failed Result");
|
|
@@ -52,6 +63,14 @@ var AsyncResult = class _AsyncResult {
|
|
|
52
63
|
andThen(f) {
|
|
53
64
|
return new _AsyncResult(this.promise.then((value) => f(value)));
|
|
54
65
|
}
|
|
66
|
+
recover(f) {
|
|
67
|
+
return new _AsyncResult(
|
|
68
|
+
this.promise.then(
|
|
69
|
+
(value) => value,
|
|
70
|
+
(error) => f(error)
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
55
74
|
unwrap() {
|
|
56
75
|
return this.then((result) => result.unwrap());
|
|
57
76
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"prettier": "^3.
|
|
42
|
-
"tsup": "^8.
|
|
43
|
-
"typescript": "^5.
|
|
44
|
-
"vite": "^6.3.
|
|
45
|
-
"vitest": "^3.
|
|
41
|
+
"prettier": "^3.6.2",
|
|
42
|
+
"tsup": "^8.5.0",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"vite": "^6.3.6",
|
|
45
|
+
"vitest": "^3.2.4"
|
|
46
46
|
},
|
|
47
47
|
"author": {
|
|
48
48
|
"email": "terry@gonguet.com",
|