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

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.
@@ -1,18 +1,28 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
- var __publicField = (obj, key, value) => {
5
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
- return value;
7
- };
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
5
 
9
6
  // src/lib/common/utils.ts
10
7
  function isFunction(input) {
11
8
  return typeof input === "function";
12
9
  }
13
10
  __name(isFunction, "isFunction");
11
+ function returnThis() {
12
+ return this;
13
+ }
14
+ __name(returnThis, "returnThis");
15
+
16
+ // src/lib/OptionError.ts
17
+ var _OptionError = class _OptionError extends Error {
18
+ get name() {
19
+ return this.constructor.name;
20
+ }
21
+ };
22
+ __name(_OptionError, "OptionError");
23
+ var OptionError = _OptionError;
14
24
 
15
- // src/lib/Result/ResultError.ts
25
+ // src/lib/ResultError.ts
16
26
  var _ResultError = class _ResultError extends Error {
17
27
  constructor(message, value) {
18
28
  super(message);
@@ -26,629 +36,1854 @@ var _ResultError = class _ResultError extends Error {
26
36
  __name(_ResultError, "ResultError");
27
37
  var ResultError = _ResultError;
28
38
 
29
- // src/lib/Result/Ok.ts
30
- var _ResultOk = class _ResultOk {
31
- constructor(value) {
32
- __publicField(this, "value");
33
- this.value = value;
34
- }
39
+ // src/lib/Result.ts
40
+ var ValueProperty = Symbol.for("@sapphire/result:Result.value");
41
+ var SuccessProperty = Symbol.for("@sapphire/result:Result.success");
42
+ var _a, _b;
43
+ var _Result = class _Result {
44
+ constructor(value, success) {
45
+ __publicField(this, _b);
46
+ __publicField(this, _a);
47
+ this[ValueProperty] = value;
48
+ this[SuccessProperty] = success;
49
+ }
50
+ /**
51
+ * Returns `true` if the result is `Ok`.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const x = ok(-3);
56
+ * assert.equal(x.isOk(), true);
57
+ * ```
58
+ * @example
59
+ * ```typescript
60
+ * const x = err('Some error message');
61
+ * assert.equal(x.isOk(), false);
62
+ * ```
63
+ *
64
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
65
+ */
35
66
  isOk() {
36
- return true;
67
+ return this[SuccessProperty];
37
68
  }
38
69
  isOkAnd(cb) {
39
- return cb(this.value);
40
- }
70
+ return this.isOk() && cb(this[ValueProperty]);
71
+ }
72
+ /**
73
+ * Returns `true` if the result is `Err`.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const x = ok(-3);
78
+ * assert.equal(x.isErr(), false);
79
+ * ```
80
+ * @example
81
+ * ```typescript
82
+ * const x = err('Some error message');
83
+ * assert.equal(x.isErr(), true);
84
+ * ```
85
+ *
86
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
87
+ */
41
88
  isErr() {
42
- return false;
43
- }
44
- isErrAnd() {
45
- return false;
89
+ return !this[SuccessProperty];
46
90
  }
91
+ isErrAnd(cb) {
92
+ return this.isErr() && cb(this[ValueProperty]);
93
+ }
94
+ /**
95
+ * Converts from `Result<T, E>` to `Option<T>`.
96
+ *
97
+ * Converts itself into an `Option<T>`, and discarding the error, if any.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const x: Result<number, string> = ok(2);
102
+ * assert.equal(x.ok(), some(2));
103
+ * ```
104
+ * @example
105
+ * ```typescript
106
+ * const x: Result<number, string> = err('Some error message');
107
+ * assert.equal(x.ok(), none);
108
+ * ```
109
+ *
110
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
111
+ */
47
112
  ok() {
48
- return createSome(this.value);
49
- }
113
+ return this.match({ ok: /* @__PURE__ */ __name((value) => some(value), "ok"), err: /* @__PURE__ */ __name(() => none, "err") });
114
+ }
115
+ /**
116
+ * Converts from `Result<T, E>` to `Option<E>`.
117
+ *
118
+ * Converts itself into an `Option<E>`, and discarding the successful value, if any.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const x: Result<number, string> = ok(2);
123
+ * assert.equal(x.err(), none);
124
+ * ```
125
+ * @example
126
+ * ```typescript
127
+ * const x: Result<number, string> = err('Some error message');
128
+ * assert.equal(x.err(), 'Some error message');
129
+ * ```
130
+ *
131
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
132
+ */
50
133
  err() {
51
- return createNone;
52
- }
134
+ return this.match({ ok: /* @__PURE__ */ __name(() => none, "ok"), err: /* @__PURE__ */ __name((error) => some(error), "err") });
135
+ }
136
+ /**
137
+ * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
138
+ * untouched.
139
+ * @param cb The predicate.
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * const x: Result<number, string> = ok(2);
144
+ * assert.equal(x.map((value) => value * 2), ok(4));
145
+ * ```
146
+ * @example
147
+ * ```typescript
148
+ * const x: Result<number, string> = err('Some error message');
149
+ * assert.equal(x.map((value) => value * 2), err('Some error message'));
150
+ * ```
151
+ *
152
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
153
+ */
53
154
  map(cb) {
54
- return createOk(cb(this.value));
55
- }
155
+ return this.match({ ok: /* @__PURE__ */ __name((value) => ok(cb(value)), "ok"), err: returnThis });
156
+ }
157
+ /**
158
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
159
+ * untouched.
160
+ *
161
+ * Unlike {@link map}, this method does not wrap the returned value inside `Ok`, but instead, it returns the
162
+ * returned value.
163
+ * @param cb The predicate.
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const x: Result<number, string> = ok(2);
168
+ * assert.equal(x.mapInto((value) => ok(value * value)), ok(4));
169
+ * ```
170
+ * @example
171
+ * ```typescript
172
+ * const x: Result<number, string> = ok(0);
173
+ * assert.equal(
174
+ * x.mapInto((value) => (value === 0 ? err('zero is not divisible') : ok(1 / value))),
175
+ * err('zero is not divisible')
176
+ * );
177
+ * ```
178
+ * @example
179
+ * ```typescript
180
+ * const x: Result<number, string> = err('Some error message');
181
+ * assert.equal(x.mapInto((value) => ok(4)), err('Some error message'));
182
+ * ```
183
+ *
184
+ * @note This is an extension not supported in Rust
185
+ */
56
186
  mapInto(cb) {
57
- return cb(this.value);
58
- }
59
- mapOr(_, cb) {
60
- return cb(this.value);
61
- }
62
- mapOrElse(_, cb) {
63
- return cb(this.value);
64
- }
65
- mapErr() {
66
- return this;
67
- }
68
- mapErrInto() {
69
- return this;
70
- }
187
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
188
+ }
189
+ /**
190
+ * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
191
+ *
192
+ * Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
193
+ * recommended to use `mapOrElse`, which is lazily evaluated.
194
+ * @param defaultValue The default value to use.
195
+ * @param cb The predicate.
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * const x = ok('hello');
200
+ * assert.equal(x.mapOr(42, (value) => value.length), 5);
201
+ * ```
202
+ * @example
203
+ * ```typescript
204
+ * const x = err('Some error message');
205
+ * assert.equal(x.mapOr(42, (value) => value.length), 42);
206
+ * ```
207
+ *
208
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
209
+ */
210
+ mapOr(defaultValue, cb) {
211
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
212
+ }
213
+ /**
214
+ * Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
215
+ * to a contained `Ok` value.
216
+ *
217
+ * This function can be used to unpack a successful result while handling an error.
218
+ * @param op The predicate that is run on `Err`.
219
+ * @param cb The predicate that is run on `Ok`.
220
+ *
221
+ * @example
222
+ * ```typescript
223
+ * const x: Result<string, string> = ok('hello');
224
+ * assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);
225
+ * ```
226
+ * @example
227
+ * ```typescript
228
+ * const x: Result<string, string> = err('Some error message');
229
+ * assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 18);
230
+ * ```
231
+ *
232
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
233
+ */
234
+ mapOrElse(op, cb) {
235
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
236
+ }
237
+ /**
238
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
239
+ * untouched.
240
+ *
241
+ * This function can be used to pass through a successful result while handling an error.
242
+ * @param cb The predicate.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * const x: Result<number, Error> = ok(2);
247
+ * assert.equal(x.mapErr((error) => error.message), ok(2));
248
+ * ```
249
+ * @example
250
+ * ```typescript
251
+ * const x: Result<number, Error> = err(new Error('Some error message'));
252
+ * assert.equal(x.mapErr((error) => error.message), err('Some error message'));
253
+ * ```
254
+ *
255
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
256
+ */
257
+ mapErr(cb) {
258
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => err(cb(error)), "err") });
259
+ }
260
+ /**
261
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
262
+ * untouched.
263
+ *
264
+ * This function can be used to pass through a successful result while handling an error.
265
+ *
266
+ * Unlike {@link mapErr}, this method does not wrap the returned value inside `Err`, but instead, it returns the
267
+ * returned value.
268
+ * @param cb The predicate.
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * const x: Result<number, Error> = ok(2);
273
+ * assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));
274
+ * ```
275
+ * @example
276
+ * ```typescript
277
+ * const x: Result<number, Error> = err(new Error('Some error message'));
278
+ * assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));
279
+ * ```
280
+ * @example
281
+ * ```typescript
282
+ * const x: Result<number, Error> = err(new Error('Some error message'));
283
+ * assert.equal(x.mapErrInto((error) => ok(4)), ok(4));
284
+ * ```
285
+ *
286
+ * @note This is an extension not supported in Rust
287
+ */
288
+ mapErrInto(cb) {
289
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
290
+ }
291
+ /**
292
+ * Calls the provided closure with a reference to the contained value (if `Ok`).
293
+ * @param cb The predicate.
294
+ * @seealso {@link inspectAsync} for the awaitable version.
295
+ *
296
+ * @example
297
+ * ```typescript
298
+ * ok(2).inspect(console.log);
299
+ * // Logs: 2
300
+ * ```
301
+ * @example
302
+ * ```typescript
303
+ * err('Some error message').inspect(console.log);
304
+ * // Doesn't log
305
+ * ```
306
+ *
307
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
308
+ */
71
309
  inspect(cb) {
72
- cb(this.value);
310
+ if (this.isOk()) cb(this[ValueProperty]);
73
311
  return this;
74
312
  }
