-
Notifications
You must be signed in to change notification settings - Fork 1
/
result.js
219 lines (188 loc) · 5.52 KB
/
result.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const inspect = require("util").inspect;
const Ok = function (t) {
const Result = Object.create(methods);
Result.is_ok = true;
Result.value = t;
return Result;
};
const Fail = function (f) {
const Result = Object.create(methods);
Result.is_ok = false;
Result.value = f;
return Result;
};
const methods = {
ok: function (/*this,*/ errorMessage) {
if (this.is_ok) {
return this.value;
} else {
if (errorMessage) {
throw new TypeError(errorMessage);
} else {
throw new TypeError("Attempted to unwrap Ok(t) but got Fail(f) instead.");
}
}
},
fail: function (/*this,*/ errorMessage) {
if (this.is_ok) {
if (errorMessage) {
throw new TypeError(errorMessage);
} else {
throw new TypeError("Attempted to unwrap Fail(f) but got Ok(t) instead.");
}
} else {
return this.value;
}
},
isOk: function (/*this*/) {
return this.is_ok;
},
isFail: function (/*this*/) {
return !this.is_ok;
},
// (Result<T, F>, T -> T') -> Result<T', F>
map: function (/*this,*/ f) {
if (this.is_ok) {
return Ok(f(this.value));
} else {
return this;
}
},
// (Result<T, F>, F -> F') -> Result<T, F'>
mapFail: function (/*this,*/ f) {
if (this.is_ok) {
return this;
} else {
return Fail(f(this.value));
}
},
// TODO(havvy): When generators are in Node, add .generator(), analogous to .iter()
// (Result<T, F>, A) -> Result<_, F> | A
// where A should := Result<T, F>
and: function (/*this,*/ rhsResult) {
if (this.is_ok) {
return rhsResult;
} else {
return this;
}
},
// (Result<T, F>, A) -> Result<T | _> | A
// where A should := Result<T, F>
or: function (/*this,*/ rhsResult) {
if (this.is_ok) {
return this;
} else {
return rhsResult;
}
},
// (Result<T, F>, FN) -> Result<T, F> | A
// where FN := T -> A
// where A should := Result<T', F>
andThen: function (/*this,*/ f) {
if (this.is_ok) {
return f(this.value);
} else {
return this;
}
},
// (Result<T, F>, FN -> Result<T, F> | A
// where FN := F -> A
// where A should := Result<T, F'>
orElse: function (/*this,*/ f) {
if (this.is_ok) {
return this;
} else {
return f(this.value);
}
},
// (Result<T, F>) -> TList
// where TList := [T]
// such that TList.length === 0 || T.List.length === 1
toArray: function (/*this*/) {
if (this.is_ok) {
return [this.value];
} else {
return [];
}
},
// (Result<T, F>, A) -> T | A
// where A should := T
unwrapOr: function (/*this,*/ defaultValue) {
if (this.is_ok) {
return this.value;
} else {
return defaultValue;
}
},
// (Result<T, F>, F -> A) -> T | A
// where A should := T
unwrapOrElse: function (/*this,*/ defaultFn) {
if (this.is_ok) {
return this.value;
} else {
return defaultFn(this.value);
}
},
// Fn(Result<T, F>, {Ok: function (value: T) -> void, Fail: function (failure: F) -> void}) -> void
match: function (/*this,*/ matchBlock) {
if (this.is_ok) {
return matchBlock.Ok(this.value);
} else {
return matchBlock.Fail(this.value);
}
},
/// https://nodejs.org/api/util.html#util_custom_inspect_function_on_objects
// Fn(Result<T, F>, Number, InspectOpts) -> String
inspect: function (/*this,*/ depth, opts) {
var tag = this.is_ok ? "Ok" : "Fail";
var padding = this.is_ok ? " " : " ";
if (depth < 0) {
return opts.stylize("[" + tag + "]", "boolean");
}
var recurseOpts = {};
Object.keys(opts).forEach(function (key) {
recurseOpts[key] = opts[key];
});
recurseOpts.depth = opts.depth === null ? null : opts.depth - 1;
const inner = inspect(this.value, recurseOpts).replace(/\n/g, "\n" + padding);
return opts.stylize(tag, "boolean") + "( " + inner + " )";
},
// Fn(Result<T, F>, Fn(String) -> void, InspectOpts)
debug: function (/*this,*/ logfn, inspectOpts) {
logfn(inspect(this, inspectOpts));
return this;
},
// Fn(Result<T, F>, Fn(String) -> void, InspectOpts)
debugOk: function(/*this,*/ logfn, inspectOpts) {
if (this.is_ok) {
logfn(inspect(this.value, inspectOpts));
}
return this;
},
// Fn(Result<T, F>, Fn(String) -> void, InspectOpts)
debugFail: function(/*this,*/ logfn, inspectOpts) {
if (!this.is_ok) {
logfn(inspect(this.value, inspectOpts));
}
return this;
}
};
module.exports = {
Ok: Ok,
Fail: Fail,
};
const methodToFunction = function (method) {
return function () {
var context = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
return method.apply(context, args);
};
};
Object.keys(methods).forEach(function (key) {
// NOTE(bbqsrc): Breaks require in repl
if (key === "inspect") {
return;
}
var method = methods[key];
module.exports[key] = methodToFunction(method);
});