@carbonteq/fp 0.8.0-alpha.3 → 0.8.0-alpha.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.cjs +660 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +190 -4
- package/dist/index.d.ts +190 -4
- package/dist/index.js +649 -5
- package/dist/index.js.map +1 -1
- package/package.json +9 -6
- package/dist/chunk-7QVYU63E.js +0 -6
- package/dist/chunk-7QVYU63E.js.map +0 -1
- package/dist/chunk-PK6SKIKE.cjs +0 -8
- package/dist/chunk-PK6SKIKE.cjs.map +0 -1
- package/dist/match.cjs +0 -21
- package/dist/match.cjs.map +0 -1
- package/dist/match.d.cts +0 -14
- package/dist/match.d.ts +0 -14
- package/dist/match.js +0 -18
- package/dist/match.js.map +0 -1
- package/dist/option.cjs +0 -230
- package/dist/option.cjs.map +0 -1
- package/dist/option.d.cts +0 -40
- package/dist/option.d.ts +0 -40
- package/dist/option.js +0 -227
- package/dist/option.js.map +0 -1
- package/dist/result.cjs +0 -421
- package/dist/result.cjs.map +0 -1
- package/dist/result.d.cts +0 -143
- package/dist/result.d.ts +0 -143
- package/dist/result.js +0 -413
- package/dist/result.js.map +0 -1
- package/dist/tuple.cjs +0 -19
- package/dist/tuple.cjs.map +0 -1
- package/dist/tuple.d.cts +0 -9
- package/dist/tuple.d.ts +0 -9
- package/dist/tuple.js +0 -16
- package/dist/tuple.js.map +0 -1
- package/dist/unit.cjs +0 -9
- package/dist/unit.cjs.map +0 -1
- package/dist/unit.d.cts +0 -4
- package/dist/unit.d.ts +0 -4
- package/dist/unit.js +0 -7
- package/dist/unit.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,651 @@
|
|
|
1
|
-
import '
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { isPromise } from 'node:util/types';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
|
|
7
|
+
// src/unit.ts
|
|
8
|
+
var UNIT = Symbol.for("UNIT");
|
|
9
|
+
|
|
10
|
+
// src/result.ts
|
|
11
|
+
var UnwrappedErrWithOk = class extends Error {
|
|
12
|
+
static {
|
|
13
|
+
__name(this, "UnwrappedErrWithOk");
|
|
14
|
+
}
|
|
15
|
+
constructor(r) {
|
|
16
|
+
super(`Attempted to call unwrapErr on an okay value: <${r}>`);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var UnwrappedOkWithErr = class extends Error {
|
|
20
|
+
static {
|
|
21
|
+
__name(this, "UnwrappedOkWithErr");
|
|
22
|
+
}
|
|
23
|
+
constructor(r) {
|
|
24
|
+
super(`Attempted to call unwrap on an Err value: <${r}>`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var Sentinel = Symbol.for("ResultSentinel");
|
|
28
|
+
var Result = class _Result {
|
|
29
|
+
static {
|
|
30
|
+
__name(this, "Result");
|
|
31
|
+
}
|
|
32
|
+
static UNIT_RESULT = new _Result(UNIT, {
|
|
33
|
+
errSlot: Sentinel
|
|
34
|
+
});
|
|
35
|
+
// 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
|
|
36
|
+
#val;
|
|
37
|
+
// to prevent unnecessary chaining - no other purpose. Should be modifiable only within the creating method
|
|
38
|
+
#ctx;
|
|
39
|
+
constructor(val, ctx) {
|
|
40
|
+
this.#val = val;
|
|
41
|
+
this.#ctx = ctx;
|
|
42
|
+
}
|
|
43
|
+
static Ok(val) {
|
|
44
|
+
return new _Result(val, {
|
|
45
|
+
errSlot: Sentinel
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
static Err(err) {
|
|
49
|
+
return new _Result(Sentinel, {
|
|
50
|
+
errSlot: err
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
isOk() {
|
|
54
|
+
return this.#ctx.errSlot === Sentinel;
|
|
55
|
+
}
|
|
56
|
+
isErr() {
|
|
57
|
+
return this.#ctx.errSlot !== Sentinel;
|
|
58
|
+
}
|
|
59
|
+
isUnit() {
|
|
60
|
+
return this.#val === UNIT;
|
|
61
|
+
}
|
|
62
|
+
toString() {
|
|
63
|
+
if (this.isOk()) {
|
|
64
|
+
return `Result::Ok<${String(this.#val)}>`;
|
|
65
|
+
}
|
|
66
|
+
return `Result::Err<${String(this.#ctx.errSlot)}>`;
|
|
67
|
+
}
|
|
68
|
+
unwrap() {
|
|
69
|
+
const curr = this.#val;
|
|
70
|
+
if (this.isErr()) {
|
|
71
|
+
const err = this.#ctx.errSlot;
|
|
72
|
+
if (err instanceof Error) throw err;
|
|
73
|
+
throw new UnwrappedOkWithErr(this);
|
|
74
|
+
}
|
|
75
|
+
if (isPromise(curr)) {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
curr.then((v) => {
|
|
78
|
+
if (v === Sentinel) {
|
|
79
|
+
let e;
|
|
80
|
+
if (this.#ctx.errSlot instanceof Error) e = this.#ctx.errSlot;
|
|
81
|
+
else e = new UnwrappedOkWithErr(this);
|
|
82
|
+
reject(e);
|
|
83
|
+
}
|
|
84
|
+
resolve(v);
|
|
85
|
+
}, reject);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return curr;
|
|
89
|
+
}
|
|
90
|
+
unwrapErr() {
|
|
91
|
+
const errSlot = this.#ctx.errSlot;
|
|
92
|
+
if (errSlot !== Sentinel) {
|
|
93
|
+
return errSlot;
|
|
94
|
+
}
|
|
95
|
+
const curr = this.#val;
|
|
96
|
+
if (isPromise(curr)) {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
curr.then((v) => {
|
|
99
|
+
if (v !== Sentinel) {
|
|
100
|
+
reject(new UnwrappedErrWithOk(this));
|
|
101
|
+
}
|
|
102
|
+
resolve(this.#ctx.errSlot);
|
|
103
|
+
}, reject);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
assert(curr !== Sentinel, "value must not be Sentinel at this point");
|
|
107
|
+
throw new UnwrappedErrWithOk(this);
|
|
108
|
+
}
|
|
109
|
+
safeUnwrap() {
|
|
110
|
+
return this.#val === Sentinel ? null : this.#val;
|
|
111
|
+
}
|
|
112
|
+
map(fn) {
|
|
113
|
+
if (this.isErr()) return _Result.Err(this.#ctx.errSlot);
|
|
114
|
+
assert(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
115
|
+
const curr = this.#val;
|
|
116
|
+
const immutableCtx = this.#ctx;
|
|
117
|
+
const mutableCtx = {
|
|
118
|
+
errSlot: Sentinel
|
|
119
|
+
};
|
|
120
|
+
if (isPromise(curr)) {
|
|
121
|
+
const p = curr;
|
|
122
|
+
const out = p.then((v) => {
|
|
123
|
+
if (v === Sentinel) {
|
|
124
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
125
|
+
return Sentinel;
|
|
126
|
+
}
|
|
127
|
+
const mapped = fn(v);
|
|
128
|
+
return mapped;
|
|
129
|
+
});
|
|
130
|
+
return new _Result(out, mutableCtx);
|
|
131
|
+
}
|
|
132
|
+
const next = fn(curr);
|
|
133
|
+
return new _Result(next, mutableCtx);
|
|
134
|
+
}
|
|
135
|
+
/** Same as map, but for the error value instead of the Ok value */
|
|
136
|
+
mapErr(fn) {
|
|
137
|
+
if (this.isErr()) {
|
|
138
|
+
const mappedErr = fn(this.#ctx.errSlot);
|
|
139
|
+
return _Result.Err(mappedErr);
|
|
140
|
+
}
|
|
141
|
+
const curr = this.#val;
|
|
142
|
+
const newCtx = {
|
|
143
|
+
errSlot: Sentinel
|
|
144
|
+
};
|
|
145
|
+
const ctx = this.#ctx;
|
|
146
|
+
if (isPromise(curr)) {
|
|
147
|
+
const p = curr.then((v) => {
|
|
148
|
+
if (ctx.errSlot !== Sentinel) {
|
|
149
|
+
const mappedErr = fn(ctx.errSlot);
|
|
150
|
+
newCtx.errSlot = mappedErr;
|
|
151
|
+
}
|
|
152
|
+
return v;
|
|
153
|
+
});
|
|
154
|
+
return new _Result(p, newCtx);
|
|
155
|
+
}
|
|
156
|
+
return new _Result(this.#val, newCtx);
|
|
157
|
+
}
|
|
158
|
+
mapBoth(fnOk, fnErr) {
|
|
159
|
+
if (this.isErr()) {
|
|
160
|
+
return this.mapErr(fnErr);
|
|
161
|
+
}
|
|
162
|
+
if (isPromise(this.#val)) {
|
|
163
|
+
return this.map(fnOk);
|
|
164
|
+
}
|
|
165
|
+
return this.map(fnOk);
|
|
166
|
+
}
|
|
167
|
+
flatMap(fn) {
|
|
168
|
+
if (this.isErr()) return _Result.Err(this.#ctx.errSlot);
|
|
169
|
+
assert(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
170
|
+
const curr = this.#val;
|
|
171
|
+
const immutableCtx = this.#ctx;
|
|
172
|
+
const mutableCtx = {
|
|
173
|
+
errSlot: Sentinel
|
|
174
|
+
};
|
|
175
|
+
if (isPromise(curr)) {
|
|
176
|
+
const newP = new Promise((resolve, reject) => {
|
|
177
|
+
curr.then((v) => {
|
|
178
|
+
if (v === Sentinel) {
|
|
179
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
180
|
+
return resolve(Sentinel);
|
|
181
|
+
}
|
|
182
|
+
const mapped2 = fn(v);
|
|
183
|
+
const p2 = _Result.flatMapHelper(mutableCtx, mapped2);
|
|
184
|
+
resolve(p2);
|
|
185
|
+
}, reject);
|
|
186
|
+
});
|
|
187
|
+
return new _Result(newP, mutableCtx);
|
|
188
|
+
}
|
|
189
|
+
const mapped = fn(curr);
|
|
190
|
+
const p = _Result.flatMapHelper(mutableCtx, mapped);
|
|
191
|
+
return new _Result(p, mutableCtx);
|
|
192
|
+
}
|
|
193
|
+
static flatMapHelper(mutableCtx, mapped) {
|
|
194
|
+
if (isPromise(mapped)) {
|
|
195
|
+
return mapped.then((r) => _Result.flatMapInnerHelper(mutableCtx, r));
|
|
196
|
+
}
|
|
197
|
+
return _Result.flatMapInnerHelper(mutableCtx, mapped);
|
|
198
|
+
}
|
|
199
|
+
static flatMapInnerHelper(mutableCtx, r) {
|
|
200
|
+
if (isPromise(r.#val)) {
|
|
201
|
+
return r.#val.then((v) => {
|
|
202
|
+
if (v === Sentinel) {
|
|
203
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
204
|
+
}
|
|
205
|
+
return v;
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (r.#val === Sentinel) {
|
|
209
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
210
|
+
return Sentinel;
|
|
211
|
+
}
|
|
212
|
+
return r.#val;
|
|
213
|
+
}
|
|
214
|
+
zipErr(fn) {
|
|
215
|
+
if (this.isErr()) {
|
|
216
|
+
return _Result.Err(this.#ctx.errSlot);
|
|
217
|
+
}
|
|
218
|
+
assert(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
219
|
+
const curr = this.#val;
|
|
220
|
+
const immutableCtx = this.#ctx;
|
|
221
|
+
const mutableCtx = {
|
|
222
|
+
errSlot: Sentinel
|
|
223
|
+
};
|
|
224
|
+
if (isPromise(curr)) {
|
|
225
|
+
const newP2 = curr.then((v) => {
|
|
226
|
+
if (v === Sentinel) {
|
|
227
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
228
|
+
return Sentinel;
|
|
229
|
+
}
|
|
230
|
+
const r2 = fn(v);
|
|
231
|
+
return _Result.zipErrHelper(v, r2, mutableCtx);
|
|
232
|
+
});
|
|
233
|
+
return new _Result(newP2, mutableCtx);
|
|
234
|
+
}
|
|
235
|
+
const r = fn(curr);
|
|
236
|
+
const newP = _Result.zipErrHelper(curr, r, mutableCtx);
|
|
237
|
+
return new _Result(newP, mutableCtx);
|
|
238
|
+
}
|
|
239
|
+
static zipErrHelper(v, r, newCtx) {
|
|
240
|
+
if (isPromise(r)) {
|
|
241
|
+
const finalPromise = r.then((newResult) => {
|
|
242
|
+
if (newResult.#ctx.errSlot !== Sentinel) newCtx.errSlot = newResult.#ctx.errSlot;
|
|
243
|
+
return v;
|
|
244
|
+
});
|
|
245
|
+
return finalPromise;
|
|
246
|
+
}
|
|
247
|
+
if (r.#ctx.errSlot !== Sentinel) newCtx.errSlot = r.#ctx.errSlot;
|
|
248
|
+
return v;
|
|
249
|
+
}
|
|
250
|
+
zip(fn) {
|
|
251
|
+
if (this.isErr()) return _Result.Err(this.#ctx.errSlot);
|
|
252
|
+
const curr = this.#val;
|
|
253
|
+
assert(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
254
|
+
const immutableCtx = this.#ctx;
|
|
255
|
+
const mutableCtx = {
|
|
256
|
+
errSlot: Sentinel
|
|
257
|
+
};
|
|
258
|
+
if (isPromise(curr)) {
|
|
259
|
+
const newP = curr.then((v) => {
|
|
260
|
+
if (v === Sentinel) {
|
|
261
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
262
|
+
return Sentinel;
|
|
263
|
+
}
|
|
264
|
+
const u2 = fn(v);
|
|
265
|
+
if (isPromise(u2)) return u2.then((uu) => [
|
|
266
|
+
v,
|
|
267
|
+
uu
|
|
268
|
+
]);
|
|
269
|
+
return [
|
|
270
|
+
v,
|
|
271
|
+
u2
|
|
272
|
+
];
|
|
273
|
+
});
|
|
274
|
+
return new _Result(newP, mutableCtx);
|
|
275
|
+
}
|
|
276
|
+
const u = fn(curr);
|
|
277
|
+
if (isPromise(u)) {
|
|
278
|
+
return new _Result(u.then((uu) => [
|
|
279
|
+
curr,
|
|
280
|
+
uu
|
|
281
|
+
]), mutableCtx);
|
|
282
|
+
}
|
|
283
|
+
return new _Result([
|
|
284
|
+
curr,
|
|
285
|
+
u
|
|
286
|
+
], mutableCtx);
|
|
287
|
+
}
|
|
288
|
+
flatZip(fn) {
|
|
289
|
+
if (this.isErr()) return _Result.Err(this.#ctx.errSlot);
|
|
290
|
+
const curr = this.#val;
|
|
291
|
+
assert(this.#val !== Sentinel, "cannot be Sentinel at this point");
|
|
292
|
+
const immutableCtx = this.#ctx;
|
|
293
|
+
const mutableCtx = {
|
|
294
|
+
errSlot: Sentinel
|
|
295
|
+
};
|
|
296
|
+
if (isPromise(curr)) {
|
|
297
|
+
const newP = curr.then((v) => {
|
|
298
|
+
if (v === Sentinel) {
|
|
299
|
+
mutableCtx.errSlot = immutableCtx.errSlot;
|
|
300
|
+
return v;
|
|
301
|
+
}
|
|
302
|
+
const mapped2 = fn(v);
|
|
303
|
+
return _Result.flatZipHelper(mutableCtx, v, mapped2);
|
|
304
|
+
});
|
|
305
|
+
return new _Result(newP, mutableCtx);
|
|
306
|
+
}
|
|
307
|
+
const mapped = fn(curr);
|
|
308
|
+
const p = _Result.flatZipHelper(mutableCtx, curr, mapped);
|
|
309
|
+
return new _Result(p, mutableCtx);
|
|
310
|
+
}
|
|
311
|
+
static flatZipHelper(mutableCtx, originalVal, mapped) {
|
|
312
|
+
if (isPromise(mapped)) {
|
|
313
|
+
return mapped.then((r) => _Result.flatZipInnerHelper(mutableCtx, originalVal, r));
|
|
314
|
+
}
|
|
315
|
+
return _Result.flatZipInnerHelper(mutableCtx, originalVal, mapped);
|
|
316
|
+
}
|
|
317
|
+
static flatZipInnerHelper(mutableCtx, originalVal, r) {
|
|
318
|
+
if (isPromise(r.#val)) {
|
|
319
|
+
return r.#val.then((v) => {
|
|
320
|
+
if (v === Sentinel) {
|
|
321
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
322
|
+
}
|
|
323
|
+
return [
|
|
324
|
+
originalVal,
|
|
325
|
+
v
|
|
326
|
+
];
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
if (r.#val === Sentinel) {
|
|
330
|
+
mutableCtx.errSlot = r.#ctx.errSlot;
|
|
331
|
+
return Sentinel;
|
|
332
|
+
}
|
|
333
|
+
return [
|
|
334
|
+
originalVal,
|
|
335
|
+
r.#val
|
|
336
|
+
];
|
|
337
|
+
}
|
|
338
|
+
//#endregion
|
|
339
|
+
innerMap(mapper) {
|
|
340
|
+
if (this.isErr()) return this;
|
|
341
|
+
if (Array.isArray(this.#val)) {
|
|
342
|
+
return new _Result(this.#val.map(mapper), this.#ctx);
|
|
343
|
+
}
|
|
344
|
+
throw new Error("Can only be called for Result<Array<T>, E>");
|
|
345
|
+
}
|
|
346
|
+
async toPromise() {
|
|
347
|
+
const v = await this.#val;
|
|
348
|
+
return new _Result(v, this.#ctx);
|
|
349
|
+
}
|
|
350
|
+
validate(validators) {
|
|
351
|
+
if (this.isErr()) return this;
|
|
352
|
+
const currVal = this.#val;
|
|
353
|
+
const currCtx = this.#ctx;
|
|
354
|
+
const mutableCtx = {
|
|
355
|
+
errSlot: Sentinel
|
|
356
|
+
};
|
|
357
|
+
if (isPromise(currVal)) {
|
|
358
|
+
return new _Result(currVal.then(async (c) => {
|
|
359
|
+
if (currCtx.errSlot !== Sentinel) {
|
|
360
|
+
mutableCtx.errSlot = currCtx.errSlot;
|
|
361
|
+
return Sentinel;
|
|
362
|
+
}
|
|
363
|
+
const results2 = validators.map((v) => v(currVal));
|
|
364
|
+
return Promise.all(results2).then((resolved) => _Result.validateHelper(resolved, mutableCtx, c));
|
|
365
|
+
}), mutableCtx);
|
|
366
|
+
}
|
|
367
|
+
const results = validators.map((v) => v(currVal));
|
|
368
|
+
if (results.some(isPromise)) {
|
|
369
|
+
return new _Result(Promise.all(results).then((resolved) => _Result.validateHelper(resolved, mutableCtx, currVal)), mutableCtx);
|
|
370
|
+
}
|
|
371
|
+
const values = results.map((r) => r.#val);
|
|
372
|
+
if (values.some(isPromise)) {
|
|
373
|
+
return new _Result(_Result.validateHelper(results, mutableCtx, currVal), mutableCtx);
|
|
374
|
+
}
|
|
375
|
+
const combinedRes = _Result.all(...results);
|
|
376
|
+
return combinedRes.isErr() ? combinedRes : this;
|
|
377
|
+
}
|
|
378
|
+
static validateHelper(results, currCtx, currVal) {
|
|
379
|
+
const combinedRes = _Result.all(...results);
|
|
380
|
+
if (isPromise(combinedRes)) {
|
|
381
|
+
return combinedRes.then((cRes) => {
|
|
382
|
+
if (cRes.isErr()) {
|
|
383
|
+
currCtx.errSlot = cRes.#ctx.errSlot;
|
|
384
|
+
return Sentinel;
|
|
385
|
+
}
|
|
386
|
+
return currVal;
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
if (combinedRes.isErr()) {
|
|
390
|
+
currCtx.errSlot = combinedRes.#ctx.errSlot;
|
|
391
|
+
return Sentinel;
|
|
392
|
+
}
|
|
393
|
+
return currVal;
|
|
394
|
+
}
|
|
395
|
+
static all(...results) {
|
|
396
|
+
const vals = results.map((r) => r.#val);
|
|
397
|
+
const mutableCtx = {
|
|
398
|
+
errSlot: Sentinel
|
|
399
|
+
};
|
|
400
|
+
if (vals.some(isPromise)) {
|
|
401
|
+
return new _Result(Promise.all(vals).then((v) => {
|
|
402
|
+
if (results.some((r) => r.isErr())) {
|
|
403
|
+
mutableCtx.errSlot = results.filter((r) => r.isErr()).map((r) => r.#ctx.errSlot);
|
|
404
|
+
return Sentinel;
|
|
405
|
+
}
|
|
406
|
+
return v;
|
|
407
|
+
}), mutableCtx);
|
|
408
|
+
}
|
|
409
|
+
if (results.some((r) => r.isErr())) {
|
|
410
|
+
return _Result.Err(results.filter((r) => r.isErr()).map((r) => r.#ctx.errSlot));
|
|
411
|
+
}
|
|
412
|
+
return _Result.Ok(vals);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
var UnwrappedNone = class extends Error {
|
|
416
|
+
static {
|
|
417
|
+
__name(this, "UnwrappedNone");
|
|
418
|
+
}
|
|
419
|
+
constructor() {
|
|
420
|
+
super("Unwrapped Option<None>");
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var UNWRAPPED_NONE_ERR = new UnwrappedNone();
|
|
424
|
+
var NONE_VAL = Symbol.for("None");
|
|
425
|
+
var Option = class _Option {
|
|
426
|
+
static {
|
|
427
|
+
__name(this, "Option");
|
|
428
|
+
}
|
|
429
|
+
val;
|
|
430
|
+
#ctx;
|
|
431
|
+
constructor(val, ctx) {
|
|
432
|
+
this.val = val;
|
|
433
|
+
this.#ctx = ctx;
|
|
434
|
+
}
|
|
435
|
+
static None = new _Option(NONE_VAL, {
|
|
436
|
+
promiseNoneSlot: true
|
|
437
|
+
});
|
|
438
|
+
static safeMap(p, mapper, ctx) {
|
|
439
|
+
return p.then((v) => {
|
|
440
|
+
if (v === NONE_VAL) {
|
|
441
|
+
ctx.promiseNoneSlot = true;
|
|
442
|
+
return NONE_VAL;
|
|
443
|
+
}
|
|
444
|
+
return mapper(v);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
static Some(val) {
|
|
448
|
+
return new _Option(val, {
|
|
449
|
+
promiseNoneSlot: false
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
static fromNullable(val) {
|
|
453
|
+
return val === null ? _Option.None : _Option.Some(val);
|
|
454
|
+
}
|
|
455
|
+
isSome() {
|
|
456
|
+
return this.val !== NONE_VAL;
|
|
457
|
+
}
|
|
458
|
+
isNone() {
|
|
459
|
+
return this.val === NONE_VAL || isPromise(this.val) && this.#ctx.promiseNoneSlot;
|
|
460
|
+
}
|
|
461
|
+
isUnit() {
|
|
462
|
+
return this.val === UNIT;
|
|
463
|
+
}
|
|
464
|
+
unwrap() {
|
|
465
|
+
if (this.isNone()) {
|
|
466
|
+
throw UNWRAPPED_NONE_ERR;
|
|
467
|
+
}
|
|
468
|
+
return this.val;
|
|
469
|
+
}
|
|
470
|
+
safeUnwrap() {
|
|
471
|
+
if (this.isNone()) return null;
|
|
472
|
+
return this.val;
|
|
473
|
+
}
|
|
474
|
+
map(mapper) {
|
|
475
|
+
if (this.isNone()) return _Option.None;
|
|
476
|
+
const ctx = this.#ctx;
|
|
477
|
+
const curr = this.val;
|
|
478
|
+
if (isPromise(curr)) {
|
|
479
|
+
const p = curr;
|
|
480
|
+
const safetlyMapped = _Option.safeMap(p, mapper, ctx);
|
|
481
|
+
return new _Option(safetlyMapped, ctx);
|
|
482
|
+
}
|
|
483
|
+
const transformed = mapper(curr);
|
|
484
|
+
return new _Option(transformed, ctx);
|
|
485
|
+
}
|
|
486
|
+
mapOr(default_, mapper) {
|
|
487
|
+
if (this.isNone()) return new _Option(default_, this.#ctx);
|
|
488
|
+
const ctx = this.#ctx;
|
|
489
|
+
const curr = this.val;
|
|
490
|
+
if (isPromise(curr)) {
|
|
491
|
+
const p = curr;
|
|
492
|
+
const safetlyMapped = _Option.safeMap(p, mapper, ctx);
|
|
493
|
+
return new _Option(safetlyMapped, ctx);
|
|
494
|
+
}
|
|
495
|
+
const transformed = mapper(curr);
|
|
496
|
+
return new _Option(transformed, ctx);
|
|
497
|
+
}
|
|
498
|
+
flatMap(mapper) {
|
|
499
|
+
if (this.isNone()) return _Option.None;
|
|
500
|
+
const ctx = this.#ctx;
|
|
501
|
+
const curr = this.val;
|
|
502
|
+
if (isPromise(curr)) {
|
|
503
|
+
const p = curr;
|
|
504
|
+
const castedNone = NONE_VAL;
|
|
505
|
+
const newP = new Promise((resolve, reject) => {
|
|
506
|
+
p.then((curr2) => {
|
|
507
|
+
if (curr2 === NONE_VAL) {
|
|
508
|
+
ctx.promiseNoneSlot = true;
|
|
509
|
+
resolve(castedNone);
|
|
510
|
+
} else {
|
|
511
|
+
const r = mapper(curr2);
|
|
512
|
+
if (isPromise(r)) {
|
|
513
|
+
r.then((innerOpt) => resolve(innerOpt.val), reject);
|
|
514
|
+
} else {
|
|
515
|
+
resolve(r.val);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
return new _Option(newP, ctx);
|
|
521
|
+
}
|
|
522
|
+
const mapped = mapper(curr);
|
|
523
|
+
if (isPromise(mapped)) {
|
|
524
|
+
return _Option.fromPromise(mapped);
|
|
525
|
+
}
|
|
526
|
+
return mapped;
|
|
527
|
+
}
|
|
528
|
+
zip(fn) {
|
|
529
|
+
if (this.isNone()) return _Option.None;
|
|
530
|
+
const curr = this.val;
|
|
531
|
+
const ctx = this.#ctx;
|
|
532
|
+
if (isPromise(curr)) {
|
|
533
|
+
const val = curr;
|
|
534
|
+
const p = val.then(async (v) => {
|
|
535
|
+
if (v === NONE_VAL) {
|
|
536
|
+
ctx.promiseNoneSlot = true;
|
|
537
|
+
return NONE_VAL;
|
|
538
|
+
}
|
|
539
|
+
const next = await fn(v);
|
|
540
|
+
return [
|
|
541
|
+
v,
|
|
542
|
+
next
|
|
543
|
+
];
|
|
544
|
+
});
|
|
545
|
+
return new _Option(p, ctx);
|
|
546
|
+
}
|
|
547
|
+
const u = fn(curr);
|
|
548
|
+
if (isPromise(u)) {
|
|
549
|
+
const p = u.then((uu) => [
|
|
550
|
+
curr,
|
|
551
|
+
uu
|
|
552
|
+
]);
|
|
553
|
+
return new _Option(p, ctx);
|
|
554
|
+
}
|
|
555
|
+
return new _Option([
|
|
556
|
+
curr,
|
|
557
|
+
u
|
|
558
|
+
], ctx);
|
|
559
|
+
}
|
|
560
|
+
flatZip(fn) {
|
|
561
|
+
if (this.isNone()) return _Option.None;
|
|
562
|
+
const curr = this.val;
|
|
563
|
+
const ctx = this.#ctx;
|
|
564
|
+
if (isPromise(curr)) {
|
|
565
|
+
const val = curr;
|
|
566
|
+
const p = val.then(async (v) => {
|
|
567
|
+
if (v === NONE_VAL) {
|
|
568
|
+
ctx.promiseNoneSlot = true;
|
|
569
|
+
return NONE_VAL;
|
|
570
|
+
}
|
|
571
|
+
const next = await fn(v);
|
|
572
|
+
if (next.isNone()) {
|
|
573
|
+
return NONE_VAL;
|
|
574
|
+
}
|
|
575
|
+
return [
|
|
576
|
+
v,
|
|
577
|
+
next.val
|
|
578
|
+
];
|
|
579
|
+
});
|
|
580
|
+
return new _Option(p, ctx);
|
|
581
|
+
}
|
|
582
|
+
const c = curr;
|
|
583
|
+
const u = fn(c);
|
|
584
|
+
if (isPromise(u)) {
|
|
585
|
+
const p = u.then((uu) => {
|
|
586
|
+
if (uu.isNone()) {
|
|
587
|
+
return NONE_VAL;
|
|
588
|
+
}
|
|
589
|
+
return [
|
|
590
|
+
c,
|
|
591
|
+
uu.val
|
|
592
|
+
];
|
|
593
|
+
});
|
|
594
|
+
return new _Option(p, ctx);
|
|
595
|
+
}
|
|
596
|
+
return u.map((inner) => [
|
|
597
|
+
c,
|
|
598
|
+
inner
|
|
599
|
+
]);
|
|
600
|
+
}
|
|
601
|
+
async toPromise() {
|
|
602
|
+
const curr = this.val;
|
|
603
|
+
let inner;
|
|
604
|
+
if (isPromise(curr)) {
|
|
605
|
+
const awaited = await curr;
|
|
606
|
+
inner = awaited;
|
|
607
|
+
} else {
|
|
608
|
+
inner = curr;
|
|
609
|
+
}
|
|
610
|
+
return new _Option(inner, this.#ctx);
|
|
611
|
+
}
|
|
612
|
+
static fromPromise(o) {
|
|
613
|
+
const ctx = {
|
|
614
|
+
promiseNoneSlot: false
|
|
615
|
+
};
|
|
616
|
+
const p = new Promise((resolve, reject) => o.then((innerOpt) => {
|
|
617
|
+
ctx.promiseNoneSlot ||= innerOpt.#ctx.promiseNoneSlot;
|
|
618
|
+
resolve(innerOpt.val);
|
|
619
|
+
}, reject));
|
|
620
|
+
return new _Option(p, ctx);
|
|
621
|
+
}
|
|
622
|
+
toString() {
|
|
623
|
+
if (this.isNone()) return "Option::None";
|
|
624
|
+
return `Option::Some(${this.val})`;
|
|
625
|
+
}
|
|
626
|
+
innerMap(mapper) {
|
|
627
|
+
if (this.isNone()) return _Option.None;
|
|
628
|
+
if (!Array.isArray(this.val)) {
|
|
629
|
+
throw new Error("Can only be called for Option<Array<T>>");
|
|
630
|
+
}
|
|
631
|
+
return new _Option(this.val.map(mapper), this.#ctx);
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
// src/match.ts
|
|
636
|
+
var matchRes = /* @__PURE__ */ __name((r, branches) => {
|
|
637
|
+
if (r.isOk()) {
|
|
638
|
+
return branches.Ok(r.unwrap());
|
|
639
|
+
}
|
|
640
|
+
return branches.Err(r.unwrapErr());
|
|
641
|
+
}, "matchRes");
|
|
642
|
+
var matchOpt = /* @__PURE__ */ __name((o, branches) => {
|
|
643
|
+
if (o.isSome()) {
|
|
644
|
+
return branches.Some(o.unwrap());
|
|
645
|
+
}
|
|
646
|
+
return branches.None();
|
|
647
|
+
}, "matchOpt");
|
|
648
|
+
|
|
649
|
+
export { Option, Result, UNIT, UnwrappedErrWithOk, UnwrappedNone, UnwrappedOkWithErr, matchOpt, matchRes };
|
|
6
650
|
//# sourceMappingURL=index.js.map
|
|
7
651
|
//# sourceMappingURL=index.js.map
|