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

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,1861 @@ 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
+ isNoneOr(cb) {
1053
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => true, "none") });
1054
+ }
1055
+ /**
1056
+ * Returns the contained `Some` value.
1057
+ * @param message The message for the error.
1058
+ * If the value is an `Err`, it throws an {@link OptionError} with the given message.
1059
+ *
1060
+ * @example
1061
+ * ```typescript
1062
+ * const x: Option<string> = some(2);
1063
+ * assert.equal(x.expect('Whoops!'), 2);
1064
+ * ```
1065
+ * @example
1066
+ * ```typescript
1067
+ * const x: Option<string> = none;
1068
+ * assert.throws(() => x.expect('Whoops!'), {
1069
+ * name: 'OptionError',
1070
+ * message: 'Whoops'
1071
+ * });
1072
+ * ```
1073
+ *
1074
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
1075
+ */
1076
+ expect(message) {
1077
+ if (this.isNone()) throw new OptionError(message);
1078
+ return this[ValueProperty2];
1079
+ }
1080
+ /**
1081
+ * Returns the contained `Some` value.
1082
+ *
1083
+ * If the value is an `Err`, it throws an {@link OptionError} with the message.
1084
+ * @seealso {@link unwrapOr}
1085
+ * @seealso {@link unwrapOrElse}
1086
+ *
1087
+ * @example
1088
+ * ```typescript
1089
+ * const x: Option<string> = some(2);
1090
+ * assert.equal(x.unwrap(), 2);
1091
+ * ```
1092
+ * @example
1093
+ * ```typescript
1094
+ * const x: Option<string> = none;
1095
+ * assert.throws(() => x.unwrap(), {
1096
+ * name: 'OptionError',
1097
+ * message: 'Unwrap failed'
1098
+ * });
1099
+ * ```
1100
+ *
1101
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
1102
+ */
182
1103
  unwrap() {
183
- return this.value;
184
- }
185
- unwrapOr() {
186
- return this.value;
187
- }
188
- unwrapOrElse() {
189
- return this.value;
190
- }
1104
+ if (this.isNone()) throw new OptionError("Unwrap failed");
1105
+ return this[ValueProperty2];
1106
+ }
1107
+ /**
1108
+ * Returns the contained `Some` value or a provided default.
1109
+ *
1110
+ * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
1111
+ * recommended to use {@link unwrapOrElse}, which is lazily evaluated.
1112
+ *
1113
+ * @example
1114
+ * ```typescript
1115
+ * assert.equal(some(2).unwrapOr(0), 2);
1116
+ * ```
1117
+ * @example
1118
+ * ```typescript
1119
+ * assert.equal(none.unwrapOr(0), 0);
1120
+ * ```
1121
+ *
1122
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
1123
+ */
1124
+ unwrapOr(defaultValue) {
1125
+ return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
1126
+ }
1127
+ /**
1128
+ * Returns the contained Some value or computes it from a closure.
1129
+ *
1130
+ * @example
1131
+ * ```typescript
1132
+ * assert.equal(some(2).unwrapOrElse(() => 0), 2);
1133
+ * ```
1134
+ * @example
1135
+ * ```typescript
1136
+ * assert.equal(none.unwrapOrElse(() => 0), 0);
1137
+ * ```
1138
+ *
1139
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
1140
+ */
1141
+ unwrapOrElse(cb) {
1142
+ return this.match({ some: /* @__PURE__ */ __name((value) => value, "some"), none: cb });
1143
+ }
1144
+ /**
1145
+ * Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
1146
+ * @param cb The predicate.
1147
+ *
1148
+ * @example
1149
+ * ```typescript
1150
+ * const maybeSomeString = some('Hello, world!');
1151
+ * const maybeSomeLength = maybeSomeString.map((value) => value.length);
1152
+ *
1153
+ * assert.equal(maybeSomeLength, some(13));
1154
+ * ```
1155
+ *
1156
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
1157
+ */
191
1158
  map(cb) {
192
- return createSome(cb(this.value));
193
- }
1159
+ return this.match({ some: /* @__PURE__ */ __name((value) => some(cb(value)), "some"), none: returnThis });
1160
+ }
1161
+ /**
1162
+ * Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
1163
+ * untouched.
1164
+ * @param cb The predicate.
1165
+ *
1166
+ * @example
1167
+ * ```typescript
1168
+ * const input: Option<string> = some('Hello, world!');
1169
+ * const result = input.mapInto((value) => some(value.length));
1170
+ *
1171
+ * assert.equal(result, some(13));
1172
+ * ```
1173
+ * @example
1174
+ * ```typescript
1175
+ * const input: Option<string> = none;
1176
+ * const result = input.mapInto((value) => some(value.length));
1177
+ *
1178
+ * assert.equal(result, none);
1179
+ * ```
1180
+ *
1181
+ * @note This is an extension not supported in Rust
1182
+ */
194
1183
  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
