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

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