313
+ /**
314
+ * Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
315
+ * @param cb The predicate.
316
+ * @seealso {@link inspect} for the sync version.
317
+ *
318
+ * @example
319
+ * ```typescript
320
+ * await ok(2).inspectAsync(console.log);
321
+ * // Logs: 2
322
+ * ```
323
+ * @example
324
+ * ```typescript
325
+ * await err('Some error message').inspectAsync(console.log);
326
+ * // Doesn't log
327
+ * ```
328
+ *
329
+ * @note This is an extension not supported in Rust
330
+ */
75
331
  async inspectAsync(cb) {
76
- await cb(this.value);
332
+ if (this.isOk()) await cb(this[ValueProperty]);
77
333
  return this;
78
334
  }
79
- inspectErr() {
335
+ /**
336
+ * Calls the provided closure with a reference to the contained error (if `Err`).
337
+ * @param cb The predicate.
338
+ * @seealso {@link inspectErrAsync} for the awaitable version.
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * ok(2).inspectErr(console.log);
343
+ * // Doesn't log
344
+ * ```
345
+ * @example
346
+ * ```typescript
347
+ * err('Some error message').inspectErr(console.log);
348
+ * // Logs: Some error message
349
+ * ```
350
+ *
351
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
352
+ */
353
+ inspectErr(cb) {
354
+ if (this.isErr()) cb(this[ValueProperty]);
80
355
  return this;
81
356
  }
82
- inspectErrAsync() {
83
- return Promise.resolve(this);
357
+ /**
358
+ * Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
359
+ * @param cb The predicate.
360
+ * @seealso {@link inspectErr} for the sync version.
361
+ *
362
+ * @example
363
+ * ```typescript
364
+ * await ok(2).inspectErrAsync(console.log);
365
+ * // Doesn't log
366
+ * ```
367
+ * @example
368
+ * ```typescript
369
+ * await err('Some error message').inspectErrAsync(console.log);
370
+ * // Logs: Some error message
371
+ * ```
372
+ *
373
+ * @note This is an extension not supported in Rust
374
+ */
375
+ async inspectErrAsync(cb) {
376
+ if (this.isErr()) await cb(this[ValueProperty]);
377
+ return this;
84
378
  }
379
+ /**
380
+ * Returns an iterator over the possibly contained value.
381
+ *
382
+ * The iterator yields one value if the result is `Ok`, otherwise none.
383
+ *
384
+ * @example
385
+ * ```typescript
386
+ * const x = ok(7);
387
+ * for (const value of x.iter()) {
388
+ * console.log(value);
389
+ * }
390
+ * // Logs 7
391
+ * ```
392
+ * @example
393
+ * ```typescript
394
+ * const x = err('Nothing!');
395
+ * for (const value of x.iter()) {
396
+ * console.log(value);
397
+ * }
398
+ * // Doesn't log
399
+ * ```
400
+ *
401
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
402
+ */
85
403
  *iter() {
86
- yield this.value;
87
- }
88
- expect() {
89
- return this.value;
90
- }
404
+ if (this.isOk()) yield this[ValueProperty];
405
+ }
406
+ /**
407
+ * Returns the contained `Ok` value.
408
+ *
409
+ * If the value is an `Err`, it throws a {@link ResultError} with the given message and the content of the `Err`.
410
+ * @param message The message for the error.
411
+ *
412
+ * @example
413
+ * ```typescript
414
+ * const x = ok(2);
415
+ * assert.equal(x.expect('Whoops!'), 2);
416
+ * ```
417
+ * @example
418
+ * ```typescript
419
+ * const x = err('Emergency failure');
420
+ * assert.throws(() => x.expect('Whoops!'), {
421
+ * name: 'ResultError',
422
+ * message: 'Whoops',
423
+ * value: 'Emergency failure'
424
+ * });
425
+ * ```
426
+ *
427
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
428
+ */
429
+ expect(message) {
430
+ if (this.isErr()) throw new ResultError(message, this[ValueProperty]);
431
+ return this[ValueProperty];
432
+ }
433
+ /**
434
+ * Returns the contained `Err` value.
435
+ *
436
+ * If the value is an `Ok`, it throws a {@link ResultError} with the given message and the content of the `Ok`.
437
+ * @param message The message for the error.
438
+ *
439
+ * @example
440
+ * ```typescript
441
+ * const x = ok(2);
442
+ * assert.throws(() => x.expectErr('Whoops!'), {
443
+ * name: 'ResultError',
444
+ * message: 'Whoops',
445
+ * value: 2
446
+ * });
447
+ * ```
448
+ * @example
449
+ * ```typescript
450
+ * const x = err('Emergency failure');
451
+ * assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
452
+ * ```
453
+ *
454
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
455
+ */
91
456
  expectErr(message) {
92
- throw new ResultError(message, this.value);
93
- }
457
+ if (this.isOk()) throw new ResultError(message, this[ValueProperty]);
458
+ return this[ValueProperty];
459
+ }
460
+ /**
461
+ * Returns the contained `Ok` value.
462
+ *
463
+ * If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`.
464
+ * @seealso {@link unwrapOr}
465
+ * @seealso {@link unwrapOrElse}
466
+ * @seealso {@link unwrapErr}
467
+ * @seealso {@link unwrapRaw}
468
+ *
469
+ * @example
470
+ * ```typescript
471
+ * const x = ok(2);
472
+ * assert.equal(x.unwrap(), 2);
473
+ * ```
474
+ * @example
475
+ * ```typescript
476
+ * const x = err('Emergency failure');
477
+ * assert.throws(() => x.unwrap(), {
478
+ * name: 'ResultError',
479
+ * message: 'Unwrap failed',
480
+ * value: 'Emergency failure'
481
+ * });
482
+ * ```
483
+ *
484
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
485
+ */
94
486
  unwrap() {
95
- return this.value;
96
- }
487
+ if (this.isErr()) throw new ResultError("Unwrap failed", this[ValueProperty]);
488
+ return this[ValueProperty];
489
+ }
490
+ /**
491
+ * Returns the contained `Err` value.
492
+ *
493
+ * If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`.
494
+ * @seealso {@link unwrap}
495
+ * @seealso {@link unwrapOr}
496
+ * @seealso {@link unwrapOrElse}
497
+ * @seealso {@link unwrapRaw}
498
+ *
499
+ * @example
500
+ * ```typescript
501
+ * const x = ok(2);
502
+ * assert.throws(() => x.unwrapErr(), {
503
+ * name: 'ResultError',
504
+ * message: 'Unwrap failed',
505
+ * value: 2
506
+ * });
507
+ * ```
508
+ * @example
509
+ * ```typescript
510
+ * const x = err('Emergency failure');
511
+ * assert.equal(x.unwrapErr(), 'Emergency failure');
512
+ * ```
513
+ *
514
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
515
+ */
97
516
  unwrapErr() {
98
- throw new ResultError("Unwrap failed", this.value);
99
- }
100
- unwrapOr() {
101
- return this.value;
102
- }
103
- unwrapOrElse() {
104
- return this.value;
105
- }
517
+ if (this.isOk()) throw new ResultError("Unwrap failed", this[ValueProperty]);
518
+ return this[ValueProperty];
519
+ }
520
+ /**
521
+ * Returns the contained `Ok` value or the provided default.
522
+ *
523
+ * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
524
+ * recommended to use {@link unwrapOrElse}, which is lazily evaluated.
525
+ * @seealso {@link unwrap}
526
+ * @seealso {@link unwrapOrElse}
527
+ * @seealso {@link unwrapErr}
528
+ * @seealso {@link unwrapRaw}
529
+ *
530
+ * @param defaultValue The default value.
531
+ *
532
+ * @example
533
+ * ```typescript
534
+ * const x: Result<number, string> = ok(9);
535
+ * assert.equal(x.unwrapOr(2), 9);
536
+ * ```
537
+ * @example
538
+ * ```typescript
539
+ * const x: Result<number, string> = err('Error');
540
+ * assert.equal(x.unwrapOr(2), 2);
541
+ * ```
542
+ *
543
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
544
+ */
545
+ unwrapOr(defaultValue) {
546
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name(() => defaultValue, "err") });
547
+ }
548
+ /**
549
+ * Returns the contained `Ok` value or computes it from a closure.
550
+ * @seealso {@link unwrap}
551
+ * @seealso {@link unwrapOr}
552
+ * @seealso {@link unwrapErr}
553
+ * @seealso {@link unwrapRaw}
554
+ *
555
+ * @param op The predicate.
556
+ *
557
+ * @example
558
+ * ```typescript
559
+ * const count = (x: string) => x.length;
560
+ *
561
+ * assert.equal(ok(2).unwrapOrElse(count), 2);
562
+ * assert.equal(err('hello').unwrapOrElse(count), 5);
563
+ * ```
564
+ *
565
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
566
+ */
567
+ unwrapOrElse(op) {
568
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: /* @__PURE__ */ __name((error) => op(error), "err") });
569
+ }
570
+ /**
571
+ * Returns the contained `Ok` value.
572
+ *
573
+ * If the value is an `Err`, it throws the contained error.
574
+ * @seealso {@link unwrap}
575
+ * @seealso {@link unwrapOr}
576
+ * @seealso {@link unwrapOrElse}
577
+ * @seealso {@link unwrapErr}
578
+ *
579
+ * @example
580
+ * ```typescript
581
+ * const x = ok(2);
582
+ * assert.equal(x.unwrapRaw(), 2);
583
+ * ```
584
+ * @example
585
+ * ```typescript
586
+ * const x = err('Emergency failure');
587
+ * assert.throws(() => x.unwrapRaw(), {
588
+ * name: 'Error',
589
+ * message: 'Unwrap failed',
590
+ * value: 'Emergency failure'
591
+ * });
592
+ * ```
593
+ */
106
594
  unwrapRaw() {
107
- return this.value;
108
- }
595
+ if (this.isErr()) throw this[ValueProperty];
596
+ return this[ValueProperty];
597
+ }
598
+ /**
599
+ * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
600
+ * @param result The result to check.
601
+ *
602
+ * @example
603
+ * ```typescript
604
+ * const x: Result<number, string> = ok(2);
605
+ * const y: Result<string, string> = err('Late error');
606
+ * assert.equal(x.and(y), err('Late error'));
607
+ * ```
608
+ * @example
609
+ * ```typescript
610
+ * const x: Result<number, string> = err('Early error');
611
+ * const y: Result<string, string> = err('Late error');
612
+ * assert.equal(x.and(y), err('Early error'));
613
+ * ```
614
+ * @example
615
+ * ```typescript
616
+ * const x: Result<number, string> = ok(2);
617
+ * const y: Result<string, string> = ok('Hello');
618
+ * assert.equal(x.and(y), ok('Hello'));
619
+ * ```
620
+ *
621
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
622
+ */
109
623
  and(result) {
110
- return result;
111
- }
624
+ return this.match({ ok: /* @__PURE__ */ __name(() => result, "ok"), err: returnThis });
625
+ }
626
+ /**
627
+ * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
628
+ *
629
+ * This function can be used for control flow based on `Result` values.
630
+ * @param cb The predicate.
631
+ *
632
+ * @example
633
+ * ```typescript
634
+ * function fractionOf4(value: number) {
635
+ * return value === 0 ? err('overflowed') : ok(4 / value);
636
+ * }
637
+ *
638
+ * assert.equal(ok(2).andThen(fractionOf4), ok(4));
639
+ * assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
640
+ * assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
641
+ * ```
642
+ *
643
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
644
+ */
112
645
  andThen(cb) {
113
- return cb(this.value);
114
- }
115
- or() {
116
- return this;
117
- }
118
- orElse() {
119
- return this;
646
+ return this.match({ ok: /* @__PURE__ */ __name((value) => cb(value), "ok"), err: returnThis });
647
+ }
648
+ /**
649
+ * Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
650
+ *
651
+ * Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended
652
+ * to use {@link orElse}, which is lazily evaluated.
653
+ * @param result The result to check.
654
+ *
655
+ * @example
656
+ * ```typescript
657
+ * const x: Result<number, string> = ok(2);
658
+ * const y: Result<number, string> = err('Late error');
659
+ * assert.equal(x.or(y), ok(2));
660
+ * ```
661
+ * @example
662
+ * ```typescript
663
+ * const x: Result<number, string> = err('Early error');
664
+ * const y: Result<number, string> = ok(2);
665
+ * assert.equal(x.or(y), ok(2));
666
+ * ```
667
+ * @example
668
+ * ```typescript
669
+ * const x: Result<number, string> = err('Early error');
670
+ * const y: Result<number, string> = err('Late error');
671
+ * assert.equal(x.or(y), err('Late error'));
672
+ * ```
673
+ * @example
674
+ * ```typescript
675
+ * const x: Result<number, string> = ok(2);
676
+ * const y: Result<number, string> = ok(100);
677
+ * assert.equal(x.or(y), ok(2));
678
+ * ```
679
+ *
680
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
681
+ */
682
+ or(result) {
683
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name(() => result, "err") });
684
+ }
685
+ /**
686
+ * Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
687
+ *
688
+ * This function can be used for control flow based on result values.
689
+ * @param cb The predicate.
690
+ *
691
+ * @example
692
+ * ```typescript
693
+ * const square = (x: number): Result<number, string> => ok(x * x);
694
+ * const wrapErr = (x: number): Result<number, string> => err(x);
695
+ *
696
+ * assert.equal(ok(2).orElse(square).orElse(square), ok(2));
697
+ * assert.equal(ok(2).orElse(wrapErr).orElse(square), ok(2));
698
+ * assert.equal(err(3).orElse(square).orElse(wrapErr), ok(9));
699
+ * assert.equal(err(3).orElse(wrapErr).orElse(wrapErr), err(3));
700
+ * ```
701
+ *
702
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
703
+ */
704
+ orElse(cb) {
705
+ return this.match({ ok: returnThis, err: /* @__PURE__ */ __name((error) => cb(error), "err") });
120
706
  }