- }
1184
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
1185
+ }
1186
+ /**
1187
+ * Returns the provided default result (if none), or applies a function to the contained value (if any).
1188
+ *
1189
+ * Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
1190
+ * recommended to use {@link mapOrElse}, which is lazily evaluated.
1191
+ * @param defaultValue The default value.
1192
+ * @param cb The predicate.
1193
+ *
1194
+ * @example
1195
+ * ```typescript
1196
+ * const x: Option<string> = some('hello');
1197
+ * assert.equal(x.mapOr(42, (value) => value.length), 5);
1198
+ * ```
1199
+ * @example
1200
+ * ```typescript
1201
+ * const x: Option<string> = none;
1202
+ * assert.equal(x.mapOr(42, (value) => value.length), 42);
1203
+ * ```
1204
+ *
1205
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
1206
+ */
1207
+ mapOr(defaultValue, cb) {
1208
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue, "none") });
1209
+ }
1210
+ /**
1211
+ * Computes a default function result (if none), or applies a different function to the contained value (if any).
1212
+ * @param defaultValue The default value.
1213
+ * @param cb The predicate.
1214
+ *
1215
+ * @example
1216
+ * ```typescript
1217
+ * const x: Option<string> = some('hello');
1218
+ * assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
1219
+ * ```
1220
+ * @example
1221
+ * ```typescript
1222
+ * const x: Option<string> = none;
1223
+ * assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);
1224
+ * ```
1225
+ *
1226
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
1227
+ */
1228
+ mapOrElse(defaultValue, cb) {
1229
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => defaultValue(), "none") });
1230
+ }
1231
+ /**
1232
+ * Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
1233
+ * untouched.
1234
+ * @param cb The predicate.
1235
+ *
1236
+ * @example
1237
+ * ```typescript
1238
+ * const input: Option<string> = some('Hello, world!');
1239
+ * const result = input.mapNoneInto(() => some(13));
1240
+ *
1241
+ * assert.equal(result, some('Hello, world!'));
1242
+ * ```
1243
+ * @example
1244
+ * ```typescript
1245
+ * const input: Option<string> = none;
1246
+ * const result = input.mapNoneInto(() => some(13));
1247
+ *
1248
+ * assert.equal(result, some(13));
1249
+ * ```
1250
+ *
1251
+ * @note This is an extension not supported in Rust
1252
+ */
1253
+ mapNoneInto(cb) {
1254
+ return this.match({ some: returnThis, none: cb });
1255
+ }
1256
+ /**
1257
+ * Calls the provided closure with a reference to the contained value (if `Some`).
1258
+ * @param cb The predicate.
1259
+ * @seealso {@link inspectAsync} for the awaitable version.
1260
+ *
1261
+ * @example
1262
+ * ```typescript
1263
+ * some(2).inspect(console.log);
1264
+ * // Logs: 2
1265
+ * ```
1266
+ * @example
1267
+ * ```typescript
1268
+ * none.inspect(console.log);
1269
+ * // Doesn't log
1270
+ * ```
1271
+ *
1272
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect}
1273
+ */
206
1274
  inspect(cb) {
207
- cb(this.value);
1275
+ if (this.isSome()) cb(this[ValueProperty2]);
208
1276
  return this;
209
1277
  }
