0
# Lodash Identity
1
2
Lodash Identity provides the identity utility function from the lodash library as a standalone Node.js module. The identity function is a fundamental utility that returns its first argument unchanged, making it essential for functional programming patterns, default callbacks, and placeholder operations.
3
4
## Package Information
5
6
- **Package Name**: lodash.identity
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.identity`
10
11
## Core Imports
12
13
```javascript
14
const identity = require('lodash.identity');
15
```
16
17
ESM:
18
19
```javascript
20
import identity from 'lodash.identity';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const identity = require('lodash.identity');
27
28
// Basic usage - returns the input unchanged
29
const value = identity(42);
30
console.log(value); // => 42
31
32
const object = { a: 1, b: 2 };
33
console.log(identity(object) === object); // => true
34
35
// Common usage as a default callback
36
const users = [
37
{ name: 'Alice', active: true },
38
{ name: 'Bob', active: false },
39
{ name: 'Charlie', active: true }
40
];
41
42
// Use as default transformation function - filter truthy values
43
const activeUsers = users.filter(user => user.active);
44
45
// Use in array operations where no transformation is needed
46
const numbers = [1, 2, 3];
47
const sameNumbers = numbers.map(identity);
48
console.log(numbers === sameNumbers); // => false (different array, same values)
49
```
50
51
## Capabilities
52
53
### Identity Function
54
55
Returns the first argument it receives unchanged. This function is commonly used as a default transformation function in functional programming patterns and as a placeholder when no transformation is needed.
56
57
```javascript { .api }
58
/**
59
* This method returns the first argument it receives.
60
*
61
* @param {*} value - Any value of any type
62
* @returns {*} Returns the input value unchanged
63
* @since 0.1.0
64
*/
65
function identity(value);
66
```
67
68
**Parameters:**
69
- `value` (*): Any value - can be any JavaScript type including primitives, objects, arrays, functions, etc.
70
71
**Returns:**
72
- (*): Returns the exact same value that was passed in, with no modifications
73
74
**Usage Examples:**
75
76
```javascript
77
const identity = require('lodash.identity');
78
79
// With primitives
80
identity(1); // => 1
81
identity('hello'); // => 'hello'
82
identity(true); // => true
83
84
// With objects (returns same reference)
85
const obj = { name: 'test' };
86
identity(obj) === obj; // => true
87
88
// With arrays (returns same reference)
89
const arr = [1, 2, 3];
90
identity(arr) === arr; // => true
91
92
// With functions
93
const fn = () => 'hello';
94
identity(fn) === fn; // => true
95
96
// Common functional programming patterns
97
const data = [1, 2, 3];
98
99
// As a default callback
100
const transformed = data.map(identity); // [1, 2, 3]
101
102
// In filter operations (truthy values)
103
const values = [1, 0, 'hello', '', true, false];
104
const truthyValues = values.filter(identity); // [1, 'hello', true]
105
106
// As a default transformation in higher-order functions
107
function processData(data, transform = identity) {
108
return data.map(transform);
109
}
110
111
processData([1, 2, 3]); // [1, 2, 3] (no transformation)
112
processData([1, 2, 3], x => x * 2); // [2, 4, 6] (with transformation)
113
```
114
115
**Typical Use Cases:**
116
117
1. **Default callback function**: When a function accepts an optional transformation callback
118
2. **Functional composition**: As a neutral element in function composition
119
3. **Array operations**: When you need to maintain array structure without transformation
120
4. **Filter operations**: To filter truthy values from arrays
121
5. **Placeholder function**: When an API requires a function but no operation is needed
122
6. **Type preservation**: In generic functions where the input type should be preserved