1 | function isValueObject(ref) {
|
2 | return !!(ref && typeof ref.equals === "function" && typeof ref.hashCode === "function");
|
3 | }
|
4 |
|
5 | function is(lh, rh) {
|
6 | if (lh === rh || lh !== lh && rh !== rh) {
|
7 | return true;
|
8 | }
|
9 | if (!lh || !rh) {
|
10 | return false;
|
11 | }
|
12 |
|
13 | if (typeof lh.valueOf === "function" && typeof rh.valueOf === "function") {
|
14 | var lh2 = lh.valueOf();
|
15 | var rh2 = rh.valueOf();
|
16 | if (lh2 === rh2 || lh2 !== lh2 && rh2 !== rh2) {
|
17 | return true;
|
18 | }
|
19 | if (!lh2 || !rh2) {
|
20 | return false;
|
21 | }
|
22 | }
|
23 |
|
24 | return !!(isValueObject(lh) && lh.equals(rh));
|
25 | }
|
26 |
|
27 | function equals(lh, rh) {
|
28 | return is(lh, rh);
|
29 | }
|
30 |
|
31 | var universalSetoid = { equals: equals };
|
32 |
|
33 | function hashCode(ref) {
|
34 | if (typeof ref === "number") {
|
35 | return ref & ref;
|
36 | }
|
37 |
|
38 | if (typeof ref.valueOf === "function") {
|
39 | var v = ref.valueOf();
|
40 | if (v !== ref) return hashCode(v);
|
41 | }
|
42 | if (isValueObject(ref)) {
|
43 | return ref.hashCode();
|
44 | }
|
45 | return hashCodeOfString(String(ref));
|
46 | }
|
47 |
|
48 | function hashCodeOfString(str) {
|
49 | var hash = 0;
|
50 |
|
51 | if (str == null || str.length === 0) return hash;
|
52 | for (var i = 0; i < str.length; i++) {
|
53 | var character = str.charCodeAt(i);
|
54 | hash = (hash << 5) - hash + character;
|
55 | hash = hash & hash;
|
56 | }
|
57 | return hash;
|
58 | }
|
59 |
|
60 | function id(a) {
|
61 | return a;
|
62 | }
|
63 |
|
64 | function applyMixins(derivedCtor, baseCtors) {
|
65 | baseCtors.forEach(function (baseCtor) {
|
66 | Object.getOwnPropertyNames(baseCtor.prototype).forEach(function (name) {
|
67 | if (!derivedCtor.prototype[name]) derivedCtor.prototype[name] = baseCtor.prototype[name];
|
68 | });
|
69 | });
|
70 | }
|
71 |
|
72 | var classCallCheck = function (instance, Constructor) {
|
73 | if (!(instance instanceof Constructor)) {
|
74 | throw new TypeError("Cannot call a class as a function");
|
75 | }
|
76 | };
|
77 |
|
78 | var createClass = function () {
|
79 | function defineProperties(target, props) {
|
80 | for (var i = 0; i < props.length; i++) {
|
81 | var descriptor = props[i];
|
82 | descriptor.enumerable = descriptor.enumerable || false;
|
83 | descriptor.configurable = true;
|
84 | if ("value" in descriptor) descriptor.writable = true;
|
85 | Object.defineProperty(target, descriptor.key, descriptor);
|
86 | }
|
87 | }
|
88 |
|
89 | return function (Constructor, protoProps, staticProps) {
|
90 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
91 | if (staticProps) defineProperties(Constructor, staticProps);
|
92 | return Constructor;
|
93 | };
|
94 | }();
|
95 |
|
96 | var inherits = function (subClass, superClass) {
|
97 | if (typeof superClass !== "function" && superClass !== null) {
|
98 | throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
99 | }
|
100 |
|
101 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
102 | constructor: {
|
103 | value: subClass,
|
104 | enumerable: false,
|
105 | writable: true,
|
106 | configurable: true
|
107 | }
|
108 | });
|
109 | if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
110 | };
|
111 |
|
112 | var possibleConstructorReturn = function (self, call) {
|
113 | if (!self) {
|
114 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
115 | }
|
116 |
|
117 | return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
118 | };
|
119 |
|
120 | var CompositeError = function (_Error) {
|
121 | inherits(CompositeError, _Error);
|
122 |
|
123 | function CompositeError(errors) {
|
124 | classCallCheck(this, CompositeError);
|
125 |
|
126 | var reasons = "";
|
127 | var _iteratorNormalCompletion = true;
|
128 | var _didIteratorError = false;
|
129 | var _iteratorError = undefined;
|
130 |
|
131 | try {
|
132 | for (var _iterator = errors.slice(0, 2)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
133 | var e = _step.value;
|
134 |
|
135 | var message = "";
|
136 | if (e instanceof Error) {
|
137 | message = e.name + "(" + e.message + ")";
|
138 | } else {
|
139 | message = "" + e;
|
140 | }
|
141 | reasons += ", " + message;
|
142 | }
|
143 | } catch (err) {
|
144 | _didIteratorError = true;
|
145 | _iteratorError = err;
|
146 | } finally {
|
147 | try {
|
148 | if (!_iteratorNormalCompletion && _iterator.return) {
|
149 | _iterator.return();
|
150 | }
|
151 | } finally {
|
152 | if (_didIteratorError) {
|
153 | throw _iteratorError;
|
154 | }
|
155 | }
|
156 | }
|
157 |
|
158 | reasons = reasons.slice(2);
|
159 | if (errors.length > 2) reasons = reasons + ", ...";
|
160 |
|
161 | var _this = possibleConstructorReturn(this, (CompositeError.__proto__ || Object.getPrototypeOf(CompositeError)).call(this, reasons));
|
162 |
|
163 | _this.name = "CompositeError";
|
164 | _this.errorsRef = errors;
|
165 |
|
166 | var self = _this;
|
167 | self.constructor = CompositeError;
|
168 | self.__proto__ = CompositeError.prototype;
|
169 | return _this;
|
170 | }
|
171 |
|
172 | createClass(CompositeError, [{
|
173 | key: "errors",
|
174 | value: function errors() {
|
175 | return this.errorsRef.slice();
|
176 | }
|
177 | }]);
|
178 | return CompositeError;
|
179 | }(Error);
|
180 |
|
181 | var DummyError = function (_Error2) {
|
182 | inherits(DummyError, _Error2);
|
183 |
|
184 | function DummyError(message) {
|
185 | classCallCheck(this, DummyError);
|
186 |
|
187 | var _this2 = possibleConstructorReturn(this, (DummyError.__proto__ || Object.getPrototypeOf(DummyError)).call(this, message));
|
188 |
|
189 | _this2.name = "DummyError";
|
190 |
|
191 | var self = _this2;
|
192 | self.constructor = DummyError;
|
193 | self.__proto__ = DummyError.prototype;
|
194 | return _this2;
|
195 | }
|
196 |
|
197 | return DummyError;
|
198 | }(Error);
|
199 |
|
200 | var NoSuchElementError = function (_Error3) {
|
201 | inherits(NoSuchElementError, _Error3);
|
202 |
|
203 | function NoSuchElementError(message) {
|
204 | classCallCheck(this, NoSuchElementError);
|
205 |
|
206 | var _this3 = possibleConstructorReturn(this, (NoSuchElementError.__proto__ || Object.getPrototypeOf(NoSuchElementError)).call(this, message));
|
207 |
|
208 | _this3.name = "NoSuchElementError";
|
209 |
|
210 | var self = _this3;
|
211 | self.constructor = NoSuchElementError;
|
212 | self.__proto__ = NoSuchElementError.prototype;
|
213 | return _this3;
|
214 | }
|
215 |
|
216 | return NoSuchElementError;
|
217 | }(Error);
|
218 |
|
219 | var IllegalInheritanceError = function (_Error4) {
|
220 | inherits(IllegalInheritanceError, _Error4);
|
221 |
|
222 | function IllegalInheritanceError(message) {
|
223 | classCallCheck(this, IllegalInheritanceError);
|
224 |
|
225 | var _this4 = possibleConstructorReturn(this, (IllegalInheritanceError.__proto__ || Object.getPrototypeOf(IllegalInheritanceError)).call(this, message));
|
226 |
|
227 | _this4.name = "IllegalInheritanceError";
|
228 |
|
229 | var self = _this4;
|
230 | self.constructor = IllegalInheritanceError;
|
231 | self.__proto__ = IllegalInheritanceError.prototype;
|
232 | return _this4;
|
233 | }
|
234 |
|
235 | return IllegalInheritanceError;
|
236 | }(Error);
|
237 |
|
238 | var IllegalStateError = function (_Error5) {
|
239 | inherits(IllegalStateError, _Error5);
|
240 |
|
241 | function IllegalStateError(message) {
|
242 | classCallCheck(this, IllegalStateError);
|
243 |
|
244 | var _this5 = possibleConstructorReturn(this, (IllegalStateError.__proto__ || Object.getPrototypeOf(IllegalStateError)).call(this, message));
|
245 |
|
246 | _this5.name = "IllegalStateError";
|
247 |
|
248 | var self = _this5;
|
249 | self.constructor = IllegalStateError;
|
250 | self.__proto__ = IllegalStateError.prototype;
|
251 | return _this5;
|
252 | }
|
253 |
|
254 | return IllegalStateError;
|
255 | }(Error);
|
256 |
|
257 | var IllegalArgumentError = function (_Error6) {
|
258 | inherits(IllegalArgumentError, _Error6);
|
259 |
|
260 | function IllegalArgumentError(message) {
|
261 | classCallCheck(this, IllegalArgumentError);
|
262 |
|
263 | var _this6 = possibleConstructorReturn(this, (IllegalArgumentError.__proto__ || Object.getPrototypeOf(IllegalArgumentError)).call(this, message));
|
264 |
|
265 | _this6.name = "IllegalArgumentError";
|
266 |
|
267 | var self = _this6;
|
268 | self.constructor = IllegalArgumentError;
|
269 | self.__proto__ = IllegalArgumentError.prototype;
|
270 | return _this6;
|
271 | }
|
272 |
|
273 | return IllegalArgumentError;
|
274 | }(Error);
|
275 |
|
276 | var NotImplementedError = function (_Error7) {
|
277 | inherits(NotImplementedError, _Error7);
|
278 |
|
279 | function NotImplementedError(message) {
|
280 | classCallCheck(this, NotImplementedError);
|
281 |
|
282 | var _this7 = possibleConstructorReturn(this, (NotImplementedError.__proto__ || Object.getPrototypeOf(NotImplementedError)).call(this, message));
|
283 |
|
284 | _this7.name = "NotImplementedError";
|
285 |
|
286 | var self = _this7;
|
287 | self.constructor = NotImplementedError;
|
288 | self.__proto__ = NotImplementedError.prototype;
|
289 | return _this7;
|
290 | }
|
291 |
|
292 | return NotImplementedError;
|
293 | }(Error);
|
294 |
|
295 | var TimeoutError = function (_Error8) {
|
296 | inherits(TimeoutError, _Error8);
|
297 |
|
298 | function TimeoutError(message) {
|
299 | classCallCheck(this, TimeoutError);
|
300 |
|
301 | var _this8 = possibleConstructorReturn(this, (TimeoutError.__proto__ || Object.getPrototypeOf(TimeoutError)).call(this, message));
|
302 |
|
303 | _this8.name = "TimeoutError";
|
304 |
|
305 | var self = _this8;
|
306 | self.constructor = TimeoutError;
|
307 | self.__proto__ = TimeoutError.prototype;
|
308 | return _this8;
|
309 | }
|
310 |
|
311 | return TimeoutError;
|
312 | }(Error);
|
313 |
|
314 | function convertToMethod(f) {
|
315 | return function () {
|
316 | var args = Array.prototype.slice.call(arguments);
|
317 | args.push(this);
|
318 | return f.apply(undefined, args);
|
319 | };
|
320 | }
|
321 |
|
322 | function fantasyLandRegister(cls, monad, setoid) {
|
323 | var c = cls;
|
324 | var p = c.prototype;
|
325 | var fl = "fantasy-land/";
|
326 | var equals = "equals";
|
327 | var flEquals = fl + equals;
|
328 | var map = "map";
|
329 | var flMap = fl + map;
|
330 | var ap = "ap";
|
331 | var flAp = fl + ap;
|
332 | var flOf = fl + "of";
|
333 | var chain = "chain";
|
334 | var flChain = fl + chain;
|
335 | var chainRec = "chainRec";
|
336 | var flChainRec = fl + chainRec;
|
337 |
|
338 | if (p[equals]) {
|
339 | p[flEquals] = p[equals];
|
340 | } else {
|
341 | if (setoid) p[flEquals] = convertToMethod(setoid.equals);
|
342 | }
|
343 |
|
344 | if (p[map]) {
|
345 | p[flMap] = p[map];
|
346 | } else {
|
347 | if (monad) p[flMap] = convertToMethod(monad.map);
|
348 | }
|
349 |
|
350 | if (p[ap]) {
|
351 | p[flAp] = p[ap];
|
352 | } else {
|
353 | if (monad) p[flAp] = convertToMethod(monad.ap);
|
354 | }
|
355 |
|
356 | if (c["pure"]) {
|
357 | c[flOf] = c["pure"];
|
358 | } else {
|
359 | if (monad) c[flOf] = monad.of;
|
360 | }
|
361 |
|
362 | if (p[chain]) {
|
363 | p[flChain] = p[chain];
|
364 | } else {
|
365 | if (monad) p[flChain] = convertToMethod(monad.chain);
|
366 | }
|
367 |
|
368 | if (c[chainRec]) {
|
369 | c[flChainRec] = c[chainRec];
|
370 | } else {
|
371 | if (monad) c[flChainRec] = monad.chainRec;
|
372 | }
|
373 | }
|
374 |
|
375 | var internals = Object.freeze({
|
376 | convertToMethod: convertToMethod,
|
377 | fantasyLandRegister: fantasyLandRegister
|
378 | });
|
379 |
|
380 | var Either = function () {
|
381 | function Either(value, tag) {
|
382 | classCallCheck(this, Either);
|
383 |
|
384 | this._isRight = tag === "right";
|
385 | this.value = value;
|
386 | }
|
387 |
|
388 | createClass(Either, [{
|
389 | key: "isLeft",
|
390 | value: function isLeft() {
|
391 | return !this._isRight;
|
392 | }
|
393 | }, {
|
394 | key: "isRight",
|
395 | value: function isRight() {
|
396 | return this._isRight;
|
397 | }
|
398 | }, {
|
399 | key: "contains",
|
400 | value: function contains(elem) {
|
401 | return this._isRight && is(this.value, elem);
|
402 | }
|
403 | }, {
|
404 | key: "exists",
|
405 | value: function exists(p) {
|
406 | return this._isRight && p(this.value);
|
407 | }
|
408 | }, {
|
409 | key: "filterOrElse",
|
410 | value: function filterOrElse(p, zero) {
|
411 | return this._isRight ? p(this.value) ? this : Left(zero()) : this;
|
412 | }
|
413 | }, {
|
414 | key: "flatMap",
|
415 | value: function flatMap(f) {
|
416 | return this._isRight ? f(this.value) : this;
|
417 | }
|
418 | }, {
|
419 | key: "chain",
|
420 | value: function chain(f) {
|
421 | return this.flatMap(f);
|
422 | }
|
423 | }, {
|
424 | key: "ap",
|
425 | value: function ap(ff) {
|
426 | var _this = this;
|
427 |
|
428 | return ff.flatMap(function (f) {
|
429 | return _this.map(f);
|
430 | });
|
431 | }
|
432 | }, {
|
433 | key: "fold",
|
434 | value: function fold(left, right) {
|
435 | return this._isRight ? right(this.value) : left(this.value);
|
436 | }
|
437 | }, {
|
438 | key: "forAll",
|
439 | value: function forAll(p) {
|
440 | return !this._isRight || p(this.value);
|
441 | }
|
442 | }, {
|
443 | key: "get",
|
444 | value: function get$$1() {
|
445 | if (this._isRight) return this.value;
|
446 | throw new NoSuchElementError("left.get()");
|
447 | }
|
448 | }, {
|
449 | key: "getOrElse",
|
450 | value: function getOrElse(fallback) {
|
451 | return this._isRight ? this.value : fallback;
|
452 | }
|
453 | }, {
|
454 | key: "getOrElseL",
|
455 | value: function getOrElseL(thunk) {
|
456 | return this._isRight ? this.value : thunk();
|
457 | }
|
458 | }, {
|
459 | key: "map",
|
460 | value: function map(f) {
|
461 | return this._isRight ? Right(f(this.value)) : this;
|
462 | }
|
463 | }, {
|
464 | key: "forEach",
|
465 | value: function forEach(cb) {
|
466 | if (this._isRight) cb(this.value);
|
467 | }
|
468 | }, {
|
469 | key: "swap",
|
470 | value: function swap() {
|
471 | return this._isRight ? Left(this.value) : Right(this.value);
|
472 | }
|
473 | }, {
|
474 | key: "toOption",
|
475 | value: function toOption() {
|
476 | return this._isRight ? Some(this.value) : None;
|
477 | }
|
478 | }, {
|
479 | key: "equals",
|
480 | value: function equals$$1(that) {
|
481 | if (that == null) return false;
|
482 | return this._isRight === that._isRight && is(this.value, that.value);
|
483 | }
|
484 | }, {
|
485 | key: "hashCode",
|
486 | value: function hashCode$$1() {
|
487 | return this._isRight ? hashCode(this.value) << 2 : hashCode(this.value) << 3;
|
488 | }
|
489 | }], [{
|
490 | key: "pure",
|
491 | value: function pure(value) {
|
492 | return new TRight(value);
|
493 | }
|
494 | }, {
|
495 | key: "left",
|
496 | value: function left(value) {
|
497 | return Left(value);
|
498 | }
|
499 | }, {
|
500 | key: "right",
|
501 | value: function right(value) {
|
502 | return Right(value);
|
503 | }
|
504 | }, {
|
505 | key: "map2",
|
506 | value: function map2(fa1, fa2, f) {
|
507 | if (fa1.isLeft()) return fa1;
|
508 | if (fa2.isLeft()) return fa2;
|
509 | return Right(f(fa1.value, fa2.value));
|
510 | }
|
511 | }, {
|
512 | key: "map3",
|
513 | value: function map3(fa1, fa2, fa3, f) {
|
514 | if (fa1.isLeft()) return fa1;
|
515 | if (fa2.isLeft()) return fa2;
|
516 | if (fa3.isLeft()) return fa3;
|
517 | return Right(f(fa1.value, fa2.value, fa3.value));
|
518 | }
|
519 | }, {
|
520 | key: "map4",
|
521 | value: function map4(fa1, fa2, fa3, fa4, f) {
|
522 | if (fa1.isLeft()) return fa1;
|
523 | if (fa2.isLeft()) return fa2;
|
524 | if (fa3.isLeft()) return fa3;
|
525 | if (fa4.isLeft()) return fa4;
|
526 | return Right(f(fa1.value, fa2.value, fa3.value, fa4.value));
|
527 | }
|
528 | }, {
|
529 | key: "map5",
|
530 | value: function map5(fa1, fa2, fa3, fa4, fa5, f) {
|
531 | if (fa1.isLeft()) return fa1;
|
532 | if (fa2.isLeft()) return fa2;
|
533 | if (fa3.isLeft()) return fa3;
|
534 | if (fa4.isLeft()) return fa4;
|
535 | if (fa5.isLeft()) return fa5;
|
536 | return Right(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value));
|
537 | }
|
538 | }, {
|
539 | key: "map6",
|
540 | value: function map6(fa1, fa2, fa3, fa4, fa5, fa6, f) {
|
541 | if (fa1.isLeft()) return fa1;
|
542 | if (fa2.isLeft()) return fa2;
|
543 | if (fa3.isLeft()) return fa3;
|
544 | if (fa4.isLeft()) return fa4;
|
545 | if (fa5.isLeft()) return fa5;
|
546 | if (fa6.isLeft()) return fa6;
|
547 | return Right(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value, fa6.value));
|
548 | }
|
549 | }, {
|
550 | key: "tailRecM",
|
551 | value: function tailRecM(a, f) {
|
552 | var cursor = a;
|
553 | while (true) {
|
554 | var result = f(cursor);
|
555 | if (!result.isRight()) return result;
|
556 | var some = result.value;
|
557 | if (some.isRight()) return Right(some.value);
|
558 | cursor = some.value;
|
559 | }
|
560 | }
|
561 | }]);
|
562 | return Either;
|
563 | }();
|
564 |
|
565 | var TLeft = function (_Either) {
|
566 | inherits(TLeft, _Either);
|
567 |
|
568 | function TLeft(value) {
|
569 | classCallCheck(this, TLeft);
|
570 | return possibleConstructorReturn(this, (TLeft.__proto__ || Object.getPrototypeOf(TLeft)).call(this, value, "left"));
|
571 | }
|
572 |
|
573 | return TLeft;
|
574 | }(Either);
|
575 |
|
576 | function Left(value) {
|
577 | return new TLeft(value);
|
578 | }
|
579 |
|
580 | var TRight = function (_Either2) {
|
581 | inherits(TRight, _Either2);
|
582 |
|
583 | function TRight(value) {
|
584 | classCallCheck(this, TRight);
|
585 | return possibleConstructorReturn(this, (TRight.__proto__ || Object.getPrototypeOf(TRight)).call(this, value, "right"));
|
586 | }
|
587 |
|
588 | return TRight;
|
589 | }(Either);
|
590 |
|
591 | function Right(value) {
|
592 | return new TRight(value);
|
593 | }
|
594 |
|
595 | var EitherModule = {
|
596 | equals: function equals$$1(x, y) {
|
597 | return x ? x.equals(y) : !y;
|
598 | },
|
599 |
|
600 | map: function map(f, fa) {
|
601 | return fa.map(f);
|
602 | },
|
603 |
|
604 | ap: function ap(ff, fa) {
|
605 | return fa.ap(ff);
|
606 | },
|
607 |
|
608 | of: Either.pure,
|
609 |
|
610 | chain: function chain(f, fa) {
|
611 | return fa.flatMap(f);
|
612 | },
|
613 |
|
614 | chainRec: function chainRec(f, a) {
|
615 | return Either.tailRecM(a, function (a) {
|
616 | return f(Either.left, Either.right, a);
|
617 | });
|
618 | }
|
619 | };
|
620 |
|
621 | fantasyLandRegister(Either, EitherModule, EitherModule);
|
622 |
|
623 | var Option = function () {
|
624 | function Option(ref, isEmpty) {
|
625 | classCallCheck(this, Option);
|
626 |
|
627 | this._isEmpty = isEmpty != null ? isEmpty : ref === null || ref === undefined;
|
628 | this.value = ref;
|
629 | }
|
630 |
|
631 | createClass(Option, [{
|
632 | key: "get",
|
633 | value: function get$$1() {
|
634 | if (!this._isEmpty) return this.value;
|
635 | throw new NoSuchElementError("Option.get");
|
636 | }
|
637 | }, {
|
638 | key: "getOrElse",
|
639 | value: function getOrElse(fallback) {
|
640 | if (!this._isEmpty) return this.value;else return fallback;
|
641 | }
|
642 | }, {
|
643 | key: "orNull",
|
644 | value: function orNull() {
|
645 | return !this._isEmpty ? this.value : null;
|
646 | }
|
647 | }, {
|
648 | key: "orUndefined",
|
649 | value: function orUndefined() {
|
650 | return !this._isEmpty ? this.value : undefined;
|
651 | }
|
652 | }, {
|
653 | key: "getOrElseL",
|
654 | value: function getOrElseL(thunk) {
|
655 | if (!this._isEmpty) return this.value;else return thunk();
|
656 | }
|
657 | }, {
|
658 | key: "orElse",
|
659 | value: function orElse(fallback) {
|
660 | if (!this._isEmpty) return this;else return fallback;
|
661 | }
|
662 | }, {
|
663 | key: "orElseL",
|
664 | value: function orElseL(thunk) {
|
665 | if (!this._isEmpty) return this;else return thunk();
|
666 | }
|
667 | }, {
|
668 | key: "isEmpty",
|
669 | value: function isEmpty() {
|
670 | return this._isEmpty;
|
671 | }
|
672 | }, {
|
673 | key: "nonEmpty",
|
674 | value: function nonEmpty() {
|
675 | return !this._isEmpty;
|
676 | }
|
677 | }, {
|
678 | key: "map",
|
679 | value: function map(f) {
|
680 | return this._isEmpty ? None : Some(f(this.value));
|
681 | }
|
682 | }, {
|
683 | key: "flatMap",
|
684 | value: function flatMap(f) {
|
685 | if (this._isEmpty) return None;else return f(this.value);
|
686 | }
|
687 | }, {
|
688 | key: "chain",
|
689 | value: function chain(f) {
|
690 | return this.flatMap(f);
|
691 | }
|
692 | }, {
|
693 | key: "ap",
|
694 | value: function ap(ff) {
|
695 | var _this4 = this;
|
696 |
|
697 | return ff.flatMap(function (f) {
|
698 | return _this4.map(f);
|
699 | });
|
700 | }
|
701 | }, {
|
702 | key: "filter",
|
703 | value: function filter(p) {
|
704 | if (this._isEmpty || !p(this.value)) return None;else return this;
|
705 | }
|
706 | }, {
|
707 | key: "fold",
|
708 | value: function fold(fallback, f) {
|
709 | if (this._isEmpty) return fallback();
|
710 | return f(this.value);
|
711 | }
|
712 | }, {
|
713 | key: "contains",
|
714 | value: function contains(elem) {
|
715 | return !this._isEmpty && is(this.value, elem);
|
716 | }
|
717 | }, {
|
718 | key: "exists",
|
719 | value: function exists(p) {
|
720 | return !this._isEmpty && p(this.value);
|
721 | }
|
722 | }, {
|
723 | key: "forAll",
|
724 | value: function forAll(p) {
|
725 | return this._isEmpty || p(this.value);
|
726 | }
|
727 | }, {
|
728 | key: "forEach",
|
729 | value: function forEach(cb) {
|
730 | if (!this._isEmpty) cb(this.value);
|
731 | }
|
732 | }, {
|
733 | key: "equals",
|
734 | value: function equals$$1(that) {
|
735 | if (that == null) return false;
|
736 | if (this.nonEmpty() && that.nonEmpty()) {
|
737 | return is(this.value, that.value);
|
738 | }
|
739 | return this.isEmpty() && that.isEmpty();
|
740 | }
|
741 | }, {
|
742 | key: "hashCode",
|
743 | value: function hashCode$$1() {
|
744 | if (this._isEmpty) return 2433880;else if (this.value == null) return 2433881 << 2;else return hashCode(this.value) << 2;
|
745 | }
|
746 | }], [{
|
747 | key: "of",
|
748 | value: function of(value) {
|
749 | return value != null ? Some(value) : None;
|
750 | }
|
751 | }, {
|
752 | key: "some",
|
753 | value: function some(value) {
|
754 | return new Option(value, false);
|
755 | }
|
756 | }, {
|
757 | key: "none",
|
758 | value: function none() {
|
759 | return None;
|
760 | }
|
761 | }, {
|
762 | key: "empty",
|
763 | value: function empty() {
|
764 | return None;
|
765 | }
|
766 | }, {
|
767 | key: "pure",
|
768 | value: function pure(value) {
|
769 | return Some(value);
|
770 | }
|
771 | }, {
|
772 | key: "map2",
|
773 | value: function map2(fa1, fa2, f) {
|
774 | return fa1.nonEmpty() && fa2.nonEmpty() ? Some(f(fa1.value, fa2.value)) : None;
|
775 | }
|
776 | }, {
|
777 | key: "map3",
|
778 | value: function map3(fa1, fa2, fa3, f) {
|
779 | return fa1.nonEmpty() && fa2.nonEmpty() && fa3.nonEmpty() ? Some(f(fa1.value, fa2.value, fa3.value)) : None;
|
780 | }
|
781 | }, {
|
782 | key: "map4",
|
783 | value: function map4(fa1, fa2, fa3, fa4, f) {
|
784 | return fa1.nonEmpty() && fa2.nonEmpty() && fa3.nonEmpty() && fa4.nonEmpty() ? Some(f(fa1.value, fa2.value, fa3.value, fa4.value)) : None;
|
785 | }
|
786 | }, {
|
787 | key: "map5",
|
788 | value: function map5(fa1, fa2, fa3, fa4, fa5, f) {
|
789 | return fa1.nonEmpty() && fa2.nonEmpty() && fa3.nonEmpty() && fa4.nonEmpty() && fa5.nonEmpty() ? Some(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value)) : None;
|
790 | }
|
791 | }, {
|
792 | key: "map6",
|
793 | value: function map6(fa1, fa2, fa3, fa4, fa5, fa6, f) {
|
794 | return fa1.nonEmpty() && fa2.nonEmpty() && fa3.nonEmpty() && fa4.nonEmpty() && fa5.nonEmpty() && fa6.nonEmpty() ? Some(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value, fa6.value)) : None;
|
795 | }
|
796 | }, {
|
797 | key: "tailRecM",
|
798 | value: function tailRecM(a, f) {
|
799 | var cursor = a;
|
800 | while (true) {
|
801 | var result = f(cursor);
|
802 | if (result.nonEmpty()) {
|
803 | var some = result.value;
|
804 | if (some.isRight()) return Some(some.value);
|
805 | cursor = some.value;
|
806 | } else {
|
807 | return None;
|
808 | }
|
809 | }
|
810 | }
|
811 | }]);
|
812 | return Option;
|
813 | }();
|
814 |
|
815 | var TSome = function (_Option) {
|
816 | inherits(TSome, _Option);
|
817 |
|
818 | function TSome(value) {
|
819 | classCallCheck(this, TSome);
|
820 | return possibleConstructorReturn(this, (TSome.__proto__ || Object.getPrototypeOf(TSome)).call(this, value, false));
|
821 | }
|
822 |
|
823 | return TSome;
|
824 | }(Option);
|
825 |
|
826 | function Some(value) {
|
827 | return new TSome(value);
|
828 | }
|
829 |
|
830 | var TNone = function (_Option2) {
|
831 | inherits(TNone, _Option2);
|
832 |
|
833 | function TNone() {
|
834 | classCallCheck(this, TNone);
|
835 | return possibleConstructorReturn(this, (TNone.__proto__ || Object.getPrototypeOf(TNone)).call(this, undefined, true));
|
836 | }
|
837 |
|
838 | return TNone;
|
839 | }(Option);
|
840 |
|
841 | var None = new TNone();
|
842 |
|
843 | var OptionModule = {
|
844 | equals: function equals$$1(x, y) {
|
845 | return x ? x.equals(y) : !y;
|
846 | },
|
847 |
|
848 | map: function map(f, fa) {
|
849 | return fa.map(f);
|
850 | },
|
851 |
|
852 | ap: function ap(ff, fa) {
|
853 | return fa.ap(ff);
|
854 | },
|
855 |
|
856 | of: Option.pure,
|
857 |
|
858 | chain: function chain(f, fa) {
|
859 | return fa.flatMap(f);
|
860 | },
|
861 |
|
862 | chainRec: function chainRec(f, a) {
|
863 | return Option.tailRecM(a, function (a) {
|
864 | return f(Either.left, Either.right, a);
|
865 | });
|
866 | }
|
867 | };
|
868 |
|
869 | fantasyLandRegister(Option, OptionModule, OptionModule);
|
870 |
|
871 | var Try = function () {
|
872 | function Try(value, tag) {
|
873 | classCallCheck(this, Try);
|
874 |
|
875 | this._isSuccess = tag === "success";
|
876 | this.value = value;
|
877 | }
|
878 |
|
879 | createClass(Try, [{
|
880 | key: "isSuccess",
|
881 | value: function isSuccess() {
|
882 | return this._isSuccess;
|
883 | }
|
884 | }, {
|
885 | key: "isFailure",
|
886 | value: function isFailure() {
|
887 | return !this._isSuccess;
|
888 | }
|
889 | }, {
|
890 | key: "get",
|
891 | value: function get$$1() {
|
892 | if (!this._isSuccess) throw this.value;
|
893 | return this.value;
|
894 | }
|
895 | }, {
|
896 | key: "getOrElse",
|
897 | value: function getOrElse(fallback) {
|
898 | return this._isSuccess ? this.value : fallback;
|
899 | }
|
900 | }, {
|
901 | key: "getOrElseL",
|
902 | value: function getOrElseL(thunk) {
|
903 | return this._isSuccess ? this.value : thunk();
|
904 | }
|
905 | }, {
|
906 | key: "orNull",
|
907 | value: function orNull() {
|
908 | return this._isSuccess ? this.value : null;
|
909 | }
|
910 | }, {
|
911 | key: "orUndefined",
|
912 | value: function orUndefined() {
|
913 | return this._isSuccess ? this.value : undefined;
|
914 | }
|
915 | }, {
|
916 | key: "orElse",
|
917 | value: function orElse(fallback) {
|
918 | if (this._isSuccess) return this;
|
919 | return fallback;
|
920 | }
|
921 | }, {
|
922 | key: "orElseL",
|
923 | value: function orElseL(thunk) {
|
924 | if (this._isSuccess) return this;
|
925 | return thunk();
|
926 | }
|
927 | }, {
|
928 | key: "failed",
|
929 | value: function failed() {
|
930 | return this._isSuccess ? Failure(new NoSuchElementError("try.failed()")) : Success(this.value);
|
931 | }
|
932 | }, {
|
933 | key: "fold",
|
934 | value: function fold(failure, success) {
|
935 | return this._isSuccess ? success(this.value) : failure(this.value);
|
936 | }
|
937 | }, {
|
938 | key: "filter",
|
939 | value: function filter(p) {
|
940 | if (!this._isSuccess) return this;
|
941 | try {
|
942 | if (p(this.value)) return this;
|
943 | return Failure(new NoSuchElementError("Predicate does not hold for " + this.value));
|
944 | } catch (e) {
|
945 | return Failure(e);
|
946 | }
|
947 | }
|
948 | }, {
|
949 | key: "flatMap",
|
950 | value: function flatMap(f) {
|
951 | if (!this._isSuccess) return this;
|
952 | try {
|
953 | return f(this.value);
|
954 | } catch (e) {
|
955 | return Failure(e);
|
956 | }
|
957 | }
|
958 | }, {
|
959 | key: "chain",
|
960 | value: function chain(f) {
|
961 | return this.flatMap(f);
|
962 | }
|
963 | }, {
|
964 | key: "ap",
|
965 | value: function ap(ff) {
|
966 | var _this7 = this;
|
967 |
|
968 | return ff.flatMap(function (f) {
|
969 | return _this7.map(f);
|
970 | });
|
971 | }
|
972 | }, {
|
973 | key: "map",
|
974 | value: function map(f) {
|
975 | var _this8 = this;
|
976 |
|
977 | return this._isSuccess ? Try.of(function () {
|
978 | return f(_this8.value);
|
979 | }) : this;
|
980 | }
|
981 | }, {
|
982 | key: "forEach",
|
983 | value: function forEach(cb) {
|
984 | if (this._isSuccess) cb(this.value);
|
985 | }
|
986 | }, {
|
987 | key: "recover",
|
988 | value: function recover(f) {
|
989 | var _this9 = this;
|
990 |
|
991 | return this._isSuccess ? this : Try.of(function () {
|
992 | return f(_this9.value);
|
993 | });
|
994 | }
|
995 | }, {
|
996 | key: "recoverWith",
|
997 | value: function recoverWith(f) {
|
998 | try {
|
999 | return this._isSuccess ? this : f(this.value);
|
1000 | } catch (e) {
|
1001 | return Failure(e);
|
1002 | }
|
1003 | }
|
1004 | }, {
|
1005 | key: "toOption",
|
1006 | value: function toOption() {
|
1007 | return this._isSuccess ? Some(this.value) : None;
|
1008 | }
|
1009 | }, {
|
1010 | key: "toEither",
|
1011 | value: function toEither() {
|
1012 | return this._isSuccess ? Right(this.value) : Left(this.value);
|
1013 | }
|
1014 | }, {
|
1015 | key: "equals",
|
1016 | value: function equals$$1(that) {
|
1017 | if (that == null) return false;
|
1018 | return this._isSuccess ? that._isSuccess && is(this.value, that.value) : !that._isSuccess && is(this.value, that.value);
|
1019 | }
|
1020 | }, {
|
1021 | key: "hashCode",
|
1022 | value: function hashCode$$1() {
|
1023 | return this._isSuccess ? hashCode(this.value) : hashCode(this.value);
|
1024 | }
|
1025 | }], [{
|
1026 | key: "of",
|
1027 | value: function of(thunk) {
|
1028 | try {
|
1029 | return Success(thunk());
|
1030 | } catch (e) {
|
1031 | return Failure(e);
|
1032 | }
|
1033 | }
|
1034 | }, {
|
1035 | key: "pure",
|
1036 | value: function pure(value) {
|
1037 | return Try.success(value);
|
1038 | }
|
1039 | }, {
|
1040 | key: "unit",
|
1041 | value: function unit() {
|
1042 | return tryUnitRef;
|
1043 | }
|
1044 | }, {
|
1045 | key: "success",
|
1046 | value: function success(value) {
|
1047 | return Success(value);
|
1048 | }
|
1049 | }, {
|
1050 | key: "failure",
|
1051 | value: function failure(e) {
|
1052 | return Failure(e);
|
1053 | }
|
1054 | }, {
|
1055 | key: "raise",
|
1056 | value: function raise(e) {
|
1057 | return Failure(e);
|
1058 | }
|
1059 | }, {
|
1060 | key: "map2",
|
1061 | value: function map2(fa1, fa2, f) {
|
1062 | if (fa1.isFailure()) return fa1;
|
1063 | if (fa2.isFailure()) return fa2;
|
1064 | try {
|
1065 | return Success(f(fa1.value, fa2.value));
|
1066 | } catch (e) {
|
1067 | return Failure(e);
|
1068 | }
|
1069 | }
|
1070 | }, {
|
1071 | key: "map3",
|
1072 | value: function map3(fa1, fa2, fa3, f) {
|
1073 | if (fa1.isFailure()) return fa1;
|
1074 | if (fa2.isFailure()) return fa2;
|
1075 | if (fa3.isFailure()) return fa3;
|
1076 | try {
|
1077 | return Success(f(fa1.value, fa2.value, fa3.value));
|
1078 | } catch (e) {
|
1079 | return Failure(e);
|
1080 | }
|
1081 | }
|
1082 | }, {
|
1083 | key: "map4",
|
1084 | value: function map4(fa1, fa2, fa3, fa4, f) {
|
1085 | if (fa1.isFailure()) return fa1;
|
1086 | if (fa2.isFailure()) return fa2;
|
1087 | if (fa3.isFailure()) return fa3;
|
1088 | if (fa4.isFailure()) return fa4;
|
1089 | try {
|
1090 | return Success(f(fa1.value, fa2.value, fa3.value, fa4.value));
|
1091 | } catch (e) {
|
1092 | return Failure(e);
|
1093 | }
|
1094 | }
|
1095 | }, {
|
1096 | key: "map5",
|
1097 | value: function map5(fa1, fa2, fa3, fa4, fa5, f) {
|
1098 | if (fa1.isFailure()) return fa1;
|
1099 | if (fa2.isFailure()) return fa2;
|
1100 | if (fa3.isFailure()) return fa3;
|
1101 | if (fa4.isFailure()) return fa4;
|
1102 | if (fa5.isFailure()) return fa5;
|
1103 | try {
|
1104 | return Success(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value));
|
1105 | } catch (e) {
|
1106 | return Failure(e);
|
1107 | }
|
1108 | }
|
1109 | }, {
|
1110 | key: "map6",
|
1111 | value: function map6(fa1, fa2, fa3, fa4, fa5, fa6, f) {
|
1112 | if (fa1.isFailure()) return fa1;
|
1113 | if (fa2.isFailure()) return fa2;
|
1114 | if (fa3.isFailure()) return fa3;
|
1115 | if (fa4.isFailure()) return fa4;
|
1116 | if (fa5.isFailure()) return fa5;
|
1117 | if (fa6.isFailure()) return fa6;
|
1118 | try {
|
1119 | return Success(f(fa1.value, fa2.value, fa3.value, fa4.value, fa5.value, fa6.value));
|
1120 | } catch (e) {
|
1121 | return Failure(e);
|
1122 | }
|
1123 | }
|
1124 | }, {
|
1125 | key: "tailRecM",
|
1126 | value: function tailRecM(a, f) {
|
1127 | var cursor = a;
|
1128 | while (true) {
|
1129 | try {
|
1130 | var result = f(cursor);
|
1131 | if (result.isFailure()) return result;
|
1132 | var some = result.get();
|
1133 | if (some.isRight()) return Success(some.value);
|
1134 | cursor = some.value;
|
1135 | } catch (e) {
|
1136 | return Failure(e);
|
1137 | }
|
1138 | }
|
1139 | }
|
1140 | }]);
|
1141 | return Try;
|
1142 | }();
|
1143 |
|
1144 | var TSuccess = function (_Try) {
|
1145 | inherits(TSuccess, _Try);
|
1146 |
|
1147 | function TSuccess(value) {
|
1148 | classCallCheck(this, TSuccess);
|
1149 | return possibleConstructorReturn(this, (TSuccess.__proto__ || Object.getPrototypeOf(TSuccess)).call(this, value, "success"));
|
1150 | }
|
1151 |
|
1152 | return TSuccess;
|
1153 | }(Try);
|
1154 |
|
1155 | function Success(value) {
|
1156 | return new TSuccess(value);
|
1157 | }
|
1158 |
|
1159 | var TFailure = function (_Try2) {
|
1160 | inherits(TFailure, _Try2);
|
1161 |
|
1162 | function TFailure(value) {
|
1163 | classCallCheck(this, TFailure);
|
1164 | return possibleConstructorReturn(this, (TFailure.__proto__ || Object.getPrototypeOf(TFailure)).call(this, value, "failure"));
|
1165 | }
|
1166 |
|
1167 | return TFailure;
|
1168 | }(Try);
|
1169 |
|
1170 | function Failure(e) {
|
1171 | return new TFailure(e);
|
1172 | }
|
1173 |
|
1174 | var TryModule = {
|
1175 | equals: function equals$$1(x, y) {
|
1176 | return x ? x.equals(y) : !y;
|
1177 | },
|
1178 |
|
1179 | map: function map(f, fa) {
|
1180 | return fa.map(f);
|
1181 | },
|
1182 |
|
1183 | ap: function ap(ff, fa) {
|
1184 | return fa.ap(ff);
|
1185 | },
|
1186 |
|
1187 | of: Try.pure,
|
1188 |
|
1189 | chain: function chain(f, fa) {
|
1190 | return fa.flatMap(f);
|
1191 | },
|
1192 |
|
1193 | chainRec: function chainRec(f, a) {
|
1194 | return Try.tailRecM(a, function (a) {
|
1195 | return f(Either.left, Either.right, a);
|
1196 | });
|
1197 | }
|
1198 | };
|
1199 |
|
1200 | fantasyLandRegister(Try, TryModule, TryModule);
|
1201 |
|
1202 | var tryUnitRef = Success(undefined);
|
1203 |
|
1204 | export { internals as coreInternals, isValueObject, is, equals, universalSetoid, hashCode, hashCodeOfString, id, applyMixins, CompositeError, DummyError, NoSuchElementError, IllegalInheritanceError, IllegalStateError, IllegalArgumentError, NotImplementedError, TimeoutError, Either, TLeft, Left, TRight, Right, EitherModule, Option, TSome, Some, TNone, None, OptionModule, Try, TSuccess, Success, TFailure, Failure, TryModule };
|
1205 |
|