1278
+ /**
1279
+ * Calls the provided closure with a reference to the contained value (if `Some`).
1280
+ * @param cb The predicate.
1281
+ * @seealso {@link inspect} for the sync version.
1282
+ *
1283
+ * @example
1284
+ * ```typescript
1285
+ * await some(2).inspectAsync(console.log);
1286
+ * // Logs: 2
1287
+ * ```
1288
+ * @example
1289
+ * ```typescript
1290
+ * await none.inspectAsync(console.log);
1291
+ * // Doesn't log
1292
+ * ```
1293
+ *
1294
+ * @note This is an extension not supported in Rust
1295
+ */
210
1296
  async inspectAsync(cb) {
211
- await cb(this.value);
1297
+ if (this.isSome()) await cb(this[ValueProperty2]);
212
1298
  return this;
213
1299
  }
214
- okOr() {
215
- return createOk(this.value);
216
- }
217
- okOrElse() {
218
- return createOk(this.value);
219
- }
1300
+ /**
1301
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
1302
+ *
1303
+ * Arguments passed to `okOr` are eagerly evaluated; if you are passing the result of a function call, it is
1304
+ * recommended to use {@link okOrElse}, which is lazily evaluated.
1305
+ * @param err The error to be used.
1306
+ *
1307
+ * @example
1308
+ * ```typescript
1309
+ * const x: Option<string> = some('hello');
1310
+ * assert.equal(x.okOr(0), ok('hello'));
1311
+ * ```
1312
+ * @example
1313
+ * ```typescript
1314
+ * const x: Option<string> = none;
1315
+ * assert.equal(x.okOr(0), err(0));
1316
+ * ```
1317
+ *
1318
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
1319
+ */
1320
+ okOr(error) {
1321
+ return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(error), "none") });
1322
+ }
1323
+ /**
1324
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
1325
+ * @param cb The error to be used.
1326
+ *
1327
+ * @example
1328
+ * ```typescript
1329
+ * const x: Option<string> = some('hello');
1330
+ * assert.equal(x.okOrElse(() => 0), ok('hello'));
1331
+ * ```
1332
+ * @example
1333
+ * ```typescript
1334
+ * const x: Option<string> = none;
1335
+ * assert.equal(x.okOrElse(() => 0), err(0));
1336
+ * ```
1337
+ *
1338
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
1339
+ */
1340
+ okOrElse(cb) {
1341
+ return this.match({ some: /* @__PURE__ */ __name((value) => ok(value), "some"), none: /* @__PURE__ */ __name(() => err(cb()), "none") });
1342
+ }
1343
+ /**
1344
+ * Returns an iterator over the possibly contained value.
1345
+ *
1346
+ * The iterator yields one value if the result is `Some`, otherwise none.
1347
+ *
1348
+ * @example
1349
+ * ```typescript
1350
+ * const x = some(7);
1351
+ * for (const value of x) {
1352
+ * console.log(value);
1353
+ * }
1354
+ * // Logs 7
1355
+ * ```
1356
+ * @example
1357
+ * ```typescript
1358
+ * const x = none;
1359
+ * for (const value of x) {
1360
+ * console.log(value);
1361
+ * }
1362
+ * // Doesn't log
1363
+ * ```
1364
+ *
1365
+ * @see {@link Option.iter}
1366
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1367
+ */
220
1368
  *iter() {
221
- yield this.value;
222
- }
1369
+ if (this.isSome()) yield this[ValueProperty2];
1370
+ }
1371
+ /**
1372
+ * Returns `None` if the option is `None`, otherwise returns `option`.
1373
+ * @param option The option.
1374
+ *
1375
+ * @example
1376
+ * ```typescript
1377
+ * const x: Option<number> = some(2);
1378
+ * const y: Option<string> = none;
1379
+ * assert.equal(x.and(y), none);
1380
+ * ```
1381
+ * @example
1382
+ * ```typescript
1383
+ * const x: Option<number> = none;
1384
+ * const y: Option<string> = some('foo');
1385
+ * assert.equal(x.and(y), none);
1386
+ * ```
1387
+ * @example
1388
+ * ```typescript
1389
+ * const x: Option<number> = some(2);
1390
+ * const y: Option<string> = some('foo');
1391
+ * assert.equal(x.and(y), some('foo'));
1392
+ * ```
1393
+ * @example
1394
+ * ```typescript
1395
+ * const x: Option<number> = none;
1396
+ * const y: Option<string> = none;
1397
+ * assert.equal(x.and(y), none);
1398
+ * ```
1399
+ *
1400
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
1401
+ */
223
1402
  and(option) {
224
- return option;
225
- }
1403
+ return this.match({ some: /* @__PURE__ */ __name(() => option, "some"), none: returnThis });
1404
+ }
1405
+ /**
1406
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1407
+ *
1408
+ * This function can be used for control flow based on `Result` values.
1409
+ * @param cb The predicate.
1410
+ *
1411
+ * @example
1412
+ * ```typescript
1413
+ * function fractionOf4(value: number) {
1414
+ * return value === 0 ? none : some(4 / value);
1415
+ * }
1416
+ *
1417
+ * assert.equal(some(2).andThen(fractionOf4), some(4));
1418
+ * assert.equal(some(0).andThen(fractionOf4), none);
1419
+ * assert.equal(none.andThen(fractionOf4), none);
1420
+ * ```
1421
+ *
1422
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
1423
+ */
226
1424
  andThen(cb) {
227
- return cb(this.value);
228
- }
229
- or() {
230
- return this;
231
- }
232
- orElse() {
233
- return this;
234
- }
1425
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: returnThis });
1426
+ }
1427
+ /**
1428
+ * Returns the option if it contains a value, otherwise returns `option`.
1429
+ * @param option The option.
1430
+ *
1431
+ * @example
1432
+ * ```typescript
1433
+ * const x: Option<number> = some(2);
1434
+ * const y: Option<number> = none;
1435
+ * assert.equal(x.or(y), some(2));
1436
+ * ```
1437
+ * @example
1438
+ * ```typescript
1439
+ * const x: Option<number> = none;
1440
+ * const y: Option<number> = some(100);
1441
+ * assert.equal(x.or(y), some(100));
1442
+ * ```
1443
+ * @example
1444
+ * ```typescript
1445
+ * const x: Option<number> = some(2);
1446
+ * const y: Option<number> = some(100);
1447
+ * assert.equal(x.or(y), some(2));
1448
+ * ```
1449
+ * @example
1450
+ * ```typescript
1451
+ * const x: Option<number> = none;
1452
+ * const y: Option<number> = none;
1453
+ * assert.equal(x.or(y), none);
1454
+ * ```
1455
+ *
1456
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
1457
+ */
1458
+ or(option) {
1459
+ return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => option, "none") });
1460
+ }
1461
+ /**
1462
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1463
+ *
1464
+ * This function can be used for control flow based on `Result` values.
1465
+ * @param cb The predicate.
1466
+ *
1467
+ * @example
1468
+ * ```typescript
1469
+ * const nobody = (): Option<string> => none;
1470
+ * const vikings = (): Option<string> => some('vikings');
1471
+ *
1472
+ * assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
1473
+ * assert.equal(none.orElse(vikings), some('vikings'));
1474
+ * assert.equal(none.orElse(nobody), none);
1475
+ * ```
1476
+ *
1477
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
1478
+ */
1479
+ orElse(cb) {
1480
+ return this.match({ some: returnThis, none: /* @__PURE__ */ __name(() => cb(), "none") });
1481
+ }
1482
+ /**
1483
+ * Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
1484
+ * @param option The option to compare.
1485
+ *
1486
+ * @example
1487
+ * ```typescript
1488
+ * const x: Option<number> = some(2);
1489
+ * const y: Option<number> = none;
1490
+ * assert.equal(x.xor(y), some(2));
1491
+ * ```
1492
+ * @example
1493
+ * ```typescript
1494
+ * const x: Option<number> = none;
1495
+ * const y: Option<number> = some(2);
1496
+ * assert.equal(x.xor(y), some(2));
1497
+ * ```
1498
+ * @example
1499
+ * ```typescript
1500
+ * const x: Option<number> = some(2);
1501
+ * const y: Option<number> = some(2);
1502
+ * assert.equal(x.xor(y), none);
1503
+ * ```
1504
+ * @example
1505
+ * ```typescript
1506
+ * const x: Option<number> = none;
1507
+ * const y: Option<number> = none;
1508
+ * assert.equal(x.xor(y), none);
1509
+ * ```
1510
+ *
1511
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
1512
+ */
235
1513
  xor(option) {
236
- return option.isSome() ? createNone : this;
1514
+ return this.match({
1515
+ some() {
1516
+ return option.isNone() ? this : none;
1517
+ },
1518
+ none: /* @__PURE__ */ __name(() => option, "none")
1519
+ });
237
1520
  }
