UNPKG

moltres-utils

Version:
57 lines (45 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _isArray = _interopRequireDefault(require("./isArray")); var _isTransformer = _interopRequireDefault(require("./isTransformer")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Returns a function that dispatches with different strategies based on the * object in list position (last argument). If it is an array, executes [fn]. * Otherwise, if it has a function with one of the given method names, it will * execute that function (functor case). Otherwise, if it is a transformer, * uses transducer [xf] to return a new transformer (transducer case). * Otherwise, it will default to executing [fn]. * * @param {Array} methodNames properties to check for a custom implementation * @param {Function} xf transducer to initialize if object is transformer * @param {Function} fn default ramda implementation * @return {Function} A function that dispatches on object in list position */ const dispatchable = (methodNames, xf, fn) => function () { if (arguments.length === 0) { return fn(); } const args = Array.prototype.slice.call(arguments, 0); const obj = args.pop(); if (!(0, _isArray.default)(obj)) { let idx = 0; while (idx < methodNames.length) { if (typeof obj[methodNames[idx]] === 'function') { return obj[methodNames[idx]].apply(obj, args); } idx += 1; } if ((0, _isTransformer.default)(obj)) { const transducer = xf.apply(null, args); return transducer(obj); } } return fn.apply(this, arguments); }; var _default = dispatchable; exports.default = _default; //# sourceMappingURL=dispatchable.js.map