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