@sapphire/result 2.6.7-pr-725.55b1ff7b.0 → 2.7.0-next.143375f7

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