[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | exports.__esModule = true;
|
---|
| 4 | exports["default"] = void 0;
|
---|
| 5 | var _ramda = require("ramda");
|
---|
| 6 | var _isFunction = _interopRequireDefault(require("./isFunction"));
|
---|
| 7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
---|
| 8 | /**
|
---|
| 9 | * The catamorphism is a way of folding a type into a value.
|
---|
| 10 | *
|
---|
| 11 | * **Either**
|
---|
| 12 | *
|
---|
| 13 | * If the either is right then the right function will be executed with
|
---|
| 14 | * the `right` value and the value of the function returned. Otherwise the left function
|
---|
| 15 | * will be called with the `left` value.
|
---|
| 16 | *
|
---|
| 17 | * **Maybe**
|
---|
| 18 | *
|
---|
| 19 | * If the maybe is Some than the right function will be executed with the `some` value and the value of the function
|
---|
| 20 | * returned. Otherwise the left function with be called without an argument.
|
---|
| 21 | *
|
---|
| 22 | * **Result**
|
---|
| 23 | *
|
---|
| 24 | * If the result is Ok than the right function will be executed with the `Ok` value and the value of the function
|
---|
| 25 | * returned. Otherwise the left function will be called with the `Error` value.
|
---|
| 26 | *
|
---|
| 27 | * **Validation**
|
---|
| 28 | *
|
---|
| 29 | * If the validation is Success than the right function will be executed with the `Success` value and the value of the function
|
---|
| 30 | * returned. Otherwise the left function will be called with the `Failure` value.
|
---|
| 31 | *
|
---|
| 32 | * Supported monadic libraries: {@link https://monet.github.io/monet.js/|monet.js}, {@link https://folktale.origamitower.com/|folktale}, {@link https://github.com/ramda/ramda-fantasy|ramda-fantasy}
|
---|
| 33 | *
|
---|
| 34 | * @func cata
|
---|
| 35 | * @memberOf RA
|
---|
| 36 | * @since {@link https://char0n.github.io/ramda-adjunct/1.4.0|v1.4.0}
|
---|
| 37 | * @category Function
|
---|
| 38 | * @sig (a -> b) -> (a -> c) -> Cata a -> b | c
|
---|
| 39 | * @param {Function} leftFn The left function that consumes the left value
|
---|
| 40 | * @param {Function} rightFn The right function that consumes the right value
|
---|
| 41 | * @param {Cata} catamorphicObj Either, Maybe or any other type with catamorphic capabilities (`cata` or `either` method)
|
---|
| 42 | * @return {*}
|
---|
| 43 | * @see {@link https://monet.github.io/monet.js/#cata|cata explained}
|
---|
| 44 | * @example
|
---|
| 45 | *
|
---|
| 46 | * // Either
|
---|
| 47 | * const eitherR = Either.Right(1);
|
---|
| 48 | * const eitherL = Either.Left(2);
|
---|
| 49 | *
|
---|
| 50 | * RA.cata(identity, identity, eitherR); //=> 1
|
---|
| 51 | * RA.cata(identity, identity, eitherL); //=> 2
|
---|
| 52 | *
|
---|
| 53 | * // Maybe
|
---|
| 54 | * const maybeSome = Maybe.Some(1);
|
---|
| 55 | * const maybeNothing = Maybe.Nothing();
|
---|
| 56 | *
|
---|
| 57 | * RA.cata(identity, identity, maybeSome); //=> 1
|
---|
| 58 | * RA.cata(identity, identity, maybeNothing); //=> undefined
|
---|
| 59 | */
|
---|
| 60 | var catamorphism = (0, _ramda.curry)(function (leftFn, rightFn, catamorphicObj) {
|
---|
| 61 | // folktale support
|
---|
| 62 | if ((0, _isFunction["default"])(catamorphicObj.matchWith)) {
|
---|
| 63 | return catamorphicObj.matchWith({
|
---|
| 64 | // Result type
|
---|
| 65 | Ok: function Ok(_ref) {
|
---|
| 66 | var value = _ref.value;
|
---|
| 67 | return rightFn(value);
|
---|
| 68 | },
|
---|
| 69 | Error: function Error(_ref2) {
|
---|
| 70 | var value = _ref2.value;
|
---|
| 71 | return leftFn(value);
|
---|
| 72 | },
|
---|
| 73 | // Maybe type
|
---|
| 74 | Just: function Just(_ref3) {
|
---|
| 75 | var value = _ref3.value;
|
---|
| 76 | return rightFn(value);
|
---|
| 77 | },
|
---|
| 78 | Nothing: function Nothing() {
|
---|
| 79 | return leftFn(undefined);
|
---|
| 80 | },
|
---|
| 81 | // Validation type
|
---|
| 82 | Success: function Success(_ref4) {
|
---|
| 83 | var value = _ref4.value;
|
---|
| 84 | return rightFn(value);
|
---|
| 85 | },
|
---|
| 86 | Failure: function Failure(_ref5) {
|
---|
| 87 | var value = _ref5.value;
|
---|
| 88 | return leftFn(value);
|
---|
| 89 | }
|
---|
| 90 | });
|
---|
| 91 | }
|
---|
| 92 | if ((0, _isFunction["default"])(catamorphicObj.cata)) {
|
---|
| 93 | return catamorphicObj.cata(leftFn, rightFn);
|
---|
| 94 | }
|
---|
| 95 | if ((0, _isFunction["default"])(catamorphicObj.getOrElse)) {
|
---|
| 96 | var elseValue = "RA.cata".concat(Math.random());
|
---|
| 97 | var value = catamorphicObj.getOrElse(elseValue);
|
---|
| 98 | return value === elseValue ? leftFn() : rightFn(value);
|
---|
| 99 | }
|
---|
| 100 | return catamorphicObj.either(leftFn, rightFn);
|
---|
| 101 | });
|
---|
| 102 | var _default = catamorphism;
|
---|
| 103 | exports["default"] = _default; |
---|