// Loaded from https://deno.land/x/ramda@v0.27.2/source/invoker.js import _curry2 from './internal/_curry2.js'; import _isFunction from './internal/_isFunction.js'; import curryN from './curryN.js'; import toString from './toString.js'; /** * Turns a named method with a specified arity into a function that can be * called directly supplied with arguments and a target object. * * The returned function is curried and accepts `arity + 1` parameters where * the final parameter is the target object. * * @func * @memberOf R * @since v0.1.0 * @category Function * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *) * @param {Number} arity Number of arguments the returned function should take * before the target object. * @param {String} method Name of any of the target object's methods to call. * @return {Function} A new curried function. * @see R.construct * @example * * const sliceFrom = R.invoker(1, 'slice'); * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm' * const sliceFrom6 = R.invoker(2, 'slice')(6); * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh' * * const dog = { * speak: async () => 'Woof!' * }; * const speak = R.invoker(0, 'speak'); * speak(dog).then(console.log) //~> 'Woof!' * * @symb R.invoker(0, 'method')(o) = o['method']() * @symb R.invoker(1, 'method')(a, o) = o['method'](a) * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b) */ var invoker = _curry2(function invoker(arity, method) { return curryN(arity + 1, function() { var target = arguments[arity]; if (target != null && _isFunction(target[method])) { return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity)); } throw new TypeError(toString(target) + ' does not have a method named "' + method + '"'); }); }); export default invoker;