121
707
  contains(value) {
122
- return this.value === value;
123
- }
124
- containsErr() {
125
- return false;
708
+ return this.isOkAnd((inner) => inner === value);
126
709
  }
710
+ containsErr(error) {
711
+ return this.isErrAnd((inner) => inner === error);
712
+ }
713
+ /**
714
+ * Transposes a `Result` of an `Option` into an `Option` of a `Result`.
715
+ *
716
+ * `ok(none)` will be mapped to `none`. `ok(some(v))` and `err(e)` will be mapped to `some(ok(v))` and `some(err(e))`.
717
+ *
718
+ * @example
719
+ * ```typescript
720
+ * const x: Result<Option<number>, Error> = ok(some(5));
721
+ * const y: Option<Result<number, Error>> = some(ok(5));
722
+ * assert.equal(x.transpose(), y);
723
+ * ```
724
+ *
725
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
726
+ */
127
727
  transpose() {
128
- return this.value.match({
129
- some: (value) => createSome(createOk(value)),
130
- none: () => createNone
728
+ return this.match({
729
+ ok: /* @__PURE__ */ __name((value) => value.map((value2) => ok(value2)), "ok"),
730
+ err() {
731
+ return some(this);
732
+ }
131
733
  });
132
734
  }
735
+ /**
736
+ * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
737
+ *
738
+ * @example
739
+ * ```typescript
740
+ * const x: Result<Result<string, number>, number> = ok(ok('Hello'));
741
+ * assert.equal(x.flatten(), ok('Hello'));
742
+ * ```
743
+ * @example
744
+ * ```typescript
745
+ * const x: Result<Result<string, number>, number> = ok(err(6));
746
+ * assert.equal(x.flatten(), err(6));
747
+ * ```
748
+ * @example
749
+ * ```typescript
750
+ * const x: Result<Result<string, number>, number> = err(6);
751
+ * assert.equal(x.flatten(), err(6));
752
+ * ```
753
+ *
754
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
755
+ */
133
756
  flatten() {
134
- return this.value;
135
- }
757
+ return this.match({ ok: /* @__PURE__ */ __name((value) => value, "ok"), err: returnThis });
758
+ }
759
+ /**
760
+ * Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
761
+ *
762
+ * @example
763
+ * ```typescript
764
+ * let x: Result<number, number> = ok(3);
765
+ * assert.equal(x.intoOkOrErr(), 3);
766
+ * ```
767
+ * @example
768
+ * ```typescript
769
+ * let x: Result<number, number> = err(4);
770
+ * assert.equal(x.intoOkOrErr(), 4);
771
+ * ```
772
+ *
773
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
774
+ */
136
775
  intoOkOrErr() {
137
- return this.value;
138
- }
139
- async intoPromise() {
140
- return createOk(await this.value);
776
+ return this[ValueProperty];
777
+ }
778
+ /**
779
+ * Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
780
+ *
781
+ * @example
782
+ * ```typescript
783
+ * let x = ok(Promise.resolve(3));
784
+ * assert.equal(await x.intoPromise(), ok(3));
785
+ * ```
786
+ *
787
+ * @note This is an extension not supported in Rust
788
+ */
789
+ intoPromise() {
790
+ return this.match({
791
+ ok: /* @__PURE__ */ __name(async (value) => ok(await value), "ok"),
792
+ // NOSONAR
793
+ err: /* @__PURE__ */ __name(async (error) => err(await error), "err")
794
+ // NOSONAR
795
+ });
141
796
  }
797
+ /**
798
+ * Checks whether or not `other` equals with self.
799
+ * @param other The other result to compare.
800
+ *
801
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
802
+ */
142
803
  eq(other) {
143
- return other.isOkAnd((value) => this.value === value);
144
- }
804
+ return this.isOk() === other.isOk() && this[ValueProperty] === other[ValueProperty];
805
+ }
806
+ /**
807
+ * Checks whether or not `other` doesn't equal with self.
808
+ * @param other The other result to compare.
809
+ *
810
+ * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
811
+ */
145
812
  ne(other) {
146
813
  return !this.eq(other);
147
814
  }
