source: node_modules/ramda/src/invoker.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2.js");
4
5var _isFunction =
6/*#__PURE__*/
7require("./internal/_isFunction.js");
8
9var curryN =
10/*#__PURE__*/
11require("./curryN.js");
12
13var toString =
14/*#__PURE__*/
15require("./toString.js");
16/**
17 * Given an `arity` (Number) and a `name` (String) the `invoker` function
18 * returns a curried function that takes `arity` arguments and a `context`
19 * object. It will "invoke" the `name`'d function (a method) on the `context`
20 * object.
21 *
22 * @func
23 * @memberOf R
24 * @since v0.1.0
25 * @category Function
26 * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *)
27 * @param {Number} arity Number of arguments the returned function should take
28 * before the target object.
29 * @param {String} method Name of any of the target object's methods to call.
30 * @return {Function} A new curried function.
31 * @see R.construct
32 * @example
33 * // A function with no arguments
34 * const asJson = invoker(0, "json")
35 * // Just like calling .then((response) => response.json())
36 * fetch("http://example.com/index.json").then(asJson)
37 *
38 * // A function with one argument
39 * const sliceFrom = invoker(1, 'slice');
40 * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
41 *
42 * // A function with two arguments
43 * const sliceFrom6 = invoker(2, 'slice')(6);
44 * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
45 *
46 * // NOTE: You can't simply pass some of the arguments to the initial invoker function.
47 * const firstCreditCardSection = invoker(2, "slice", 0, 4)
48 * firstCreditCardSection("4242 4242 4242 4242") // => Function<...>
49 *
50 * // Since invoker returns a curried function, you may partially apply it to create the function you need.
51 * const firstCreditCardSection = invoker(2, "slice")(0, 4)
52 * firstCreditCardSection("4242 4242 4242 4242") // => "4242"
53 *
54 * @symb R.invoker(0, 'method')(o) = o['method']()
55 * @symb R.invoker(1, 'method')(a, o) = o['method'](a)
56 * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b)
57 */
58
59
60var invoker =
61/*#__PURE__*/
62_curry2(function invoker(arity, method) {
63 return curryN(arity + 1, function () {
64 var target = arguments[arity];
65
66 if (target != null && _isFunction(target[method])) {
67 return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity));
68 }
69
70 throw new TypeError(toString(target) + ' does not have a method named "' + method + '"');
71 });
72});
73
74module.exports = invoker;
Note: See TracBrowser for help on using the repository browser.