UNPKG

fp-ts

Version:

Functional programming in TypeScript

162 lines (161 loc) 7.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pipeable = exports.pipe = void 0; var function_1 = require("./function"); // TODO: remove module in v3 /** * Use [`pipe`](https://gcanti.github.io/fp-ts/modules/function.ts.html#flow) from `function` module instead. * * @since 2.0.0 */ exports.pipe = function_1.pipe; var isFunctor = function (I) { return typeof I.map === 'function'; }; var isContravariant = function (I) { return typeof I.contramap === 'function'; }; var isFunctorWithIndex = function (I) { return typeof I.mapWithIndex === 'function'; }; var isApply = function (I) { return typeof I.ap === 'function'; }; var isChain = function (I) { return typeof I.chain === 'function'; }; var isBifunctor = function (I) { return typeof I.bimap === 'function'; }; var isExtend = function (I) { return typeof I.extend === 'function'; }; var isFoldable = function (I) { return typeof I.reduce === 'function'; }; var isFoldableWithIndex = function (I) { return typeof I.reduceWithIndex === 'function'; }; var isAlt = function (I) { return typeof I.alt === 'function'; }; var isCompactable = function (I) { return typeof I.compact === 'function'; }; var isFilterable = function (I) { return typeof I.filter === 'function'; }; var isFilterableWithIndex = function (I) { return typeof I.filterWithIndex === 'function'; }; var isProfunctor = function (I) { return typeof I.promap === 'function'; }; var isSemigroupoid = function (I) { return typeof I.compose === 'function'; }; var isMonadThrow = function (I) { return typeof I.throwError === 'function'; }; function pipeable(I) { var r = {}; if (isFunctor(I)) { var map = function (f) { return function (fa) { return I.map(fa, f); }; }; r.map = map; } if (isContravariant(I)) { var contramap = function (f) { return function (fa) { return I.contramap(fa, f); }; }; r.contramap = contramap; } if (isFunctorWithIndex(I)) { var mapWithIndex = function (f) { return function (fa) { return I.mapWithIndex(fa, f); }; }; r.mapWithIndex = mapWithIndex; } if (isApply(I)) { var ap = function (fa) { return function (fab) { return I.ap(fab, fa); }; }; var apFirst = function (fb) { return function (fa) { return I.ap(I.map(fa, function (a) { return function () { return a; }; }), fb); }; }; r.ap = ap; r.apFirst = apFirst; r.apSecond = function (fb) { return function (fa) { return I.ap(I.map(fa, function () { return function (b) { return b; }; }), fb); }; }; } if (isChain(I)) { var chain = function (f) { return function (ma) { return I.chain(ma, f); }; }; var chainFirst = function (f) { return function (ma) { return I.chain(ma, function (a) { return I.map(f(a), function () { return a; }); }); }; }; var flatten = function (mma) { return I.chain(mma, function_1.identity); }; r.chain = chain; r.chainFirst = chainFirst; r.flatten = flatten; } if (isBifunctor(I)) { var bimap = function (f, g) { return function (fa) { return I.bimap(fa, f, g); }; }; var mapLeft = function (f) { return function (fa) { return I.mapLeft(fa, f); }; }; r.bimap = bimap; r.mapLeft = mapLeft; } if (isExtend(I)) { var extend = function (f) { return function (wa) { return I.extend(wa, f); }; }; var duplicate = function (wa) { return I.extend(wa, function_1.identity); }; r.extend = extend; r.duplicate = duplicate; } if (isFoldable(I)) { var reduce = function (b, f) { return function (fa) { return I.reduce(fa, b, f); }; }; var foldMap = function (M) { var foldMapM = I.foldMap(M); return function (f) { return function (fa) { return foldMapM(fa, f); }; }; }; var reduceRight = function (b, f) { return function (fa) { return I.reduceRight(fa, b, f); }; }; r.reduce = reduce; r.foldMap = foldMap; r.reduceRight = reduceRight; } if (isFoldableWithIndex(I)) { var reduceWithIndex = function (b, f) { return function (fa) { return I.reduceWithIndex(fa, b, f); }; }; var foldMapWithIndex = function (M) { var foldMapM = I.foldMapWithIndex(M); return function (f) { return function (fa) { return foldMapM(fa, f); }; }; }; var reduceRightWithIndex = function (b, f) { return function (fa) { return I.reduceRightWithIndex(fa, b, f); }; }; r.reduceWithIndex = reduceWithIndex; r.foldMapWithIndex = foldMapWithIndex; r.reduceRightWithIndex = reduceRightWithIndex; } if (isAlt(I)) { var alt = function (that) { return function (fa) { return I.alt(fa, that); }; }; r.alt = alt; } if (isCompactable(I)) { r.compact = I.compact; r.separate = I.separate; } if (isFilterable(I)) { var filter = function (predicate) { return function (fa) { return I.filter(fa, predicate); }; }; var filterMap = function (f) { return function (fa) { return I.filterMap(fa, f); }; }; var partition = function (predicate) { return function (fa) { return I.partition(fa, predicate); }; }; var partitionMap = function (f) { return function (fa) { return I.partitionMap(fa, f); }; }; r.filter = filter; r.filterMap = filterMap; r.partition = partition; r.partitionMap = partitionMap; } if (isFilterableWithIndex(I)) { var filterWithIndex = function (predicateWithIndex) { return function (fa) { return I.filterWithIndex(fa, predicateWithIndex); }; }; var filterMapWithIndex = function (f) { return function (fa) { return I.filterMapWithIndex(fa, f); }; }; var partitionWithIndex = function (predicateWithIndex) { return function (fa) { return I.partitionWithIndex(fa, predicateWithIndex); }; }; var partitionMapWithIndex = function (f) { return function (fa) { return I.partitionMapWithIndex(fa, f); }; }; r.filterWithIndex = filterWithIndex; r.filterMapWithIndex = filterMapWithIndex; r.partitionWithIndex = partitionWithIndex; r.partitionMapWithIndex = partitionMapWithIndex; } if (isProfunctor(I)) { var promap = function (f, g) { return function (fa) { return I.promap(fa, f, g); }; }; r.promap = promap; } if (isSemigroupoid(I)) { var compose = function (that) { return function (fa) { return I.compose(fa, that); }; }; r.compose = compose; } if (isMonadThrow(I)) { var fromOption = function (onNone) { return function (ma) { return ma._tag === 'None' ? I.throwError(onNone()) : I.of(ma.value); }; }; var fromEither = function (ma) { return ma._tag === 'Left' ? I.throwError(ma.left) : I.of(ma.right); }; var fromPredicate = function (predicate, onFalse) { return function (a) { return (predicate(a) ? I.of(a) : I.throwError(onFalse(a))); }; }; var filterOrElse = function (predicate, onFalse) { return function (ma) { return I.chain(ma, function (a) { return (predicate(a) ? I.of(a) : I.throwError(onFalse(a))); }); }; }; r.fromOption = fromOption; r.fromEither = fromEither; r.fromPredicate = fromPredicate; r.filterOrElse = filterOrElse; } return r; } exports.pipeable = pipeable;