815
+ /**
816
+ * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
817
+ * @param branches The branches to match.
818
+ *
819
+ * @example
820
+ * ```typescript
821
+ * const result = ok(4).match({
822
+ * ok: (v) => v,
823
+ * err: () => 0
824
+ * });
825
+ * assert.equal(result, 4);
826
+ * ```
827
+ * @example
828
+ * ```typescript
829
+ * const result = err('Hello').match({
830
+ * ok: (v) => v,
831
+ * err: () => 0
832
+ * });
833
+ * assert.equal(result, 0);
834
+ * ```
835
+ */
148
836
  match(branches) {
149
- return branches.ok(this.value);
837
+ return this.isOk() ? branches.ok.call(this, this[ValueProperty]) : branches.err.call(this, this[ValueProperty]);
838
+ }
839
+ /**
840
+ * Returns an iterator over the possibly contained value.
841
+ *
842
+ * The iterator yields one value if the result is `Ok`, otherwise none.
843
+ *
844
+ * @example
845
+ * ```typescript
846
+ * const x = ok(7);
847
+ * for (const value of x) {
848
+ * console.log(value);
849
+ * }
850
+ * // Logs 7
851
+ * ```
852
+ * @example
853
+ * ```typescript
854
+ * const x = err('Nothing!');
855
+ * for (const value of x) {
856
+ * console.log(value);
857
+ * }
858
+ * // Doesn't log
859
+ * ```
860
+ *
861
+ * @see {@link IResult.iter}
862
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
863
+ */
864
+ [(_b = ValueProperty, _a = SuccessProperty, Symbol.iterator)]() {
865
+ return this.iter();
866
+ }
867
+ get [Symbol.toStringTag]() {
868
+ return this.match({ ok: /* @__PURE__ */ __name(() => "Ok", "ok"), err: /* @__PURE__ */ __name(() => "Err", "err") });
869
+ }
870
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
871
+ static ok(value) {
872
+ return new _Result(value, true);
873
+ }
874
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
875
+ static err(value) {
876
+ return new _Result(value, false);
877
+ }
878
+ /**
879
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object. This override
880
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
881
+ * library or from a different build.
882
+ *
883
+ * @param instance The instance to check.
884
+ * @returns Whether or not the instance is a `Result`.
885
+ *
886
+ * @example
887
+ * ```typescript
888
+ * import { Result } from '@sapphire/result';
889
+ * const { ok } = require('@sapphire/result');
890
+ *
891
+ * ok(2) instanceof Result; // true
892
+ * ```
893
+ */
894
+ static [Symbol.hasInstance](instance) {
895
+ return typeof instance === "object" && instance !== null && ValueProperty in instance && SuccessProperty in instance;
896
+ }
897
+ /**
898
+ * @deprecated Use {@link Result.isResult} instead.
899
+ *
900
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
901
+ *
902
+ * @param instance The instance to check.
903
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
904
+ *
905
+ * @example
906
+ * ```typescript
907
+ * import { Result } from '@sapphire/result';
908
+ * const { ok } = require('@sapphire/result');
909
+ *
910
+ * Result.isResult(ok(2)); // true
911
+ * ```
912
+ */
913
+ static is(instance) {
914
+ return _Result[Symbol.hasInstance](instance);
915
+ }
916
+ /**
917
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
918
+ *
919
+ * @param instance The instance to check.
920
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
921
+ *
922
+ * @example
923
+ * ```typescript
924
+ * import { Result } from '@sapphire/result';
925
+ * const { ok } = require('@sapphire/result');
926
+ *
927
+ * Result.isResult(ok(2)); // true
928
+ * ```
929
+ */
930
+ static isResult(instance) {
931
+ return _Result[Symbol.hasInstance](instance);
932
+ }
933
+ /**
934
+ * Creates a {@link Result} out of a callback.
935
+ *
936
+ * @typeparam T The result's type.
937
+ * @typeparam E The error's type.
938
+ */
939
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
940
+ static from(op) {
941
+ try {
942
+ return resolve(isFunction(op) ? op() : op);
943
+ } catch (error) {
944
+ return err(error);
945
+ }
946
+ }
947
+ /**
948
+ * Creates a {@link Result} out of a promise or async callback.
949
+ *
950
+ * @typeparam T The result's type.
951
+ * @typeparam E The error's type.
952
+ */
953
+ static async fromAsync(op) {
954
+ try {
955
+ return resolve(await (isFunction(op) ? op() : op));
956
+ } catch (error) {
957
+ return err(error);
958
+ }
150
959
  }
