or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-method

Creates functions for invoking methods at paths on objects, including method and methodOf utilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.method@3.7.x

To install, run

npx @tessl/cli install tessl/npm-lodash-method@3.7.0

index.mddocs/

Lodash Method

Lodash Method provides utility functions that create higher-order functions for invoking methods at specified paths on objects. It includes both method and methodOf functions that enable functional programming patterns by allowing developers to create reusable method invokers that can be passed to higher-order functions like map, filter, or forEach.

Package Information

  • Package Name: lodash.method
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.method

Core Imports

ESM:

import method from "lodash.method";

CommonJS:

const method = require("lodash.method");

For accessing both method and methodOf:

import { method, methodOf } from "lodash";
// or
const { method, methodOf } = require("lodash");

Basic Usage

import method from "lodash.method";

// Create objects with methods at nested paths
const objects = [
  { a: { b: { c: function() { return 2; } } } },
  { a: { b: { c: function() { return 1; } } } }
];

// Create a method invoker for the nested path
const invokeABC = method('a.b.c');

// Use with array methods
const results = objects.map(invokeABC);
// => [2, 1]

// Works with array paths too
const invokeArray = method(['a', 'b', 'c']);
const arrayResults = objects.map(invokeArray);
// => [2, 1]

// Pass additional arguments to the invoked method
const objectsWithArgs = [
  { fn: function(x, y) { return x + y + this.base; }, base: 10 },
  { fn: function(x, y) { return x + y + this.base; }, base: 20 }
];

const invokeWithArgs = method('fn', 1, 2);
const argResults = objectsWithArgs.map(invokeWithArgs);
// => [13, 23]

// methodOf - the opposite of method
import { methodOf } from "lodash";

const object = { 
  a: [function() { return 'a0'; }, function() { return 'a1'; }, function() { return 'a2'; }],
  b: [function() { return 'b0'; }, function() { return 'b1'; }]
};

// Create a function that invokes methods on the object at different paths
const invokeOnObject = methodOf(object);

// Use with different paths
const paths = ['a[2]', 'b[0]'];
const methodOfResults = paths.map(invokeOnObject);
// => ['a2', 'b0']

Capabilities

Method Path Invocation

Creates a function which invokes the method at the specified path on a given object.

/**
 * Creates a function which invokes the method at `path` on a given object.
 * @param {Array|string} path - The path of the method to invoke
 * @param {...*} args - Additional arguments to pass to the invoked method
 * @returns {Function} Returns the new function that accepts an object and invokes the method
 */
function method(path, ...args);

Path Resolution:

  • Supports both string paths (e.g., 'a.b.c') and array paths (e.g., ['a', 'b', 'c'])
  • Handles deep property access safely
  • Direct keys take precedence over nested paths (if object has both 'a.b.c' key and nested a.b.c, uses the direct key)
  • Works with inherited properties
  • Returns undefined for nullish objects or missing methods

Method Invocation:

  • Preserves correct this binding when invoking methods
  • Supports passing additional arguments to the invoked method
  • Arguments are applied consistently across all invocations

Usage Examples:

// String path vs array path
const obj = { 
  method: function() { return 'called'; },
  nested: { method: function() { return 'nested called'; } }
};

method('method')(obj);           // => 'called'
method(['method'])(obj);         // => 'called'
method('nested.method')(obj);    // => 'nested called'
method(['nested', 'method'])(obj); // => 'nested called'

// Key precedence example
const ambiguous = {
  'a.b': function() { return 'direct key'; },
  a: { b: function() { return 'nested path'; } }
};

method('a.b')(ambiguous);        // => 'direct key' (direct key wins)

// Null/undefined handling
method('toString')(null);        // => undefined
method('deep.method')(undefined); // => undefined

// Arguments passing
const calculator = {
  add: function(x, y) { return x + y; },
  base: 100,
  addWithBase: function(x, y) { return x + y + this.base; }
};

method('add', 5, 3)(calculator);        // => 8
method('addWithBase', 5, 3)(calculator); // => 108

// Inherited properties
function MyClass() {}
MyClass.prototype.getValue = function() { return 42; };

const instance = new MyClass();
method('getValue')(instance);    // => 42

// Non-string paths (coerced to strings)
const arrayLike = {
  0: function() { return 'index 0'; },
  1: function() { return 'index 1'; }
};

method(0)(arrayLike);           // => 'index 0'
method([1])(arrayLike);         // => 'index 1'

Method Object Invocation

The opposite of method; creates a function which invokes methods at given paths on a specific object.

/**
 * The opposite of `method`; creates a function which invokes 
 * methods at given paths on `object`.
 * @param {Object} object - The object to query
 * @param {...*} args - Additional arguments to pass to the invoked method
 * @returns {Function} Returns a function that accepts a path and invokes the method
 */
function methodOf(object, ...args);

Object Binding:

  • Binds the function to a specific object (opposite of method)
  • Accepts paths dynamically when the returned function is called
  • Supports the same path types as method (string and array paths)
  • Maintains correct this binding when invoking methods

Usage Examples:

// Basic methodOf usage
const array = [
  function() { return 'first'; },
  function() { return 'second'; },
  function() { return 'third'; }
];

const object = { a: array, b: array, c: array };

// Create a method invoker bound to the object
const invokeOnObject = methodOf(object);

// Use with different paths
invokeOnObject('a[0]');  // => 'first'
invokeOnObject('b[1]');  // => 'second'
invokeOnObject(['c', '2']); // => 'third'

// With array methods
const paths = ['a[0]', 'b[1]', 'c[2]'];
const results = paths.map(methodOf(object));
// => ['first', 'second', 'third']

// With arguments
const calculator = {
  multiply: function(x, y) { return x * y * this.factor; },
  factor: 10
};

const calc = methodOf(calculator, 5, 3);
calc('multiply'); // => 150

// Null/undefined handling
methodOf(null)('toString');        // => undefined
methodOf(undefined)('deep.method'); // => undefined

Types

/**
 * The method function type
 * @typedef {Function} MethodFunction
 * @param {Array|string} path - The path of the method to invoke
 * @param {...*} args - Additional arguments to pass to the invoked method
 * @returns {Function} Returns a function that accepts an object and invokes the method at path
 */

/**
 * The returned invoker function type
 * @typedef {Function} MethodInvoker
 * @param {Object} object - The object to invoke the method on
 * @returns {*} The result of the method invocation, or undefined if the method doesn't exist
 */

/**
 * The methodOf function type
 * @typedef {Function} MethodOfFunction
 * @param {Object} object - The object to query
 * @param {...*} args - Additional arguments to pass to the invoked method
 * @returns {Function} Returns a function that accepts a path and invokes the method on object
 */

/**
 * The returned path invoker function type from methodOf
 * @typedef {Function} PathInvoker
 * @param {Array|string} path - The path of the method to invoke
 * @returns {*} The result of the method invocation, or undefined if the method doesn't exist
 */