or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--method

Creates a function that invokes a method at a given path of an object with provided arguments.

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

To install, run

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

index.mddocs/

lodash.method

The lodash _.method function creates a function that invokes a method at a given path of an object, with any additional arguments provided to the invoked method. It's designed for functional programming patterns where you need to create reusable method invokers for object properties, particularly useful with array methods like _.map to invoke methods on collections of objects.

Package Information

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

Core Imports

var method = require('lodash.method');

For ES modules (when using bundlers like Webpack that support CommonJS interop):

import method from 'lodash.method';

Basic Usage

var method = require('lodash.method');

// Official example from lodash documentation
var objects = [
  { 'a': { 'b': _.constant(2) } },
  { 'a': { 'b': _.constant(1) } }
];

_.map(objects, _.method('a.b'));
// => [2, 1]

_.map(objects, _.method(['a', 'b']));
// => [2, 1]

// More practical examples without lodash dependencies
var objects = [
  { 'a': { 'b': function() { return 2; } } },
  { 'a': { 'b': function() { return 1; } } }
];

// Create a method invoker that calls 'a.b' method on each object
var invoker = method('a.b');
var results = objects.map(invoker);
// => [2, 1]

// With method arguments
var objectsWithArgs = [
  { 'a': { 'b': function(multiplier) { return 2 * multiplier; } } },
  { 'a': { 'b': function(multiplier) { return 1 * multiplier; } } }
];

var invokerWithArgs = method('a.b', 3);
var results = objectsWithArgs.map(invokerWithArgs);
// => [6, 3]

Capabilities

Method Invoker Creation

Creates a function that invokes a method at a given path of an object with any additional arguments provided to the invoked method.

/**
 * Creates a function that invokes the method at `path` of a given object.
 * Any additional arguments are provided to the invoked method.
 *
 * @param {Array|string} path The path of the method to invoke.
 * @param {...*} [args] The arguments to invoke the method with.
 * @returns {Function} Returns the new invoker function.
 */
function method(path, ...args);

// Return type signature:
type InvokerFunction = (object: any) => any;

Parameters:

  • path (Array|string): The path of the method to invoke. Can be a dot-separated string like 'a.b.c' or an array like ['a', 'b', 'c']
  • ...args (...*): Optional arguments to pass to the invoked method

Returns:

  • Function: A new function that takes an object and invokes the method at the specified path with the provided arguments

Usage Examples:

var method = require('lodash.method');

// String path
var getValue = method('getValue');
var obj = { getValue: function() { return 'hello'; } };
getValue(obj); // => 'hello'

// Deep path with dot notation
var getNestedValue = method('nested.getValue');
var obj = { nested: { getValue: function() { return 'nested hello'; } } };
getNestedValue(obj); // => 'nested hello'

// Array path notation
var getNestedValue2 = method(['nested', 'getValue']);
var obj = { nested: { getValue: function() { return 'nested hello'; } } };
getNestedValue2(obj); // => 'nested hello'

// With method arguments
var multiply = method('calculate', 2, 3);
var calculator = { calculate: function(a, b) { return a * b; } };
multiply(calculator); // => 6

// Common use case with array methods
var objects = [
  { name: 'Alice', greet: function(greeting) { return greeting + ', ' + this.name; } },
  { name: 'Bob', greet: function(greeting) { return greeting + ', ' + this.name; } }
];

var greeter = method('greet', 'Hello');
var greetings = objects.map(greeter);
// => ['Hello, Alice', 'Hello, Bob']

Path Resolution:

  • Supports both string paths ('a.b.c') and array paths (['a', 'b', 'c'])
  • Handles deep property access safely
  • Returns undefined if the path doesn't exist or the method is not a function
  • Properly binds the method context to the object being invoked on