238
1521
  filter(predicate) {
239
- return predicate(this.value) ? this : createNone;
240
- }
1522
+ return this.isSomeAnd(predicate) ? this : none;
1523
+ }
1524
+ /**
1525
+ * Returns `true` if the option is a `Some` value containing the given value.
1526
+ * @param value The value to compare.
1527
+ *
1528
+ * @example
1529
+ * ```typescript
1530
+ * const x: Option<number> = some(2);
1531
+ * assert.equal(x.contains(2), true);
1532
+ * ```
1533
+ * @example
1534
+ * ```typescript
1535
+ * const x: Option<number> = some(3);
1536
+ * assert.equal(x.contains(2), false);
1537
+ * ```
1538
+ * @example
1539
+ * ```typescript
1540
+ * const x: Option<number> = none;
1541
+ * assert.equal(x.contains(2), false);
1542
+ * ```
1543
+ *
1544
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
1545
+ */
241
1546
  contains(value) {
242
- return this.value === value;
243
- }
1547
+ return this.isSomeAnd((inner) => inner === value);
1548
+ }
1549
+ /**
1550
+ * Zips self with another `Option`.
1551
+ *
1552
+ * If self is `Some(s)` and `other` is `Some(o)`, this method returns `Some([s, o])`. Otherwise, `None` is returned.
1553
+ * @param other The option to zip self with.
1554
+ *
1555
+ * @example
1556
+ * ```typescript
1557
+ * const x = some(1);
1558
+ * const y = some('hi');
1559
+ * const z = none;
1560
+ *
1561
+ * assert.equal(x.zip(y), some([1, 'hi']));
1562
+ * assert.equal(x.zip(z), none);
1563
+ * ```
1564
+ *
1565
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
1566
+ */
244
1567
  zip(other) {
245
- return other.map((o) => [this.value, o]);
246
- }
1568
+ return this.isSome() && other.isSome() ? some([this[ValueProperty2], other[ValueProperty2]]) : none;
1569
+ }
1570
+ /**
1571
+ * Zips self and another `Option` with function `f`.
1572
+ *
1573
+ * If self is `Some(s)` and other is `Some(o)`, this method returns `Some(f(s, o))`. Otherwise, `None` is returned.
1574
+ * @param other The option to zip self with.
1575
+ * @param f The function that computes the returned value.
1576
+ *
1577
+ * @example
1578
+ * ```typescript
1579
+ * class Point {
1580
+ * public readonly x: number;
1581
+ * public readonly y: number;
1582
+ *
1583
+ * public constructor(x: number, y: number) {
1584
+ * this.x = x;
1585
+ * this.y = y;
1586
+ * }
1587
+ * }
1588
+ *
1589
+ * const x = some(17.5);
1590
+ * const y = some(42.7);
1591
+ *
1592
+ * assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
1593
+ * assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);
1594
+ * ```
1595
+ *
1596
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
1597
+ */
247
1598
  zipWith(other, f) {
248
- return other.map((o) => f(this.value, o));
249
- }
1599
+ return this.isSome() && other.isSome() ? some(f(this[ValueProperty2], other[ValueProperty2])) : none;
1600
+ }
1601
+ /**
1602
+ * Unzips an option containing a tuple of two options.
1603
+ *
1604
+ * If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1605
+ *
1606
+ * @example
1607
+ * ```typescript
1608
+ * const x: Option<[number, string]> = some([1, 'hi']);
1609
+ * assert.equal(x.unzip(), [some(1), some('hi')]);
1610
+ * ```
1611
+ * @example
1612
+ * ```typescript
1613
+ * const x: Option<[number, string]> = none;
1614
+ * assert.equal(x.unzip(), [none, none]);
1615
+ * ```
1616
+ *
1617
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
1618
+ */
250
1619
  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)
