result_option 0.1.0 → 0.1.3

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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Represents a value that may or may not exist.
3
- */
3
+ */
4
4
  export declare class Option<T> {
5
5
  #private;
6
6
  private constructor();
@@ -18,7 +18,7 @@ export declare class Option<T> {
18
18
  * @example
19
19
  * const option = Option.None();
20
20
  */
21
- static None(): Option<never>;
21
+ static None<T>(): Option<T>;
22
22
  /**
23
23
  *
24
24
  * @returns true if the `Option` is a `Some` value.
@@ -74,7 +74,7 @@ export declare class Option<T> {
74
74
  *
75
75
  * const x = Option.None();
76
76
  * const y = x.unwrap_or("bus"); // -> y = "bus"
77
- */
77
+ */
78
78
  unwrap_or(dft: T): T;
79
79
  /**
80
80
  * @param f lambda.
@@ -85,7 +85,7 @@ export declare class Option<T> {
85
85
  *
86
86
  * const x = Option.None();
87
87
  * const y = x.unwrap_or(() => "bus"); // -> y = "bus"
88
- */
88
+ */
89
89
  unwrap_or_else(f: () => T): T;
90
90
  /**
91
91
  *
@@ -502,3 +502,34 @@ export declare class Result<T, E> {
502
502
  Err: (Err: E) => R;
503
503
  }): R;
504
504
  }
505
+ /**
506
+ * Helper function for `Option.Some`.
507
+ * @param value - The value to be `Some`.
508
+ * @returns Option<T> with `Some` = `value`.
509
+ * @example
510
+ * const option = Option.Some(10);
511
+ */
512
+ export declare function Some<T>(value: T): Option<T>;
513
+ /**
514
+ * Helper function for `Option.None`.
515
+ * @returns Option<T> with `None`.
516
+ * @example
517
+ * const option = Option.None();
518
+ */
519
+ export declare function None<T>(): Option<T>;
520
+ /**
521
+ * Helper function for `Result.Ok`.
522
+ * @param value - The value to be `Ok`.
523
+ * @returns `Result<T, E>` with `Ok` = `value`.
524
+ * @example
525
+ * const result = Result.Ok(10);
526
+ */
527
+ export declare function Ok<T, E>(value: T): Result<T, E>;
528
+ /**
529
+ * Helper function for `Result.Err`.
530
+ * @param error - The error to be `Err`.
531
+ * @returns `Result<T, E>` with `Err` = `error`.
532
+ * @example
533
+ * const result = Result.Err("something went wrong");
534
+ */
535
+ export declare function Err<T, E>(error: E): Result<T, E>;
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
12
12
  var _Option_val, _Result_val, _Result_err;
13
13
  /**
14
14
  * Represents a value that may or may not exist.
15
- */
15
+ */
16
16
  export class Option {
17
17
  constructor(value) {
18
18
  _Option_val.set(this, void 0);
@@ -45,7 +45,7 @@ export class Option {
45
45
  * x.is_some(); // -> true
46
46
  */
47
47
  is_some() {
48
- return __classPrivateFieldGet(this, _Option_val, "f") ? true : false;
48
+ return !!__classPrivateFieldGet(this, _Option_val, "f");
49
49
  }
50
50
  /**
51
51
  *
@@ -66,7 +66,7 @@ export class Option {
66
66
  is_some_and(f) {
67
67
  return this.match({
68
68
  Some: f,
69
- None: () => false
69
+ None: () => false,
70
70
  });
71
71
  }
72
72
  /**
@@ -101,7 +101,7 @@ export class Option {
101
101
  Some: (val) => val,
102
102
  None: () => {
103
103
  throw new Error("called Option.unwrap() on a none value");
104
- }
104
+ },
105
105
  });
106
106
  }
107
107
  /**
@@ -113,11 +113,11 @@ export class Option {
113
113
  *
114
114
  * const x = Option.None();
115
115
  * const y = x.unwrap_or("bus"); // -> y = "bus"
116
- */
116
+ */
117
117
  unwrap_or(dft) {
118
118
  return this.match({
119
119
  Some: (val) => val,
120
- None: () => dft
120
+ None: () => dft,
121
121
  });
122
122
  }
123
123
  /**
@@ -129,11 +129,11 @@ export class Option {
129
129
  *
130
130
  * const x = Option.None();
131
131
  * const y = x.unwrap_or(() => "bus"); // -> y = "bus"
132
- */
132
+ */
133
133
  unwrap_or_else(f) {
134
134
  return this.match({
135
135
  Some: (val) => val,
136
- None: f
136
+ None: f,
137
137
  });
138
138
  }
139
139
  /**
@@ -153,7 +153,7 @@ export class Option {
153
153
  Some: (val) => val,
154
154
  None: () => {
155
155
  throw new Error(msg);
156
- }
156
+ },
157
157
  });
158
158
  }
159
159
  /**
@@ -169,7 +169,7 @@ export class Option {
169
169
  map(f) {
170
170
  return this.match({
171
171
  Some: (val) => Option.Some(f(val)),
172
- None: () => Option.None()
172
+ None: () => Option.None(),
173
173
  });
174
174
  }
175
175
  /**
@@ -187,7 +187,7 @@ export class Option {
187
187
  map_or(dft, f) {
188
188
  return this.match({
189
189
  Some: f,
190
- None: () => dft
190
+ None: () => dft,
191
191
  });
192
192
  }
193
193
  /**
@@ -205,7 +205,7 @@ export class Option {
205
205
  map_or_else(dft, f) {
206
206
  return this.match({
207
207
  Some: f,
208
- None: dft
208
+ None: dft,
209
209
  });
210
210
  }
211
211
  /**
@@ -222,7 +222,7 @@ export class Option {
222
222
  ok_or(err) {
223
223
  return this.match({
224
224
  Some: (val) => Result.Ok(val),
225
- None: () => Result.Err(err)
225
+ None: () => Result.Err(err),
226
226
  });
227
227
  }
228
228
  /**
@@ -239,7 +239,7 @@ export class Option {
239
239
  ok_or_else(err) {
240
240
  return this.match({
241
241
  Some: (val) => Result.Ok(val),
242
- None: () => Result.Err(err())
242
+ None: () => Result.Err(err()),
243
243
  });
244
244
  }
245
245
  /**
@@ -258,7 +258,7 @@ export class Option {
258
258
  and(optb) {
259
259
  return this.match({
260
260
  Some: (_) => optb,
261
- None: () => Option.None()
261
+ None: () => Option.None(),
262
262
  });
263
263
  }
264
264
  /**
@@ -296,7 +296,7 @@ export class Option {
296
296
  or(optb) {
297
297
  return this.match({
298
298
  Some: (_) => this,
299
- None: () => optb
299
+ None: () => optb,
300
300
  });
301
301
  }
302
302
  /**
@@ -315,7 +315,7 @@ export class Option {
315
315
  or_else(f) {
316
316
  return this.match({
317
317
  Some: (_) => this,
318
- None: f
318
+ None: f,
319
319
  });
320
320
  }
321
321
  /**
@@ -337,9 +337,7 @@ export class Option {
337
337
  * });
338
338
  */
339
339
  match(cases) {
340
- if (__classPrivateFieldGet(this, _Option_val, "f"))
341
- return cases.Some(__classPrivateFieldGet(this, _Option_val, "f"));
342
- return cases.None();
340
+ return __classPrivateFieldGet(this, _Option_val, "f") === null ? cases.None() : cases.Some(__classPrivateFieldGet(this, _Option_val, "f"));
343
341
  }
344
342
  }
345
343
  _Option_val = new WeakMap();
@@ -378,7 +376,7 @@ export class Result {
378
376
  * x.is_ok(); // -> false
379
377
  */
380
378
  is_ok() {
381
- return __classPrivateFieldGet(this, _Result_val, "f") ? true : false;
379
+ return !!__classPrivateFieldGet(this, _Result_val, "f");
382
380
  }
383
381
  /**
384
382
  * @returns `true` if the `Result` is `Err`.
@@ -404,7 +402,7 @@ export class Result {
404
402
  is_ok_and(f) {
405
403
  return this.match({
406
404
  Ok: f,
407
- Err: (_) => false
405
+ Err: (_) => false,
408
406
  });
409
407
  }
410
408
  /**
@@ -419,7 +417,7 @@ export class Result {
419
417
  is_err_and(f) {
420
418
  return this.match({
421
419
  Ok: (_) => false,
422
- Err: f
420
+ Err: f,
423
421
  });
424
422
  }
425
423
  /**
@@ -434,7 +432,7 @@ export class Result {
434
432
  ok() {
435
433
  return this.match({
436
434
  Ok: (x) => Option.Some(x),
437
- Err: (_) => Option.None()
435
+ Err: (_) => Option.None(),
438
436
  });
439
437
  }
440
438
  /**
@@ -449,7 +447,7 @@ export class Result {
449
447
  err() {
450
448
  return this.match({
451
449
  Ok: (_) => Option.None(),
452
- Err: (x) => Option.Some(x)
450
+ Err: (x) => Option.Some(x),
453
451
  });
454
452
  }
455
453
  /**
@@ -465,7 +463,7 @@ export class Result {
465
463
  map(f) {
466
464
  return this.match({
467
465
  Ok: (t) => Result.Ok(f(t)),
468
- Err: (e) => Result.Err(e)
466
+ Err: (e) => Result.Err(e),
469
467
  });
470
468
  }
471
469
  /**
@@ -482,7 +480,7 @@ export class Result {
482
480
  map_or(dft, f) {
483
481
  return this.match({
484
482
  Ok: f,
485
- Err: (_) => dft
483
+ Err: (_) => dft,
486
484
  });
487
485
  }
488
486
  /**
@@ -499,7 +497,7 @@ export class Result {
499
497
  map_or_else(dft, f) {
500
498
  return this.match({
501
499
  Ok: f,
502
- Err: dft
500
+ Err: dft,
503
501
  });
504
502
  }
505
503
  /**
@@ -515,7 +513,7 @@ export class Result {
515
513
  map_err(op) {
516
514
  return this.match({
517
515
  Ok: (val) => Result.Ok(val),
518
- Err: (e) => Result.Err(op(e))
516
+ Err: (e) => Result.Err(op(e)),
519
517
  });
520
518
  }
521
519
  /**
@@ -533,7 +531,7 @@ export class Result {
533
531
  Ok: (t) => t,
534
532
  Err: (e) => {
535
533
  throw new Error(`called Result.unwrap() on an Err value: ${e}`);
536
- }
534
+ },
537
535
  });
538
536
  }
539
537
  /**
@@ -565,7 +563,7 @@ export class Result {
565
563
  unwrap_or_else(op) {
566
564
  return this.match({
567
565
  Ok: (t) => t,
568
- Err: op
566
+ Err: op,
569
567
  });
570
568
  }
571
569
  /**
@@ -602,7 +600,7 @@ export class Result {
602
600
  Ok: (t) => t,
603
601
  Err: (e) => {
604
602
  throw new Error(`${msg}: ${e}`);
605
- }
603
+ },
606
604
  });
607
605
  }
608
606
  /**
@@ -639,7 +637,7 @@ export class Result {
639
637
  and(res) {
640
638
  return this.match({
641
639
  Ok: (_) => res,
642
- Err: (e) => Result.Err(e)
640
+ Err: (e) => Result.Err(e),
643
641
  });
644
642
  }
645
643
  /**
@@ -655,7 +653,7 @@ export class Result {
655
653
  and_then(op) {
656
654
  return this.match({
657
655
  Ok: op,
658
- Err: (e) => Result.Err(e)
656
+ Err: (e) => Result.Err(e),
659
657
  });
660
658
  }
661
659
  /**
@@ -673,7 +671,7 @@ export class Result {
673
671
  or(res) {
674
672
  return this.match({
675
673
  Ok: (v) => Result.Ok(v),
676
- Err: (_) => res
674
+ Err: (_) => res,
677
675
  });
678
676
  }
679
677
  /**
@@ -689,7 +687,7 @@ export class Result {
689
687
  or_else(op) {
690
688
  return this.match({
691
689
  Ok: (t) => Result.Ok(t),
692
- Err: op
690
+ Err: op,
693
691
  });
694
692
  }
695
693
  /**
@@ -711,9 +709,47 @@ export class Result {
711
709
  * });
712
710
  */
713
711
  match(cases) {
714
- if (__classPrivateFieldGet(this, _Result_val, "f"))
715
- return cases.Ok(__classPrivateFieldGet(this, _Result_val, "f"));
716
- return cases.Err(__classPrivateFieldGet(this, _Result_err, "f"));
712
+ // biome-ignore lint/style/noNonNullAssertion: <Should not happen ever.>
713
+ return __classPrivateFieldGet(this, _Result_val, "f") === null ? cases.Err(__classPrivateFieldGet(this, _Result_err, "f")) : cases.Ok(__classPrivateFieldGet(this, _Result_val, "f"));
717
714
  }
718
715
  }
719
716
  _Result_val = new WeakMap(), _Result_err = new WeakMap();
717
+ /**
718
+ * Helper function for `Option.Some`.
719
+ * @param value - The value to be `Some`.
720
+ * @returns Option<T> with `Some` = `value`.
721
+ * @example
722
+ * const option = Option.Some(10);
723
+ */
724
+ export function Some(value) {
725
+ return Option.Some(value);
726
+ }
727
+ /**
728
+ * Helper function for `Option.None`.
729
+ * @returns Option<T> with `None`.
730
+ * @example
731
+ * const option = Option.None();
732
+ */
733
+ export function None() {
734
+ return Option.None();
735
+ }
736
+ /**
737
+ * Helper function for `Result.Ok`.
738
+ * @param value - The value to be `Ok`.
739
+ * @returns `Result<T, E>` with `Ok` = `value`.
740
+ * @example
741
+ * const result = Result.Ok(10);
742
+ */
743
+ export function Ok(value) {
744
+ return Result.Ok(value);
745
+ }
746
+ /**
747
+ * Helper function for `Result.Err`.
748
+ * @param error - The error to be `Err`.
749
+ * @returns `Result<T, E>` with `Err` = `error`.
750
+ * @example
751
+ * const result = Result.Err("something went wrong");
752
+ */
753
+ export function Err(error) {
754
+ return Result.Err(error);
755
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,373 @@
1
+ // src/index.test.ts
2
+ import { describe, expect, it } from "vitest";
3
+ import { Option, Result } from "./index";
4
+ // ==================== Option ====================
5
+ describe("Option.Some", () => {
6
+ it("should create a Some value", () => {
7
+ expect(Option.Some(42).is_some()).toBe(true);
8
+ });
9
+ it("should not be None", () => {
10
+ expect(Option.Some(42).is_none()).toBe(false);
11
+ });
12
+ });
13
+ describe("Option.None", () => {
14
+ it("should create a None value", () => {
15
+ expect(Option.None().is_none()).toBe(true);
16
+ });
17
+ it("should not be Some", () => {
18
+ expect(Option.None().is_some()).toBe(false);
19
+ });
20
+ });
21
+ describe("Option.is_some_and", () => {
22
+ it("should return true if Some and predicate matches", () => {
23
+ expect(Option.Some(10).is_some_and((x) => x > 5)).toBe(true);
24
+ });
25
+ it("should return false if Some and predicate does not match", () => {
26
+ expect(Option.Some(10).is_some_and((x) => x > 20)).toBe(false);
27
+ });
28
+ it("should return false if None", () => {
29
+ expect(Option.None().is_some_and((x) => x > 5)).toBe(false);
30
+ });
31
+ });
32
+ describe("Option.is_none_or", () => {
33
+ it("should return true if None", () => {
34
+ expect(Option.None().is_none_or((x) => x > 5)).toBe(true);
35
+ });
36
+ it("should return true if Some and predicate matches", () => {
37
+ expect(Option.Some(10).is_none_or((x) => x > 5)).toBe(true);
38
+ });
39
+ it("should return false if Some and predicate does not match", () => {
40
+ expect(Option.Some(10).is_none_or((x) => x > 20)).toBe(false);
41
+ });
42
+ });
43
+ describe("Option.unwrap", () => {
44
+ it("should return the value if Some", () => {
45
+ expect(Option.Some(42).unwrap()).toBe(42);
46
+ });
47
+ it("should throw if None", () => {
48
+ expect(() => Option.None().unwrap()).toThrow();
49
+ });
50
+ });
51
+ describe("Option.unwrap_or", () => {
52
+ it("should return the value if Some", () => {
53
+ expect(Option.Some(42).unwrap_or(0)).toBe(42);
54
+ });
55
+ it("should return the default if None", () => {
56
+ expect(Option.None().unwrap_or(0)).toBe(0);
57
+ });
58
+ });
59
+ describe("Option.unwrap_or_else", () => {
60
+ it("should return the value if Some", () => {
61
+ expect(Option.Some(42).unwrap_or_else(() => 0)).toBe(42);
62
+ });
63
+ it("should call the lambda and return its result if None", () => {
64
+ expect(Option.None().unwrap_or_else(() => 0)).toBe(0);
65
+ });
66
+ });
67
+ describe("Option.expect", () => {
68
+ it("should return the value if Some", () => {
69
+ expect(Option.Some(42).expect("error")).toBe(42);
70
+ });
71
+ it("should throw with the message if None", () => {
72
+ expect(() => Option.None().expect("my message")).toThrow("my message");
73
+ });
74
+ });
75
+ describe("Option.map", () => {
76
+ it("should apply the lambda if Some", () => {
77
+ expect(Option.Some(10)
78
+ .map((x) => x * 2)
79
+ .unwrap()).toBe(20);
80
+ });
81
+ it("should return None if None", () => {
82
+ expect(Option.None()
83
+ .map((x) => x * 2)
84
+ .is_none()).toBe(true);
85
+ });
86
+ });
87
+ describe("Option.map_or", () => {
88
+ it("should apply the lambda if Some", () => {
89
+ expect(Option.Some(10).map_or(0, (x) => x * 2)).toBe(20);
90
+ });
91
+ it("should return the default if None", () => {
92
+ expect(Option.None().map_or(0, (x) => x * 2)).toBe(0);
93
+ });
94
+ });
95
+ describe("Option.map_or_else", () => {
96
+ it("should apply f if Some", () => {
97
+ expect(Option.Some(10).map_or_else(() => 0, (x) => x * 2)).toBe(20);
98
+ });
99
+ it("should call dft if None", () => {
100
+ expect(Option.None().map_or_else(() => 0, (x) => x * 2)).toBe(0);
101
+ });
102
+ });
103
+ describe("Option.ok_or", () => {
104
+ it("should return Ok if Some", () => {
105
+ expect(Option.Some("foo").ok_or("error").unwrap()).toBe("foo");
106
+ });
107
+ it("should return Err if None", () => {
108
+ expect(Option.None().ok_or("error").unwrap_err()).toBe("error");
109
+ });
110
+ });
111
+ describe("Option.ok_or_else", () => {
112
+ it("should return Ok if Some", () => {
113
+ expect(Option.Some("foo")
114
+ .ok_or_else(() => "error")
115
+ .unwrap()).toBe("foo");
116
+ });
117
+ it("should return Err if None", () => {
118
+ expect(Option.None()
119
+ .ok_or_else(() => "error")
120
+ .unwrap_err()).toBe("error");
121
+ });
122
+ });
123
+ describe("Option.and", () => {
124
+ it("should return optb if Some", () => {
125
+ expect(Option.Some("hello").and(Option.Some(3)).unwrap()).toBe(3);
126
+ });
127
+ it("should return None if None", () => {
128
+ expect(Option.None().and(Option.Some(3)).is_none()).toBe(true);
129
+ });
130
+ });
131
+ describe("Option.and_then", () => {
132
+ it("should call f and return the result if Some", () => {
133
+ expect(Option.Some(10)
134
+ .and_then((x) => Option.Some(x * 2))
135
+ .unwrap()).toBe(20);
136
+ });
137
+ it("should return None if None", () => {
138
+ expect(Option.None()
139
+ .and_then((x) => Option.Some(x * 2))
140
+ .is_none()).toBe(true);
141
+ });
142
+ });
143
+ describe("Option.or", () => {
144
+ it("should return self if Some", () => {
145
+ expect(Option.Some(2).or(Option.None()).unwrap()).toBe(2);
146
+ });
147
+ it("should return optb if None", () => {
148
+ expect(Option.None().or(Option.Some(100)).unwrap()).toBe(100);
149
+ });
150
+ });
151
+ describe("Option.or_else", () => {
152
+ it("should return self if Some", () => {
153
+ expect(Option.Some(2)
154
+ .or_else(() => Option.Some(100))
155
+ .unwrap()).toBe(2);
156
+ });
157
+ it("should call f and return result if None", () => {
158
+ expect(Option.None()
159
+ .or_else(() => Option.Some(100))
160
+ .unwrap()).toBe(100);
161
+ });
162
+ });
163
+ describe("Option.match", () => {
164
+ it("should call Some arm if Some", () => {
165
+ expect(Option.Some(42).match({
166
+ Some: (x) => `got ${x}`,
167
+ None: () => "nothing",
168
+ })).toBe("got 42");
169
+ });
170
+ it("should call None arm if None", () => {
171
+ expect(Option.None().match({
172
+ Some: (x) => `got ${x}`,
173
+ None: () => "nothing",
174
+ })).toBe("nothing");
175
+ });
176
+ });
177
+ // ==================== Result ====================
178
+ describe("Result.Ok", () => {
179
+ it("should create an Ok value", () => {
180
+ expect(Result.Ok(42).is_ok()).toBe(true);
181
+ });
182
+ it("should not be Err", () => {
183
+ expect(Result.Ok(42).is_err()).toBe(false);
184
+ });
185
+ });
186
+ describe("Result.Err", () => {
187
+ it("should create an Err value", () => {
188
+ expect(Result.Err("oops").is_err()).toBe(true);
189
+ });
190
+ it("should not be Ok", () => {
191
+ expect(Result.Err("oops").is_ok()).toBe(false);
192
+ });
193
+ });
194
+ describe("Result.is_ok_and", () => {
195
+ it("should return true if Ok and predicate matches", () => {
196
+ expect(Result.Ok(10).is_ok_and((x) => x > 5)).toBe(true);
197
+ });
198
+ it("should return false if Ok and predicate does not match", () => {
199
+ expect(Result.Ok(10).is_ok_and((x) => x > 20)).toBe(false);
200
+ });
201
+ it("should return false if Err", () => {
202
+ expect(Result.Err("oops").is_ok_and((x) => x > 5)).toBe(false);
203
+ });
204
+ });
205
+ describe("Result.is_err_and", () => {
206
+ it("should return true if Err and predicate matches", () => {
207
+ expect(Result.Err("oops").is_err_and((e) => e === "oops")).toBe(true);
208
+ });
209
+ it("should return false if Err and predicate does not match", () => {
210
+ expect(Result.Err("oops").is_err_and((e) => e === "other")).toBe(false);
211
+ });
212
+ it("should return false if Ok", () => {
213
+ expect(Result.Ok(10).is_err_and((e) => e === "oops")).toBe(false);
214
+ });
215
+ });
216
+ describe("Result.ok", () => {
217
+ it("should return Some if Ok", () => {
218
+ expect(Result.Ok("hello").ok().unwrap()).toBe("hello");
219
+ });
220
+ it("should return None if Err", () => {
221
+ expect(Result.Err("error").ok().is_none()).toBe(true);
222
+ });
223
+ });
224
+ describe("Result.err", () => {
225
+ it("should return Some if Err", () => {
226
+ expect(Result.Err("oops").err().unwrap()).toBe("oops");
227
+ });
228
+ it("should return None if Ok", () => {
229
+ expect(Result.Ok(10).err().is_none()).toBe(true);
230
+ });
231
+ });
232
+ describe("Result.unwrap", () => {
233
+ it("should return the value if Ok", () => {
234
+ expect(Result.Ok(42).unwrap()).toBe(42);
235
+ });
236
+ it("should throw if Err", () => {
237
+ expect(() => Result.Err("oops").unwrap()).toThrow();
238
+ });
239
+ });
240
+ describe("Result.unwrap_or", () => {
241
+ it("should return the value if Ok", () => {
242
+ expect(Result.Ok(42).unwrap_or(0)).toBe(42);
243
+ });
244
+ it("should return the default if Err", () => {
245
+ expect(Result.Err("oops").unwrap_or(0)).toBe(0);
246
+ });
247
+ });
248
+ describe("Result.unwrap_or_else", () => {
249
+ it("should return the value if Ok", () => {
250
+ expect(Result.Ok(42).unwrap_or_else(() => 0)).toBe(42);
251
+ });
252
+ it("should call op and return its result if Err", () => {
253
+ expect(Result.Err("oops").unwrap_or_else(() => 0)).toBe(0);
254
+ });
255
+ });
256
+ describe("Result.unwrap_err", () => {
257
+ it("should return the error if Err", () => {
258
+ expect(Result.Err("oops").unwrap_err()).toBe("oops");
259
+ });
260
+ it("should throw if Ok", () => {
261
+ expect(() => Result.Ok(42).unwrap_err()).toThrow();
262
+ });
263
+ });
264
+ describe("Result.expect", () => {
265
+ it("should return the value if Ok", () => {
266
+ expect(Result.Ok(42).expect("error")).toBe(42);
267
+ });
268
+ it("should throw with the message if Err", () => {
269
+ expect(() => Result.Err("oops").expect("my message")).toThrow("my message");
270
+ });
271
+ });
272
+ describe("Result.expect_err", () => {
273
+ it("should return the error if Err", () => {
274
+ expect(Result.Err("oops").expect_err("error")).toBe("oops");
275
+ });
276
+ it("should throw with the message if Ok", () => {
277
+ expect(() => Result.Ok(42).expect_err("my message")).toThrow("my message");
278
+ });
279
+ });
280
+ describe("Result.map", () => {
281
+ it("should apply f if Ok", () => {
282
+ expect(Result.Ok(10)
283
+ .map((x) => x * 2)
284
+ .unwrap()).toBe(20);
285
+ });
286
+ it("should return Err untouched if Err", () => {
287
+ expect(Result.Err("oops")
288
+ .map((x) => x * 2)
289
+ .unwrap_err()).toBe("oops");
290
+ });
291
+ });
292
+ describe("Result.map_or", () => {
293
+ it("should apply f if Ok", () => {
294
+ expect(Result.Ok(10).map_or(0, (x) => x * 2)).toBe(20);
295
+ });
296
+ it("should return the default if Err", () => {
297
+ expect(Result.Err("oops").map_or(0, (x) => x * 2)).toBe(0);
298
+ });
299
+ });
300
+ describe("Result.map_or_else", () => {
301
+ it("should apply f if Ok", () => {
302
+ expect(Result.Ok(10).map_or_else(() => 0, (x) => x * 2)).toBe(20);
303
+ });
304
+ it("should call dft if Err", () => {
305
+ expect(Result.Err("oops").map_or_else(() => 0, (x) => x * 2)).toBe(0);
306
+ });
307
+ });
308
+ describe("Result.map_err", () => {
309
+ it("should apply op to the error if Err", () => {
310
+ expect(Result.Err("oops")
311
+ .map_err((e) => e.length)
312
+ .unwrap_err()).toBe(4);
313
+ });
314
+ it("should return Ok untouched if Ok", () => {
315
+ expect(Result.Ok(10)
316
+ .map_err((e) => e.length)
317
+ .unwrap()).toBe(10);
318
+ });
319
+ });
320
+ describe("Result.and", () => {
321
+ it("should return res if Ok", () => {
322
+ expect(Result.Ok(10).and(Result.Ok(20)).unwrap()).toBe(20);
323
+ });
324
+ it("should return Err if Err", () => {
325
+ expect(Result.Err("oops").and(Result.Ok(20)).unwrap_err()).toBe("oops");
326
+ });
327
+ });
328
+ describe("Result.and_then", () => {
329
+ it("should call op and return its result if Ok", () => {
330
+ expect(Result.Ok(10)
331
+ .and_then((x) => Result.Ok(x * 2))
332
+ .unwrap()).toBe(20);
333
+ });
334
+ it("should return Err if Err", () => {
335
+ expect(Result.Err("oops")
336
+ .and_then((x) => Result.Ok(x * 2))
337
+ .unwrap_err()).toBe("oops");
338
+ });
339
+ });
340
+ describe("Result.or", () => {
341
+ it("should return self if Ok", () => {
342
+ expect(Result.Ok(10).or(Result.Err("late")).unwrap()).toBe(10);
343
+ });
344
+ it("should return res if Err", () => {
345
+ expect(Result.Err("oops").or(Result.Ok(0)).unwrap()).toBe(0);
346
+ });
347
+ });
348
+ describe("Result.or_else", () => {
349
+ it("should return self if Ok", () => {
350
+ expect(Result.Ok(10)
351
+ .or_else(() => Result.Ok(0))
352
+ .unwrap()).toBe(10);
353
+ });
354
+ it("should call op and return its result if Err", () => {
355
+ expect(Result.Err("oops")
356
+ .or_else(() => Result.Ok(0))
357
+ .unwrap()).toBe(0);
358
+ });
359
+ });
360
+ describe("Result.match", () => {
361
+ it("should call Ok arm if Ok", () => {
362
+ expect(Result.Ok(42).match({
363
+ Ok: (x) => `got ${x}`,
364
+ Err: (e) => `error: ${e}`,
365
+ })).toBe("got 42");
366
+ });
367
+ it("should call Err arm if Err", () => {
368
+ expect(Result.Err("oops").match({
369
+ Ok: (x) => `got ${x}`,
370
+ Err: (e) => `error: ${e}`,
371
+ })).toBe("error: oops");
372
+ });
373
+ });
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "result_option",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Result and Option types for typescript.",
5
5
  "main": "./dist/index.ts",
6
6
  "module": "./dist/index.mts",
7
7
  "types": "./dist/index.d.ts",
8
+ "repository": "https://github.com/Cunha-Renato/result_option",
8
9
  "files": [
9
- "dist"
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE.APACHE",
13
+ "LICENSE.MIT"
10
14
  ],
11
15
  "keywords": [
12
16
  "Result",
@@ -14,7 +18,7 @@
14
18
  "Type"
15
19
  ],
16
20
  "author": "Renato Cunha",
17
- "license": "ISC",
21
+ "license": "MIT or Apache-2.0",
18
22
  "dependencies": {
19
23
  "typescript": "^6.0.2"
20
24
  },