1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Either = exports.isUndefined = exports.truth = exports.revoke = exports.identity = void 0;
|
7 |
|
8 | var R = _interopRequireWildcard(require("ramda"));
|
9 |
|
10 | var _isFunction = _interopRequireDefault(require("./isFunction"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
13 |
|
14 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
|
15 |
|
16 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
17 |
|
18 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
19 |
|
20 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
21 |
|
22 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
23 |
|
24 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
25 |
|
26 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
27 |
|
28 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
29 |
|
30 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
31 |
|
32 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
33 |
|
34 | var identity = function identity(v) {
|
35 | return v;
|
36 | };
|
37 |
|
38 | exports.identity = identity;
|
39 |
|
40 | var revoke = function revoke() {
|
41 | return undefined;
|
42 | };
|
43 |
|
44 | exports.revoke = revoke;
|
45 |
|
46 | var truth = function truth(v) {
|
47 | return !!v;
|
48 | };
|
49 |
|
50 | exports.truth = truth;
|
51 |
|
52 | var isUndefined = function isUndefined(v) {
|
53 | return Object.is(v, undefined);
|
54 | };
|
55 |
|
56 | exports.isUndefined = isUndefined;
|
57 |
|
58 | var Either =
|
59 |
|
60 | function () {
|
61 | function Either(args) {
|
62 | _classCallCheck(this, Either);
|
63 |
|
64 | if (!(this instanceof Either ? this.constructor : void 0) || (this instanceof Either ? this.constructor : void 0) === Either) {
|
65 | throw new Error("no instanciation allowed for Abstract Class 'Either'");
|
66 | }
|
67 |
|
68 | this.value = args;
|
69 | }
|
70 |
|
71 | _createClass(Either, [{
|
72 | key: "inspect",
|
73 | value: function inspect(f) {
|
74 | var _inspect = "".concat(this.constructor.name, "(").concat(this.value, ")");
|
75 |
|
76 | Either.of(f, _isFunction["default"]).fold(function () {
|
77 | return f(_inspect);
|
78 | }, function () {
|
79 | return console.log(_inspect);
|
80 | });
|
81 | return this;
|
82 | }
|
83 | }, {
|
84 | key: Symbol.iterator,
|
85 | value:
|
86 |
|
87 | regeneratorRuntime.mark(function value() {
|
88 | return regeneratorRuntime.wrap(function value$(_context) {
|
89 | while (1) {
|
90 | switch (_context.prev = _context.next) {
|
91 | case 0:
|
92 | _context.next = 2;
|
93 | return this.value;
|
94 |
|
95 | case 2:
|
96 | case "end":
|
97 | return _context.stop();
|
98 | }
|
99 | }
|
100 | }, value, this);
|
101 | })
|
102 | }, {
|
103 | key: 'try',
|
104 | value: function _try() {
|
105 | return this;
|
106 | }
|
107 | }, {
|
108 | key: 'throw',
|
109 | value: function _throw() {
|
110 | return this;
|
111 | }
|
112 | }, {
|
113 | key: 'catch',
|
114 | value: function _catch() {
|
115 | return this;
|
116 | }
|
117 | }, {
|
118 | key: 'clone',
|
119 | value: function clone() {
|
120 | return this;
|
121 | }
|
122 | }, {
|
123 | key: 'apply',
|
124 | value: function apply() {
|
125 | return this;
|
126 | }
|
127 | }, {
|
128 | key: "tap",
|
129 | value: function tap() {
|
130 | var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console.log;
|
131 | f(this.value);
|
132 | return this;
|
133 | }
|
134 | }, {
|
135 | key: "of",
|
136 | value: function of() {
|
137 | return this;
|
138 | }
|
139 | }, {
|
140 | key: "take",
|
141 | value: function take() {
|
142 | return this;
|
143 | }
|
144 | }, {
|
145 | key: "map",
|
146 | value: function map() {
|
147 | return this;
|
148 | }
|
149 | }, {
|
150 | key: "chain",
|
151 | value: function chain() {
|
152 | return this;
|
153 | }
|
154 | }, {
|
155 | key: "filter",
|
156 | value: function filter() {
|
157 | return this;
|
158 | }
|
159 | }, {
|
160 | key: "fold",
|
161 | value: function fold() {
|
162 | return this;
|
163 | }
|
164 | }, {
|
165 | key: "done",
|
166 | value: function done() {
|
167 | return this;
|
168 | }
|
169 | }, {
|
170 | key: "doneIf",
|
171 | value: function doneIf() {
|
172 | return this;
|
173 | }
|
174 | }, {
|
175 | key: "throwIf",
|
176 | value: function throwIf() {
|
177 | return this;
|
178 | }
|
179 | }]);
|
180 |
|
181 | return Either;
|
182 | }();
|
183 |
|
184 | exports.Either = Either;
|
185 | ;
|
186 |
|
187 | Either.of = function (v, f) {
|
188 | return (0, _isFunction["default"])(f) ? f(v) ? Either.right(v) : Either.left(v) : Either.right(v);
|
189 | };
|
190 |
|
191 | Either.fromNullable = function (v) {
|
192 | return Either.of(v, truth);
|
193 | };
|
194 |
|
195 | Either.filter = function (f) {
|
196 | var cond = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : truth;
|
197 | var v = f();
|
198 | return cond(v) ? Either.right(v) : Either.left(v);
|
199 | };
|
200 |
|
201 | Either.right = R.curry(function (v) {
|
202 | return new Right(v);
|
203 | });
|
204 | Either.left = R.curry(function (v) {
|
205 | return new Left(v);
|
206 | });
|
207 |
|
208 | Either.done = function (v) {
|
209 | return new Done(Either.right(v));
|
210 | };
|
211 |
|
212 | Either.doneIf = function (f, v) {
|
213 | return f(v) ? Either.done(v) : Either.right(v);
|
214 | };
|
215 |
|
216 | Either["throw"] = function (v) {
|
217 | return new Throw(Either.right(v));
|
218 | };
|
219 |
|
220 | Either.throwIf = function (f, v) {
|
221 | return f(v) ? new Throw(Either.right(v)) : Either.right(v);
|
222 | };
|
223 |
|
224 | Either["try"] = function (f) {
|
225 | try {
|
226 | return Either.right(f());
|
227 | } catch (e) {
|
228 | return Either.left(e);
|
229 | }
|
230 | };
|
231 |
|
232 | var Right =
|
233 |
|
234 | function (_Either) {
|
235 | _inherits(Right, _Either);
|
236 |
|
237 | function Right(args) {
|
238 | _classCallCheck(this, Right);
|
239 |
|
240 | return _possibleConstructorReturn(this, _getPrototypeOf(Right).call(this, args));
|
241 | }
|
242 |
|
243 | _createClass(Right, [{
|
244 | key: 'try',
|
245 | value: function _try(f) {
|
246 | try {
|
247 | return Either.right(f(this.value));
|
248 | } catch (e) {
|
249 | return Either.left(e);
|
250 | }
|
251 | }
|
252 | }, {
|
253 | key: 'throw',
|
254 | value: function _throw() {
|
255 | return Either["throw"](this.value);
|
256 | }
|
257 | }, {
|
258 | key: 'clone',
|
259 | value: function clone(v) {
|
260 | return Either.right(isUndefined(v) ? this.value : v);
|
261 | }
|
262 | }, {
|
263 | key: 'apply',
|
264 | value: function apply(o) {
|
265 | return o.map(this.value);
|
266 | }
|
267 | }, {
|
268 | key: "of",
|
269 | value: function of(v, f) {
|
270 | return (0, _isFunction["default"])(f) ? f(v) ? Either.right(v) : Either.left(v) : Either.right(v);
|
271 | }
|
272 | }, {
|
273 | key: "take",
|
274 | value: function take() {
|
275 | return this.value;
|
276 | }
|
277 | }, {
|
278 | key: "map",
|
279 | value: function map(f) {
|
280 | return Either.right(f(this.value));
|
281 | }
|
282 | }, {
|
283 | key: "chain",
|
284 | value: function chain(f) {
|
285 | return f(this.value);
|
286 | }
|
287 | }, {
|
288 | key: "filter",
|
289 | value: function filter(f) {
|
290 | return Either.of(this.value, f);
|
291 | }
|
292 | }, {
|
293 | key: "fold",
|
294 | value: function fold() {
|
295 | var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
|
296 | var no_use = arguments.length > 1 ? arguments[1] : undefined;
|
297 | return f(this.value);
|
298 | }
|
299 | }, {
|
300 | key: "done",
|
301 | value: function done(v) {
|
302 | return Either.done(isUndefined(v) ? this.value : v);
|
303 | }
|
304 | }, {
|
305 | key: "doneIf",
|
306 | value: function doneIf(f, v) {
|
307 | return f(this.value) ? Either.done(isUndefined(v) ? this.value : v) : this;
|
308 | }
|
309 | }, {
|
310 | key: "throwIf",
|
311 | value: function throwIf(f, v) {
|
312 | return f(this.value) ? Either["throw"](isUndefined(v) ? this.value : v) : this;
|
313 | }
|
314 | }]);
|
315 |
|
316 | return Right;
|
317 | }(Either);
|
318 |
|
319 |
|
320 | var Left =
|
321 |
|
322 | function (_Either2) {
|
323 | _inherits(Left, _Either2);
|
324 |
|
325 | function Left(args) {
|
326 | _classCallCheck(this, Left);
|
327 |
|
328 | return _possibleConstructorReturn(this, _getPrototypeOf(Left).call(this, args));
|
329 | }
|
330 |
|
331 | _createClass(Left, [{
|
332 | key: "take",
|
333 | value: function take() {
|
334 | return this.value;
|
335 | }
|
336 | }, {
|
337 | key: "fold",
|
338 | value: function fold(no_use) {
|
339 | var f = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : identity;
|
340 | return f(this.value);
|
341 | }
|
342 | }]);
|
343 |
|
344 | return Left;
|
345 | }(Either);
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | var Throw =
|
351 |
|
352 | function (_Either3) {
|
353 | _inherits(Throw, _Either3);
|
354 |
|
355 | function Throw(args) {
|
356 | _classCallCheck(this, Throw);
|
357 |
|
358 | if (!(args instanceof Either)) throw new Error("Throw() needs instance of Either");
|
359 | return _possibleConstructorReturn(this, _getPrototypeOf(Throw).call(this, args));
|
360 | }
|
361 |
|
362 | _createClass(Throw, [{
|
363 | key: 'catch',
|
364 | value: function _catch(handler) {
|
365 | return (0, _isFunction["default"])(handler) ? this.value.map(handler) : this.value;
|
366 | }
|
367 | }, {
|
368 | key: "tap",
|
369 | value: function tap() {
|
370 | var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console.log;
|
371 |
|
372 | (function recur(obj) {
|
373 | return obj && obj instanceof Either && obj.value instanceof Either ? recur(obj.value) : f(obj.value);
|
374 | })(this);
|
375 |
|
376 | return this;
|
377 | }
|
378 | }, {
|
379 | key: "inspect",
|
380 | value: function inspect(f) {
|
381 | var _inspect = function recur(obj) {
|
382 | return obj instanceof Either ? "".concat(obj.constructor.name, "(").concat(recur(obj.value), ")") : "".concat(obj);
|
383 | }(this);
|
384 |
|
385 | Either.of(f, _isFunction["default"]).fold(function () {
|
386 | return f(_inspect);
|
387 | }, function () {
|
388 | return console.log(_inspect);
|
389 | });
|
390 | return this;
|
391 | }
|
392 | }]);
|
393 |
|
394 | return Throw;
|
395 | }(Either);
|
396 |
|
397 |
|
398 | var Done =
|
399 |
|
400 | function (_Either4) {
|
401 | _inherits(Done, _Either4);
|
402 |
|
403 | function Done(args) {
|
404 | _classCallCheck(this, Done);
|
405 |
|
406 | if (!(args instanceof Either)) throw new Error("argument must be of Either");
|
407 | return _possibleConstructorReturn(this, _getPrototypeOf(Done).call(this, args));
|
408 | }
|
409 |
|
410 |
|
411 | _createClass(Done, [{
|
412 | key: "tap",
|
413 | value: function tap() {
|
414 | var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console.log;
|
415 |
|
416 | (function recur(obj) {
|
417 | return obj && obj instanceof Either && obj.value instanceof Either ? recur(obj.value) : f(obj.value);
|
418 | })(this);
|
419 |
|
420 | return this;
|
421 | }
|
422 | }, {
|
423 | key: "fold",
|
424 | value: function fold() {
|
425 | var f = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : identity;
|
426 | var g = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : identity;
|
427 | return function recur(obj) {
|
428 | return obj && obj instanceof Either && obj.value instanceof Either ? recur(obj.value) : obj.fold(f, g);
|
429 | }(this);
|
430 | }
|
431 | }, {
|
432 | key: "inspect",
|
433 | value: function inspect(f) {
|
434 | var _inspect = function recur(obj) {
|
435 | return obj instanceof Either ? "".concat(obj.constructor.name, "(").concat(recur(obj.value), ")") : "".concat(obj);
|
436 | }(this);
|
437 |
|
438 | Either.of(f, _isFunction["default"]).fold(function () {
|
439 | return f(_inspect);
|
440 | }, function () {
|
441 | return console.log(_inspect);
|
442 | });
|
443 | return this;
|
444 | }
|
445 | }, {
|
446 | key: "take",
|
447 | value: function take() {
|
448 | return function recur(obj) {
|
449 | return obj && obj instanceof Either && obj.value instanceof Either ? recur(obj.value) : obj.value;
|
450 | }(this);
|
451 | }
|
452 | }]);
|
453 |
|
454 | return Done;
|
455 | }(Either); |
\ | No newline at end of file |