@ls-stack/utils 2.14.0 → 3.1.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/assertions.cjs +5 -5
- package/dist/assertions.d.cts +2 -2
- package/dist/assertions.d.ts +2 -2
- package/dist/assertions.js +1 -1
- package/dist/cache.cjs +13 -10
- package/dist/cache.d.cts +6 -6
- package/dist/cache.d.ts +6 -6
- package/dist/cache.js +11 -11
- package/dist/castValues.cjs +12 -4
- package/dist/castValues.js +1 -1
- package/dist/{chunk-4UGSP3L3.js → chunk-2TIVYE43.js} +5 -5
- package/dist/{chunk-T5WDDPFI.js → chunk-4DCLNY64.js} +1 -1
- package/dist/{chunk-KBFP7INB.js → chunk-55DQGPY3.js} +67 -38
- package/dist/{chunk-RK6PT7JY.js → chunk-5MNYPLZI.js} +1 -1
- package/dist/chunk-II4R3VVX.js +25 -0
- package/dist/createThrottleController.js +4 -4
- package/dist/enhancedMap.js +2 -2
- package/dist/exhaustiveMatch.cjs +13 -2
- package/dist/exhaustiveMatch.d.cts +4 -1
- package/dist/exhaustiveMatch.d.ts +4 -1
- package/dist/exhaustiveMatch.js +11 -1
- package/dist/getValueStableKey.cjs +83 -0
- package/dist/getValueStableKey.d.cts +10 -0
- package/dist/getValueStableKey.d.ts +10 -0
- package/dist/getValueStableKey.js +55 -0
- package/dist/interpolate.js +1 -1
- package/dist/main.d.ts +2 -2
- package/dist/parallelAsyncCalls.cjs +51 -18
- package/dist/parallelAsyncCalls.d.cts +1 -2
- package/dist/parallelAsyncCalls.d.ts +1 -2
- package/dist/parallelAsyncCalls.js +5 -5
- package/dist/testUtils.js +1 -1
- package/dist/time.cjs +12 -4
- package/dist/time.js +2 -2
- package/dist/{rsResult.cjs → tsResult.cjs} +74 -43
- package/dist/{rsResult.d.cts → tsResult.d.cts} +34 -23
- package/dist/{rsResult.d.ts → tsResult.d.ts} +34 -23
- package/dist/tsResult.js +16 -0
- package/dist/typingTestUtils.cjs +4 -1
- package/dist/typingTestUtils.d.cts +26 -0
- package/dist/typingTestUtils.d.ts +26 -0
- package/dist/typingTestUtils.js +4 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.js +1 -1
- package/package.json +15 -15
- package/dist/chunk-GBFS2I67.js +0 -17
- package/dist/getObjStableKey.cjs +0 -83
- package/dist/getObjStableKey.d.cts +0 -3
- package/dist/getObjStableKey.d.ts +0 -3
- package/dist/getObjStableKey.js +0 -44
- package/dist/rsResult.js +0 -20
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/getValueStableKey.ts
|
|
21
|
+
var getValueStableKey_exports = {};
|
|
22
|
+
__export(getValueStableKey_exports, {
|
|
23
|
+
getValueStableKey: () => getValueStableKey
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(getValueStableKey_exports);
|
|
26
|
+
|
|
27
|
+
// src/assertions.ts
|
|
28
|
+
function isObject(value) {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/getValueStableKey.ts
|
|
33
|
+
function getValueStableKey(input, maxSortingDepth = 3) {
|
|
34
|
+
if (typeof input === "string") return `"${input}`;
|
|
35
|
+
if (!input || typeof input !== "object") return `$${input}`;
|
|
36
|
+
return stringifyCompact(input, maxSortingDepth, 0, /* @__PURE__ */ new WeakSet());
|
|
37
|
+
}
|
|
38
|
+
function stringifyCompact(input, maxSortingDepth, depth, refs) {
|
|
39
|
+
const isJsObj = input && typeof input === "object";
|
|
40
|
+
if (isJsObj) {
|
|
41
|
+
if (refs.has(input)) {
|
|
42
|
+
throw new Error("Circular reference detected");
|
|
43
|
+
}
|
|
44
|
+
refs.add(input);
|
|
45
|
+
}
|
|
46
|
+
let result;
|
|
47
|
+
if (Array.isArray(input)) {
|
|
48
|
+
result = "[";
|
|
49
|
+
for (const v of input) {
|
|
50
|
+
if (result.length > 1) result += ",";
|
|
51
|
+
result += stringifyCompact(v, maxSortingDepth, depth + 1, refs);
|
|
52
|
+
}
|
|
53
|
+
result += "]";
|
|
54
|
+
} else if (isObject(input)) {
|
|
55
|
+
let entries = Object.entries(input);
|
|
56
|
+
if (entries.length === 0) {
|
|
57
|
+
result = "{}";
|
|
58
|
+
} else {
|
|
59
|
+
if (depth < maxSortingDepth) {
|
|
60
|
+
entries = entries.sort(
|
|
61
|
+
([a], [b]) => a < b ? -1 : a > b ? 1 : 0
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
result = "{";
|
|
65
|
+
for (const [k, v] of entries) {
|
|
66
|
+
if (v === void 0) continue;
|
|
67
|
+
if (result.length > 1) result += ",";
|
|
68
|
+
result += `${k}:${stringifyCompact(v, maxSortingDepth, depth + 1, refs)}`;
|
|
69
|
+
}
|
|
70
|
+
result += "}";
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
result = JSON.stringify(input);
|
|
74
|
+
}
|
|
75
|
+
if (isJsObj) {
|
|
76
|
+
refs.delete(input);
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
getValueStableKey
|
|
83
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stable key for the input value.
|
|
3
|
+
*
|
|
4
|
+
* @param input - The value to get a stable key for.
|
|
5
|
+
* @param maxSortingDepth - The maximum depth to sort the input value. Default is 3.
|
|
6
|
+
* @returns A stable key for the input value.
|
|
7
|
+
*/
|
|
8
|
+
declare function getValueStableKey(input: unknown, maxSortingDepth?: number): string;
|
|
9
|
+
|
|
10
|
+
export { getValueStableKey };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stable key for the input value.
|
|
3
|
+
*
|
|
4
|
+
* @param input - The value to get a stable key for.
|
|
5
|
+
* @param maxSortingDepth - The maximum depth to sort the input value. Default is 3.
|
|
6
|
+
* @returns A stable key for the input value.
|
|
7
|
+
*/
|
|
8
|
+
declare function getValueStableKey(input: unknown, maxSortingDepth?: number): string;
|
|
9
|
+
|
|
10
|
+
export { getValueStableKey };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isObject
|
|
3
|
+
} from "./chunk-2TIVYE43.js";
|
|
4
|
+
|
|
5
|
+
// src/getValueStableKey.ts
|
|
6
|
+
function getValueStableKey(input, maxSortingDepth = 3) {
|
|
7
|
+
if (typeof input === "string") return `"${input}`;
|
|
8
|
+
if (!input || typeof input !== "object") return `$${input}`;
|
|
9
|
+
return stringifyCompact(input, maxSortingDepth, 0, /* @__PURE__ */ new WeakSet());
|
|
10
|
+
}
|
|
11
|
+
function stringifyCompact(input, maxSortingDepth, depth, refs) {
|
|
12
|
+
const isJsObj = input && typeof input === "object";
|
|
13
|
+
if (isJsObj) {
|
|
14
|
+
if (refs.has(input)) {
|
|
15
|
+
throw new Error("Circular reference detected");
|
|
16
|
+
}
|
|
17
|
+
refs.add(input);
|
|
18
|
+
}
|
|
19
|
+
let result;
|
|
20
|
+
if (Array.isArray(input)) {
|
|
21
|
+
result = "[";
|
|
22
|
+
for (const v of input) {
|
|
23
|
+
if (result.length > 1) result += ",";
|
|
24
|
+
result += stringifyCompact(v, maxSortingDepth, depth + 1, refs);
|
|
25
|
+
}
|
|
26
|
+
result += "]";
|
|
27
|
+
} else if (isObject(input)) {
|
|
28
|
+
let entries = Object.entries(input);
|
|
29
|
+
if (entries.length === 0) {
|
|
30
|
+
result = "{}";
|
|
31
|
+
} else {
|
|
32
|
+
if (depth < maxSortingDepth) {
|
|
33
|
+
entries = entries.sort(
|
|
34
|
+
([a], [b]) => a < b ? -1 : a > b ? 1 : 0
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
result = "{";
|
|
38
|
+
for (const [k, v] of entries) {
|
|
39
|
+
if (v === void 0) continue;
|
|
40
|
+
if (result.length > 1) result += ",";
|
|
41
|
+
result += `${k}:${stringifyCompact(v, maxSortingDepth, depth + 1, refs)}`;
|
|
42
|
+
}
|
|
43
|
+
result += "}";
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
result = JSON.stringify(input);
|
|
47
|
+
}
|
|
48
|
+
if (isJsObj) {
|
|
49
|
+
refs.delete(input);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
getValueStableKey
|
|
55
|
+
};
|
package/dist/interpolate.js
CHANGED
package/dist/main.d.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
///<reference path="deepEqual.d.ts" />
|
|
11
11
|
///<reference path="enhancedMap.d.ts" />
|
|
12
12
|
///<reference path="exhaustiveMatch.d.ts" />
|
|
13
|
-
///<reference path="
|
|
13
|
+
///<reference path="getValueStableKey.d.ts" />
|
|
14
14
|
///<reference path="internalUtils.d.ts" />
|
|
15
15
|
///<reference path="interpolate.d.ts" />
|
|
16
16
|
///<reference path="levenshtein.d.ts" />
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
///<reference path="parallelAsyncCalls.d.ts" />
|
|
21
21
|
///<reference path="promiseUtils.d.ts" />
|
|
22
22
|
///<reference path="retryOnError.d.ts" />
|
|
23
|
-
///<reference path="rsResult.d.ts" />
|
|
24
23
|
///<reference path="runShellCmd.d.ts" />
|
|
25
24
|
///<reference path="safeJson.d.ts" />
|
|
26
25
|
///<reference path="shallowEqual.d.ts" />
|
|
@@ -28,6 +27,7 @@
|
|
|
28
27
|
///<reference path="stringUtils.d.ts" />
|
|
29
28
|
///<reference path="testUtils.d.ts" />
|
|
30
29
|
///<reference path="time.d.ts" />
|
|
30
|
+
///<reference path="tsResult.d.ts" />
|
|
31
31
|
///<reference path="typingFnUtils.d.ts" />
|
|
32
32
|
///<reference path="typingTestUtils.d.ts" />
|
|
33
33
|
///<reference path="typingUtils.d.ts" />
|
|
@@ -34,6 +34,11 @@ function isObject(value) {
|
|
|
34
34
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// src/sleep.ts
|
|
38
|
+
function sleep(ms) {
|
|
39
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
40
|
+
}
|
|
41
|
+
|
|
37
42
|
// src/safeJson.ts
|
|
38
43
|
function safeJsonStringify(value) {
|
|
39
44
|
try {
|
|
@@ -43,7 +48,7 @@ function safeJsonStringify(value) {
|
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
// src/
|
|
51
|
+
// src/tsResult.ts
|
|
47
52
|
function okUnwrapOr() {
|
|
48
53
|
return this.value;
|
|
49
54
|
}
|
|
@@ -62,28 +67,40 @@ function mapOkAndErr({
|
|
|
62
67
|
function returnResult() {
|
|
63
68
|
return this;
|
|
64
69
|
}
|
|
70
|
+
function okOnOk(fn) {
|
|
71
|
+
if (this.ok) {
|
|
72
|
+
fn(this.value);
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
function errOnErr(fn) {
|
|
77
|
+
if (!this.ok) {
|
|
78
|
+
fn(this.error);
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
65
82
|
function ok(value = void 0) {
|
|
66
|
-
|
|
67
|
-
ok: true,
|
|
68
|
-
error: false,
|
|
69
|
-
value,
|
|
83
|
+
const methods = {
|
|
70
84
|
unwrapOrNull: okUnwrapOr,
|
|
71
85
|
unwrapOr: okUnwrapOr,
|
|
72
86
|
unwrap: okUnwrapOr,
|
|
73
87
|
mapOk: okMap,
|
|
74
88
|
mapErr: returnResult,
|
|
75
|
-
mapOkAndErr
|
|
89
|
+
mapOkAndErr,
|
|
90
|
+
ifOk: okOnOk,
|
|
91
|
+
ifErr: returnResult
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
error: false,
|
|
96
|
+
value,
|
|
97
|
+
...methods
|
|
76
98
|
};
|
|
77
99
|
}
|
|
78
100
|
function err(error) {
|
|
79
|
-
|
|
80
|
-
ok: false,
|
|
81
|
-
error,
|
|
101
|
+
const methods = {
|
|
82
102
|
unwrapOrNull: () => null,
|
|
83
103
|
unwrapOr: (defaultValue) => defaultValue,
|
|
84
|
-
errorResult() {
|
|
85
|
-
return err(error);
|
|
86
|
-
},
|
|
87
104
|
unwrap: () => {
|
|
88
105
|
if (error instanceof Error) {
|
|
89
106
|
throw error;
|
|
@@ -92,7 +109,17 @@ function err(error) {
|
|
|
92
109
|
},
|
|
93
110
|
mapOk: returnResult,
|
|
94
111
|
mapErr: errMap,
|
|
95
|
-
mapOkAndErr
|
|
112
|
+
mapOkAndErr,
|
|
113
|
+
ifOk: returnResult,
|
|
114
|
+
ifErr: errOnErr
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
error,
|
|
119
|
+
errorResult() {
|
|
120
|
+
return err(error);
|
|
121
|
+
},
|
|
122
|
+
...methods
|
|
96
123
|
};
|
|
97
124
|
}
|
|
98
125
|
function unknownToResultError(error) {
|
|
@@ -132,7 +159,8 @@ var Result = {
|
|
|
132
159
|
err,
|
|
133
160
|
unknownToError: unknownToResultError,
|
|
134
161
|
asyncUnwrap,
|
|
135
|
-
asyncMap
|
|
162
|
+
asyncMap,
|
|
163
|
+
getOkErr
|
|
136
164
|
};
|
|
137
165
|
function unknownToError(error) {
|
|
138
166
|
if (error instanceof Error) return error;
|
|
@@ -149,10 +177,15 @@ function unknownToError(error) {
|
|
|
149
177
|
cause: error
|
|
150
178
|
});
|
|
151
179
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
180
|
+
var typedResult = {
|
|
181
|
+
ok,
|
|
182
|
+
err,
|
|
183
|
+
get _type() {
|
|
184
|
+
throw new Error("usage as value is not allowed");
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
function getOkErr() {
|
|
188
|
+
return typedResult;
|
|
156
189
|
}
|
|
157
190
|
|
|
158
191
|
// src/parallelAsyncCalls.ts
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Result } from './
|
|
2
|
-
import './safeJson.cjs';
|
|
1
|
+
import { Result } from './tsResult.cjs';
|
|
3
2
|
|
|
4
3
|
type ValidMetadata = string | number | boolean | Record<string, unknown>;
|
|
5
4
|
type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Result } from './
|
|
2
|
-
import './safeJson.js';
|
|
1
|
+
import { Result } from './tsResult.js';
|
|
3
2
|
|
|
4
3
|
type ValidMetadata = string | number | boolean | Record<string, unknown>;
|
|
5
4
|
type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
sleep
|
|
3
|
-
} from "./chunk-5DZT3Z5Z.js";
|
|
4
1
|
import {
|
|
5
2
|
Result,
|
|
6
3
|
unknownToError
|
|
7
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-55DQGPY3.js";
|
|
5
|
+
import {
|
|
6
|
+
sleep
|
|
7
|
+
} from "./chunk-5DZT3Z5Z.js";
|
|
8
8
|
import "./chunk-VAAMRG4K.js";
|
|
9
9
|
import {
|
|
10
10
|
invariant,
|
|
11
11
|
isObject
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-2TIVYE43.js";
|
|
13
13
|
|
|
14
14
|
// src/parallelAsyncCalls.ts
|
|
15
15
|
var ParallelAsyncResultCalls = class {
|
package/dist/testUtils.js
CHANGED
package/dist/time.cjs
CHANGED
|
@@ -41,11 +41,19 @@ module.exports = __toCommonJS(time_exports);
|
|
|
41
41
|
|
|
42
42
|
// src/castValues.ts
|
|
43
43
|
function castToNumber(value) {
|
|
44
|
-
return
|
|
44
|
+
return isFiniteNumeric(value) ? Number(value) : null;
|
|
45
45
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
function isFiniteNumeric(num) {
|
|
47
|
+
switch (typeof num) {
|
|
48
|
+
case "number":
|
|
49
|
+
return num - num === 0;
|
|
50
|
+
case "string":
|
|
51
|
+
return num.trim() !== "" && Number.isFinite(+num);
|
|
52
|
+
case "bigint":
|
|
53
|
+
return Number.isFinite(Number(num));
|
|
54
|
+
default:
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
// src/mathUtils.ts
|
package/dist/time.js
CHANGED
|
@@ -15,9 +15,9 @@ import {
|
|
|
15
15
|
getUnixSeconds,
|
|
16
16
|
msToTimeString,
|
|
17
17
|
parseTimeStringToMs
|
|
18
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-5MNYPLZI.js";
|
|
19
19
|
import "./chunk-HTCYUMDR.js";
|
|
20
|
-
import "./chunk-
|
|
20
|
+
import "./chunk-II4R3VVX.js";
|
|
21
21
|
export {
|
|
22
22
|
DAY_AS_MS,
|
|
23
23
|
DAY_AS_SECS,
|
|
@@ -17,23 +17,27 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
20
|
+
// src/tsResult.ts
|
|
21
|
+
var tsResult_exports = {};
|
|
22
|
+
__export(tsResult_exports, {
|
|
23
23
|
Result: () => Result,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
normalizeError: () => normalizeError,
|
|
24
|
+
err: () => err,
|
|
25
|
+
ok: () => ok,
|
|
27
26
|
resultify: () => resultify,
|
|
28
|
-
safeJsonStringify: () => safeJsonStringify2,
|
|
29
27
|
unknownToError: () => unknownToError
|
|
30
28
|
});
|
|
31
|
-
module.exports = __toCommonJS(
|
|
29
|
+
module.exports = __toCommonJS(tsResult_exports);
|
|
32
30
|
|
|
33
31
|
// src/assertions.ts
|
|
34
32
|
function isObject(value) {
|
|
35
33
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
34
|
}
|
|
35
|
+
function isFunction(value) {
|
|
36
|
+
return typeof value === "function";
|
|
37
|
+
}
|
|
38
|
+
function isPromise(value) {
|
|
39
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
40
|
+
}
|
|
37
41
|
|
|
38
42
|
// src/safeJson.ts
|
|
39
43
|
function safeJsonStringify(value) {
|
|
@@ -44,7 +48,7 @@ function safeJsonStringify(value) {
|
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
// src/
|
|
51
|
+
// src/tsResult.ts
|
|
48
52
|
function okUnwrapOr() {
|
|
49
53
|
return this.value;
|
|
50
54
|
}
|
|
@@ -63,28 +67,40 @@ function mapOkAndErr({
|
|
|
63
67
|
function returnResult() {
|
|
64
68
|
return this;
|
|
65
69
|
}
|
|
70
|
+
function okOnOk(fn) {
|
|
71
|
+
if (this.ok) {
|
|
72
|
+
fn(this.value);
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
function errOnErr(fn) {
|
|
77
|
+
if (!this.ok) {
|
|
78
|
+
fn(this.error);
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
66
82
|
function ok(value = void 0) {
|
|
67
|
-
|
|
68
|
-
ok: true,
|
|
69
|
-
error: false,
|
|
70
|
-
value,
|
|
83
|
+
const methods = {
|
|
71
84
|
unwrapOrNull: okUnwrapOr,
|
|
72
85
|
unwrapOr: okUnwrapOr,
|
|
73
86
|
unwrap: okUnwrapOr,
|
|
74
87
|
mapOk: okMap,
|
|
75
88
|
mapErr: returnResult,
|
|
76
|
-
mapOkAndErr
|
|
89
|
+
mapOkAndErr,
|
|
90
|
+
ifOk: okOnOk,
|
|
91
|
+
ifErr: returnResult
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
error: false,
|
|
96
|
+
value,
|
|
97
|
+
...methods
|
|
77
98
|
};
|
|
78
99
|
}
|
|
79
100
|
function err(error) {
|
|
80
|
-
|
|
81
|
-
ok: false,
|
|
82
|
-
error,
|
|
101
|
+
const methods = {
|
|
83
102
|
unwrapOrNull: () => null,
|
|
84
103
|
unwrapOr: (defaultValue) => defaultValue,
|
|
85
|
-
errorResult() {
|
|
86
|
-
return err(error);
|
|
87
|
-
},
|
|
88
104
|
unwrap: () => {
|
|
89
105
|
if (error instanceof Error) {
|
|
90
106
|
throw error;
|
|
@@ -93,7 +109,17 @@ function err(error) {
|
|
|
93
109
|
},
|
|
94
110
|
mapOk: returnResult,
|
|
95
111
|
mapErr: errMap,
|
|
96
|
-
mapOkAndErr
|
|
112
|
+
mapOkAndErr,
|
|
113
|
+
ifOk: returnResult,
|
|
114
|
+
ifErr: errOnErr
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
error,
|
|
119
|
+
errorResult() {
|
|
120
|
+
return err(error);
|
|
121
|
+
},
|
|
122
|
+
...methods
|
|
97
123
|
};
|
|
98
124
|
}
|
|
99
125
|
function unknownToResultError(error) {
|
|
@@ -133,20 +159,27 @@ var Result = {
|
|
|
133
159
|
err,
|
|
134
160
|
unknownToError: unknownToResultError,
|
|
135
161
|
asyncUnwrap,
|
|
136
|
-
asyncMap
|
|
162
|
+
asyncMap,
|
|
163
|
+
getOkErr
|
|
137
164
|
};
|
|
138
165
|
function resultify(fn, errorNormalizer) {
|
|
139
|
-
|
|
140
|
-
return
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
166
|
+
if (!isFunction(fn)) {
|
|
167
|
+
return fn.then((value) => ok(value)).catch(
|
|
168
|
+
(error) => err(
|
|
169
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
170
|
+
)
|
|
144
171
|
);
|
|
145
172
|
}
|
|
146
|
-
}
|
|
147
|
-
async function asyncResultify(fn, errorNormalizer) {
|
|
148
173
|
try {
|
|
149
|
-
|
|
174
|
+
const result = fn();
|
|
175
|
+
if (isPromise(result)) {
|
|
176
|
+
return result.then((value) => ok(value)).catch(
|
|
177
|
+
(error) => err(
|
|
178
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
179
|
+
)
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
return ok(result);
|
|
150
183
|
} catch (error) {
|
|
151
184
|
return err(
|
|
152
185
|
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
@@ -168,23 +201,21 @@ function unknownToError(error) {
|
|
|
168
201
|
cause: error
|
|
169
202
|
});
|
|
170
203
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
204
|
+
var typedResult = {
|
|
205
|
+
ok,
|
|
206
|
+
err,
|
|
207
|
+
get _type() {
|
|
208
|
+
throw new Error("usage as value is not allowed");
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
function getOkErr() {
|
|
212
|
+
return typedResult;
|
|
180
213
|
}
|
|
181
214
|
// Annotate the CommonJS export names for ESM import in node:
|
|
182
215
|
0 && (module.exports = {
|
|
183
216
|
Result,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
normalizeError,
|
|
217
|
+
err,
|
|
218
|
+
ok,
|
|
187
219
|
resultify,
|
|
188
|
-
safeJsonStringify,
|
|
189
220
|
unknownToError
|
|
190
221
|
});
|