action-debian-package/node_modules/ramda/src/invoker.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-26 14:37:35 +00:00
var _curry2 =
/*#__PURE__*/
require("./internal/_curry2");
var _isFunction =
/*#__PURE__*/
require("./internal/_isFunction");
var curryN =
/*#__PURE__*/
require("./curryN");
var toString =
/*#__PURE__*/
require("./toString");
/**
* 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 =
/*#__PURE__*/
_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 + '"');
});
});
module.exports = invoker;