UNPKG

entrust

Version:

delegatee-last structure for curried functions

85 lines (68 loc) 2.77 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.eD = exports.eN = exports.entrustN = undefined; exports.entrustD = entrustD; var _katsuCurry = require('katsu-curry'); var _e = require('./e0'); var _e2 = require('./e1'); var _e3 = require('./e2'); var _e4 = require('./e3'); var _e5 = require('./e4'); var _e6 = require('./e5'); var _e7 = require('./e6'); var _e8 = require('./e7'); var _e9 = require('./e8'); var _e10 = require('./e9'); var _e11 = require('./e10'); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var entrustN = exports.entrustN = function entrustN(n, method, args, delegatee) { var entrustees = [_e.e0, _e2.e1, _e3.e2, _e4.e3, _e5.e4, _e6.e5, _e7.e6, _e8.e7, _e9.e8, _e10.e9, _e11.e10]; var params = [method].concat(_toConsumableArray(args), [delegatee]); return entrustees[n].apply(null, params); }; /** * invoke a delegated method with arguments as an array. enforces specific arity * @method eN * @param {number} n - 0 - 10 * @param {string} method - a function name on your delegatee * @param {Array} args - arguments to pass to your delegatee's method * @param {*} delegatee - something with methods * @returns {*} the result of delegating to the method with some arguments * @public * @example * import {eN} from 'entrust' * eN(0, `toUpperCase`, [], `cool`) // `COOL` * eN(1, `map`, [(x) => x * 2], [1,2,3]) // [2,4,6] * eN(2, `reduce`, [(a, b) => (a + b), 0], [1,2,3]) // 6 */ var eN = exports.eN = (0, _katsuCurry.curry)(entrustN); function entrustD(n, m, a, d) { if (n !== a.length) { // eslint-disable-next-line fp/no-throw throw new Error(m + ' expects total args (' + a.length + ') to equal the given arity (' + n + ')'); } return entrustN(n, m, a, d); } /** * invoke a delegated method with arguments as an array. enforces specific arity * Yells at you if you give arguments that don't match the expected arity. * @method eD * @param {number} n - 0 - 10 * @param {string} method - a function name on your delegatee * @param {Array} args - arguments to pass to your delegatee's method * @param {*} delegatee - something with methods * @returns {*} the result of delegating to the method with some arguments * @public * @example * import {eD} from 'entrust' * eD(0, `toUpperCase`, [], `cool`) // `COOL` * eD(1, `map`, [(x) => x * 2], [1,2,3]) // [2,4,6] * eD(2, `reduce`, [(a, b) => (a + b), 0], [1,2,3]) // 6 * eD(2, `reduce`, [(a, b) => (a + b)], [1, 2, 3]) // throws error */ /* istanbul ignore next */ var eD = exports.eD = (0, _katsuCurry.curry)( /* istanbul ignore next */ entrustD);