@carbonteq/fp 0.7.0 → 0.8.0-alpha.1
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/README.md +585 -12
- package/dist/chunk-7QVYU63E.js +2 -3
- package/dist/chunk-7QVYU63E.js.map +1 -1
- package/dist/chunk-PK6SKIKE.cjs +8 -0
- package/dist/chunk-PK6SKIKE.cjs.map +1 -0
- package/dist/index.cjs +42 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +6 -15
- package/dist/index.js.map +1 -1
- package/dist/match.cjs +10 -32
- package/dist/match.cjs.map +1 -1
- package/dist/match.js +5 -7
- package/dist/match.js.map +1 -1
- package/dist/option.cjs +179 -176
- package/dist/option.cjs.map +1 -1
- package/dist/option.d.cts +20 -38
- package/dist/option.d.ts +20 -38
- package/dist/option.js +173 -150
- package/dist/option.js.map +1 -1
- package/dist/result.cjs +346 -213
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +69 -76
- package/dist/result.d.ts +69 -76
- package/dist/result.js +334 -185
- package/dist/result.js.map +1 -1
- package/dist/tuple.cjs +10 -32
- package/dist/tuple.cjs.map +1 -1
- package/dist/tuple.js +5 -7
- package/dist/tuple.js.map +1 -1
- package/dist/unit.cjs +7 -27
- package/dist/unit.cjs.map +1 -1
- package/dist/unit.js +5 -4
- package/dist/unit.js.map +1 -1
- package/package.json +16 -16
package/dist/result.cjs
CHANGED
|
@@ -1,34 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var result_exports = {};
|
|
21
|
-
__export(result_exports, {
|
|
22
|
-
Result: () => Result,
|
|
23
|
-
UnwrappedErrWithOk: () => UnwrappedErrWithOk,
|
|
24
|
-
UnwrappedOkWithErr: () => UnwrappedOkWithErr
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(result_exports);
|
|
27
|
-
var import_types = require("node:util/types");
|
|
28
|
-
var import_unit = require("./unit.js");
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkPK6SKIKE_cjs = require('./chunk-PK6SKIKE.cjs');
|
|
4
|
+
var assert = require('node:assert');
|
|
5
|
+
var types = require('node:util/types');
|
|
6
|
+
var unit_js = require('./unit.js');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var assert__default = /*#__PURE__*/_interopDefault(assert);
|
|
11
|
+
|
|
29
12
|
class UnwrappedErrWithOk extends Error {
|
|
30
13
|
static {
|
|
31
|
-
__name(this, "UnwrappedErrWithOk");
|
|
14
|
+
chunkPK6SKIKE_cjs.__name(this, "UnwrappedErrWithOk");
|
|
32
15
|
}
|
|
33
16
|
constructor(r) {
|
|
34
17
|
super(`Attempted to call unwrapErr on an okay value: <${r}>`);
|
|
@@ -36,253 +19,403 @@ class UnwrappedErrWithOk extends Error {
|
|
|
36
19
|
}
|
|
37
20
|
class UnwrappedOkWithErr extends Error {
|
|
38
21
|
static {
|
|
39
|
-
__name(this, "UnwrappedOkWithErr");
|
|
22
|
+
chunkPK6SKIKE_cjs.__name(this, "UnwrappedOkWithErr");
|
|
40
23
|
}
|
|
41
24
|
constructor(r) {
|
|
42
25
|
super(`Attempted to call unwrap on an Err value: <${r}>`);
|
|
43
26
|
}
|
|
44
27
|
}
|
|
45
|
-
const okPred = /* @__PURE__ */ __name((el) => el.isOk(), "okPred");
|
|
46
|
-
const errPred = /* @__PURE__ */ __name((el) => el.isErr(), "errPred");
|
|
47
|
-
const preds = [
|
|
48
|
-
okPred,
|
|
49
|
-
errPred
|
|
50
|
-
];
|
|
51
28
|
const Sentinel = Symbol.for("ResultSentinel");
|
|
52
29
|
class Result {
|
|
53
30
|
static {
|
|
54
|
-
__name(this, "Result");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
31
|
+
chunkPK6SKIKE_cjs.__name(this, "Result");
|
|
32
|
+
}
|
|
33
|
+
static UNIT_RESULT = new Result(unit_js.UNIT, {
|
|
34
|
+
errSlot: Sentinel
|
|
35
|
+
});
|
|
36
|
+
// Actual value holder. In async ops, will be used to chain. Cannot go back to normal after promise chain starts, although that could be looked into
|
|
37
|
+
#val;
|
|
38
|
+
// to prevent unnecessary chaining - no other purpose. Should be modifiable only within the creating method
|
|
39
|
+
#ctx;
|
|
40
|
+
constructor(val, ctx) {
|
|
41
|
+
this.#val = val;
|
|
42
|
+
this.#ctx = ctx;
|
|
62
43
|
}
|
|
63
44
|
static Ok(val) {
|
|
64
|
-
return new Result(val,
|
|
45
|
+
return new Result(val, {
|
|
46
|
+
errSlot: Sentinel
|
|
47
|
+
});
|
|
65
48
|
}
|
|
66
49
|
static Err(err) {
|
|
67
|
-
return new Result(Sentinel,
|
|
50
|
+
return new Result(Sentinel, {
|
|
51
|
+
errSlot: err
|
|
52
|
+
});
|
|
68
53
|
}
|
|
69
54
|
isOk() {
|
|
70
|
-
return this.
|
|
55
|
+
return this.#ctx.errSlot === Sentinel;
|
|
71
56
|
}
|
|
72
57
|
isErr() {
|
|
73
|
-
return this.
|
|
58
|
+
return this.#ctx.errSlot !== Sentinel;
|
|
74
59
|
}
|
|
75
60
|
isUnit() {
|
|
76
|
-
return this
|
|
61
|
+
return this.#val === unit_js.UNIT;
|
|
77
62
|
}
|
|
78
63
|
toString() {
|
|
79
|
-
if (this.
|
|
80
|
-
return `Result::Ok<${this
|
|
64
|
+
if (this.isOk()) {
|
|
65
|
+
return `Result::Ok<${String(this.#val)}>`;
|
|
81
66
|
}
|
|
82
|
-
return `Result::Err<${this.
|
|
67
|
+
return `Result::Err<${String(this.#ctx.errSlot)}>`;
|
|
83
68
|
}
|
|
84
69
|
unwrap() {
|
|
85
|
-
|
|
86
|
-
|
|
70
|
+
const curr = this.#val;
|
|
71
|
+
if (this.isErr()) {
|
|
72
|
+
const err = this.#ctx.errSlot;
|
|
73
|
+
if (err instanceof Error) throw err;
|
|
87
74
|
throw new UnwrappedOkWithErr(this);
|
|
88
75
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
76
|
+
if (types.isPromise(curr)) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
curr.then((v) => {
|
|
79
|
+
if (v === Sentinel) {
|
|
80
|
+
let e;
|
|
81
|
+
if (this.#ctx.errSlot instanceof Error) e = this.#ctx.errSlot;
|
|
82
|
+
else e = new UnwrappedOkWithErr(this);
|
|
83
|
+
reject(e);
|
|
84
|
+
}
|
|
85
|
+
resolve(v);
|
|
86
|
+
}, reject);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return curr;
|
|
101
90
|
}
|
|
102
91
|
unwrapErr() {
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
const errSlot = this.#ctx.errSlot;
|
|
93
|
+
if (errSlot !== Sentinel) {
|
|
94
|
+
return errSlot;
|
|
105
95
|
}
|
|
106
|
-
|
|
96
|
+
const curr = this.#val;
|
|
97
|
+
if (types.isPromise(curr)) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
curr.then((v) => {
|
|
100
|
+
if (v !== Sentinel) {
|
|
101
|
+
reject(new UnwrappedErrWithOk(this));
|
|
102
|
+
}
|
|
103
|
+
resolve(this.#ctx.errSlot);
|
|
104
|
+
}, reject);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
assert__default.default(curr !== Sentinel, "value must not be Sentinel at this point");
|
|
108
|
+
throw new UnwrappedErrWithOk(this);
|
|
107
109
|
}
|
|
108
|
-
|
|
109
|
-
return this
|
|
110
|
+
safeUnwrap() {
|
|
111
|
+
return this.#val === Sentinel ? null : this.#val;
|
|
110
112
|
}
|
|
111
113
|
map(fn) {
|
|
112
|
-
if (this.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
if (this.isErr()) return Result.Err(this.#ctx.errSlot);
|
|
115
|
+
assert__default.default(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
116
|
+
const curr = this.#val;
|
|
117
|
+
const immutableCtx = this.#ctx;
|
|
118
|
+
const mutableCtx = {
|
|
119
|
+
errSlot: Sentinel
|
|
120
|
+
};
|
|
121
|
+
if (types.isPromise(curr)) {
|
|
122
|
+
const p = curr;
|
|
123
|
+
const out = p.then((v) => {
|
|
124
|
+
if (v === Sentinel) {
|
|
125
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
126
|
+
return Sentinel;
|
|
127
|
+
}
|
|
128
|
+
const mapped = fn(v);
|
|
129
|
+
return mapped;
|
|
130
|
+
});
|
|
131
|
+
return new Result(out, mutableCtx);
|
|
116
132
|
}
|
|
117
|
-
|
|
133
|
+
const next = fn(curr);
|
|
134
|
+
return new Result(next, mutableCtx);
|
|
118
135
|
}
|
|
119
136
|
/** Same as map, but for the error value instead of the Ok value */
|
|
120
137
|
mapErr(fn) {
|
|
121
|
-
if (this.
|
|
122
|
-
const mappedErr = fn(this.
|
|
138
|
+
if (this.isErr()) {
|
|
139
|
+
const mappedErr = fn(this.#ctx.errSlot);
|
|
123
140
|
return Result.Err(mappedErr);
|
|
124
141
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
142
|
+
const curr = this.#val;
|
|
143
|
+
const newCtx = {
|
|
144
|
+
errSlot: Sentinel
|
|
145
|
+
};
|
|
146
|
+
const ctx = this.#ctx;
|
|
147
|
+
if (types.isPromise(curr)) {
|
|
148
|
+
const p = curr.then((v) => {
|
|
149
|
+
if (ctx.errSlot !== Sentinel) {
|
|
150
|
+
const mappedErr = fn(ctx.errSlot);
|
|
151
|
+
newCtx.errSlot = mappedErr;
|
|
152
|
+
}
|
|
153
|
+
return v;
|
|
154
|
+
});
|
|
155
|
+
return new Result(p, newCtx);
|
|
156
|
+
}
|
|
157
|
+
return new Result(this.#val, newCtx);
|
|
134
158
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
159
|
+
mapBoth(fnOk, fnErr) {
|
|
160
|
+
if (this.isErr()) {
|
|
161
|
+
return this.mapErr(fnErr);
|
|
162
|
+
}
|
|
163
|
+
if (types.isPromise(this.#val)) {
|
|
164
|
+
return this.map(fnOk);
|
|
165
|
+
}
|
|
166
|
+
return this.map(fnOk);
|
|
143
167
|
}
|
|
144
168
|
flatMap(fn) {
|
|
145
|
-
if (this.
|
|
146
|
-
|
|
169
|
+
if (this.isErr()) return Result.Err(this.#ctx.errSlot);
|
|
170
|
+
assert__default.default(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
171
|
+
const curr = this.#val;
|
|
172
|
+
const immutableCtx = this.#ctx;
|
|
173
|
+
const mutableCtx = {
|
|
174
|
+
errSlot: Sentinel
|
|
175
|
+
};
|
|
176
|
+
if (types.isPromise(curr)) {
|
|
177
|
+
const newP = new Promise((resolve, reject) => {
|
|
178
|
+
curr.then((v) => {
|
|
179
|
+
if (v === Sentinel) {
|
|
180
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
181
|
+
return resolve(Sentinel);
|
|
182
|
+
}
|
|
183
|
+
const mapped2 = fn(v);
|
|
184
|
+
const p2 = Result.flatMapHelper(mutableCtx, mapped2);
|
|
185
|
+
resolve(p2);
|
|
186
|
+
}, reject);
|
|
187
|
+
});
|
|
188
|
+
return new Result(newP, mutableCtx);
|
|
189
|
+
}
|
|
190
|
+
const mapped = fn(curr);
|
|
191
|
+
const p = Result.flatMapHelper(mutableCtx, mapped);
|
|
192
|
+
return new Result(p, mutableCtx);
|
|
147
193
|
}
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
|
|
194
|
+
static flatMapHelper(mutableCtx, mapped) {
|
|
195
|
+
if (types.isPromise(mapped)) {
|
|
196
|
+
return mapped.then((r) => Result.flatMapInnerHelper(mutableCtx, r));
|
|
197
|
+
}
|
|
198
|
+
return Result.flatMapInnerHelper(mutableCtx, mapped);
|
|
199
|
+
}
|
|
200
|
+
static flatMapInnerHelper(mutableCtx, r) {
|
|
201
|
+
if (types.isPromise(r.#val)) {
|
|
202
|
+
return r.#val.then((v) => {
|
|
203
|
+
if (v === Sentinel) {
|
|
204
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
205
|
+
}
|
|
206
|
+
return v;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (r.#val === Sentinel) {
|
|
210
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
211
|
+
return Sentinel;
|
|
212
|
+
}
|
|
213
|
+
return r.#val;
|
|
151
214
|
}
|
|
152
215
|
zipErr(fn) {
|
|
153
|
-
if (this.
|
|
154
|
-
|
|
155
|
-
if ((0, import_types.isPromise)(r)) {
|
|
156
|
-
return r.then((result) => result.map((_) => this.val));
|
|
216
|
+
if (this.isErr()) {
|
|
217
|
+
return Result.Err(this.#ctx.errSlot);
|
|
157
218
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
219
|
+
assert__default.default(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
220
|
+
const curr = this.#val;
|
|
221
|
+
const immutableCtx = this.#ctx;
|
|
222
|
+
const mutableCtx = {
|
|
223
|
+
errSlot: Sentinel
|
|
224
|
+
};
|
|
225
|
+
if (types.isPromise(curr)) {
|
|
226
|
+
const newP2 = curr.then((v) => {
|
|
227
|
+
if (v === Sentinel) {
|
|
228
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
229
|
+
return Sentinel;
|
|
230
|
+
}
|
|
231
|
+
const r2 = fn(v);
|
|
232
|
+
return Result.zipErrHelper(v, r2, mutableCtx);
|
|
233
|
+
});
|
|
234
|
+
return new Result(newP2, mutableCtx);
|
|
164
235
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
236
|
+
const r = fn(curr);
|
|
237
|
+
const newP = Result.zipErrHelper(curr, r, mutableCtx);
|
|
238
|
+
return new Result(newP, mutableCtx);
|
|
239
|
+
}
|
|
240
|
+
static zipErrHelper(v, r, newCtx) {
|
|
241
|
+
if (types.isPromise(r)) {
|
|
242
|
+
const finalPromise = r.then((newResult) => {
|
|
243
|
+
if (newResult.#ctx.errSlot !== Sentinel) newCtx.errSlot = newResult.#ctx.errSlot;
|
|
244
|
+
return v;
|
|
245
|
+
});
|
|
246
|
+
return finalPromise;
|
|
174
247
|
}
|
|
175
|
-
|
|
248
|
+
if (r.#ctx.errSlot !== Sentinel) newCtx.errSlot = r.#ctx.errSlot;
|
|
249
|
+
return v;
|
|
176
250
|
}
|
|
177
251
|
zip(fn) {
|
|
178
|
-
if (this.
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
252
|
+
if (this.isErr()) return Result.Err(this.#ctx.errSlot);
|
|
253
|
+
const curr = this.#val;
|
|
254
|
+
assert__default.default(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
255
|
+
const immutableCtx = this.#ctx;
|
|
256
|
+
const mutableCtx = {
|
|
257
|
+
errSlot: Sentinel
|
|
258
|
+
};
|
|
259
|
+
if (types.isPromise(curr)) {
|
|
260
|
+
const newP = curr.then((v) => {
|
|
261
|
+
if (v === Sentinel) {
|
|
262
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
263
|
+
return Sentinel;
|
|
264
|
+
}
|
|
265
|
+
const u2 = fn(v);
|
|
266
|
+
if (types.isPromise(u2)) return u2.then((uu) => [
|
|
267
|
+
v,
|
|
268
|
+
uu
|
|
269
|
+
]);
|
|
270
|
+
return [
|
|
271
|
+
v,
|
|
272
|
+
u2
|
|
273
|
+
];
|
|
274
|
+
});
|
|
275
|
+
return new Result(newP, mutableCtx);
|
|
185
276
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (this.val === Sentinel) return Result.Err(this.error);
|
|
193
|
-
const r = fn(this.val);
|
|
194
|
-
if ((0, import_types.isPromise)(r)) {
|
|
195
|
-
return r.then((other) => other.map((u) => [
|
|
196
|
-
this.val,
|
|
197
|
-
u
|
|
198
|
-
]));
|
|
277
|
+
const u = fn(curr);
|
|
278
|
+
if (types.isPromise(u)) {
|
|
279
|
+
return new Result(u.then((uu) => [
|
|
280
|
+
curr,
|
|
281
|
+
uu
|
|
282
|
+
]), mutableCtx);
|
|
199
283
|
}
|
|
200
|
-
return
|
|
201
|
-
|
|
284
|
+
return new Result([
|
|
285
|
+
curr,
|
|
202
286
|
u
|
|
203
|
-
]);
|
|
287
|
+
], mutableCtx);
|
|
204
288
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
289
|
+
flatZip(fn) {
|
|
290
|
+
if (this.isErr()) return Result.Err(this.#ctx.errSlot);
|
|
291
|
+
const curr = this.#val;
|
|
292
|
+
assert__default.default(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
293
|
+
const immutableCtx = this.#ctx;
|
|
294
|
+
const mutableCtx = {
|
|
295
|
+
errSlot: Sentinel
|
|
296
|
+
};
|
|
297
|
+
if (types.isPromise(curr)) {
|
|
298
|
+
const newP = curr.then((v) => {
|
|
299
|
+
if (v === Sentinel) {
|
|
300
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
301
|
+
return v;
|
|
302
|
+
}
|
|
303
|
+
const mapped2 = fn(v);
|
|
304
|
+
return Result.flatZipHelper(mutableCtx, v, mapped2);
|
|
305
|
+
});
|
|
306
|
+
return new Result(newP, mutableCtx);
|
|
209
307
|
}
|
|
210
|
-
|
|
308
|
+
const mapped = fn(curr);
|
|
309
|
+
const p = Result.flatZipHelper(mutableCtx, curr, mapped);
|
|
310
|
+
return new Result(p, mutableCtx);
|
|
211
311
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
for (const el of seq) {
|
|
216
|
-
if (pred(el)) return true;
|
|
312
|
+
static flatZipHelper(mutableCtx, originalVal, mapped) {
|
|
313
|
+
if (types.isPromise(mapped)) {
|
|
314
|
+
return mapped.then((r) => Result.flatZipInnerHelper(mutableCtx, originalVal, r));
|
|
217
315
|
}
|
|
218
|
-
return
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
vals.push(r.unwrap());
|
|
316
|
+
return Result.flatZipInnerHelper(mutableCtx, originalVal, mapped);
|
|
317
|
+
}
|
|
318
|
+
static flatZipInnerHelper(mutableCtx, originalVal, r) {
|
|
319
|
+
if (types.isPromise(r.#val)) {
|
|
320
|
+
return r.#val.then((v) => {
|
|
321
|
+
if (v === Sentinel) {
|
|
322
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
323
|
+
}
|
|
324
|
+
return [
|
|
325
|
+
originalVal,
|
|
326
|
+
v
|
|
327
|
+
];
|
|
328
|
+
});
|
|
232
329
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
330
|
+
if (r.#val === Sentinel) {
|
|
331
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
332
|
+
return Sentinel;
|
|
333
|
+
}
|
|
334
|
+
return [
|
|
335
|
+
originalVal,
|
|
336
|
+
r.#val
|
|
337
|
+
];
|
|
239
338
|
}
|
|
240
339
|
//#endregion
|
|
241
|
-
// static innerMap<T, U, E>(
|
|
242
|
-
// r: Result<Array<T>, E>,
|
|
243
|
-
// mapper: (val: T) => U,
|
|
244
|
-
// ): Result<Array<U>, E> {
|
|
245
|
-
// return r.map((inner) => inner.map(mapper));
|
|
246
|
-
// }
|
|
247
|
-
// innerMap<Inner, U>(
|
|
248
|
-
// this: Result<Array<Inner>, E>,
|
|
249
|
-
// mapper: (val: T) => U,
|
|
250
|
-
// ): Result<Array<U>, E>;
|
|
251
|
-
// innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U): never;
|
|
252
|
-
// innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U) {
|
|
253
|
-
// if (this.val === Sentinel) return this;
|
|
254
|
-
//
|
|
255
|
-
// if (Array.isArray(this.val)) {
|
|
256
|
-
// return Result.Ok(this.val.map(mapper));
|
|
257
|
-
// }
|
|
258
|
-
//
|
|
259
|
-
// throw new Error("Can only be called for Result<Array<T>, E>");
|
|
260
|
-
// }
|
|
261
340
|
innerMap(mapper) {
|
|
262
|
-
if (this.
|
|
263
|
-
if (Array.isArray(this
|
|
264
|
-
return Result
|
|
341
|
+
if (this.isErr()) return this;
|
|
342
|
+
if (Array.isArray(this.#val)) {
|
|
343
|
+
return new Result(this.#val.map(mapper), this.#ctx);
|
|
265
344
|
}
|
|
266
345
|
throw new Error("Can only be called for Result<Array<T>, E>");
|
|
267
346
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
347
|
+
async toPromise() {
|
|
348
|
+
const v = await this.#val;
|
|
349
|
+
return new Result(v, this.#ctx);
|
|
350
|
+
}
|
|
351
|
+
validate(validators) {
|
|
352
|
+
if (this.isErr()) return this;
|
|
353
|
+
const currVal = this.#val;
|
|
354
|
+
const currCtx = this.#ctx;
|
|
355
|
+
const mutableCtx = {
|
|
356
|
+
errSlot: Sentinel
|
|
357
|
+
};
|
|
358
|
+
if (types.isPromise(currVal)) {
|
|
359
|
+
return new Result(currVal.then(async (c) => {
|
|
360
|
+
if (currCtx.errSlot !== Sentinel) {
|
|
361
|
+
mutableCtx.errSlot = currCtx.errSlot;
|
|
362
|
+
return Sentinel;
|
|
363
|
+
}
|
|
364
|
+
const results2 = validators.map((v) => v(currVal));
|
|
365
|
+
return Promise.all(results2).then((resolved) => Result.validateHelper(resolved, mutableCtx, c));
|
|
366
|
+
}), mutableCtx);
|
|
367
|
+
}
|
|
368
|
+
const results = validators.map((v) => v(currVal));
|
|
369
|
+
if (results.some(types.isPromise)) {
|
|
370
|
+
return new Result(Promise.all(results).then((resolved) => Result.validateHelper(resolved, mutableCtx, currVal)), mutableCtx);
|
|
371
|
+
}
|
|
372
|
+
const values = results.map((r) => r.#val);
|
|
373
|
+
if (values.some(types.isPromise)) {
|
|
374
|
+
return new Result(Result.validateHelper(results, mutableCtx, currVal), mutableCtx);
|
|
375
|
+
}
|
|
376
|
+
const combinedRes = Result.all(...results);
|
|
377
|
+
return combinedRes.isErr() ? combinedRes : this;
|
|
378
|
+
}
|
|
379
|
+
static validateHelper(results, currCtx, currVal) {
|
|
380
|
+
const combinedRes = Result.all(...results);
|
|
381
|
+
if (types.isPromise(combinedRes)) {
|
|
382
|
+
return combinedRes.then((cRes) => {
|
|
383
|
+
if (cRes.isErr()) {
|
|
384
|
+
currCtx.errSlot = cRes.#ctx.errSlot;
|
|
385
|
+
return Sentinel;
|
|
386
|
+
}
|
|
387
|
+
return currVal;
|
|
388
|
+
});
|
|
272
389
|
}
|
|
273
|
-
|
|
390
|
+
if (combinedRes.isErr()) {
|
|
391
|
+
currCtx.errSlot = combinedRes.#ctx.errSlot;
|
|
392
|
+
return Sentinel;
|
|
393
|
+
}
|
|
394
|
+
return currVal;
|
|
395
|
+
}
|
|
396
|
+
static all(...results) {
|
|
397
|
+
const vals = results.map((r) => r.#val);
|
|
398
|
+
const mutableCtx = {
|
|
399
|
+
errSlot: Sentinel
|
|
400
|
+
};
|
|
401
|
+
if (vals.some(types.isPromise)) {
|
|
402
|
+
return new Result(Promise.all(vals).then((v) => {
|
|
403
|
+
if (results.some((r) => r.isErr())) {
|
|
404
|
+
mutableCtx.errSlot = results.filter((r) => r.isErr()).map((r) => r.#ctx.errSlot);
|
|
405
|
+
return Sentinel;
|
|
406
|
+
}
|
|
407
|
+
return v;
|
|
408
|
+
}), mutableCtx);
|
|
409
|
+
}
|
|
410
|
+
if (results.some((r) => r.isErr())) {
|
|
411
|
+
return Result.Err(results.filter((r) => r.isErr()).map((r) => r.#ctx.errSlot));
|
|
412
|
+
}
|
|
413
|
+
return Result.Ok(vals);
|
|
274
414
|
}
|
|
275
415
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const res2 = Result.Ok(123);
|
|
282
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
283
|
-
0 && (module.exports = {
|
|
284
|
-
Result,
|
|
285
|
-
UnwrappedErrWithOk,
|
|
286
|
-
UnwrappedOkWithErr
|
|
287
|
-
});
|
|
416
|
+
|
|
417
|
+
exports.Result = Result;
|
|
418
|
+
exports.UnwrappedErrWithOk = UnwrappedErrWithOk;
|
|
419
|
+
exports.UnwrappedOkWithErr = UnwrappedOkWithErr;
|
|
420
|
+
//# sourceMappingURL=result.cjs.map
|
|
288
421
|
//# sourceMappingURL=result.cjs.map
|