source: node_modules/ramda-adjunct/src/invokeArgs.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: 1.0 KB
Line 
1import { curryN, path, apply, init, bind } from 'ramda';
2
3import isNotFunction from './isNotFunction';
4import isEmptyArray from './isEmptyArray';
5
6/**
7 * Invokes the method at path of object with given arguments.
8 *
9 * @func invokeArgs
10 * @memberOf RA
11 * @since {@link https://char0n.github.io/ramda-adjunct/2.27.0|v2.27.0}
12 * @category Object
13 * @sig Array -> Array -> Object -> *
14 * @param {Array.<string|number>} path The path of the method to invoke
15 * @param {Array} args The arguments to invoke the method with
16 * @param {Object} obj The object to query
17 * @return {*}
18 * @example
19 *
20 * RA.invokeArgs(['abs'], [-1], Math); //=> 1
21 * RA.invokeArgs(['path', 'to', 'non-existent', 'method'], [-1], Math); //=> undefined
22 */
23
24const invokeArgs = curryN(3, (mpath, args, obj) => {
25 const method = path(mpath, obj);
26 const context = path(init(mpath), obj);
27
28 if (isNotFunction(method)) return undefined;
29 if (isEmptyArray(mpath)) return undefined;
30
31 const boundMethod = bind(method, context);
32
33 return apply(boundMethod, args);
34});
35
36export default invokeArgs;
Note: See TracBrowser for help on using the repository browser.