or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

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

0

# lodash.method

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash.method

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.method`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

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

18

19

```javascript

20

import method from 'lodash.method';

21

```

22

23

## Basic Usage

24

25

```javascript

26

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

27

28

// Official example from lodash documentation

29

var objects = [

30

{ 'a': { 'b': _.constant(2) } },

31

{ 'a': { 'b': _.constant(1) } }

32

];

33

34

_.map(objects, _.method('a.b'));

35

// => [2, 1]

36

37

_.map(objects, _.method(['a', 'b']));

38

// => [2, 1]

39

40

// More practical examples without lodash dependencies

41

var objects = [

42

{ 'a': { 'b': function() { return 2; } } },

43

{ 'a': { 'b': function() { return 1; } } }

44

];

45

46

// Create a method invoker that calls 'a.b' method on each object

47

var invoker = method('a.b');

48

var results = objects.map(invoker);

49

// => [2, 1]

50

51

// With method arguments

52

var objectsWithArgs = [

53

{ 'a': { 'b': function(multiplier) { return 2 * multiplier; } } },

54

{ 'a': { 'b': function(multiplier) { return 1 * multiplier; } } }

55

];

56

57

var invokerWithArgs = method('a.b', 3);

58

var results = objectsWithArgs.map(invokerWithArgs);

59

// => [6, 3]

60

```

61

62

## Capabilities

63

64

### Method Invoker Creation

65

66

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

67

68

```javascript { .api }

69

/**

70

* Creates a function that invokes the method at `path` of a given object.

71

* Any additional arguments are provided to the invoked method.

72

*

73

* @param {Array|string} path The path of the method to invoke.

74

* @param {...*} [args] The arguments to invoke the method with.

75

* @returns {Function} Returns the new invoker function.

76

*/

77

function method(path, ...args);

78

79

// Return type signature:

80

type InvokerFunction = (object: any) => any;

81

```

82

83

**Parameters:**

84

- `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']`

85

- `...args` (...*): Optional arguments to pass to the invoked method

86

87

**Returns:**

88

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

89

90

**Usage Examples:**

91

92

```javascript

93

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

94

95

// String path

96

var getValue = method('getValue');

97

var obj = { getValue: function() { return 'hello'; } };

98

getValue(obj); // => 'hello'

99

100

// Deep path with dot notation

101

var getNestedValue = method('nested.getValue');

102

var obj = { nested: { getValue: function() { return 'nested hello'; } } };

103

getNestedValue(obj); // => 'nested hello'

104

105

// Array path notation

106

var getNestedValue2 = method(['nested', 'getValue']);

107

var obj = { nested: { getValue: function() { return 'nested hello'; } } };

108

getNestedValue2(obj); // => 'nested hello'

109

110

// With method arguments

111

var multiply = method('calculate', 2, 3);

112

var calculator = { calculate: function(a, b) { return a * b; } };

113

multiply(calculator); // => 6

114

115

// Common use case with array methods

116

var objects = [

117

{ name: 'Alice', greet: function(greeting) { return greeting + ', ' + this.name; } },

118

{ name: 'Bob', greet: function(greeting) { return greeting + ', ' + this.name; } }

119

];

120

121

var greeter = method('greet', 'Hello');

122

var greetings = objects.map(greeter);

123

// => ['Hello, Alice', 'Hello, Bob']

124

```

125

126

**Path Resolution:**

127

- Supports both string paths (`'a.b.c'`) and array paths (`['a', 'b', 'c']`)

128

- Handles deep property access safely

129

- Returns `undefined` if the path doesn't exist or the method is not a function

130

- Properly binds the method context to the object being invoked on