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