1620
+ return this.match({
1621
+ some: /* @__PURE__ */ __name(([value0, value1]) => [some(value0), some(value1)], "some"),
1622
+ none: /* @__PURE__ */ __name(() => [none, none], "none")
258
1623
  });
259
1624
  }
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
- }
1625
+ /**
1626
+ * Transposes an `Option` of a `Result` into a `Result` of an `Option`.
1627
+ *
1628
+ * `none` will be mapped to `ok(none)`. `some(ok(v))` and `some(err(e))` will be mapped to `ok(some(v))` and `err(e)`.
1629
+ *
1630
+ * @example
1631
+ * ```typescript
1632
+ * const x: Option<Result<number, Error>> = some(ok(5));
1633
+ * const y: Result<Option<number>, Error> = ok(some(5));
1634
+ * assert.equal(x.transpose(), y);
1635
+ * ```
1636
+ *
1637
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
1638
+ */
383
1639
  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);
1640
+ return this.match({
1641
+ // @ts-expect-error Complex types
1642
+ some: /* @__PURE__ */ __name((result) => result.map(some), "some"),
1643
+ none: /* @__PURE__ */ __name(() => ok(none), "none")
1644
+ });
507
1645
  }
1646
+ /**
1647
+ * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
1648
+ *
1649
+ * @example
1650
+ * ```typescript
1651
+ * const x: Option<Option<number>> = some(some(6));
1652
+ * assert.equal(x.flatten(), some(6));
1653
+ * ```
1654
+ * @example
1655
+ * ```typescript
1656
+ * const x: Option<Option<number>> = some(none);
1657
+ * assert.equal(x.flatten(), none);
1658
+ * ```
1659
+ * @example
1660
+ * ```typescript
1661
+ * const x: Option<Option<number>> = none;
1662
+ * assert.equal(x.flatten(), none);
1663
+ * ```
1664
+ *
1665
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
1666
+ */
508
1667
  flatten() {
509
- return this;
510
- }
1668
+ return this.match({ some: /* @__PURE__ */ __name((inner) => inner, "some"), none: returnThis });
1669
+ }
1670
+ /**
1671
+ * Returns a `Promise` object with the awaited value (if `Some`).
1672
+ *
1673
+ * @example
1674
+ * ```typescript
1675
+ * let x = some(Promise.resolve(3));
1676
+ * assert.equal(await x.intoPromise(), some(3));
1677
+ * ```
1678
+ *
1679
+ * @note This is an extension not supported in Rust
1680
+ */
511
1681
  intoPromise() {
512
- return Promise.resolve(createNone);
1682
+ return this.match({
1683
+ some: /* @__PURE__ */ __name(async (value) => some(await value), "some"),
1684
+ // NOSONAR
1685
+ none: /* @__PURE__ */ __name(() => Promise.resolve(none), "none")
1686
+ });
513
1687
  }
