@sapphire/result 3.0.0-pr-589.aa473f9.0 → 3.0.0-pr-935.7da5c8bb

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.
@@ -0,0 +1,1938 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+
6
+ // src/lib/common/utils.ts
7
+ function isFunction(input) {
8
+ return typeof input === "function";
9
+ }
10
+ __name(isFunction, "isFunction");
11
+ function returnThis() {
12
+ return this;
13
+ }
14
+ __name(returnThis, "returnThis");
15
+
16
+ // src/lib/OptionError.ts
17
+ var _OptionError = class _OptionError extends Error {
18
+ get name() {
19
+ return this.constructor.name;
20
+ }
21
+ };
22
+ __name(_OptionError, "OptionError");
23
+ var OptionError = _OptionError;
24
+
25
+ // src/lib/ResultError.ts
26
+ var _ResultError = class _ResultError extends Error {
27
+ constructor(message, value) {
28
+ super(message);
29
+ __publicField(this, "value");
30
+ this.value = value;
31
+ }
32
+ get name() {
33
+ return this.constructor.name;
34
+ }
35
+ };
36
+ __name(_ResultError, "ResultError");
37
+ var ResultError = _ResultError;
38
+
39
+ // src/lib/Result.ts
40
+ var ValueProperty = Symbol.for("@sapphire/result:Result.value");
41
+ var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
42
+ var UnwrapSafeProperty = Symbol.for("@sapphire/result:Result.safeUnwrap");
43
+ var _a, _b;
44
+ var _Result = class _Result {
45
+ constructor(value, success) {
46
+ __publicField(this, _b);
47
+ __publicField(this, _a);
48
+ this[ValueProperty] = value;
49
+ this[SuccessProperty] = success;
50
+ }
51
+ /**
52
+ * Returns `true` if the result is `Ok`.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const x = ok(-3);
57
+ * assert.equal(x.isOk(), true);
58
+ * ```
59
+ * @example
60
+ * ```typescript
61
+ * const x = err('Some error message');
62
+ * assert.equal(x.isOk(), false);
63
+ * ```
64
+ *
65
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
66
+ */
67
+ isOk() {
68
+ return this[SuccessProperty];
69
+ }
70
+ isOkAnd(cb) {
71
+ return this.isOk() && cb(this[ValueProperty]);
72
+ }
73
+ /**
74
+ * Returns `true` if the result is `Err`.
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const x = ok(-3);
79
+ * assert.equal(x.isErr(), false);
80
+ * ```
81
+ * @example
82
+ * ```typescript
83
+ * const x = err('Some error message');
84
+ * assert.equal(x.isErr(), true);
85
+ * ```
86
+ *
87
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
88
+ */
89
+ isErr() {
90
+ return !this[SuccessProperty];
91
+ }
92
+ isErrAnd(cb) {
93
+ return this.isErr() && cb(this[ValueProperty]);
94
+ }
95
+ /**
96
+ * Converts from `Result<T, E>` to `Option<T>`.
97
+ *
98
+ * Converts itself into an `Option<T>`, and discarding the error, if any.
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const x: Result<number, string> = ok(2);
103
+ * assert.equal(x.ok(), some(2));
104
+ * ```
105
+ * @example
106
+ * ```typescript
107
+ * const x: Result<number, string> = err('Some error message');
108
+ * assert.equal(x.ok(), none);
109
+ * ```
110
+ *
111
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
112
+ */
113
+ ok() {
114
+ return this.match({ ok: /* @__PURE__ */ __name((value) => some(value), "ok"), err: /* @__PURE__ */ __name(() => none, "err") });
115
+ }
116
+ /**
117
+ * Converts from `Result<T, E>` to `Option<E>`.
118
+ *
119
+ * Converts itself into an `Option<E>`, and discarding the successful value, if any.
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * const x: Result<number, string> = ok(2);
124
+ * assert.equal(x.err(), none);
125
+ * ```
126
+ * @example
127
+ * ```typescript
128
+ * const x: Result<number, string> = err('Some error message');
129
+ * assert.equal(x.err(), 'Some error message');
130
+ * ```
131
+ *
132
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
133
+ */
134
+ err() {
135
+ return this.match({ ok: /* @__PURE__ */ __name(() => none, "ok"), err: /* @__PURE__ */ __name((error) => some(error), "err") });
136
+ }
137
+ /**
138
+ * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
139
+ * untouched.
140
+ * @param cb The predicate.
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * const x: Result<number, string> = ok(2);
145
+ * assert.equal(x.map((value) => value * 2), ok(4));
146
+ * ```
147
+ * @example
148
+ * ```typescript
149
+ * const x: Result<number, string> = err('Some error message');
150
+ * assert.equal(x.map((value) => value * 2), err('Some error message'));
151
+ * ```
152
+ *
153
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
154
+ */
155
+ map(cb) {
156
+ return this.match({ ok: /* @__PURE__ */ __name((value) => ok(cb(value)), "ok"), err: returnThis });
157
+ }
158
+ /**
159
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
160
+ * untouched.
161
+ *
162
+ * Unlike {@link map}, this method does not wrap the returned value inside `Ok`, but instead, it returns the
163
+ * returned value.
164
+ * @param cb The predicate.
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * const x: Result<number, string> = ok(2);
169
+ * assert.equal(x.mapInto((value) => ok(value * value)), ok(4));
170
+ * ```
171
+ * @example
172
+ * ```typescript
173
+ * const x: Result<number, string> = ok(0);
174
+ * assert.equal(
175
+ * x.mapInto((value) => (value === 0 ? err('zero is not divisible') : ok(1 / value))),
176
+ * err('zero is not divisible')
177
+ * );
178
+ * ```
179
+ * @example
180
+ * ```typescript
181
+ * const x: Result<number, string> = err('Some error message');
182
+ * assert.equal(x.mapInto((value) => ok(4)), err('Some error message'));
183
+ * ```
184
+ *
185
+ * @note This is an extension not supported in Rust
186
+ */
187
+ mapInto(cb) {
188
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
189
+ }
190
+ /**
191
+ * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
192
+ *
193
+ * Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
194
+ * recommended to use `mapOrElse`, which is lazily evaluated.
195
+ * @param defaultValue The default value to use.
196
+ * @param cb The predicate.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * const x = ok('hello');
201
+ * assert.equal(x.mapOr(42, (value) => value.length), 5);
202
+ * ```
203
+ * @example
204
+ * ```typescript
205
+ * const x = err('Some error message');
206
+ * assert.equal(x.mapOr(42, (value) => value.length), 42);
207
+ * ```
208
+ *
209
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
210
+ */
211
+ mapOr(defaultValue, cb) {
212
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
213
+ }
214
+ /**
215
+ * Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
216
+ * to a contained `Ok` value.
217
+ *
218
+ * This function can be used to unpack a successful result while handling an error.
219
+ * @param op The predicate that is run on `Err`.
220
+ * @param cb The predicate that is run on `Ok`.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * const x: Result<string, string> = ok('hello');
225
+ * assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);
226
+ * ```
227
+ * @example
228
+ * ```typescript
229
+ * const x: Result<string, string> = err('Some error message');
230
+ * assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 18);
231
+ * ```
232
+ *
233
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
234
+ */
235
+ mapOrElse(op, cb) {
236
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
237
+ }
238
+ /**
239
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
240
+ * untouched.
241
+ *
242
+ * This function can be used to pass through a successful result while handling an error.
243
+ * @param cb The predicate.
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * const x: Result<number, Error> = ok(2);
248
+ * assert.equal(x.mapErr((error) => error.message), ok(2));
249
+ * ```
250
+ * @example
251
+ * ```typescript
252
+ * const x: Result<number, Error> = err(new Error('Some error message'));
253
+ * assert.equal(x.mapErr((error) => error.message), err('Some error message'));
254
+ * ```
255
+ *
256
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
257
+ */
258
+ mapErr(cb) {
259
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => err(cb(error)), "err") });
260
+ }
261
+ /**
262
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
263
+ * untouched.
264
+ *
265
+ * This function can be used to pass through a successful result while handling an error.
266
+ *
267
+ * Unlike {@link mapErr}, this method does not wrap the returned value inside `Err`, but instead, it returns the
268
+ * returned value.
269
+ * @param cb The predicate.
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * const x: Result<number, Error> = ok(2);
274
+ * assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));
275
+ * ```
276
+ * @example
277
+ * ```typescript
278
+ * const x: Result<number, Error> = err(new Error('Some error message'));
279
+ * assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));
280
+ * ```
281
+ * @example
282
+ * ```typescript
283
+ * const x: Result<number, Error> = err(new Error('Some error message'));
284
+ * assert.equal(x.mapErrInto((error) => ok(4)), ok(4));
285
+ * ```
286
+ *
287
+ * @note This is an extension not supported in Rust
288
+ */
289
+ mapErrInto(cb) {
290
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
291
+ }
292
+ /**
293
+ * Calls the provided closure with a reference to the contained value (if `Ok`).
294
+ * @param cb The predicate.
295
+ * @seealso {@link inspectAsync} for the awaitable version.
296
+ *
297
+ * @example
298
+ * ```typescript
299
+ * ok(2).inspect(console.log);
300
+ * // Logs: 2
301
+ * ```
302
+ * @example
303
+ * ```typescript
304
+ * err('Some error message').inspect(console.log);
305
+ * // Doesn't log
306
+ * ```
307
+ *
308
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
309
+ */
310
+ inspect(cb) {
311
+ if (this.isOk()) cb(this[ValueProperty]);
312
+ return this;
313
+ }
314
+ /**
315
+ * Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
316
+ * @param cb The predicate.
317
+ * @seealso {@link inspect} for the sync version.
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * await ok(2).inspectAsync(console.log);
322
+ * // Logs: 2
323
+ * ```
324
+ * @example
325
+ * ```typescript
326
+ * await err('Some error message').inspectAsync(console.log);
327
+ * // Doesn't log
328
+ * ```
329
+ *
330
+ * @note This is an extension not supported in Rust
331
+ */
332
+ async inspectAsync(cb) {
333
+ if (this.isOk()) await cb(this[ValueProperty]);
334
+ return this;
335
+ }
336
+ /**
337
+ * Calls the provided closure with a reference to the contained error (if `Err`).
338
+ * @param cb The predicate.
339
+ * @seealso {@link inspectErrAsync} for the awaitable version.
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * ok(2).inspectErr(console.log);
344
+ * // Doesn't log
345
+ * ```
346
+ * @example
347
+ * ```typescript
348
+ * err('Some error message').inspectErr(console.log);
349
+ * // Logs: Some error message
350
+ * ```
351
+ *
352
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
353
+ */
354
+ inspectErr(cb) {
355
+ if (this.isErr()) cb(this[ValueProperty]);
356
+ return this;
357
+ }
358
+ /**
359
+ * Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
360
+ * @param cb The predicate.
361
+ * @seealso {@link inspectErr} for the sync version.
362
+ *
363
+ * @example
364
+ * ```typescript
365
+ * await ok(2).inspectErrAsync(console.log);
366
+ * // Doesn't log
367
+ * ```
368
+ * @example
369
+ * ```typescript
370
+ * await err('Some error message').inspectErrAsync(console.log);
371
+ * // Logs: Some error message
372
+ * ```
373
+ *
374
+ * @note This is an extension not supported in Rust
375
+ */
376
+ async inspectErrAsync(cb) {
377
+ if (this.isErr()) await cb(this[ValueProperty]);
378
+ return this;
379
+ }
380
+ /**
381
+ * Returns an iterator over the possibly contained value.
382
+ *
383
+ * The iterator yields one value if the result is `Ok`, otherwise none.
384
+ *
385
+ * @example
386
+ * ```typescript
387
+ * const x = ok(7);
388
+ * for (const value of x.iter()) {
389
+ * console.log(value);
390
+ * }
391
+ * // Logs 7
392
+ * ```
393
+ * @example
394
+ * ```typescript
395
+ * const x = err('Nothing!');
396
+ * for (const value of x.iter()) {
397
+ * console.log(value);
398
+ * }
399
+ * // Doesn't log
400
+ * ```
401
+ *
402
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
403
+ */
404
+ *iter() {
405
+ if (this.isOk()) yield this[ValueProperty];
406
+ }
407
+ /**
408
+ * Returns the contained `Ok` value.
409
+ *
410
+ * If the value is an `Err`, it throws a {@link ResultError} with the given message and the content of the `Err`.
411
+ * @param message The message for the error.
412
+ *
413
+ * @example
414
+ * ```typescript
415
+ * const x = ok(2);
416
+ * assert.equal(x.expect('Whoops!'), 2);
417
+ * ```
418
+ * @example
419
+ * ```typescript
420
+ * const x = err('Emergency failure');
421
+ * assert.throws(() => x.expect('Whoops!'), {
422
+ * name: 'ResultError',
423
+ * message: 'Whoops',
424
+ * value: 'Emergency failure'
425
+ * });
426
+ * ```
427
+ *
428
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
429
+ */
430
+ expect(message) {
431
+ if (this.isErr()) throw new ResultError(message, this[ValueProperty]);
432
+ return this[ValueProperty];
433
+ }
434
+ /**
435
+ * Returns the contained `Err` value.
436
+ *
437
+ * If the value is an `Ok`, it throws a {@link ResultError} with the given message and the content of the `Ok`.
438
+ * @param message The message for the error.
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * const x = ok(2);
443
+ * assert.throws(() => x.expectErr('Whoops!'), {
444
+ * name: 'ResultError',
445
+ * message: 'Whoops',
446
+ * value: 2
447
+ * });
448
+ * ```
449
+ * @example
450
+ * ```typescript
451
+ * const x = err('Emergency failure');
452
+ * assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
453
+ * ```
454
+ *
455
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
456
+ */
457
+ expectErr(message) {
458
+ if (this.isOk()) throw new ResultError(message, this[ValueProperty]);
459
+ return this[ValueProperty];
460
+ }
461
+ /**
462
+ * Returns the contained `Ok` value.
463
+ *
464
+ * If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`.
465
+ * @seealso {@link unwrapOr}
466
+ * @seealso {@link unwrapOrElse}
467
+ * @seealso {@link unwrapErr}
468
+ * @seealso {@link unwrapRaw}
469
+ * @seealso {@link unwrapSafe}
470
+ *
471
+ * @example
472
+ * ```typescript
473
+ * const x = ok(2);
474
+ * assert.equal(x.unwrap(), 2);
475
+ * ```
476
+ * @example
477
+ * ```typescript
478
+ * const x = err('Emergency failure');
479
+ * assert.throws(() => x.unwrap(), {
480
+ * name: 'ResultError',
481
+ * message: 'Unwrap failed',
482
+ * value: 'Emergency failure'
483
+ * });
484
+ * ```
485
+ *
486
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
487
+ */
488
+ unwrap() {
489
+ if (this.isErr()) throw new ResultError("Unwrap failed", this[ValueProperty]);
490
+ return this[ValueProperty];
491
+ }
492
+ /**
493
+ * Returns the contained `Err` value.
494
+ *
495
+ * If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`.
496
+ * @seealso {@link unwrap}
497
+ * @seealso {@link unwrapOr}
498
+ * @seealso {@link unwrapOrElse}
499
+ * @seealso {@link unwrapRaw}
500
+ * @seealso {@link unwrapSafe}
501
+ *
502
+ * @example
503
+ * ```typescript
504
+ * const x = ok(2);
505
+ * assert.throws(() => x.unwrapErr(), {
506
+ * name: 'ResultError',
507
+ * message: 'Unwrap failed',
508
+ * value: 2
509
+ * });
510
+ * ```
511
+ * @example
512
+ * ```typescript
513
+ * const x = err('Emergency failure');
514
+ * assert.equal(x.unwrapErr(), 'Emergency failure');
515
+ * ```
516
+ *
517
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
518
+ */
519
+ unwrapErr() {
520
+ if (this.isOk()) throw new ResultError("Unwrap failed", this[ValueProperty]);
521
+ return this[ValueProperty];
522
+ }
523
+ /**
524
+ * Returns the contained `Ok` value or the provided default.
525
+ *
526
+ * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
527
+ * recommended to use {@link unwrapOrElse}, which is lazily evaluated.
528
+ * @seealso {@link unwrap}
529
+ * @seealso {@link unwrapOrElse}
530
+ * @seealso {@link unwrapErr}
531
+ * @seealso {@link unwrapRaw}
532
+ * @seealso {@link unwrapSafe}
533
+ *
534
+ * @param defaultValue The default value.
535
+ *
536
+ * @example
537
+ * ```typescript
538
+ * const x: Result<number, string> = ok(9);
539
+ * assert.equal(x.unwrapOr(2), 9);
540
+ * ```
541
+ * @example
542
+ * ```typescript
543
+ * const x: Result<number, string> = err('Error');
544
+ * assert.equal(x.unwrapOr(2), 2);
545
+ * ```
546
+ *
547
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
548
+ */
549
+ unwrapOr(defaultValue) {
550
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
551
+ }
552
+ /**
553
+ * Returns the contained `Ok` value or computes it from a closure.
554
+ * @seealso {@link unwrap}
555
+ * @seealso {@link unwrapOr}
556
+ * @seealso {@link unwrapErr}
557
+ * @seealso {@link unwrapRaw}
558
+ * @seealso {@link unwrapSafe}
559
+ *
560
+ * @param op The predicate.
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * const count = (x: string) => x.length;
565
+ *
566
+ * assert.equal(ok(2).unwrapOrElse(count), 2);
567
+ * assert.equal(err('hello').unwrapOrElse(count), 5);
568
+ * ```
569
+ *
570
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
571
+ */
572
+ unwrapOrElse(op) {
573
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
574
+ }
575
+ /**
576
+ * Returns the contained `Ok` value.
577
+ *
578
+ * If the value is an `Err`, it throws the contained error.
579
+ * @seealso {@link unwrap}
580
+ * @seealso {@link unwrapOr}
581
+ * @seealso {@link unwrapOrElse}
582
+ * @seealso {@link unwrapErr}
583
+ * @seealso {@link unwrapSafe}
584
+ *
585
+ * @example
586
+ * ```typescript
587
+ * const x = ok(2);
588
+ * assert.equal(x.unwrapRaw(), 2);
589
+ * ```
590
+ * @example
591
+ * ```typescript
592
+ * const x = err('Emergency failure');
593
+ * assert.throws(() => x.unwrapRaw(), {
594
+ * name: 'Error',
595
+ * message: 'Unwrap failed',
596
+ * value: 'Emergency failure'
597
+ * });
598
+ * ```
599
+ */
600
+ unwrapRaw() {
601
+ if (this.isErr()) throw this[ValueProperty];
602
+ return this[ValueProperty];
603
+ }
604
+ /**
605
+ * Returns the contained `Ok` value or yelds the contained `Err` value.
606
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
607
+ *
608
+ * If used outside of a `safeTry`'s' body, throws the contained error.
609
+ * @seealso {@link unwrap}
610
+ * @seealso {@link unwrapOr}
611
+ * @seealso {@link unwrapErr}
612
+ * @seealso {@link unwrapRaw}
613
+ * @seealso {@link unwrapSafe}
614
+ *
615
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
616
+ */
617
+ *unwrapSafe() {
618
+ if (this.isOk()) {
619
+ return this[ValueProperty];
620
+ }
621
+ yield this;
622
+ throw new ResultError("Should not be used outside of a safe try generator", this[ValueProperty]);
623
+ }
624
+ /**
625
+ * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
626
+ * @param result The result to check.
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * const x: Result<number, string> = ok(2);
631
+ * const y: Result<string, string> = err('Late error');
632
+ * assert.equal(x.and(y), err('Late error'));
633
+ * ```
634
+ * @example
635
+ * ```typescript
636
+ * const x: Result<number, string> = err('Early error');
637
+ * const y: Result<string, string> = err('Late error');
638
+ * assert.equal(x.and(y), err('Early error'));
639
+ * ```
640
+ * @example
641
+ * ```typescript
642
+ * const x: Result<number, string> = ok(2);
643
+ * const y: Result<string, string> = ok('Hello');
644
+ * assert.equal(x.and(y), ok('Hello'));
645
+ * ```
646
+ *
647
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
648
+ */
649
+ and(result) {
650
+ return this.match({ ok: /* @__PURE__ */ __name(() => result, "ok"), err: returnThis });
651
+ }
652
+ /**
653
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
654
+ *
655
+ * This function can be used for control flow based on `Result` values.
656
+ * @param cb The predicate.
657
+ *
658
+ * @example
659
+ * ```typescript
660
+ * function fractionOf4(value: number) {
661
+ * return value === 0 ? err('overflowed') : ok(4 / value);
662
+ * }
663
+ *
664
+ * assert.equal(ok(2).andThen(fractionOf4), ok(4));
665
+ * assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
666
+ * assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
667
+ * ```
668
+ *
669
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
670
+ */
671
+ andThen(cb) {
672
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
673
+ }
674
+ /**
675
+ * Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
676
+ *
677
+ * Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended
678
+ * to use {@link orElse}, which is lazily evaluated.
679
+ * @param result The result to check.
680
+ *
681
+ * @example
682
+ * ```typescript
683
+ * const x: Result<number, string> = ok(2);
684
+ * const y: Result<number, string> = err('Late error');
685
+ * assert.equal(x.or(y), ok(2));
686
+ * ```
687
+ * @example
688
+ * ```typescript
689
+ * const x: Result<number, string> = err('Early error');
690
+ * const y: Result<number, string> = ok(2);
691
+ * assert.equal(x.or(y), ok(2));
692
+ * ```
693
+ * @example
694
+ * ```typescript
695
+ * const x: Result<number, string> = err('Early error');
696
+ * const y: Result<number, string> = err('Late error');
697
+ * assert.equal(x.or(y), err('Late error'));
698
+ * ```
699
+ * @example
700
+ * ```typescript
701
+ * const x: Result<number, string> = ok(2);
702
+ * const y: Result<number, string> = ok(100);
703
+ * assert.equal(x.or(y), ok(2));
704
+ * ```
705
+ *
706
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
707
+ */
708
+ or(result) {
709
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name(() => result, "err") });
710
+ }
711
+ /**
712
+ * Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
713
+ *
714
+ * This function can be used for control flow based on result values.
715
+ * @param cb The predicate.
716
+ *
717
+ * @example
718
+ * ```typescript
719
+ * const square = (x: number): Result<number, string> => ok(x * x);
720
+ * const wrapErr = (x: number): Result<number, string> => err(x);
721
+ *
722
+ * assert.equal(ok(2).orElse(square).orElse(square), ok(2));
723
+ * assert.equal(ok(2).orElse(wrapErr).orElse(square), ok(2));
724
+ * assert.equal(err(3).orElse(square).orElse(wrapErr), ok(9));
725
+ * assert.equal(err(3).orElse(wrapErr).orElse(wrapErr), err(3));
726
+ * ```
727
+ *
728
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
729
+ */
730
+ orElse(cb) {
731
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
732
+ }
733
+ contains(value) {
734
+ return this.isOkAnd((inner) => inner === value);
735
+ }
736
+ containsErr(error) {
737
+ return this.isErrAnd((inner) => inner === error);
738
+ }
739
+ /**
740
+ * Transposes a `Result` of an `Option` into an `Option` of a `Result`.
741
+ *
742
+ * `ok(none)` will be mapped to `none`. `ok(some(v))` and `err(e)` will be mapped to `some(ok(v))` and `some(err(e))`.
743
+ *
744
+ * @example
745
+ * ```typescript
746
+ * const x: Result<Option<number>, Error> = ok(some(5));
747
+ * const y: Option<Result<number, Error>> = some(ok(5));
748
+ * assert.equal(x.transpose(), y);
749
+ * ```
750
+ *
751
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
752
+ */
753
+ transpose() {
754
+ return this.match({
755
+ ok: /* @__PURE__ */ __name((value) => value.map((value2) => ok(value2)), "ok"),
756
+ err() {
757
+ return some(this);
758
+ }
759
+ });
760
+ }
761
+ /**
762
+ * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
763
+ *
764
+ * @example
765
+ * ```typescript
766
+ * const x: Result<Result<string, number>, number> = ok(ok('Hello'));
767
+ * assert.equal(x.flatten(), ok('Hello'));
768
+ * ```
769
+ * @example
770
+ * ```typescript
771
+ * const x: Result<Result<string, number>, number> = ok(err(6));
772
+ * assert.equal(x.flatten(), err(6));
773
+ * ```
774
+ * @example
775
+ * ```typescript
776
+ * const x: Result<Result<string, number>, number> = err(6);
777
+ * assert.equal(x.flatten(), err(6));
778
+ * ```
779
+ *
780
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
781
+ */
782
+ flatten() {
783
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: returnThis });
784
+ }
785
+ /**
786
+ * Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
787
+ *
788
+ * @example
789
+ * ```typescript
790
+ * let x: Result<number, number> = ok(3);
791
+ * assert.equal(x.intoOkOrErr(), 3);
792
+ * ```
793
+ * @example
794
+ * ```typescript
795
+ * let x: Result<number, number> = err(4);
796
+ * assert.equal(x.intoOkOrErr(), 4);
797
+ * ```
798
+ *
799
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
800
+ */
801
+ intoOkOrErr() {
802
+ return this[ValueProperty];
803
+ }
804
+ /**
805
+ * Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
806
+ *
807
+ * @example
808
+ * ```typescript
809
+ * let x = ok(Promise.resolve(3));
810
+ * assert.equal(await x.intoPromise(), ok(3));
811
+ * ```
812
+ *
813
+ * @note This is an extension not supported in Rust
814
+ */
815
+ intoPromise() {
816
+ return this.match({
817
+ // @ts-expect-error Complex types
818
+ ok: /* @__PURE__ */ __name(async (value) => ok(await value), "ok"),
819
+ // NOSONAR
820
+ // @ts-expect-error Complex types
821
+ err: /* @__PURE__ */ __name(async (error) => err(await error), "err")
822
+ // NOSONAR
823
+ });
824
+ }
825
+ /**
826
+ * Checks whether or not `other` equals with self.
827
+ * @param other The other result to compare.
828
+ *
829
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
830
+ */
831
+ eq(other) {
832
+ return this.isOk() === other.isOk() && this[ValueProperty] === other[ValueProperty];
833
+ }
834
+ /**
835
+ * Checks whether or not `other` doesn't equal with self.
836
+ * @param other The other result to compare.
837
+ *
838
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
839
+ */
840
+ ne(other) {
841
+ return !this.eq(other);
842
+ }
843
+ /**
844
+ * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
845
+ * @param branches The branches to match.
846
+ *
847
+ * @example
848
+ * ```typescript
849
+ * const result = ok(4).match({
850
+ * ok: (v) => v,
851
+ * err: () => 0
852
+ * });
853
+ * assert.equal(result, 4);
854
+ * ```
855
+ * @example
856
+ * ```typescript
857
+ * const result = err('Hello').match({
858
+ * ok: (v) => v,
859
+ * err: () => 0
860
+ * });
861
+ * assert.equal(result, 0);
862
+ * ```
863
+ */
864
+ match(branches) {
865
+ return this.isOk() ? branches.ok.call(this, this[ValueProperty]) : branches.err.call(this, this[ValueProperty]);
866
+ }
867
+ /**
868
+ * Returns an iterator over the possibly contained value.
869
+ *
870
+ * The iterator yields one value if the result is `Ok`, otherwise none.
871
+ *
872
+ * @example
873
+ * ```typescript
874
+ * const x = ok(7);
875
+ * for (const value of x) {
876
+ * console.log(value);
877
+ * }
878
+ * // Logs 7
879
+ * ```
880
+ * @example
881
+ * ```typescript
882
+ * const x = err('Nothing!');
883
+ * for (const value of x) {
884
+ * console.log(value);
885
+ * }
886
+ * // Doesn't log
887
+ * ```
888
+ *
889
+ * @see {@link IResult.iter}
890
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
891
+ */
892
+ [(_b = ValueProperty, _a = SuccessProperty, Symbol.iterator)]() {
893
+ return this.iter();
894
+ }
895
+ get [Symbol.toStringTag]() {
896
+ return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
897
+ }
898
+ /**
899
+ * This function, in combination with `[$]`, is intended to emulate
900
+ * Rust's ? operator.
901
+ *
902
+ * @see {@link Result.safeTry}
903
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
904
+ */
905
+ get [UnwrapSafeProperty]() {
906
+ return this.unwrapSafe();
907
+ }
908
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
909
+ static ok(value) {
910
+ return new _Result(value, true);
911
+ }
912
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
913
+ static err(value) {
914
+ return new _Result(value, false);
915
+ }
916
+ /**
917
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object. This override
918
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
919
+ * library or from a different build.
920
+ *
921
+ * @param instance The instance to check.
922
+ * @returns Whether or not the instance is a `Result`.
923
+ *
924
+ * @example
925
+ * ```typescript
926
+ * import { Result } from '@sapphire/result';
927
+ * const { ok } = require('@sapphire/result');
928
+ *
929
+ * ok(2) instanceof Result; // true
930
+ * ```
931
+ */
932
+ static [Symbol.hasInstance](instance) {
933
+ return typeof instance === "object" && instance !== null && ValueProperty in instance && SuccessProperty in instance;
934
+ }
935
+ /**
936
+ * @deprecated Use {@link Result.isResult} instead.
937
+ *
938
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
939
+ *
940
+ * @param instance The instance to check.
941
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
942
+ *
943
+ * @example
944
+ * ```typescript
945
+ * import { Result } from '@sapphire/result';
946
+ * const { ok } = require('@sapphire/result');
947
+ *
948
+ * Result.isResult(ok(2)); // true
949
+ * ```
950
+ */
951
+ static is(instance) {
952
+ return _Result[Symbol.hasInstance](instance);
953
+ }
954
+ /**
955
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
956
+ *
957
+ * @param instance The instance to check.
958
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
959
+ *
960
+ * @example
961
+ * ```typescript
962
+ * import { Result } from '@sapphire/result';
963
+ * const { ok } = require('@sapphire/result');
964
+ *
965
+ * Result.isResult(ok(2)); // true
966
+ * ```
967
+ */
968
+ static isResult(instance) {
969
+ return _Result[Symbol.hasInstance](instance);
970
+ }
971
+ /**
972
+ * Creates a {@link Result} out of a callback.
973
+ *
974
+ * @typeparam T The result's type.
975
+ * @typeparam E The error's type.
976
+ */
977
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
978
+ static from(op) {
979
+ try {
980
+ return resolve(isFunction(op) ? op() : op);
981
+ } catch (error) {
982
+ return err(error);
983
+ }
984
+ }
985
+ /**
986
+ * Creates a {@link Result} out of a promise or async callback.
987
+ *
988
+ * @typeparam T The result's type.
989
+ * @typeparam E The error's type.
990
+ */
991
+ static async fromAsync(op) {
992
+ try {
993
+ return resolve(await (isFunction(op) ? op() : op));
994
+ } catch (error) {
995
+ return err(error);
996
+ }
997
+ }
998
+ /**
999
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
1000
+ * {@link Err} encountered.
1001
+ *
1002
+ * @param results An array of {@link Result}s.
1003
+ * @returns A new {@link Result}.
1004
+ */
1005
+ static all(results) {
1006
+ const values = [];
1007
+ for (const result of results) {
1008
+ if (result.isErr()) return result;
1009
+ values.push(result[ValueProperty]);
1010
+ }
1011
+ return ok(values);
1012
+ }
1013
+ /**
1014
+ * Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
1015
+ *
1016
+ * @param results An array of {@link Result}s.
1017
+ * @returns A new {@link Result}.
1018
+ */
1019
+ static any(results) {
1020
+ const errors = [];
1021
+ for (const result of results) {
1022
+ if (result.isOk()) return result;
1023
+ errors.push(result[ValueProperty]);
1024
+ }
1025
+ return err(errors);
1026
+ }
1027
+ static safeTry(body) {
1028
+ const n = body({ $: UnwrapSafeProperty, $async: unwrapSafeAsync }).next();
1029
+ if (n instanceof Promise) {
1030
+ return n.then((r) => r.value);
1031
+ }
1032
+ return n.value;
1033
+ }
1034
+ };
1035
+ __name(_Result, "Result");
1036
+ var Result = _Result;
1037
+ var { ok, err } = Result;
1038
+ function resolve(value) {
1039
+ return Result.isResult(value) ? value : ok(value);
1040
+ }
1041
+ __name(resolve, "resolve");
1042
+ async function* unwrapSafeAsync(result) {
1043
+ const _result = await result;
1044
+ return yield* _result.unwrapSafe();
1045
+ }
1046
+ __name(unwrapSafeAsync, "unwrapSafeAsync");
1047
+
1048
+ // src/lib/Option.ts
1049
+ var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");
1050
+ var ExistsProperty = Symbol.for("@sapphire/result:Option.exists");
1051
+ var _a2, _b2;
1052
+ var _Option = class _Option {
1053
+ constructor(value, exists) {
1054
+ __publicField(this, _b2);
1055
+ __publicField(this, _a2);
1056
+ this[ValueProperty2] = value;
1057
+ this[ExistsProperty] = exists;
1058
+ }
1059
+ /**
1060
+ * Returns `true` if the option is a `Some` value.
1061
+ *
1062
+ * @example
1063
+ * ```typescript
1064
+ * const x: Option<number> = some(2);
1065
+ * assert.equal(x.isSome(), true);
1066
+ * ```
1067
+ * @example
1068
+ * ```typescript
1069
+ * const x: Option<number> = none;
1070
+ * assert.equal(x.isSome(), false);
1071
+ * ```
1072
+ *
1073
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
1074
+ */
1075
+ isSome() {
1076
+ return this[ExistsProperty];
1077
+ }
1078
+ isSomeAnd(cb) {
1079
+ return this.isSome() && cb(this[ValueProperty2]);
1080
+ }
1081
+ /**
1082
+ * Returns `true` if the option is a `None` value.
1083
+ *
1084
+ * @example
1085
+ * ```typescript
1086
+ * const x: Option<number> = some(2);
1087
+ * assert.equal(x.isNone(), false);
1088
+ * ```
1089
+ * @example
1090
+ * ```typescript
1091
+ * const x: Option<number> = none;
1092
+ * assert.equal(x.isNone(), true);
1093
+ * ```
1094
+ *
1095
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
1096
+ */
1097
+ isNone() {
1098
+ return !this[ExistsProperty];
1099
+ }
1100
+ isNoneOr(cb) {
1101
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => true, "none") });
1102
+ }
1103
+ /**
1104
+ * Returns the contained `Some` value.
1105
+ * @param message The message for the error.
1106
+ * If the value is an `Err`, it throws an {@link OptionError} with the given message.
1107
+ *
1108
+ * @example
1109
+ * ```typescript
1110
+ * const x: Option<string> = some(2);
1111
+ * assert.equal(x.expect('Whoops!'), 2);
1112
+ * ```
1113
+ * @example
1114
+ * ```typescript
1115
+ * const x: Option<string> = none;
1116
+ * assert.throws(() => x.expect('Whoops!'), {
1117
+ * name: 'OptionError',
1118
+ * message: 'Whoops'
1119
+ * });
1120
+ * ```
1121
+ *
1122
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
1123
+ */
1124
+ expect(message) {
1125
+ if (this.isNone()) throw new OptionError(message);
1126
+ return this[ValueProperty2];
1127
+ }
1128
+ /**
1129
+ * Returns the contained `Some` value.
1130
+ *
1131
+ * If the value is an `Err`, it throws an {@link OptionError} with the message.
1132
+ * @seealso {@link unwrapOr}
1133
+ * @seealso {@link unwrapOrElse}
1134
+ *
1135
+ * @example
1136
+ * ```typescript
1137
+ * const x: Option<string> = some(2);
1138
+ * assert.equal(x.unwrap(), 2);
1139
+ * ```
1140
+ * @example
1141
+ * ```typescript
1142
+ * const x: Option<string> = none;
1143
+ * assert.throws(() => x.unwrap(), {
1144
+ * name: 'OptionError',
1145
+ * message: 'Unwrap failed'
1146
+ * });
1147
+ * ```
1148
+ *
1149
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
1150
+ */
1151
+ unwrap() {
1152
+ if (this.isNone()) throw new OptionError("Unwrap failed");
1153
+ return this[ValueProperty2];
1154
+ }
1155
+ /**
1156
+ * Returns the contained `Some` value or a provided default.
1157
+ *
1158
+ * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
1159
+ * recommended to use {@link unwrapOrElse}, which is lazily evaluated.
1160
+ *
1161
+ * @example
1162
+ * ```typescript
1163
+ * assert.equal(some(2).unwrapOr(0), 2);
1164
+ * ```
1165
+ * @example
1166
+ * ```typescript
1167
+ * assert.equal(none.unwrapOr(0), 0);
1168
+ * ```
1169
+ *
1170
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
1171
+ */
1172
+ unwrapOr(defaultValue) {
1173
+ return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
1174
+ }
1175
+ /**
1176
+ * Returns the contained Some value or computes it from a closure.
1177
+ *
1178
+ * @example
1179
+ * ```typescript
1180
+ * assert.equal(some(2).unwrapOrElse(() => 0), 2);
1181
+ * ```
1182
+ * @example
1183
+ * ```typescript
1184
+ * assert.equal(none.unwrapOrElse(() => 0), 0);
1185
+ * ```
1186
+ *
1187
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
1188
+ */
1189
+ unwrapOrElse(cb) {
1190
+ return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: cb });
1191
+ }
1192
+ /**
1193
+ * Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
1194
+ * @param cb The predicate.
1195
+ *
1196
+ * @example
1197
+ * ```typescript
1198
+ * const maybeSomeString = some('Hello, world!');
1199
+ * const maybeSomeLength = maybeSomeString.map((value) => value.length);
1200
+ *
1201
+ * assert.equal(maybeSomeLength, some(13));
1202
+ * ```
1203
+ *
1204
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
1205
+ */
1206
+ map(cb) {
1207
+ return this.match({ some: /* @__PURE__ */ __name((value) => some(cb(value)), "some"), none: returnThis });
1208
+ }
1209
+ /**
1210
+ * Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
1211
+ * untouched.
1212
+ * @param cb The predicate.
1213
+ *
1214
+ * @example
1215
+ * ```typescript
1216
+ * const input: Option<string> = some('Hello, world!');
1217
+ * const result = input.mapInto((value) => some(value.length));
1218
+ *
1219
+ * assert.equal(result, some(13));
1220
+ * ```
1221
+ * @example
1222
+ * ```typescript
1223
+ * const input: Option<string> = none;
1224
+ * const result = input.mapInto((value) => some(value.length));
1225
+ *
1226
+ * assert.equal(result, none);
1227
+ * ```
1228
+ *
1229
+ * @note This is an extension not supported in Rust
1230
+ */
1231
+ mapInto(cb) {
1232
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
1233
+ }
1234
+ /**
1235
+ * Returns the provided default result (if none), or applies a function to the contained value (if any).
1236
+ *
1237
+ * Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
1238
+ * recommended to use {@link mapOrElse}, which is lazily evaluated.
1239
+ * @param defaultValue The default value.
1240
+ * @param cb The predicate.
1241
+ *
1242
+ * @example
1243
+ * ```typescript
1244
+ * const x: Option<string> = some('hello');
1245
+ * assert.equal(x.mapOr(42, (value) => value.length), 5);
1246
+ * ```
1247
+ * @example
1248
+ * ```typescript
1249
+ * const x: Option<string> = none;
1250
+ * assert.equal(x.mapOr(42, (value) => value.length), 42);
1251
+ * ```
1252
+ *
1253
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
1254
+ */
1255
+ mapOr(defaultValue, cb) {
1256
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
1257
+ }
1258
+ /**
1259
+ * Computes a default function result (if none), or applies a different function to the contained value (if any).
1260
+ * @param defaultValue The default value.
1261
+ * @param cb The predicate.
1262
+ *
1263
+ * @example
1264
+ * ```typescript
1265
+ * const x: Option<string> = some('hello');
1266
+ * assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
1267
+ * ```
1268
+ * @example
1269
+ * ```typescript
1270
+ * const x: Option<string> = none;
1271
+ * assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);
1272
+ * ```
1273
+ *
1274
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
1275
+ */
1276
+ mapOrElse(defaultValue, cb) {
1277
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue(), "none") });
1278
+ }
1279
+ /**
1280
+ * Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
1281
+ * untouched.
1282
+ * @param cb The predicate.
1283
+ *
1284
+ * @example
1285
+ * ```typescript
1286
+ * const input: Option<string> = some('Hello, world!');
1287
+ * const result = input.mapNoneInto(() => some(13));
1288
+ *
1289
+ * assert.equal(result, some('Hello, world!'));
1290
+ * ```
1291
+ * @example
1292
+ * ```typescript
1293
+ * const input: Option<string> = none;
1294
+ * const result = input.mapNoneInto(() => some(13));
1295
+ *
1296
+ * assert.equal(result, some(13));
1297
+ * ```
1298
+ *
1299
+ * @note This is an extension not supported in Rust
1300
+ */
1301
+ mapNoneInto(cb) {
1302
+ return this.match({ some: returnThis, none: cb });
1303
+ }
1304
+ /**
1305
+ * Calls the provided closure with a reference to the contained value (if `Some`).
1306
+ * @param cb The predicate.
1307
+ * @seealso {@link inspectAsync} for the awaitable version.
1308
+ *
1309
+ * @example
1310
+ * ```typescript
1311
+ * some(2).inspect(console.log);
1312
+ * // Logs: 2
1313
+ * ```
1314
+ * @example
1315
+ * ```typescript
1316
+ * none.inspect(console.log);
1317
+ * // Doesn't log
1318
+ * ```
1319
+ *
1320
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect}
1321
+ */
1322
+ inspect(cb) {
1323
+ if (this.isSome()) cb(this[ValueProperty2]);
1324
+ return this;
1325
+ }
1326
+ /**
1327
+ * Calls the provided closure with a reference to the contained value (if `Some`).
1328
+ * @param cb The predicate.
1329
+ * @seealso {@link inspect} for the sync version.
1330
+ *
1331
+ * @example
1332
+ * ```typescript
1333
+ * await some(2).inspectAsync(console.log);
1334
+ * // Logs: 2
1335
+ * ```
1336
+ * @example
1337
+ * ```typescript
1338
+ * await none.inspectAsync(console.log);
1339
+ * // Doesn't log
1340
+ * ```
1341
+ *
1342
+ * @note This is an extension not supported in Rust
1343
+ */
1344
+ async inspectAsync(cb) {
1345
+ if (this.isSome()) await cb(this[ValueProperty2]);
1346
+ return this;
1347
+ }
1348
+ /**
1349
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
1350
+ *
1351
+ * Arguments passed to `okOr` are eagerly evaluated; if you are passing the result of a function call, it is
1352
+ * recommended to use {@link okOrElse}, which is lazily evaluated.
1353
+ * @param err The error to be used.
1354
+ *
1355
+ * @example
1356
+ * ```typescript
1357
+ * const x: Option<string> = some('hello');
1358
+ * assert.equal(x.okOr(0), ok('hello'));
1359
+ * ```
1360
+ * @example
1361
+ * ```typescript
1362
+ * const x: Option<string> = none;
1363
+ * assert.equal(x.okOr(0), err(0));
1364
+ * ```
1365
+ *
1366
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
1367
+ */
1368
+ okOr(error) {
1369
+ return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(error), "none") });
1370
+ }
1371
+ /**
1372
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
1373
+ * @param cb The error to be used.
1374
+ *
1375
+ * @example
1376
+ * ```typescript
1377
+ * const x: Option<string> = some('hello');
1378
+ * assert.equal(x.okOrElse(() => 0), ok('hello'));
1379
+ * ```
1380
+ * @example
1381
+ * ```typescript
1382
+ * const x: Option<string> = none;
1383
+ * assert.equal(x.okOrElse(() => 0), err(0));
1384
+ * ```
1385
+ *
1386
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
1387
+ */
1388
+ okOrElse(cb) {
1389
+ return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(cb()), "none") });
1390
+ }
1391
+ /**
1392
+ * Returns an iterator over the possibly contained value.
1393
+ *
1394
+ * The iterator yields one value if the result is `Some`, otherwise none.
1395
+ *
1396
+ * @example
1397
+ * ```typescript
1398
+ * const x = some(7);
1399
+ * for (const value of x) {
1400
+ * console.log(value);
1401
+ * }
1402
+ * // Logs 7
1403
+ * ```
1404
+ * @example
1405
+ * ```typescript
1406
+ * const x = none;
1407
+ * for (const value of x) {
1408
+ * console.log(value);
1409
+ * }
1410
+ * // Doesn't log
1411
+ * ```
1412
+ *
1413
+ * @see {@link Option.iter}
1414
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1415
+ */
1416
+ *iter() {
1417
+ if (this.isSome()) yield this[ValueProperty2];
1418
+ }
1419
+ /**
1420
+ * Returns `None` if the option is `None`, otherwise returns `option`.
1421
+ * @param option The option.
1422
+ *
1423
+ * @example
1424
+ * ```typescript
1425
+ * const x: Option<number> = some(2);
1426
+ * const y: Option<string> = none;
1427
+ * assert.equal(x.and(y), none);
1428
+ * ```
1429
+ * @example
1430
+ * ```typescript
1431
+ * const x: Option<number> = none;
1432
+ * const y: Option<string> = some('foo');
1433
+ * assert.equal(x.and(y), none);
1434
+ * ```
1435
+ * @example
1436
+ * ```typescript
1437
+ * const x: Option<number> = some(2);
1438
+ * const y: Option<string> = some('foo');
1439
+ * assert.equal(x.and(y), some('foo'));
1440
+ * ```
1441
+ * @example
1442
+ * ```typescript
1443
+ * const x: Option<number> = none;
1444
+ * const y: Option<string> = none;
1445
+ * assert.equal(x.and(y), none);
1446
+ * ```
1447
+ *
1448
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
1449
+ */
1450
+ and(option) {
1451
+ return this.match({ some: /* @__PURE__ */ __name(() => option, "some"), none: returnThis });
1452
+ }
1453
+ /**
1454
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1455
+ *
1456
+ * This function can be used for control flow based on `Result` values.
1457
+ * @param cb The predicate.
1458
+ *
1459
+ * @example
1460
+ * ```typescript
1461
+ * function fractionOf4(value: number) {
1462
+ * return value === 0 ? none : some(4 / value);
1463
+ * }
1464
+ *
1465
+ * assert.equal(some(2).andThen(fractionOf4), some(4));
1466
+ * assert.equal(some(0).andThen(fractionOf4), none);
1467
+ * assert.equal(none.andThen(fractionOf4), none);
1468
+ * ```
1469
+ *
1470
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
1471
+ */
1472
+ andThen(cb) {
1473
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
1474
+ }
1475
+ /**
1476
+ * Returns the option if it contains a value, otherwise returns `option`.
1477
+ * @param option The option.
1478
+ *
1479
+ * @example
1480
+ * ```typescript
1481
+ * const x: Option<number> = some(2);
1482
+ * const y: Option<number> = none;
1483
+ * assert.equal(x.or(y), some(2));
1484
+ * ```
1485
+ * @example
1486
+ * ```typescript
1487
+ * const x: Option<number> = none;
1488
+ * const y: Option<number> = some(100);
1489
+ * assert.equal(x.or(y), some(100));
1490
+ * ```
1491
+ * @example
1492
+ * ```typescript
1493
+ * const x: Option<number> = some(2);
1494
+ * const y: Option<number> = some(100);
1495
+ * assert.equal(x.or(y), some(2));
1496
+ * ```
1497
+ * @example
1498
+ * ```typescript
1499
+ * const x: Option<number> = none;
1500
+ * const y: Option<number> = none;
1501
+ * assert.equal(x.or(y), none);
1502
+ * ```
1503
+ *
1504
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
1505
+ */
1506
+ or(option) {
1507
+ return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => option, "none") });
1508
+ }
1509
+ /**
1510
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1511
+ *
1512
+ * This function can be used for control flow based on `Result` values.
1513
+ * @param cb The predicate.
1514
+ *
1515
+ * @example
1516
+ * ```typescript
1517
+ * const nobody = (): Option<string> => none;
1518
+ * const vikings = (): Option<string> => some('vikings');
1519
+ *
1520
+ * assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
1521
+ * assert.equal(none.orElse(vikings), some('vikings'));
1522
+ * assert.equal(none.orElse(nobody), none);
1523
+ * ```
1524
+ *
1525
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
1526
+ */
1527
+ orElse(cb) {
1528
+ return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => cb(), "none") });
1529
+ }
1530
+ /**
1531
+ * Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
1532
+ * @param option The option to compare.
1533
+ *
1534
+ * @example
1535
+ * ```typescript
1536
+ * const x: Option<number> = some(2);
1537
+ * const y: Option<number> = none;
1538
+ * assert.equal(x.xor(y), some(2));
1539
+ * ```
1540
+ * @example
1541
+ * ```typescript
1542
+ * const x: Option<number> = none;
1543
+ * const y: Option<number> = some(2);
1544
+ * assert.equal(x.xor(y), some(2));
1545
+ * ```
1546
+ * @example
1547
+ * ```typescript
1548
+ * const x: Option<number> = some(2);
1549
+ * const y: Option<number> = some(2);
1550
+ * assert.equal(x.xor(y), none);
1551
+ * ```
1552
+ * @example
1553
+ * ```typescript
1554
+ * const x: Option<number> = none;
1555
+ * const y: Option<number> = none;
1556
+ * assert.equal(x.xor(y), none);
1557
+ * ```
1558
+ *
1559
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
1560
+ */
1561
+ xor(option) {
1562
+ return this.match({
1563
+ some() {
1564
+ return option.isNone() ? this : none;
1565
+ },
1566
+ none: /* @__PURE__ */ __name(() => option, "none")
1567
+ });
1568
+ }
1569
+ filter(predicate) {
1570
+ return this.isSomeAnd(predicate) ? this : none;
1571
+ }
1572
+ /**
1573
+ * Returns `true` if the option is a `Some` value containing the given value.
1574
+ * @param value The value to compare.
1575
+ *
1576
+ * @example
1577
+ * ```typescript
1578
+ * const x: Option<number> = some(2);
1579
+ * assert.equal(x.contains(2), true);
1580
+ * ```
1581
+ * @example
1582
+ * ```typescript
1583
+ * const x: Option<number> = some(3);
1584
+ * assert.equal(x.contains(2), false);
1585
+ * ```
1586
+ * @example
1587
+ * ```typescript
1588
+ * const x: Option<number> = none;
1589
+ * assert.equal(x.contains(2), false);
1590
+ * ```
1591
+ *
1592
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
1593
+ */
1594
+ contains(value) {
1595
+ return this.isSomeAnd((inner) => inner === value);
1596
+ }
1597
+ /**
1598
+ * Zips self with another `Option`.
1599
+ *
1600
+ * If self is `Some(s)` and `other` is `Some(o)`, this method returns `Some([s, o])`. Otherwise, `None` is returned.
1601
+ * @param other The option to zip self with.
1602
+ *
1603
+ * @example
1604
+ * ```typescript
1605
+ * const x = some(1);
1606
+ * const y = some('hi');
1607
+ * const z = none;
1608
+ *
1609
+ * assert.equal(x.zip(y), some([1, 'hi']));
1610
+ * assert.equal(x.zip(z), none);
1611
+ * ```
1612
+ *
1613
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
1614
+ */
1615
+ zip(other) {
1616
+ return this.isSome() && other.isSome() ? some([this[ValueProperty2], other[ValueProperty2]]) : none;
1617
+ }
1618
+ /**
1619
+ * Zips self and another `Option` with function `f`.
1620
+ *
1621
+ * If self is `Some(s)` and other is `Some(o)`, this method returns `Some(f(s, o))`. Otherwise, `None` is returned.
1622
+ * @param other The option to zip self with.
1623
+ * @param f The function that computes the returned value.
1624
+ *
1625
+ * @example
1626
+ * ```typescript
1627
+ * class Point {
1628
+ * public readonly x: number;
1629
+ * public readonly y: number;
1630
+ *
1631
+ * public constructor(x: number, y: number) {
1632
+ * this.x = x;
1633
+ * this.y = y;
1634
+ * }
1635
+ * }
1636
+ *
1637
+ * const x = some(17.5);
1638
+ * const y = some(42.7);
1639
+ *
1640
+ * assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
1641
+ * assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);
1642
+ * ```
1643
+ *
1644
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
1645
+ */
1646
+ zipWith(other, f) {
1647
+ return this.isSome() && other.isSome() ? some(f(this[ValueProperty2], other[ValueProperty2])) : none;
1648
+ }
1649
+ /**
1650
+ * Unzips an option containing a tuple of two options.
1651
+ *
1652
+ * If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1653
+ *
1654
+ * @example
1655
+ * ```typescript
1656
+ * const x: Option<[number, string]> = some([1, 'hi']);
1657
+ * assert.equal(x.unzip(), [some(1), some('hi')]);
1658
+ * ```
1659
+ * @example
1660
+ * ```typescript
1661
+ * const x: Option<[number, string]> = none;
1662
+ * assert.equal(x.unzip(), [none, none]);
1663
+ * ```
1664
+ *
1665
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
1666
+ */
1667
+ unzip() {
1668
+ return this.match({
1669
+ some: /* @__PURE__ */ __name(([value0, value1]) => [some(value0), some(value1)], "some"),
1670
+ none: /* @__PURE__ */ __name(() => [none, none], "none")
1671
+ });
1672
+ }
1673
+ /**
1674
+ * Transposes an `Option` of a `Result` into a `Result` of an `Option`.
1675
+ *
1676
+ * `none` will be mapped to `ok(none)`. `some(ok(v))` and `some(err(e))` will be mapped to `ok(some(v))` and `err(e)`.
1677
+ *
1678
+ * @example
1679
+ * ```typescript
1680
+ * const x: Option<Result<number, Error>> = some(ok(5));
1681
+ * const y: Result<Option<number>, Error> = ok(some(5));
1682
+ * assert.equal(x.transpose(), y);
1683
+ * ```
1684
+ *
1685
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
1686
+ */
1687
+ transpose() {
1688
+ return this.match({
1689
+ some: /* @__PURE__ */ __name((result) => result.map(some), "some"),
1690
+ none: /* @__PURE__ */ __name(() => ok(none), "none")
1691
+ });
1692
+ }
1693
+ /**
1694
+ * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
1695
+ *
1696
+ * @example
1697
+ * ```typescript
1698
+ * const x: Option<Option<number>> = some(some(6));
1699
+ * assert.equal(x.flatten(), some(6));
1700
+ * ```
1701
+ * @example
1702
+ * ```typescript
1703
+ * const x: Option<Option<number>> = some(none);
1704
+ * assert.equal(x.flatten(), none);
1705
+ * ```
1706
+ * @example
1707
+ * ```typescript
1708
+ * const x: Option<Option<number>> = none;
1709
+ * assert.equal(x.flatten(), none);
1710
+ * ```
1711
+ *
1712
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
1713
+ */
1714
+ flatten() {
1715
+ return this.match({ some: /* @__PURE__ */ __name((inner) => inner, "some"), none: returnThis });
1716
+ }
1717
+ /**
1718
+ * Returns a `Promise` object with the awaited value (if `Some`).
1719
+ *
1720
+ * @example
1721
+ * ```typescript
1722
+ * let x = some(Promise.resolve(3));
1723
+ * assert.equal(await x.intoPromise(), some(3));
1724
+ * ```
1725
+ *
1726
+ * @note This is an extension not supported in Rust
1727
+ */
1728
+ intoPromise() {
1729
+ return this.match({
1730
+ some: /* @__PURE__ */ __name(async (value) => some(await value), "some"),
1731
+ // NOSONAR
1732
+ none: /* @__PURE__ */ __name(() => Promise.resolve(none), "none")
1733
+ });
1734
+ }
1735
+ /**
1736
+ * Checks whether or not `other` equals with self.
1737
+ * @param other The other option to compare.
1738
+ *
1739
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
1740
+ */
1741
+ eq(other) {
1742
+ return this.isSome() === other.isSome() && this[ValueProperty2] === other[ValueProperty2];
1743
+ }
1744
+ /**
1745
+ * Checks whether or not `other` doesn't equal with self.
1746
+ * @param other The other option to compare.
1747
+ *
1748
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
1749
+ */
1750
+ ne(other) {
1751
+ return !this.eq(other);
1752
+ }
1753
+ /**
1754
+ * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
1755
+ * @param branches The branches to match.
1756
+ *
1757
+ * @example
1758
+ * ```typescript
1759
+ * const option = some(4).match({
1760
+ * some: (v) => v,
1761
+ * none: () => 0
1762
+ * });
1763
+ * assert.equal(option, 4);
1764
+ * ```
1765
+ * @example
1766
+ * ```typescript
1767
+ * const option = none.match({
1768
+ * some: (v) => v,
1769
+ * none: () => 0
1770
+ * });
1771
+ * assert.equal(option, 0);
1772
+ * ```
1773
+ */
1774
+ match(branches) {
1775
+ return this.isSome() ? branches.some.call(this, this[ValueProperty2]) : branches.none.call(this);
1776
+ }
1777
+ /**
1778
+ * Returns an iterator over the possibly contained value.
1779
+ *
1780
+ * The iterator yields one value if the result is `Some`, otherwise none.
1781
+ *
1782
+ * @example
1783
+ * ```typescript
1784
+ * const x = some(7);
1785
+ * for (const value of x) {
1786
+ * console.log(value);
1787
+ * }
1788
+ * // Logs 7
1789
+ * ```
1790
+ * @example
1791
+ * ```typescript
1792
+ * const x = none;
1793
+ * for (const value of x) {
1794
+ * console.log(value);
1795
+ * }
1796
+ * // Doesn't log
1797
+ * ```
1798
+ *
1799
+ * @see {@link IOption.iter}
1800
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1801
+ */
1802
+ [(_b2 = ValueProperty2, _a2 = ExistsProperty, Symbol.iterator)]() {
1803
+ return this.iter();
1804
+ }
1805
+ get [Symbol.toStringTag]() {
1806
+ return this.match({ some: /* @__PURE__ */ __name(() => "Some", "some"), none: /* @__PURE__ */ __name(() => "None", "none") });
1807
+ }
1808
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1809
+ static some(value) {
1810
+ return new _Option(value, true);
1811
+ }
1812
+ /**
1813
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object. This override
1814
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
1815
+ * library or from a different build.
1816
+ *
1817
+ * @param instance The instance to check.
1818
+ * @returns Whether or not the instance is a `Option`.
1819
+ *
1820
+ * @example
1821
+ * ```typescript
1822
+ * import { Option } from '@sapphire/result';
1823
+ * const { some } = require('@sapphire/result');
1824
+ *
1825
+ * some(2) instanceof Option; // true
1826
+ * ```
1827
+ */
1828
+ static [Symbol.hasInstance](instance) {
1829
+ return typeof instance === "object" && instance !== null && ValueProperty2 in instance && ExistsProperty in instance;
1830
+ }
1831
+ /**
1832
+ * @deprecated Use {@link Option.isOption} instead.
1833
+ *
1834
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1835
+ *
1836
+ * @param instance The instance to check.
1837
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1838
+ *
1839
+ * @example
1840
+ * ```typescript
1841
+ * import { Option } from '@sapphire/result';
1842
+ * const { some } = require('@sapphire/result');
1843
+ *
1844
+ * Option.isOption(some(2)); // true
1845
+ * ```
1846
+ */
1847
+ static is(instance) {
1848
+ return _Option[Symbol.hasInstance](instance);
1849
+ }
1850
+ /**
1851
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1852
+ *
1853
+ * @param instance The instance to check.
1854
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1855
+ *
1856
+ * @example
1857
+ * ```typescript
1858
+ * import { Option } from '@sapphire/result';
1859
+ * const { some } = require('@sapphire/result');
1860
+ *
1861
+ * Option.isOption(some(2)); // true
1862
+ * ```
1863
+ */
1864
+ static isOption(instance) {
1865
+ return _Option[Symbol.hasInstance](instance);
1866
+ }
1867
+ /**
1868
+ * Creates a {@link Result} out of a callback.
1869
+ *
1870
+ * @typeparam T The result's type.
1871
+ * @typeparam E The error's type.
1872
+ */
1873
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1874
+ static from(op) {
1875
+ try {
1876
+ return resolve2(isFunction(op) ? op() : op);
1877
+ } catch {
1878
+ return none;
1879
+ }
1880
+ }
1881
+ /**
1882
+ * Creates a {@link Result} out of a promise or async callback.
1883
+ *
1884
+ * @typeparam T The result's type.
1885
+ * @typeparam E The error's type.
1886
+ */
1887
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1888
+ static async fromAsync(op) {
1889
+ try {
1890
+ return resolve2(await (isFunction(op) ? op() : op));
1891
+ } catch {
1892
+ return none;
1893
+ }
1894
+ }
1895
+ /**
1896
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
1897
+ * {@link Err} encountered.
1898
+ *
1899
+ * @param results An array of {@link Result}s.
1900
+ * @returns A new {@link Result}.
1901
+ */
1902
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1903
+ static all(results) {
1904
+ const values = [];
1905
+ for (const result of results) {
1906
+ if (result.isNone()) return result;
1907
+ values.push(result[ValueProperty2]);
1908
+ }
1909
+ return some(values);
1910
+ }
1911
+ /**
1912
+ * Returns the first encountered {@link Some}, or a {@link None} if none was found.
1913
+ *
1914
+ * @param options An array of {@link Option}s.
1915
+ * @returns A new {@link Option}.
1916
+ */
1917
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1918
+ static any(results) {
1919
+ for (const result of results) {
1920
+ if (result.isSome()) return result;
1921
+ }
1922
+ return none;
1923
+ }
1924
+ };
1925
+ __name(_Option, "Option");
1926
+ __publicField(_Option, "none", new _Option(null, false));
1927
+ var Option = _Option;
1928
+ var { some, none } = Option;
1929
+ function resolve2(value) {
1930
+ if (value === null || value === void 0) return none;
1931
+ if (Option.isOption(value)) return value;
1932
+ return some(value);
1933
+ }
1934
+ __name(resolve2, "resolve");
1935
+
1936
+ export { Option, OptionError, Result, ResultError, err, none, ok, some };
1937
+ //# sourceMappingURL=index.mjs.map
1938
+ //# sourceMappingURL=index.mjs.map