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

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