1688
+ /**
1689
+ * Checks whether or not `other` equals with self.
1690
+ * @param other The other option to compare.
1691
+ *
1692
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
1693
+ */
514
1694
  eq(other) {
515
- return other.isNone();
516
- }
1695
+ return this.isSome() === other.isSome() && this[ValueProperty2] === other[ValueProperty2];
1696
+ }
1697
+ /**
1698
+ * Checks whether or not `other` doesn't equal with self.
1699
+ * @param other The other option to compare.
1700
+ *
1701
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
1702
+ */
517
1703
  ne(other) {
518
- return other.isSome();
1704
+ return !this.eq(other);
519
1705
  }
1706
+ /**
1707
+ * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
1708
+ * @param branches The branches to match.
1709
+ *
1710
+ * @example
1711
+ * ```typescript
1712
+ * const option = some(4).match({
1713
+ * some: (v) => v,
1714
+ * none: () => 0
1715
+ * });
1716
+ * assert.equal(option, 4);
1717
+ * ```
1718
+ * @example
1719
+ * ```typescript
1720
+ * const option = none.match({
1721
+ * some: (v) => v,
1722
+ * none: () => 0
1723
+ * });
1724
+ * assert.equal(option, 0);
1725
+ * ```
1726
+ */
520
1727
  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);
