[d24f17c] | 1 | import { curryN, path, apply, init, bind } from 'ramda';
|
---|
| 2 |
|
---|
| 3 | import isNotFunction from './isNotFunction';
|
---|
| 4 | import 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 |
|
---|
| 24 | const 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 |
|
---|
| 36 | export default invokeArgs;
|
---|