151
- *[Symbol.iterator]() {
152
- yield this.value;
960
+ /**
961
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
962
+ * {@link Err} encountered.
963
+ *
964
+ * @param results An array of {@link Result}s.
965
+ * @returns A new {@link Result}.
966
+ */
967
+ static all(results) {
968
+ const values = [];
969
+ for (const result of results) {
970
+ if (result.isErr()) return result;
971
+ values.push(result[ValueProperty]);
972
+ }
973
+ return ok(values);
974
+ }
975
+ /**
976
+ * Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
977
+ *
978
+ * @param results An array of {@link Result}s.
979
+ * @returns A new {@link Result}.
980
+ */
981
+ static any(results) {
982
+ const errors = [];
983
+ for (const result of results) {
984
+ if (result.isOk()) return result;
985
+ errors.push(result[ValueProperty]);
986
+ }
987
+ return err(errors);
153
988
  }
154
989
  };
155
- __name(_ResultOk, "ResultOk");
156
- var ResultOk = _ResultOk;
157
- function createOk(x) {
158
- return new ResultOk(x);
990
+ __name(_Result, "Result");
991
+ var Result = _Result;
992
+ var { ok, err } = Result;
993
+ function resolve(value) {
994
+ return Result.isResult(value) ? value : ok(value);
159
995
  }
160
- __name(createOk, "createOk");
996
+ __name(resolve, "resolve");
161
997
 
162
- // src/lib/Option/Some.ts
163
- var _OptionSome = class _OptionSome {
164
- constructor(value) {
165
- __publicField(this, "value");
166
- this.value = value;
167
- }
998
+ // src/lib/Option.ts
999
+ var ValueProperty2 = Symbol.for("@sapphire/result:Option.value");
1000
+ var ExistsProperty = Symbol.for("@sapphire/result:Option.exists");
1001
+ var _a2, _b2;
1002
+ var _Option = class _Option {
1003
+ constructor(value, exists) {
1004
+ __publicField(this, _b2);
1005
+ __publicField(this, _a2);
1006
+ this[ValueProperty2] = value;
1007
+ this[ExistsProperty] = exists;
1008
+ }
1009
+ /**
1010
+ * Returns `true` if the option is a `Some` value.
1011
+ *
1012
+ * @example
1013
+ * ```typescript
1014
+ * const x: Option<number> = some(2);
1015
+ * assert.equal(x.isSome(), true);
1016
+ * ```
1017
+ * @example
1018
+ * ```typescript
1019
+ * const x: Option<number> = none;
1020
+ * assert.equal(x.isSome(), false);
1021
+ * ```
1022
+ *
1023
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
1024
+ */
168
1025
  isSome() {
169
- return true;
1026
+ return this[ExistsProperty];
170
1027
  }
171
1028
  isSomeAnd(cb) {
172
- return cb(this.value);
173
- }
1029
+ return this.isSome() && cb(this[ValueProperty2]);
1030
+ }
1031
+ /**
1032
+ * Returns `true` if the option is a `None` value.
1033
+ *
1034
+ * @example
1035
+ * ```typescript
1036
+ * const x: Option<number> = some(2);
1037
+ * assert.equal(x.isNone(), false);
1038
+ * ```
1039
+ * @example
1040
+ * ```typescript
1041
+ * const x: Option<number> = none;
1042
+ * assert.equal(x.isNone(), true);
1043
+ * ```
1044
+ *
1045
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
1046
+ */
174
1047
  isNone() {
175
- return false;
176
- }
177
- expect() {
178
- return this.value;
179
- }
1048
+ return !this[ExistsProperty];
1049
+ }
1050
+ isNoneOr(cb) {
1051
+ return this.match({ some: /* @__PURE__ */ __name((value) => cb(value), "some"), none: /* @__PURE__ */ __name(() => true, "none") });
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
+ */
180
1101
  unwrap() {
181
- return this.value;
182
- }
183
- unwrapOr() {
184
- return this.value;
185
- }
186
- unwrapOrElse() {
187
- return this.value;
188
- }
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
+ */
189
1156
  map(cb) {
190
- return createSome(cb(this.value));
191
- }
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
+ */
192
1181
  mapInto(cb) {
193
- return cb(this.value);
194
- }
195
- mapOr(_, cb) {
196
- return cb(this.value);
197
- }
198
- mapOrElse(_, cb) {
199
- return cb(this.value);
200
- }
201
- mapNoneInto() {
202
- return this;
203
- }
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
+ */
204
1272
  inspect(cb) {
205
- cb(this.value);
1273
+ if (this.isSome()) cb(this[ValueProperty2]);
206
1274
  return this;
207
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
+ */
208
1294
  async inspectAsync(cb) {
209
- await cb(this.value);
1295
+ if (this.isSome()) await cb(this[ValueProperty2]);
210
1296
  return this;
211
1297
  }
212
- okOr() {
213
- return createOk(this.value);
214
- }
215
- okOrElse() {
216
- return createOk(this.value);
217
- }
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
+ */
218
1366
  *iter() {
219
- yield this.value;
220
- }
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
+ */
221
1400
  and(option) {
222
- return option;
223
- }
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
+ */
224
1422
  andThen(cb) {
225
- return cb(this.value);
226
- }
227
- or() {
228
- return this;
229
- }
230
- orElse() {
231
- return this;
232
- }
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
+ */
233
1511
  xor(option) {
234
- 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
+ });
235
1518
  }
236
1519
  filter(predicate) {
237
- return predicate(this.value) ? this : createNone;
238
- }
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
+ */
239
1544
  contains(value) {
240
- return this.value === value;
241
- }
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
+ */
242
1565
  zip(other) {
243
- return other.map((o) => [this.value, o]);
244
- }
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
+ */
245
1596
  zipWith(other, f) {
246
- return other.map((o) => f(this.value, o));
247
- }
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
+ */
248
1617
  unzip() {
249
- const [s, o] = this.value;
250
- return [createSome(s), createSome(o)];
251
- }
252
- transpose() {
253
- return this.value.match({
254
- ok: (v) => createOk(createSome(v)),
255
- 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")
256
1621
  });
257
1622
  }
258
- flatten() {
259
- return this.value;
260
- }
261
- async intoPromise() {
262
- return createSome(await this.value);
263
- }
264
- eq(other) {
265
- return other.isSomeAnd((value) => this.value === value);
266
- }
267
- ne(other) {
268
- return !this.eq(other);
269
- }
270
- match(branches) {
271
- return branches.some(this.value);
272
- }
273
- *[Symbol.iterator]() {
274
- yield this.value;
275
- }
276
- };
277
- __name(_OptionSome, "OptionSome");
278
- var OptionSome = _OptionSome;
279
- function createSome(value) {
280
- return new OptionSome(value);
281
- }
282
- __name(createSome, "createSome");
283
-
284
- // src/lib/Result/Err.ts
285
- var _ResultErr = class _ResultErr {
286
- constructor(error) {
287
- __publicField(this, "error");
288
- this.error = error;
289
- }
290
- isOk() {
291
- return false;
292
- }
293
- isOkAnd() {
294
- return false;
295
- }
296
- isErr() {
297
- return true;
298
- }
299
- isErrAnd(cb) {
300
- return cb(this.error);
301
- }
302
- ok() {
303
- return createNone;
304
- }
305
- err() {
306
- return createSome(this.error);
307
- }
308
- map() {
309
- return this;
310
- }
311
- mapInto() {
312
- return this;
313
- }
314
- mapOr(defaultValue) {
315
- return defaultValue;
316
- }
317
- mapOrElse(op) {
318
- return op(this.error);
319
- }
320
- mapErr(cb) {
321
- return createErr(cb(this.error));
322
- }
323
- mapErrInto(cb) {
324
- return cb(this.error);
325
- }
326
- inspect() {
327
- return this;
328
- }
329
- inspectAsync() {
330
- return Promise.resolve(this);
331
- }
332
- inspectErr(cb) {
333
- cb(this.error);
334
- return this;
335
- }
336
- async inspectErrAsync(cb) {
337
- await cb(this.error);
338
- return this;
339
- }
340
- *iter() {
341
- }
342
- expect(message) {
343
- throw new ResultError(message, this.error);
344
- }
345
- expectErr() {
346
- return this.error;
347
- }
348
- unwrap() {
349
- throw new ResultError("Unwrap failed", this.error);
350
- }
351
- unwrapErr() {
352
- return this.error;
353
- }
354
- unwrapOr(defaultValue) {
355
- return defaultValue;
356
- }
357
- unwrapOrElse(op) {
358
- return op(this.error);
359
- }
360
- unwrapRaw() {
361
- throw this.error;
362
- }
363
- and() {
364
- return this;
365
- }
366
- andThen() {
367
- return this;
368
- }
369
- or(result) {
370
- return result;
371
- }
372
- orElse(cb) {
373
- return cb(this.error);
374
- }
375
- contains() {
376
- return false;
377
- }
378
- containsErr(error) {
379
- return this.error === error;
380
- }
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
+ */
381
1637
  transpose() {
382
- return createSome(this);
383
- }
384
- flatten() {
385
- return this;
386
- }
387
- intoOkOrErr() {
388
- return this.error;
389
- }
390
- async intoPromise() {
391
- return createErr(await this.error);
392
- }
393
- eq(other) {
394
- return other.isErrAnd((error) => this.error === error);
395
- }
396
- ne(other) {
397
- return !this.eq(other);
398
- }
399
- match(branches) {
400
- return branches.err(this.error);
401
- }
402
- *[Symbol.iterator]() {
403
- }
404
- };
405
- __name(_ResultErr, "ResultErr");
406
- var ResultErr = _ResultErr;
407
- function createErr(x) {
408
- return new ResultErr(x);
409
- }
410
- __name(createErr, "createErr");
411
-
412
- // src/lib/Option/OptionError.ts
413
- var _OptionError = class _OptionError extends Error {
414
- get name() {
415
- return this.constructor.name;
416
- }
417
- };
418
- __name(_OptionError, "OptionError");
419
- var OptionError = _OptionError;
420
-
421
- // src/lib/Option/None.ts
422
- var _OptionNone = class _OptionNone {
423
- isSome() {
424
- return false;
425
- }
426
- isSomeAnd() {
427
- return false;
428
- }
429
- isNone() {
430
- return true;
431
- }
432
- expect(message) {
433
- throw new OptionError(message);
434
- }
435
- unwrap() {
436
- throw new OptionError("Unwrap failed");
437
- }
438
- unwrapOr(defaultValue) {
439
- return defaultValue;
440
- }
441
- unwrapOrElse(cb) {
442
- return cb();
443
- }
444
- map() {
445
- return this;
446
- }
447
- mapInto() {
448
- return this;
449
- }
450
- mapOr(defaultValue) {
451
- return defaultValue;
452
- }
453
- mapOrElse(defaultValue) {
454
- return defaultValue();
455
- }
456
- mapNoneInto(cb) {
457
- return cb();
458
- }
459
- inspect() {
460
- return this;
461
- }
462
- inspectAsync() {
463
- return Promise.resolve(this);
464
- }
465
- okOr(error) {
466
- return createErr(error);
467
- }
468
- okOrElse(cb) {
469
- return createErr(cb());
470
- }
471
- *iter() {
472
- }
473
- and() {
474
- return this;
475
- }
476
- andThen() {
477
- return this;
478
- }
479
- or(option) {
480
- return option;
481
- }
482
- orElse(cb) {
483
- return cb();
484
- }
485
- xor(option) {
486
- return option.isSome() ? option : this;
487
- }
488
- filter() {
489
- return this;
490
- }
491
- contains() {
492
- return false;
493
- }
494
- zip() {
495
- return this;
496
- }
497
- zipWith() {
498
- return this;
499
- }
500
- unzip() {
501
- return [this, this];
502
- }
503
- transpose() {
504
- 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
+ });
505
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
+ */
506
1665
  flatten() {
507
- return this;
508
- }
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
+ */
509
1679
  intoPromise() {
510
- 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
+ });
511
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
+ */
512
1692
  eq(other) {
513
- return other.isNone();
514
- }
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
+ */
515
1701
  ne(other) {
516
- return other.isSome();
1702
+ return !this.eq(other);
517
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
+ */
518
1725
  match(branches) {
519
- return branches.none();
520
- }
521
- *[Symbol.iterator]() {
522
- }
523
- };
524
- __name(_OptionNone, "OptionNone");
525
- var OptionNone = _OptionNone;
526
- var createNone = new OptionNone();
527
-
528
- // src/lib/Option.ts
529
- var Option;
530
- ((Option2) => {
531
- function resolve(value) {
532
- if (value === null || value === void 0)
533
- return Option2.none;
534
- if (is(value))
535
- return value;
536
- return (0, Option2.some)(value);
537
- }
538
- __name(resolve, "resolve");
539
- function is(value) {
540
- return value instanceof OptionNone || value instanceof OptionSome;
541
- }
542
- Option2.is = is;
543
- __name(is, "is");
544
- function from(op) {
545
- if (!isFunction(op))
546
- 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) {
547
1826
  try {
548
- return resolve(op());
1827
+ return resolve2(isFunction(op) ? op() : op);
549
1828
  } catch {
550
- return Option2.none;
1829
+ return none;
551
1830
  }
552
1831
  }
553
- Option2.from = from;
554
- __name(from, "from");
555
- 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) {
556
1840
  try {
557
- return resolve(await (isFunction(op) ? op() : op));
1841
+ return resolve2(await (isFunction(op) ? op() : op));
558
1842
  } catch {
559
- return Option2.none;
560
- }
561
- }
562
- Option2.fromAsync = fromAsync;
563
- __name(fromAsync, "fromAsync");
564
- function all(options) {
565
- const values = [];
566
- for (const option of options) {
567
- if (option.isNone()) {
568
- return option;
569
- }
570
- values.push(option.unwrap());
571
- }
572
- return (0, Option2.some)(values);
573
- }
574
- Option2.all = all;
575
- __name(all, "all");
576
- function any(options) {
577
- for (const result of options) {
578
- if (result.isSome()) {
579
- return result;
580
- }
581
- }
582
- return Option2.none;
583
- }
584
- Option2.any = any;
585
- __name(any, "any");
586
- Option2.none = createNone;
587
- Option2.some = createSome;
588
- })(Option || (Option = {}));
589
-
590
- // src/lib/Result.ts
591
- var Result;
592
- ((Result2) => {
593
- function resolve(value) {
594
- if (is(value))
595
- return value;
596
- return (0, Result2.ok)(value);
597
- }
598
- __name(resolve, "resolve");
599
- function is(value) {
600
- return value instanceof ResultOk || value instanceof ResultErr;
601
- }
602
- Result2.is = is;
603
- __name(is, "is");
604
- function from(op) {
605
- if (!isFunction(op))
606
- return resolve(op);
607
- try {
608
- return resolve(op());
609
- } catch (error) {
610
- return (0, Result2.err)(error);
611
- }
612
- }
613
- Result2.from = from;
614
- __name(from, "from");
615
- async function fromAsync(op) {
616
- try {
617
- return resolve(await (isFunction(op) ? op() : op));
618
- } catch (error) {
619
- return (0, Result2.err)(error);
1843
+ return none;
620
1844
  }
621
1845
  }
622
- Result2.fromAsync = fromAsync;
623
- __name(fromAsync, "fromAsync");
624
- 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) {
625
1855
  const values = [];
626
1856
  for (const result of results) {
627
- if (result.isErr()) {
628
- return result;
629
- }
630
- values.push(result.unwrap());
1857
+ if (result.isNone()) return result;
1858
+ values.push(result[ValueProperty2]);
631
1859
  }
632
- return (0, Result2.ok)(values);
633
- }
634
- Result2.all = all;
635
- __name(all, "all");
636
- function any(results) {
637
- 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) {
638
1870
  for (const result of results) {
639
- if (result.isOk()) {
640
- return result;
641
- }
642
- errors.push(result.unwrapErr());
1871
+ if (result.isSome()) return result;
643
1872
  }
644
- return (0, Result2.err)(errors);
1873
+ return none;
645
1874
  }
646
- Result2.any = any;
647
- __name(any, "any");
648
- Result2.err = createErr;
649
- Result2.ok = createOk;
650
- })(Result || (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");
651
1886
 
652
- export { Option, OptionError, Result, ResultError, createErr as err, createNone as none, createOk as ok, createSome as some };
653
- //# sourceMappingURL=out.js.map
1887
+ export { Option, OptionError, Result, ResultError, err, none, ok, some };
1888
+ //# sourceMappingURL=index.mjs.map
654
1889
  //# sourceMappingURL=index.mjs.map