1728
+ return this.isSome() ? branches.some.call(this, this[ValueProperty2]) : branches.none.call(this);
1729
+ }
1730
+ /**
1731
+ * Returns an iterator over the possibly contained value.
1732
+ *
1733
+ * The iterator yields one value if the result is `Some`, otherwise none.
1734
+ *
1735
+ * @example
1736
+ * ```typescript
1737
+ * const x = some(7);
1738
+ * for (const value of x) {
1739
+ * console.log(value);
1740
+ * }
1741
+ * // Logs 7
1742
+ * ```
1743
+ * @example
1744
+ * ```typescript
1745
+ * const x = none;
1746
+ * for (const value of x) {
1747
+ * console.log(value);
1748
+ * }
1749
+ * // Doesn't log
1750
+ * ```
1751
+ *
1752
+ * @see {@link IOption.iter}
1753
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1754
+ */
1755
+ [(_b2 = ValueProperty2, _a2 = ExistsProperty, Symbol.iterator)]() {
1756
+ return this.iter();
1757
+ }
1758
+ get [Symbol.toStringTag]() {
1759
+ return this.match({ some: /* @__PURE__ */ __name(() => "Some", "some"), none: /* @__PURE__ */ __name(() => "None", "none") });
1760
+ }
1761
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1762
+ static some(value) {
1763
+ return new _Option(value, true);
1764
+ }
1765
+ /**
1766
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object. This override
1767
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
1768
+ * library or from a different build.
1769
+ *
1770
+ * @param instance The instance to check.
1771
+ * @returns Whether or not the instance is a `Option`.
1772
+ *
1773
+ * @example
1774
+ * ```typescript
1775
+ * import { Option } from '@sapphire/result';
1776
+ * const { some } = require('@sapphire/result');
1777
+ *
1778
+ * some(2) instanceof Option; // true
1779
+ * ```
1780
+ */
1781
+ static [Symbol.hasInstance](instance) {
1782
+ return typeof instance === "object" && instance !== null && ValueProperty2 in instance && ExistsProperty in instance;
1783
+ }
1784
+ /**
1785
+ * @deprecated Use {@link Option.isOption} instead.
1786
+ *
1787
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1788
+ *
1789
+ * @param instance The instance to check.
1790
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1791
+ *
1792
+ * @example
1793
+ * ```typescript
1794
+ * import { Option } from '@sapphire/result';
1795
+ * const { some } = require('@sapphire/result');
1796
+ *
1797
+ * Option.isOption(some(2)); // true
1798
+ * ```
1799
+ */
1800
+ static is(instance) {
1801
+ return _Option[Symbol.hasInstance](instance);
1802
+ }
1803
+ /**
1804
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1805
+ *
1806
+ * @param instance The instance to check.
1807
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1808
+ *
1809
+ * @example
1810
+ * ```typescript
1811
+ * import { Option } from '@sapphire/result';
1812
+ * const { some } = require('@sapphire/result');
1813
+ *
1814
+ * Option.isOption(some(2)); // true
1815
+ * ```
1816
+ */
1817
+ static isOption(instance) {
1818
+ return _Option[Symbol.hasInstance](instance);
1819
+ }
1820
+ /**
1821
+ * Creates a {@link Result} out of a callback.
1822
+ *
1823
+ * @typeparam T The result's type.
1824
+ * @typeparam E The error's type.
1825
+ */
1826
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1827
+ static from(op) {
549
1828
  try {
550
- return resolve(op());
1829
+ return resolve2(isFunction(op) ? op() : op);
551
1830
  } catch {
552
- return Option2.none;
1831
+ return none;
553
1832
  }
554
1833
  }
