-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutcome.mjs
75 lines (66 loc) · 1.15 KB
/
outcome.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
export class Outcome {
static of(result) {
return new Success(result);
}
map(f) {
mustImplement();
}
forEach(f) {
mustImplement();
}
chain(f) {
mustImplement();
}
}
export class Success extends Outcome {
constructor(result) {
super();
this.success = true;
this.result = result;
}
// f: A => B
// ret: Outcome<B>
map(f) {
return new Success(f(this.result));
}
// f: A => void
// ret: Outcome<A>
forEach(f) {
f(this.result);
return this;
}
// f: A => Outcome<B>
// ret: Outcome<B>
chain(f) {
return f(this.result);
}
}
export class Failure extends Outcome {
constructor(error, subErrors) {
super();
this.success = false;
this.result = error;
this.subErrors = subErrors;
}
toString() {
return `Failure { result: ${this.result}, subErrors: ${this.subErrors} }`;
}
// f: A => B
// ret: Outcome<B>
map(f) {
return this;
}
// f: A => void
// ret: Outcome<A>
forEach(f) {
return this;
}
// f: A => Outcome<B>
// ret: Outcome<B>
chain(f) {
return this;
}
}
function mustImplement() {
throw new Error('Must implement');
}