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