555
- Option2.from = from;
556
- __name(from, "from");
557
- async function fromAsync(op) {
1834
+ /**
1835
+ * Creates a {@link Result} out of a promise or async callback.
1836
+ *
1837
+ * @typeparam T The result's type.
1838
+ * @typeparam E The error's type.
1839
+ */
1840
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1841
+ static async fromAsync(op) {
558
1842
  try {
559
- return resolve(await (isFunction(op) ? op() : op));
1843
+ return resolve2(await (isFunction(op) ? op() : op));
560
1844
  } 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);
1845
+ return none;
622
1846
  }
623
1847
  }
624
- Result2.fromAsync = fromAsync;
625
- __name(fromAsync, "fromAsync");
626
- function all(results) {
1848
+ /**
1849
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
1850
+ * {@link Err} encountered.
1851
+ *
1852
+ * @param results An array of {@link Result}s.
1853
+ * @returns A new {@link Result}.
1854
+ */
1855
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1856
+ static all(results) {
627
1857
  const values = [];
628
1858
  for (const result of results) {
629
- if (result.isErr()) {
630
- return result;
631
- }
632
- values.push(result.unwrap());
1859
+ if (result.isNone()) return result;
1860
+ values.push(result[ValueProperty2]);
633
1861
  }
634
- return (0, Result2.ok)(values);
635
- }
636
- Result2.all = all;
637
- __name(all, "all");
638
- function any(results) {
639
- const errors = [];
1862
+ return some(values);
1863
+ }
1864
+ /**
1865
+ * Returns the first encountered {@link Some}, or a {@link None} if none was found.
1866
+ *
1867
+ * @param options An array of {@link Option}s.
1868
+ * @returns A new {@link Option}.
1869
+ */
1870
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
1871
+ static any(results) {
640
1872
  for (const result of results) {
641
- if (result.isOk()) {
642
- return result;
643
- }
644
- errors.push(result.unwrapErr());
1873
+ if (result.isSome()) return result;
645
1874
  }
646
- return (0, Result2.err)(errors);
1875
+ return none;
647
1876
  }
648
- Result2.any = any;
649
- __name(any, "any");
650
- Result2.err = createErr;
651
- Result2.ok = createOk;
652
- })(exports.Result || (exports.Result = {}));
1877
+ };
1878
+ __name(_Option, "Option");
1879
+ __publicField(_Option, "none", new _Option(null, false));
1880
+ var Option = _Option;
1881
+ var { some, none } = Option;
1882
+ function resolve2(value) {
1883
+ if (value === null || value === void 0) return none;
1884
+ if (Option.isOption(value)) return value;
1885
+ return some(value);
1886
+ }
1887
+ __name(resolve2, "resolve");
653
1888
 
1889
+ exports.Option = Option;
654
1890
  exports.OptionError = OptionError;
1891
+ exports.Result = Result;
655
1892
  exports.ResultError = ResultError;
656
- exports.err = createErr;
657
- exports.none = createNone;
658
- exports.ok = createOk;
659
- exports.some = createSome;
660
- //# sourceMappingURL=out.js.map
1893
+ exports.err = err;
1894
+ exports.none = none;
1895
+ exports.ok = ok;
1896
+ exports.some = some;
1897
+ //# sourceMappingURL=index.cjs.map
661
1898
  //# sourceMappingURL=index.cjs.map