@lodestar/utils 1.6.0-dev.b00d84f2a5 → 1.6.0-dev.e98d8e9fad
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/lib/err.d.ts +34 -0
- package/lib/err.js +81 -0
- package/lib/err.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
package/lib/err.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare const symErr: unique symbol;
|
|
2
|
+
export type Err<T> = {
|
|
3
|
+
[symErr]: true;
|
|
4
|
+
error: T;
|
|
5
|
+
};
|
|
6
|
+
export type Result<T, E> = T | Err<E>;
|
|
7
|
+
export declare function Err<T>(error: T): Err<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Typeguard for Err<T>. Allows the pattern
|
|
10
|
+
* ```ts
|
|
11
|
+
* function getNumSquare(): Result<number, Error> {
|
|
12
|
+
* const value = getNum();
|
|
13
|
+
* if (isErr(value)) {
|
|
14
|
+
* return value; // return as error
|
|
15
|
+
* }
|
|
16
|
+
* return value ** 2;
|
|
17
|
+
* }
|
|
18
|
+
* function getNum(): Result<number, Error>
|
|
19
|
+
* ```
|
|
20
|
+
* Since the non-error is not wrapped, it uses a symbol to prevent collisions
|
|
21
|
+
*/
|
|
22
|
+
export declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
|
|
23
|
+
/**
|
|
24
|
+
* Given an array of results, run a function only on an array of ok results.
|
|
25
|
+
* Returns a new array of results with same length as `results` where the ok
|
|
26
|
+
* value may be Err or T2.
|
|
27
|
+
*/
|
|
28
|
+
export declare function mapOkResults<T1, T2, E>(results: Result<T1, E>[], fn: (items: T1[]) => Result<T2, E>[]): Result<T2, E>[];
|
|
29
|
+
/**
|
|
30
|
+
* See {@link mapOkResults} but `fn` is async
|
|
31
|
+
*/
|
|
32
|
+
export declare function mapOkResultsAsync<T1, T2, E>(results: Result<T1, E>[], fn: (items: T1[]) => Promise<Result<T2, E>[]>): Promise<Result<T2, E>[]>;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=err.d.ts.map
|
package/lib/err.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const symErr = Symbol("err");
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3
|
+
export function Err(error) {
|
|
4
|
+
return { [symErr]: true, error };
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Typeguard for Err<T>. Allows the pattern
|
|
8
|
+
* ```ts
|
|
9
|
+
* function getNumSquare(): Result<number, Error> {
|
|
10
|
+
* const value = getNum();
|
|
11
|
+
* if (isErr(value)) {
|
|
12
|
+
* return value; // return as error
|
|
13
|
+
* }
|
|
14
|
+
* return value ** 2;
|
|
15
|
+
* }
|
|
16
|
+
* function getNum(): Result<number, Error>
|
|
17
|
+
* ```
|
|
18
|
+
* Since the non-error is not wrapped, it uses a symbol to prevent collisions
|
|
19
|
+
*/
|
|
20
|
+
export function isErr(result) {
|
|
21
|
+
return result !== null && typeof result === "object" && result[symErr] === true;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Given an array of results, run a function only on an array of ok results.
|
|
25
|
+
* Returns a new array of results with same length as `results` where the ok
|
|
26
|
+
* value may be Err or T2.
|
|
27
|
+
*/
|
|
28
|
+
export function mapOkResults(results, fn) {
|
|
29
|
+
const oks = [];
|
|
30
|
+
for (let i = 0; i < results.length; i++) {
|
|
31
|
+
const result = results[i];
|
|
32
|
+
if (!isErr(result)) {
|
|
33
|
+
oks.push(result);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const outOksResults = fn(oks);
|
|
37
|
+
if (outOksResults.length !== oks.length) {
|
|
38
|
+
throw Error("mapOkResults fn must return same length");
|
|
39
|
+
}
|
|
40
|
+
const outResults = [];
|
|
41
|
+
for (let i = 0, j = 0; i < results.length; i++) {
|
|
42
|
+
const result = results[i];
|
|
43
|
+
if (isErr(result)) {
|
|
44
|
+
outResults.push(result);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
outResults.push(outOksResults[j]);
|
|
48
|
+
j++;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return outResults;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* See {@link mapOkResults} but `fn` is async
|
|
55
|
+
*/
|
|
56
|
+
export async function mapOkResultsAsync(results, fn) {
|
|
57
|
+
const oks = [];
|
|
58
|
+
for (let i = 0; i < results.length; i++) {
|
|
59
|
+
const result = results[i];
|
|
60
|
+
if (!isErr(result)) {
|
|
61
|
+
oks.push(result);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const outOksResults = await fn(oks);
|
|
65
|
+
if (outOksResults.length !== oks.length) {
|
|
66
|
+
throw Error("mapOkResults fn must return same length");
|
|
67
|
+
}
|
|
68
|
+
const outResults = [];
|
|
69
|
+
for (let i = 0, j = 0; i < results.length; i++) {
|
|
70
|
+
const result = results[i];
|
|
71
|
+
if (isErr(result)) {
|
|
72
|
+
outResults.push(result);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
outResults.push(outOksResults[j]);
|
|
76
|
+
j++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return outResults;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=err.js.map
|
package/lib/err.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"err.js","sourceRoot":"","sources":["../src/err.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;AAM7B,gEAAgE;AAChE,MAAM,UAAU,GAAG,CAAI,KAAQ;IAC7B,OAAO,EAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,KAAK,CAAO,MAAoB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,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACvC,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAE1B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE;YAClB,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SAClB;KACF;IAED,MAAM,aAAa,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;IACpC,IAAI,aAAa,CAAC,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE;QACvC,MAAM,KAAK,CAAC,yCAAyC,CAAC,CAAC;KACxD;IAED,MAAM,UAAU,GAAoB,EAAE,CAAC;IAEvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAE1B,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE;YACjB,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SACzB;aAAM;YACL,UAAU,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;YAClC,CAAC,EAAE,CAAC;SACL;KACF;IAED,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAC,KAAK,EAAe,MAAM,YAAY,CAAC;AAC/C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAmB,OAAO,EAAC,MAAM,YAAY,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAC,KAAK,EAAe,MAAM,YAAY,CAAC;AAC/C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAmB,OAAO,EAAC,MAAM,YAAY,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/ChainSafe/lodestar/issues"
|
|
13
13
|
},
|
|
14
|
-
"version": "1.6.0-dev.
|
|
14
|
+
"version": "1.6.0-dev.e98d8e9fad",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": "./lib/index.js",
|
|
17
17
|
"files": [
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"beacon",
|
|
58
58
|
"blockchain"
|
|
59
59
|
],
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "ec526e04f77f4418f4a90d85639f1ed79ce5b8ea"
|
|
61
61
|
}
|