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