@carbonteq/fp 0.9.1 → 0.11.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 +326 -97
- package/dist/errors.cjs +2 -3
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +2 -5
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +2 -5
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +1 -2
- package/dist/errors.mjs.map +1 -1
- package/dist/flow-experimental.cjs +332 -0
- package/dist/flow-experimental.cjs.map +1 -0
- package/dist/flow-experimental.d.cts +69 -0
- package/dist/flow-experimental.d.cts.map +1 -0
- package/dist/flow-experimental.d.mts +69 -0
- package/dist/flow-experimental.d.mts.map +1 -0
- package/dist/flow-experimental.mjs +330 -0
- package/dist/flow-experimental.mjs.map +1 -0
- package/dist/flow.cjs +340 -0
- package/dist/flow.cjs.map +1 -0
- package/dist/flow.d.cts +77 -0
- package/dist/flow.d.cts.map +1 -0
- package/dist/flow.d.mts +77 -0
- package/dist/flow.d.mts.map +1 -0
- package/dist/flow.mjs +338 -0
- package/dist/flow.mjs.map +1 -0
- package/dist/index.cjs +29 -14
- package/dist/index.d.cts +10 -5
- package/dist/index.d.mts +10 -5
- package/dist/index.mjs +10 -5
- package/dist/match.cjs +376 -3
- package/dist/match.cjs.map +1 -1
- package/dist/match.d.cts +467 -8
- package/dist/match.d.cts.map +1 -1
- package/dist/match.d.mts +467 -8
- package/dist/match.d.mts.map +1 -1
- package/dist/match.mjs +371 -3
- package/dist/match.mjs.map +1 -1
- package/dist/option-experimental.cjs +1547 -0
- package/dist/option-experimental.cjs.map +1 -0
- package/dist/option-experimental.d.cts +1380 -0
- package/dist/option-experimental.d.cts.map +1 -0
- package/dist/option-experimental.d.mts +1380 -0
- package/dist/option-experimental.d.mts.map +1 -0
- package/dist/option-experimental.mjs +1545 -0
- package/dist/option-experimental.mjs.map +1 -0
- package/dist/option.cjs +650 -71
- package/dist/option.cjs.map +1 -1
- package/dist/option.d.cts +528 -39
- package/dist/option.d.cts.map +1 -1
- package/dist/option.d.mts +528 -39
- package/dist/option.d.mts.map +1 -1
- package/dist/option.mjs +648 -70
- package/dist/option.mjs.map +1 -1
- package/dist/result-experimental.cjs +1346 -0
- package/dist/result-experimental.cjs.map +1 -0
- package/dist/result-experimental.d.cts +1127 -0
- package/dist/result-experimental.d.cts.map +1 -0
- package/dist/result-experimental.d.mts +1127 -0
- package/dist/result-experimental.d.mts.map +1 -0
- package/dist/result-experimental.mjs +1345 -0
- package/dist/result-experimental.mjs.map +1 -0
- package/dist/result.cjs +746 -81
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +799 -104
- package/dist/result.d.cts.map +1 -1
- package/dist/result.d.mts +799 -104
- package/dist/result.d.mts.map +1 -1
- package/dist/result.mjs +742 -73
- package/dist/result.mjs.map +1 -1
- package/dist/tuple.cjs +5 -4
- package/dist/tuple.cjs.map +1 -1
- package/dist/tuple.d.cts +9 -7
- package/dist/tuple.d.cts.map +1 -1
- package/dist/tuple.d.mts +9 -7
- package/dist/tuple.d.mts.map +1 -1
- package/dist/tuple.mjs +4 -3
- package/dist/tuple.mjs.map +1 -1
- package/dist/unit.cjs +3 -3
- package/dist/unit.cjs.map +1 -1
- package/dist/unit.d.cts +4 -5
- package/dist/unit.d.cts.map +1 -1
- package/dist/unit.d.mts +4 -5
- package/dist/unit.d.mts.map +1 -1
- package/dist/unit.mjs +2 -2
- package/dist/unit.mjs.map +1 -1
- package/dist/utils.cjs +50 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +14 -4
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +14 -4
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +46 -3
- package/dist/utils.mjs.map +1 -1
- package/examples/README.md +60 -0
- package/examples/flow/01-basics.ts +74 -0
- package/examples/flow/02-async.ts +73 -0
- package/examples/flow/03-recovery.ts +60 -0
- package/examples/flow/04-complex-backend.ts +240 -0
- package/examples/flow/05-complex-backend-asyncGen.ts +224 -0
- package/examples/flow/index.ts +13 -0
- package/examples/flow-experimental/01-basics.ts +78 -0
- package/examples/flow-experimental/02-async.ts +77 -0
- package/examples/flow-experimental/03-recovery.ts +93 -0
- package/examples/flow-experimental/04-complex-backend.ts +244 -0
- package/examples/flow-experimental/05-complex-backend-asyncGen.ts +229 -0
- package/examples/flow-experimental/index.ts +14 -0
- package/examples/match/01-stable-match.ts +154 -0
- package/examples/match/index.ts +19 -0
- package/examples/match-experimental/01-experimental-match.ts +157 -0
- package/examples/match-experimental/index.ts +18 -0
- package/examples/option/01-basics.ts +16 -0
- package/examples/option/02-map-flatMap.ts +36 -0
- package/examples/option/03-zip-flatZip.ts +23 -0
- package/examples/option/04-constructors.ts +21 -0
- package/examples/option/05-transform.ts +15 -0
- package/examples/option/06-aggregation.ts +14 -0
- package/examples/option/index.ts +49 -0
- package/examples/option-experimental/01-map.ts +109 -0
- package/examples/option-experimental/02-flatMap.ts +234 -0
- package/examples/option-experimental/03-zip.ts +197 -0
- package/examples/option-experimental/04-flatZip.ts +251 -0
- package/examples/option-experimental/05-gen.ts +360 -0
- package/examples/option-experimental/06-genAdapter.ts +463 -0
- package/examples/option-experimental/07-asyncGen.ts +353 -0
- package/examples/option-experimental/08-asyncGenAdapter.ts +514 -0
- package/examples/option-experimental/09-workflow-simple-sync.ts +321 -0
- package/examples/option-experimental/10-workflow-async.ts +378 -0
- package/examples/option-experimental/index.ts +341 -0
- package/examples/option-experimental/shared-domain.ts +325 -0
- package/examples/result/01-basics.ts +16 -0
- package/examples/result/02-map-flatMap.ts +34 -0
- package/examples/result/03-zip-flatZip.ts +23 -0
- package/examples/result/04-validation.ts +44 -0
- package/examples/result/05-constructors.ts +30 -0
- package/examples/result/06-utilities.ts +36 -0
- package/examples/result/index.ts +53 -0
- package/examples/result-experimental/01-map.ts +60 -0
- package/examples/result-experimental/02-flatMap.ts +169 -0
- package/examples/result-experimental/03-zip.ts +145 -0
- package/examples/result-experimental/04-flatZip.ts +252 -0
- package/examples/result-experimental/05-gen.ts +293 -0
- package/examples/result-experimental/06-genAdapter.ts +396 -0
- package/examples/result-experimental/07-asyncGen.ts +379 -0
- package/examples/result-experimental/08-asyncGenAdapter.ts +553 -0
- package/examples/result-experimental/09-workflow-simple-sync.ts +304 -0
- package/examples/result-experimental/10-workflow-async.ts +356 -0
- package/examples/result-experimental/index.ts +312 -0
- package/examples/result-experimental/shared-domain.ts +263 -0
- package/package.json +54 -31
package/dist/option.cjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_result = require("./result.cjs");
|
|
3
|
+
const require_unit = require("./unit.cjs");
|
|
4
|
+
const require_utils = require("./utils.cjs");
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
// src/option.ts
|
|
6
7
|
var UnwrappedNone = class extends Error {
|
|
7
8
|
name = "UnwrapError";
|
|
8
9
|
constructor() {
|
|
@@ -11,11 +12,52 @@ var UnwrappedNone = class extends Error {
|
|
|
11
12
|
};
|
|
12
13
|
const UNWRAPPED_NONE_ERR = new UnwrappedNone();
|
|
13
14
|
const NONE_VAL = Symbol("Option::None");
|
|
15
|
+
/**
|
|
16
|
+
* Wrapper that makes Option yieldable with proper type tracking.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
var OptionYieldWrap = class {
|
|
21
|
+
option;
|
|
22
|
+
constructor(option) {
|
|
23
|
+
this.option = option;
|
|
24
|
+
}
|
|
25
|
+
*[Symbol.iterator]() {
|
|
26
|
+
return yield this;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Wrapper that makes Option yieldable in async generators with proper type tracking.
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
var AsyncOptionYieldWrap = class {
|
|
35
|
+
option;
|
|
36
|
+
constructor(option) {
|
|
37
|
+
this.option = require_utils.isPromiseLike(option) ? option : Promise.resolve(option);
|
|
38
|
+
}
|
|
39
|
+
async *[Symbol.asyncIterator]() {
|
|
40
|
+
return yield this;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Represents an optional value: `Some<T>` or `None`.
|
|
45
|
+
* Provides composable, null-safe transformation and chaining methods.
|
|
46
|
+
*/
|
|
14
47
|
var Option = class Option {
|
|
15
48
|
/** Discriminant tag for type-level identification */
|
|
16
49
|
_tag;
|
|
17
50
|
#ctx;
|
|
18
51
|
#val;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new Option instance with internal context.
|
|
54
|
+
*
|
|
55
|
+
* @param val - Stored value or sentinel
|
|
56
|
+
* @param ctx - Internal async tracking context
|
|
57
|
+
* @param tag - Discriminant tag for Some/None
|
|
58
|
+
*
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
19
61
|
constructor(val, ctx, tag) {
|
|
20
62
|
this.#val = val;
|
|
21
63
|
this.#ctx = ctx;
|
|
@@ -23,31 +65,77 @@ var Option = class Option {
|
|
|
23
65
|
}
|
|
24
66
|
/** Singleton None instance */
|
|
25
67
|
static None = new Option(NONE_VAL, { promiseNoneSlot: true }, "None");
|
|
26
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Creates a Some variant containing the provided value.
|
|
70
|
+
*
|
|
71
|
+
* @template Inner - Type of the wrapped value
|
|
72
|
+
* @param val - Value to wrap in Some
|
|
73
|
+
* @returns Option in the Some state
|
|
74
|
+
*/
|
|
27
75
|
static Some(val) {
|
|
28
76
|
return new Option(val, { promiseNoneSlot: false }, "Some");
|
|
29
77
|
}
|
|
30
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Creates an Option from a nullable value, returning None for null/undefined.
|
|
80
|
+
*
|
|
81
|
+
* @template T - Input type that may include null/undefined
|
|
82
|
+
* @param val - Value to convert
|
|
83
|
+
* @returns Some for non-nullish values, otherwise None
|
|
84
|
+
*/
|
|
31
85
|
static fromNullable(val) {
|
|
32
86
|
return val === null || val === void 0 ? Option.None : Option.Some(val);
|
|
33
87
|
}
|
|
34
|
-
/**
|
|
88
|
+
/**
|
|
89
|
+
* Creates an Option from a potentially falsy value, returning None for falsy.
|
|
90
|
+
*
|
|
91
|
+
* Treats false, 0, "", null, and undefined as absence.
|
|
92
|
+
*
|
|
93
|
+
* @template T - Input type
|
|
94
|
+
* @param val - Value to convert
|
|
95
|
+
* @returns Some for truthy values, otherwise None
|
|
96
|
+
*/
|
|
35
97
|
static fromFalsy(val) {
|
|
36
98
|
return val ? Option.Some(val) : Option.None;
|
|
37
99
|
}
|
|
38
|
-
/**
|
|
100
|
+
/**
|
|
101
|
+
* Creates an Option based on a predicate.
|
|
102
|
+
*
|
|
103
|
+
* @template T - Input type
|
|
104
|
+
* @param val - Value to test
|
|
105
|
+
* @param pred - Predicate to decide Some vs None
|
|
106
|
+
* @returns Some when predicate passes, otherwise None
|
|
107
|
+
*/
|
|
39
108
|
static fromPredicate(val, pred) {
|
|
40
109
|
return pred(val) ? Option.Some(val) : Option.None;
|
|
41
110
|
}
|
|
42
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* Wraps a Promise<Option<T>> as Option<Promise<T>>.
|
|
113
|
+
*
|
|
114
|
+
* NOTE: the returned value starts as `Some(Promise<T>)`; final `None` is known
|
|
115
|
+
* only after settlement. Use `toPromise()` (or await `unwrap`/`safeUnwrap`) to
|
|
116
|
+
* observe settled absence.
|
|
117
|
+
*
|
|
118
|
+
* @template U - Inner value type
|
|
119
|
+
* @param o - Promise resolving to an Option
|
|
120
|
+
* @returns Option containing a Promise of the inner value
|
|
121
|
+
*/
|
|
43
122
|
static fromPromise(o) {
|
|
44
123
|
const ctx = { promiseNoneSlot: false };
|
|
45
|
-
|
|
124
|
+
const p = new Promise((resolve, reject) => o.then((innerOpt) => {
|
|
46
125
|
if (innerOpt.isNone()) ctx.promiseNoneSlot = true;
|
|
47
126
|
resolve(innerOpt.#val);
|
|
48
|
-
}, reject))
|
|
127
|
+
}, reject));
|
|
128
|
+
return new Option(p, ctx, "Some");
|
|
49
129
|
}
|
|
50
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* Combines multiple Options into a single Option of an array.
|
|
132
|
+
*
|
|
133
|
+
* Returns Some of all values if all inputs are Some; otherwise None.
|
|
134
|
+
*
|
|
135
|
+
* @template T - Variadic tuple of Option types
|
|
136
|
+
* @param options - Options to combine
|
|
137
|
+
* @returns Some of tuple values, or None if any input is None
|
|
138
|
+
*/
|
|
51
139
|
static all(...options) {
|
|
52
140
|
const vals = [];
|
|
53
141
|
for (const opt of options) {
|
|
@@ -56,44 +144,109 @@ var Option = class Option {
|
|
|
56
144
|
}
|
|
57
145
|
return Option.Some(vals);
|
|
58
146
|
}
|
|
59
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* Returns the first Some in the list, or None if all are None.
|
|
149
|
+
*
|
|
150
|
+
* Awaits candidates in input order, without waiting for the suffix after success.
|
|
151
|
+
*
|
|
152
|
+
* @template T - Value type
|
|
153
|
+
* @param options - Options to search
|
|
154
|
+
* @returns The first Some found, otherwise None
|
|
155
|
+
*/
|
|
60
156
|
static any(...options) {
|
|
61
|
-
|
|
62
|
-
|
|
157
|
+
let hasPending = false;
|
|
158
|
+
for (const opt of options) {
|
|
159
|
+
if (!opt.isSome()) continue;
|
|
160
|
+
if (require_utils.isPromiseLike(opt.#val)) {
|
|
161
|
+
hasPending = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
return opt;
|
|
165
|
+
}
|
|
166
|
+
if (!hasPending) return Option.None;
|
|
167
|
+
for (const opt of options) if (require_utils.isPromiseLike(opt.#val)) Promise.resolve(opt.#val).then(void 0, () => {});
|
|
168
|
+
const ctx = { promiseNoneSlot: false };
|
|
169
|
+
const p = (async () => {
|
|
170
|
+
for (const opt of options) {
|
|
171
|
+
if (!opt.isSome()) continue;
|
|
172
|
+
const val = await opt.#val;
|
|
173
|
+
if (!opt.#ctx.promiseNoneSlot && val !== NONE_VAL) return val;
|
|
174
|
+
}
|
|
175
|
+
ctx.promiseNoneSlot = true;
|
|
176
|
+
return NONE_VAL;
|
|
177
|
+
})();
|
|
178
|
+
return new Option(p, ctx, "Some");
|
|
63
179
|
}
|
|
64
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* Type guard that narrows to Some.
|
|
182
|
+
*
|
|
183
|
+
* @returns true if this Option is Some
|
|
184
|
+
*/
|
|
65
185
|
isSome() {
|
|
66
186
|
return this._tag === "Some" && !this.isNone();
|
|
67
187
|
}
|
|
68
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Type guard that narrows to None.
|
|
190
|
+
*
|
|
191
|
+
* Includes async None tracking while preserving the stored payload type.
|
|
192
|
+
*
|
|
193
|
+
* @returns true if this Option is None
|
|
194
|
+
*/
|
|
69
195
|
isNone() {
|
|
70
196
|
return this._tag === "None" || this.#val === NONE_VAL || require_utils.isPromiseLike(this.#val) && this.#ctx.promiseNoneSlot;
|
|
71
197
|
}
|
|
72
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* Type guard for the UNIT sentinel value.
|
|
200
|
+
*
|
|
201
|
+
* @returns true if the contained value is UNIT
|
|
202
|
+
*/
|
|
73
203
|
isUnit() {
|
|
74
204
|
return this.#val === require_unit.UNIT;
|
|
75
205
|
}
|
|
76
|
-
/** Returns value or throws UnwrapError */
|
|
77
206
|
unwrap() {
|
|
78
207
|
if (this.isNone()) throw UNWRAPPED_NONE_ERR;
|
|
79
|
-
|
|
208
|
+
const curr = this.#val;
|
|
209
|
+
if (require_utils.isPromiseLike(curr)) {
|
|
210
|
+
const ctx = this.#ctx;
|
|
211
|
+
return curr.then((value) => {
|
|
212
|
+
if (value === NONE_VAL || ctx.promiseNoneSlot) throw UNWRAPPED_NONE_ERR;
|
|
213
|
+
return value;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return curr;
|
|
80
217
|
}
|
|
81
|
-
/** Returns value or the provided default */
|
|
82
218
|
unwrapOr(defaultValue) {
|
|
83
|
-
|
|
84
|
-
|
|
219
|
+
const curr = this.#val;
|
|
220
|
+
if (require_utils.isPromiseLike(curr)) {
|
|
221
|
+
const ctx = this.#ctx;
|
|
222
|
+
return curr.then((value) => value === NONE_VAL || ctx.promiseNoneSlot ? defaultValue : value);
|
|
223
|
+
}
|
|
224
|
+
return this.isNone() ? defaultValue : curr;
|
|
85
225
|
}
|
|
86
|
-
/** Returns value or calls factory to get default */
|
|
87
226
|
unwrapOrElse(fn) {
|
|
88
|
-
|
|
89
|
-
|
|
227
|
+
const curr = this.#val;
|
|
228
|
+
if (require_utils.isPromiseLike(curr)) {
|
|
229
|
+
const ctx = this.#ctx;
|
|
230
|
+
return curr.then((value) => value === NONE_VAL || ctx.promiseNoneSlot ? fn() : value);
|
|
231
|
+
}
|
|
232
|
+
return this.isNone() ? fn() : curr;
|
|
90
233
|
}
|
|
91
|
-
/** Returns value or null for None */
|
|
92
234
|
safeUnwrap() {
|
|
93
235
|
if (this.isNone()) return null;
|
|
94
|
-
|
|
236
|
+
const curr = this.#val;
|
|
237
|
+
if (require_utils.isPromiseLike(curr)) {
|
|
238
|
+
const ctx = this.#ctx;
|
|
239
|
+
return curr.then((value) => value === NONE_VAL || ctx.promiseNoneSlot ? null : value);
|
|
240
|
+
}
|
|
241
|
+
return curr;
|
|
95
242
|
}
|
|
96
|
-
/**
|
|
243
|
+
/**
|
|
244
|
+
* Exhaustive pattern match on Option state.
|
|
245
|
+
*
|
|
246
|
+
* @template U - Result type
|
|
247
|
+
* @param cases - Handlers for Some and None
|
|
248
|
+
* @returns Result of the matching handler
|
|
249
|
+
*/
|
|
97
250
|
match(cases) {
|
|
98
251
|
if (this.isNone()) return cases.None();
|
|
99
252
|
return cases.Some(this.#val);
|
|
@@ -104,12 +257,23 @@ var Option = class Option {
|
|
|
104
257
|
if (require_utils.isPromiseLike(curr)) {
|
|
105
258
|
const p = curr;
|
|
106
259
|
const newCtx = { promiseNoneSlot: false };
|
|
107
|
-
|
|
260
|
+
const safetlyMapped = Option.safeMap(p, mapper, newCtx);
|
|
261
|
+
return new Option(safetlyMapped, newCtx, "Some");
|
|
108
262
|
}
|
|
109
263
|
const transformed = mapper(curr);
|
|
110
264
|
if (require_utils.isPromiseLike(transformed)) return new Option(transformed, { promiseNoneSlot: false }, "Some");
|
|
111
265
|
return new Option(transformed, { promiseNoneSlot: false }, "Some");
|
|
112
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Safely maps a promised value while tracking None in async context.
|
|
269
|
+
*
|
|
270
|
+
* @template Curr - Input value type
|
|
271
|
+
* @template U - Output value type
|
|
272
|
+
* @param p - Promise to map over
|
|
273
|
+
* @param mapper - Transform function
|
|
274
|
+
* @param ctx - Async context for None tracking
|
|
275
|
+
* @returns Promise of the transformed value or sentinel
|
|
276
|
+
*/
|
|
113
277
|
static safeMap(p, mapper, ctx) {
|
|
114
278
|
return p.then((v) => {
|
|
115
279
|
if (v === NONE_VAL) {
|
|
@@ -122,10 +286,14 @@ var Option = class Option {
|
|
|
122
286
|
mapOr(defaultVal, fn) {
|
|
123
287
|
if (this.isNone()) return require_utils.isPromiseLike(this.#val) ? Promise.resolve(defaultVal) : defaultVal;
|
|
124
288
|
const curr = this.#val;
|
|
125
|
-
if (require_utils.isPromiseLike(curr))
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
289
|
+
if (require_utils.isPromiseLike(curr)) {
|
|
290
|
+
const p = curr;
|
|
291
|
+
const ctx = this.#ctx;
|
|
292
|
+
return p.then(async (v) => {
|
|
293
|
+
if (v === NONE_VAL || ctx.promiseNoneSlot) return defaultVal;
|
|
294
|
+
return fn(v);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
129
297
|
return fn(curr);
|
|
130
298
|
}
|
|
131
299
|
flatMap(mapper) {
|
|
@@ -134,25 +302,20 @@ var Option = class Option {
|
|
|
134
302
|
if (require_utils.isPromiseLike(curr)) {
|
|
135
303
|
const p = curr;
|
|
136
304
|
const newCtx = { promiseNoneSlot: false };
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
resolve(NONE_VAL);
|
|
152
|
-
} else resolve(r.#val);
|
|
153
|
-
}
|
|
154
|
-
}, reject);
|
|
155
|
-
}), newCtx, "Some");
|
|
305
|
+
const newP = p.then(async (innerVal) => {
|
|
306
|
+
if (innerVal === NONE_VAL) {
|
|
307
|
+
newCtx.promiseNoneSlot = true;
|
|
308
|
+
return NONE_VAL;
|
|
309
|
+
}
|
|
310
|
+
const mapped = await mapper(innerVal);
|
|
311
|
+
const value = await mapped.#val;
|
|
312
|
+
if (mapped.isNone() || value === NONE_VAL) {
|
|
313
|
+
newCtx.promiseNoneSlot = true;
|
|
314
|
+
return NONE_VAL;
|
|
315
|
+
}
|
|
316
|
+
return value;
|
|
317
|
+
});
|
|
318
|
+
return new Option(newP, newCtx, "Some");
|
|
156
319
|
}
|
|
157
320
|
const mapped = mapper(curr);
|
|
158
321
|
if (require_utils.isPromiseLike(mapped)) return Option.fromPromise(mapped);
|
|
@@ -164,7 +327,7 @@ var Option = class Option {
|
|
|
164
327
|
if (require_utils.isPromiseLike(curr)) {
|
|
165
328
|
const p = curr;
|
|
166
329
|
const newCtx = { promiseNoneSlot: false };
|
|
167
|
-
|
|
330
|
+
const next = p.then(async (v) => {
|
|
168
331
|
if (v === NONE_VAL) {
|
|
169
332
|
newCtx.promiseNoneSlot = true;
|
|
170
333
|
return NONE_VAL;
|
|
@@ -174,16 +337,18 @@ var Option = class Option {
|
|
|
174
337
|
return NONE_VAL;
|
|
175
338
|
}
|
|
176
339
|
return v;
|
|
177
|
-
})
|
|
340
|
+
});
|
|
341
|
+
return new Option(next, newCtx, "Some");
|
|
178
342
|
}
|
|
179
343
|
const result = pred(curr);
|
|
180
344
|
if (require_utils.isPromiseLike(result)) {
|
|
181
345
|
const newCtx = { promiseNoneSlot: false };
|
|
182
|
-
|
|
346
|
+
const p = result.then((passed) => {
|
|
183
347
|
if (passed) return curr;
|
|
184
348
|
newCtx.promiseNoneSlot = true;
|
|
185
349
|
return NONE_VAL;
|
|
186
|
-
})
|
|
350
|
+
});
|
|
351
|
+
return new Option(p, newCtx, "Some");
|
|
187
352
|
}
|
|
188
353
|
return result ? this : Option.None;
|
|
189
354
|
}
|
|
@@ -193,17 +358,21 @@ var Option = class Option {
|
|
|
193
358
|
if (require_utils.isPromiseLike(curr)) {
|
|
194
359
|
const val = curr;
|
|
195
360
|
const newCtx = { promiseNoneSlot: false };
|
|
196
|
-
|
|
361
|
+
const p = val.then(async (v) => {
|
|
197
362
|
if (v === NONE_VAL) {
|
|
198
363
|
newCtx.promiseNoneSlot = true;
|
|
199
364
|
return NONE_VAL;
|
|
200
365
|
}
|
|
201
366
|
return [v, await fn(v)];
|
|
202
|
-
})
|
|
367
|
+
});
|
|
368
|
+
return new Option(p, newCtx, "Some");
|
|
203
369
|
}
|
|
204
370
|
const value = curr;
|
|
205
371
|
const u = fn(value);
|
|
206
|
-
if (require_utils.isPromiseLike(u))
|
|
372
|
+
if (require_utils.isPromiseLike(u)) {
|
|
373
|
+
const p = u.then((uu) => [value, uu]);
|
|
374
|
+
return new Option(p, { promiseNoneSlot: false }, "Some");
|
|
375
|
+
}
|
|
207
376
|
return new Option([value, u], { promiseNoneSlot: false }, "Some");
|
|
208
377
|
}
|
|
209
378
|
flatZip(fn) {
|
|
@@ -212,7 +381,7 @@ var Option = class Option {
|
|
|
212
381
|
if (require_utils.isPromiseLike(curr)) {
|
|
213
382
|
const val = curr;
|
|
214
383
|
const newCtx = { promiseNoneSlot: false };
|
|
215
|
-
|
|
384
|
+
const p = val.then(async (v) => {
|
|
216
385
|
if (v === NONE_VAL) {
|
|
217
386
|
newCtx.promiseNoneSlot = true;
|
|
218
387
|
return NONE_VAL;
|
|
@@ -223,58 +392,468 @@ var Option = class Option {
|
|
|
223
392
|
return NONE_VAL;
|
|
224
393
|
}
|
|
225
394
|
return [v, next.#val];
|
|
226
|
-
})
|
|
395
|
+
});
|
|
396
|
+
return new Option(p, newCtx, "Some");
|
|
227
397
|
}
|
|
228
398
|
const c = curr;
|
|
229
399
|
const u = fn(c);
|
|
230
400
|
if (require_utils.isPromiseLike(u)) {
|
|
231
401
|
const newCtx = { promiseNoneSlot: false };
|
|
232
|
-
|
|
402
|
+
const p = u.then((uu) => {
|
|
233
403
|
if (uu.isNone()) {
|
|
234
404
|
newCtx.promiseNoneSlot = true;
|
|
235
405
|
return NONE_VAL;
|
|
236
406
|
}
|
|
237
407
|
return [c, uu.#val];
|
|
238
|
-
})
|
|
408
|
+
});
|
|
409
|
+
return new Option(p, newCtx, "Some");
|
|
239
410
|
}
|
|
240
411
|
return u.map((inner) => [c, inner]);
|
|
241
412
|
}
|
|
242
|
-
/**
|
|
413
|
+
/**
|
|
414
|
+
* Executes a side effect for Some values and returns self.
|
|
415
|
+
*
|
|
416
|
+
* For `Option<Promise<T>>`, `tap` receives the wrapped promise object,
|
|
417
|
+
* not the settled inner value.
|
|
418
|
+
*
|
|
419
|
+
* @param fn - Side effect function
|
|
420
|
+
* @returns The same Option (chainable)
|
|
421
|
+
*/
|
|
243
422
|
tap(fn) {
|
|
244
423
|
if (this.isSome()) fn(this.#val);
|
|
245
424
|
return this;
|
|
246
425
|
}
|
|
247
|
-
/**
|
|
426
|
+
/**
|
|
427
|
+
* Converts this Option to a Result, using the provided error for None.
|
|
428
|
+
* Promise-backed Options preserve settled absence and propagate rejections.
|
|
429
|
+
*
|
|
430
|
+
* @template E - Error type
|
|
431
|
+
* @param error - Error value for None
|
|
432
|
+
* @returns Ok with value when Some, otherwise Err with error
|
|
433
|
+
*/
|
|
248
434
|
toResult(error) {
|
|
435
|
+
if (require_utils.isPromiseLike(this.#val)) return require_result.Result.fromPromise(this.toPromise().then((option) => option.isNone() ? require_result.Result.Err(error) : require_result.Result.Ok(option.unwrap())));
|
|
249
436
|
if (this.isNone()) return require_result.Result.Err(error);
|
|
250
437
|
return require_result.Result.Ok(this.#val);
|
|
251
438
|
}
|
|
252
|
-
/**
|
|
439
|
+
/**
|
|
440
|
+
* Resolves the inner promise (if any) while preserving Option structure.
|
|
441
|
+
*
|
|
442
|
+
* @template Curr - Awaited value type
|
|
443
|
+
* @returns Promise of Option with awaited value, or None
|
|
444
|
+
*/
|
|
253
445
|
async toPromise() {
|
|
254
446
|
if (this.isNone()) return Option.None;
|
|
255
447
|
const curr = this.#val;
|
|
256
448
|
let inner;
|
|
257
449
|
if (require_utils.isPromiseLike(curr)) {
|
|
258
450
|
const awaited = await curr;
|
|
259
|
-
if (awaited === NONE_VAL) return Option.None;
|
|
451
|
+
if (awaited === NONE_VAL || this.#ctx.promiseNoneSlot) return Option.None;
|
|
260
452
|
inner = awaited;
|
|
261
453
|
} else inner = curr;
|
|
262
454
|
return new Option(inner, this.#ctx, "Some");
|
|
263
455
|
}
|
|
264
|
-
/**
|
|
456
|
+
/**
|
|
457
|
+
* Maps over array elements inside an Option<Array<T>>.
|
|
458
|
+
*
|
|
459
|
+
* @template Inner - Array element type
|
|
460
|
+
* @template Out - Mapped element type
|
|
461
|
+
* @param mapper - Function to map each element
|
|
462
|
+
* @returns Option containing the mapped array
|
|
463
|
+
* @throws TypeError if called on a non-array value
|
|
464
|
+
*/
|
|
265
465
|
innerMap(mapper) {
|
|
266
466
|
if (this.isNone()) return Option.None;
|
|
267
467
|
if (!Array.isArray(this.#val)) throw new TypeError("innerMap can only be called on Option<Array<T>>");
|
|
268
468
|
return new Option(this.#val.map(mapper), this.#ctx, "Some");
|
|
269
469
|
}
|
|
270
|
-
/**
|
|
470
|
+
/**
|
|
471
|
+
* Returns a string representation of the Option.
|
|
472
|
+
*
|
|
473
|
+
* @returns "Option::None" or "Option::Some(value)"
|
|
474
|
+
*/
|
|
271
475
|
toString() {
|
|
272
476
|
if (this.isNone()) return "Option::None";
|
|
273
477
|
return `Option::Some(${String(this.#val)})`;
|
|
274
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* Makes Option iterable for use with generator-based syntax.
|
|
481
|
+
*
|
|
482
|
+
* Yields self and returns the unwrapped value when resumed.
|
|
483
|
+
* Used internally by {@link Option.gen} for `yield*` syntax.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```ts
|
|
487
|
+
* const result = Option.gen(function* () {
|
|
488
|
+
* const value = yield* Option.Some(42);
|
|
489
|
+
* return value * 2;
|
|
490
|
+
* });
|
|
491
|
+
* // Some(84)
|
|
492
|
+
* ```
|
|
493
|
+
*
|
|
494
|
+
* @internal
|
|
495
|
+
*/
|
|
496
|
+
*[Symbol.iterator]() {
|
|
497
|
+
const trace = (/* @__PURE__ */ new Error()).stack;
|
|
498
|
+
return yield new require_utils.CapturedTrace(this, trace);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Makes Option iterable for use with async generator-based syntax.
|
|
502
|
+
*
|
|
503
|
+
* Yields self and returns the unwrapped value when resumed.
|
|
504
|
+
* Used internally by {@link Option.asyncGen} for `yield*` syntax.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```ts
|
|
508
|
+
* const result = await Option.asyncGen(async function* () {
|
|
509
|
+
* const value = yield* await Promise.resolve(Option.Some(42));
|
|
510
|
+
* return value * 2;
|
|
511
|
+
* });
|
|
512
|
+
* // Some(84)
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* @internal
|
|
516
|
+
*/
|
|
517
|
+
async *[Symbol.asyncIterator]() {
|
|
518
|
+
const trace = (/* @__PURE__ */ new Error()).stack;
|
|
519
|
+
return yield new require_utils.CapturedTrace(this, trace);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Generator-based syntax for chaining Option operations (simplified, no adapter).
|
|
523
|
+
*
|
|
524
|
+
* Provides imperative-style code while maintaining functional Option handling.
|
|
525
|
+
* Use `yield*` to unwrap Options or short-circuit on `None`.
|
|
526
|
+
*
|
|
527
|
+
* Short-circuits on first `None`, returning singleton `None`. Uses iteration
|
|
528
|
+
* instead of recursion to avoid stack overflow on deep chains.
|
|
529
|
+
*
|
|
530
|
+
* @template T - The return type of the generator
|
|
531
|
+
* @param genFn - A generator function that yields Options
|
|
532
|
+
* @returns `Some<T>` with the generator's return value, or `None`
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* // Simple sync chain
|
|
537
|
+
* const result = Option.gen(function* () {
|
|
538
|
+
* const a = yield* Option.Some(1);
|
|
539
|
+
* const b = yield* Option.Some(2);
|
|
540
|
+
* return a + b;
|
|
541
|
+
* });
|
|
542
|
+
* // Some(3)
|
|
543
|
+
*
|
|
544
|
+
* // None short-circuit
|
|
545
|
+
* const shortCircuit = Option.gen(function* () {
|
|
546
|
+
* const a = yield* Option.Some(1);
|
|
547
|
+
* const b = yield* Option.None; // Short-circuits here
|
|
548
|
+
* const c = yield* Option.Some(3); // Never executes
|
|
549
|
+
* return a + b + c;
|
|
550
|
+
* });
|
|
551
|
+
* // None
|
|
552
|
+
*
|
|
553
|
+
* // Chaining optional operations
|
|
554
|
+
* const email = Option.gen(function* () {
|
|
555
|
+
* const user = yield* findUser(userId);
|
|
556
|
+
* const profile = yield* Option.fromNullable(user.profile);
|
|
557
|
+
* const settings = yield* Option.fromNullable(profile.settings);
|
|
558
|
+
* return settings.email;
|
|
559
|
+
* });
|
|
560
|
+
*
|
|
561
|
+
* // Nested optional access
|
|
562
|
+
* const city = Option.gen(function* () {
|
|
563
|
+
* const user = yield* Option.fromNullable(getUser());
|
|
564
|
+
* const address = yield* Option.fromNullable(user?.address);
|
|
565
|
+
* const city = yield* Option.fromNullable(address?.city);
|
|
566
|
+
* return city;
|
|
567
|
+
* });
|
|
568
|
+
* ```
|
|
569
|
+
*
|
|
570
|
+
* @see {@link genAdapter} for better type inference in complex chains
|
|
571
|
+
* @see {@link asyncGen} for async operations
|
|
572
|
+
*/
|
|
573
|
+
static gen(genFn) {
|
|
574
|
+
const iterator = genFn();
|
|
575
|
+
let nextArg;
|
|
576
|
+
let currentResult;
|
|
577
|
+
let naturalCompleted = false;
|
|
578
|
+
try {
|
|
579
|
+
while (true) {
|
|
580
|
+
const next = iterator.next(nextArg);
|
|
581
|
+
if (next.done) {
|
|
582
|
+
naturalCompleted = true;
|
|
583
|
+
currentResult = Option.Some(next.value);
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
let yielded = next.value;
|
|
587
|
+
if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
|
|
588
|
+
if (yielded.isNone()) {
|
|
589
|
+
currentResult = Option.None;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
nextArg = yielded.unwrap();
|
|
593
|
+
}
|
|
594
|
+
} finally {
|
|
595
|
+
if (!naturalCompleted) require_utils.closeGenerator(iterator, (yielded) => {
|
|
596
|
+
if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
|
|
597
|
+
const effect = yielded;
|
|
598
|
+
return effect.isNone() ? void 0 : effect.unwrap();
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return currentResult;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Generator-based syntax for chaining Option operations with an adapter function.
|
|
605
|
+
*
|
|
606
|
+
* Provides better type inference and IDE support compared to {@link gen}.
|
|
607
|
+
* The `$` adapter wraps Options and enables clearer type tracking.
|
|
608
|
+
*
|
|
609
|
+
* Short-circuits on first `None`, returning singleton `None`. Uses iteration
|
|
610
|
+
* instead of recursion to avoid stack overflow on deep chains.
|
|
611
|
+
*
|
|
612
|
+
* @template Eff - The effect type (inferred from usage)
|
|
613
|
+
* @template T - The return type of the generator
|
|
614
|
+
* @param genFn - A generator function receiving an adapter `$`
|
|
615
|
+
* @returns `Some<T>` with the generator's return value, or `None`
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```ts
|
|
619
|
+
* // Better type inference
|
|
620
|
+
* const result = Option.genAdapter(function* ($) {
|
|
621
|
+
* const a = yield* $(Option.Some(1));
|
|
622
|
+
* const b = yield* $(Option.Some(2));
|
|
623
|
+
* return a + b;
|
|
624
|
+
* });
|
|
625
|
+
* // Some(3)
|
|
626
|
+
*
|
|
627
|
+
* // Deep nested access with better type safety
|
|
628
|
+
* const email = Option.genAdapter(function* ($) {
|
|
629
|
+
* const user = yield* $(Option.fromNullable(apiResponse?.user));
|
|
630
|
+
* const profile = yield* $(Option.fromNullable(user?.profile));
|
|
631
|
+
* const contact = yield* $(Option.fromNullable(profile?.contact));
|
|
632
|
+
* return contact?.email;
|
|
633
|
+
* });
|
|
634
|
+
*
|
|
635
|
+
* // Complex validation chain
|
|
636
|
+
* const validConfig = Option.genAdapter(function* ($) {
|
|
637
|
+
* const raw = yield* $(loadConfig());
|
|
638
|
+
* const parsed = yield* $(parseConfig(raw));
|
|
639
|
+
* const validated = yield* $(validateConfig(parsed));
|
|
640
|
+
* return validated;
|
|
641
|
+
* });
|
|
642
|
+
* ```
|
|
643
|
+
*
|
|
644
|
+
* @see {@link gen} for simpler chains without adapter
|
|
645
|
+
* @see {@link asyncGenAdapter} for async operations
|
|
646
|
+
*/
|
|
647
|
+
static genAdapter(genFn) {
|
|
648
|
+
const adapter = (option) => new OptionYieldWrap(option);
|
|
649
|
+
const iterator = genFn(adapter);
|
|
650
|
+
let nextArg;
|
|
651
|
+
let currentResult;
|
|
652
|
+
let naturalCompleted = false;
|
|
653
|
+
try {
|
|
654
|
+
while (true) {
|
|
655
|
+
const next = iterator.next(nextArg);
|
|
656
|
+
if (next.done) {
|
|
657
|
+
naturalCompleted = true;
|
|
658
|
+
currentResult = Option.Some(next.value);
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
let option = next.value.option;
|
|
662
|
+
if (require_utils.isCapturedTrace(option)) option = option.value;
|
|
663
|
+
if (option.isNone()) {
|
|
664
|
+
currentResult = Option.None;
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
nextArg = option.unwrap();
|
|
668
|
+
}
|
|
669
|
+
} finally {
|
|
670
|
+
if (!naturalCompleted) require_utils.closeGenerator(iterator, (yielded) => {
|
|
671
|
+
if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
|
|
672
|
+
let effect = yielded.option;
|
|
673
|
+
if (require_utils.isCapturedTrace(effect)) effect = effect.value;
|
|
674
|
+
return effect.isNone() ? void 0 : effect.unwrap();
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
return currentResult;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Async generator-based syntax for chaining Option operations (simplified, no adapter).
|
|
681
|
+
*
|
|
682
|
+
* Use `yield*` with `Option<T>` values directly. For `Promise<Option<T>>`,
|
|
683
|
+
* await first then `yield*`.
|
|
684
|
+
*
|
|
685
|
+
* Short-circuits on first `None`, returning singleton `None`. Uses async iteration
|
|
686
|
+
* instead of recursion to avoid stack overflow on deep chains.
|
|
687
|
+
*
|
|
688
|
+
* @template T - The return type of the generator
|
|
689
|
+
* @param genFn - An async generator function that yields Options
|
|
690
|
+
* @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
|
|
691
|
+
*
|
|
692
|
+
* @example
|
|
693
|
+
* ```ts
|
|
694
|
+
* // Simple async chain
|
|
695
|
+
* const result = await Option.asyncGen(async function* () {
|
|
696
|
+
* const a = yield* Option.Some(1);
|
|
697
|
+
* const b = yield* await asyncOperation(a); // await Promise<Option> first
|
|
698
|
+
* const c = yield* Option.Some(3);
|
|
699
|
+
* return a + b + c;
|
|
700
|
+
* });
|
|
701
|
+
* // Some(result)
|
|
702
|
+
*
|
|
703
|
+
* // None short-circuit in async
|
|
704
|
+
* const shortCircuit = await Option.asyncGen(async function* () {
|
|
705
|
+
* const data = yield* await fetchOptionalData();
|
|
706
|
+
* const parsed = yield* parse(data); // Short-circuits on None
|
|
707
|
+
* const validated = yield* validate(parsed); // Never executes
|
|
708
|
+
* return validated;
|
|
709
|
+
* });
|
|
710
|
+
* // None
|
|
711
|
+
*
|
|
712
|
+
* // Mixed sync/async workflow
|
|
713
|
+
* const result = await Option.asyncGen(async function* () {
|
|
714
|
+
* const id = yield* Option.Some(parseInt(input)); // sync
|
|
715
|
+
* const user = yield* await fetchUser(id); // async
|
|
716
|
+
* const profile = yield* Option.fromNullable(user?.profile); // sync
|
|
717
|
+
* const enriched = yield* await enrichProfile(profile); // async
|
|
718
|
+
* return enriched;
|
|
719
|
+
* });
|
|
720
|
+
*
|
|
721
|
+
* // Complex pipeline with multiple optional steps
|
|
722
|
+
* async function processLead(email: string): Promise<Option<Lead>> {
|
|
723
|
+
* return await Option.asyncGen(async function* () {
|
|
724
|
+
* const normalized = yield* Option.Some(normalizeEmail(email));
|
|
725
|
+
* const existing = yield* await findExistingLead(normalized);
|
|
726
|
+
* const enriched = yield* await enrichLeadData(existing);
|
|
727
|
+
* const validated = yield* validateLead(enriched);
|
|
728
|
+
* return validated;
|
|
729
|
+
* });
|
|
730
|
+
* }
|
|
731
|
+
* ```
|
|
732
|
+
*
|
|
733
|
+
* Settles promise-backed Options before checking for None; raw rejections propagate.
|
|
734
|
+
* @see {@link asyncGenAdapter} for better type inference and cleaner syntax
|
|
735
|
+
* @see {@link gen} for synchronous operations
|
|
736
|
+
*/
|
|
737
|
+
static async asyncGen(genFn) {
|
|
738
|
+
const iterator = genFn();
|
|
739
|
+
let nextArg;
|
|
740
|
+
let currentResult;
|
|
741
|
+
let naturalCompleted = false;
|
|
742
|
+
try {
|
|
743
|
+
while (true) {
|
|
744
|
+
const next = await iterator.next(nextArg);
|
|
745
|
+
if (next.done) {
|
|
746
|
+
naturalCompleted = true;
|
|
747
|
+
currentResult = Option.Some(next.value);
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
let option = next.value;
|
|
751
|
+
if (require_utils.isCapturedTrace(option)) option = option.value;
|
|
752
|
+
option = await option.toPromise();
|
|
753
|
+
if (option.isNone()) {
|
|
754
|
+
currentResult = Option.None;
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
nextArg = await option.unwrap();
|
|
758
|
+
}
|
|
759
|
+
} finally {
|
|
760
|
+
if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, async (yielded) => {
|
|
761
|
+
if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
|
|
762
|
+
let effect = yielded;
|
|
763
|
+
effect = await effect.toPromise();
|
|
764
|
+
return effect.isNone() ? void 0 : effect.unwrap();
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
return currentResult;
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* Async generator-based syntax for chaining Option operations with an adapter function.
|
|
771
|
+
*
|
|
772
|
+
* The `$` adapter handles both sync `Option<T>` and async `Promise<Option<T>>`,
|
|
773
|
+
* automatically awaiting promises. This provides cleaner syntax for mixed sync/async workflows.
|
|
774
|
+
* Settles promise-backed Options before checking for None; raw rejections propagate.
|
|
775
|
+
*
|
|
776
|
+
* Short-circuits on first `None`, returning singleton `None`. Uses async iteration
|
|
777
|
+
* instead of recursion to avoid stack overflow on deep chains.
|
|
778
|
+
*
|
|
779
|
+
* @template Eff - The effect type (inferred from usage)
|
|
780
|
+
* @template T - The return type of the generator
|
|
781
|
+
* @param genFn - An async generator function receiving an adapter `$`
|
|
782
|
+
* @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
|
|
783
|
+
*
|
|
784
|
+
* @example
|
|
785
|
+
* ```ts
|
|
786
|
+
* // No need to manually await - adapter handles it
|
|
787
|
+
* const result = await Option.asyncGenAdapter(async function* ($) {
|
|
788
|
+
* const a = yield* $(Option.Some(1)); // sync Option
|
|
789
|
+
* const b = yield* $(asyncOperation(a)); // Promise<Option> - auto-awaited
|
|
790
|
+
* const c = yield* $(Option.Some(3));
|
|
791
|
+
* return a + b + c;
|
|
792
|
+
* });
|
|
793
|
+
*
|
|
794
|
+
* // Complex workflow with database and API calls
|
|
795
|
+
* const userData = await Option.asyncGenAdapter(async function* ($) {
|
|
796
|
+
* const session = yield* $(getSession()); // Promise<Option<Session>>
|
|
797
|
+
* const userId = yield* $(Option.fromNullable(session?.userId));
|
|
798
|
+
* const profile = yield* $(await fetchProfile(userId)); // Promise<Option<Profile>>
|
|
799
|
+
* const preferences = yield* $(Option.fromNullable(profile?.preferences));
|
|
800
|
+
* const enriched = yield* $(await enrichPreferences(preferences));
|
|
801
|
+
* return enriched;
|
|
802
|
+
* });
|
|
803
|
+
*
|
|
804
|
+
* // Real-world: Optional data enrichment
|
|
805
|
+
* async function getProductDetails(productId: string): Promise<Option<ProductDetails>> {
|
|
806
|
+
* return await Option.asyncGenAdapter(async function* ($) {
|
|
807
|
+
* const product = yield* $(await fetchProduct(productId));
|
|
808
|
+
* const pricing = yield* $(await fetchPricing(product.sku));
|
|
809
|
+
* const inventory = yield* $(await checkInventory(product.id));
|
|
810
|
+
* const reviews = yield* $(await fetchReviews(product.id));
|
|
811
|
+
* const related = yield* $(await fetchRelatedProducts(product.category));
|
|
812
|
+
*
|
|
813
|
+
* return { product, pricing, inventory, reviews, related };
|
|
814
|
+
* });
|
|
815
|
+
* }
|
|
816
|
+
* ```
|
|
817
|
+
*
|
|
818
|
+
* @see {@link asyncGen} when you want explicit `await` for async operations
|
|
819
|
+
* @see {@link genAdapter} for synchronous operations
|
|
820
|
+
*/
|
|
821
|
+
static async asyncGenAdapter(genFn) {
|
|
822
|
+
const adapter = (option) => new AsyncOptionYieldWrap(option);
|
|
823
|
+
const iterator = genFn(adapter);
|
|
824
|
+
let nextArg;
|
|
825
|
+
let currentResult;
|
|
826
|
+
let naturalCompleted = false;
|
|
827
|
+
try {
|
|
828
|
+
while (true) {
|
|
829
|
+
const next = await iterator.next(nextArg);
|
|
830
|
+
if (next.done) {
|
|
831
|
+
naturalCompleted = true;
|
|
832
|
+
currentResult = Option.Some(next.value);
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
let option = await next.value.option;
|
|
836
|
+
if (require_utils.isCapturedTrace(option)) option = option.value;
|
|
837
|
+
option = await option.toPromise();
|
|
838
|
+
if (option.isNone()) {
|
|
839
|
+
currentResult = Option.None;
|
|
840
|
+
break;
|
|
841
|
+
}
|
|
842
|
+
nextArg = await option.unwrap();
|
|
843
|
+
}
|
|
844
|
+
} finally {
|
|
845
|
+
if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, async (yielded) => {
|
|
846
|
+
if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
|
|
847
|
+
let effect = await yielded.option;
|
|
848
|
+
if (require_utils.isCapturedTrace(effect)) effect = effect.value;
|
|
849
|
+
effect = await effect.toPromise();
|
|
850
|
+
return effect.isNone() ? void 0 : effect.unwrap();
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
return currentResult;
|
|
854
|
+
}
|
|
275
855
|
};
|
|
276
856
|
|
|
277
|
-
//#endregion
|
|
278
857
|
exports.Option = Option;
|
|
279
858
|
exports.UnwrappedNone = UnwrappedNone;
|
|
280
859
|
//# sourceMappingURL=option.cjs.map
|