invoke()
invoke<
T>(object,path,args?):unknown
Invokes the method at path of object with the given arguments.
DEPRECATED
Use get(obj, path)?.(...args) or optional chaining directly instead.
Type Parametersβ
T: Tβ
Parametersβ
object: Tβ
The object to query.
path: string | (string | number | symbol)[]β
The path of the method to invoke.
args?: unknown[] = []β
The arguments to invoke the method with.
Returns: unknownβ
The result of the invoked method.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
invoke (Lodash, Antfu) Β· β (es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash)
Exampleβ
const object = {
a: {
b: {
greet: (name: string) => `Hello, ${name}!`
}
}
};
// β Deprecated approach
invoke(object, 'a.b.greet', ['World']);
// => 'Hello, World!'
// β
Recommended approach
get(object, 'a.b.greet')?.('World');
// => 'Hello, World!'
// Or with optional chaining
object.a?.b?.greet?.('World');
// => 'Hello, World!'
How it works?β
Invokes method at path with given arguments.
Processing Flowβ
Common Inputsβ
| Object | Path | Args | Result |
|---|---|---|---|
{greet: n => 'Hi ' + n} | 'greet' | ['Bob'] | 'Hi Bob' |
{a: 1} | 'a' | [] | undefined |
Deprecated: Use
get(obj, path)?.(...args)or optional chaining.
Use Casesβ
Call method at path πβ
Invoke method at nested path.
import { get } from '@pithos/arkhe';
const method = get(obj, 'a.b.greet');
const result = typeof method === 'function' ? method('World') : undefined;
Optional method callβ
Safely call method if exists.
obj.a?.b?.greet?.('World');
Deprecated: Use