Back to search
Author
tessl
Last updated
Spec files

npm-lodash

Description
A modern JavaScript utility library delivering modularity, performance & extras
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/npm-lodash@4.4.0

index.md docs/

1
# Lodash
2
3
Lodash is a modern JavaScript utility library delivering modularity, performance & extras. It provides utility functions for common programming tasks using the functional programming paradigm. Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
4
5
## Package Information
6
7
- **Package Name**: lodash
8
- **Package Type**: npm
9
- **Language**: JavaScript
10
- **Installation**: `npm install lodash`
11
12
## Core Imports
13
14
```javascript
15
// ES Modules - Full library
16
import _ from "lodash";
17
18
// ES Modules - Named imports (recommended)
19
import { forEach, map, filter, get, set } from "lodash";
20
21
// ES Modules - Category imports
22
import { chunk, compact, flatten } from "lodash/array";
23
import { camelCase, kebabCase } from "lodash/string";
24
```
25
26
For CommonJS:
27
28
```javascript
29
// CommonJS - Full library
30
const _ = require("lodash");
31
32
// CommonJS - Named destructuring
33
const { forEach, map, filter, get, set } = require("lodash");
34
```
35
36
## Basic Usage
37
38
```javascript
39
import { map, filter, get, debounce, camelCase } from "lodash";
40
41
// Array operations
42
const numbers = [1, 2, 3, 4, 5];
43
const doubled = map(numbers, n => n * 2);
44
const evens = filter(numbers, n => n % 2 === 0);
45
46
// Object operations
47
const user = { profile: { name: "Alice", age: 30 } };
48
const name = get(user, "profile.name"); // "Alice"
49
const missing = get(user, "profile.email", "No email"); // "No email"
50
51
// Function utilities
52
const debouncedSave = debounce(() => {
53
console.log("Saving...");
54
}, 300);
55
56
// String utilities
57
const title = camelCase("hello world"); // "helloWorld"
58
```
59
60
## Architecture
61
62
Lodash is built around several key design principles:
63
64
- **Functional Programming**: Pure functions without side effects
65
- **Modularity**: Each function is available as a separate module
66
- **Performance**: Optimized for speed and memory efficiency
67
- **Consistency**: Uniform API across all utilities
68
- **Iteratee Support**: Functions accept various iteratee formats (functions, objects, strings)
69
70
The library is organized into distinct categories, each focusing on specific data types and operations.
71
72
## Capabilities
73
74
### Collection Operations
75
76
Core iteration and transformation functions for arrays and objects.
77
78
```javascript { .api }
79
/**
80
* Iterates over elements of collection and invokes iteratee for each element
81
*/
82
function forEach(collection, iteratee);
83
84
/**
85
* Creates an array of values by running each element through iteratee
86
*/
87
function map(collection, iteratee);
88
89
/**
90
* Iterates over elements, returning an array of all elements predicate returns truthy for
91
*/
92
function filter(collection, predicate);
93
94
/**
95
* Reduces collection to a value which is the accumulated result of running each element through iteratee
96
*/
97
function reduce(collection, iteratee, accumulator);
98
99
/**
100
* Iterates over elements, returning the first element predicate returns truthy for
101
*/
102
function find(collection, predicate);
103
104
/**
105
* Checks if predicate returns truthy for any element of collection
106
*/
107
function some(collection, predicate);
108
109
/**
110
* Checks if predicate returns truthy for all elements of collection
111
*/
112
function every(collection, predicate);
113
114
/**
115
* Checks if value is in collection
116
*/
117
function includes(collection, value, fromIndex);
118
```
119
120
### Object Manipulation
121
122
Functions for working with object properties, nested data, and object transformations.
123
124
```javascript { .api }
125
/**
126
* Gets the value at path of object
127
*/
128
function get(object, path, defaultValue);
129
130
/**
131
* Sets the value at path of object
132
*/
133
function set(object, path, value);
134
135
/**
136
* Checks if path is a direct property of object
137
*/
138
function has(object, path);
139
140
/**
141
* Creates an object composed of the picked object properties
142
*/
143
function pick(object, ...paths);
144
145
/**
146
* Creates an object composed of properties that are not omitted
147
*/
148
function omit(object, ...paths);
149
150
/**
151
* Recursively merges own and inherited enumerable string keyed properties
152
*/
153
function merge(object, ...sources);
154
155
/**
156
* Assigns own enumerable string keyed properties of source objects to destination object
157
*/
158
function assign(object, ...sources);
159
160
/**
161
* Creates an array of the own enumerable property names of object
162
*/
163
function keys(object);
164
165
/**
166
* Creates an array of the own enumerable string keyed property values of object
167
*/
168
function values(object);
169
170
/**
171
* Iterates over own enumerable string keyed properties of an object
172
*/
173
function forOwn(object, iteratee);
174
```
175
176
### Array Operations
177
178
Specialized functions for array manipulation, filtering, and transformation.
179
180
```javascript { .api }
181
/**
182
* Creates an array of elements split into groups the length of size
183
*/
184
function chunk(array, size);
185
186
/**
187
* Flattens array a single level deep
188
*/
189
function flatten(array);
190
191
/**
192
* Creates a duplicate-free version of an array
193
*/
194
function uniq(array);
195
196
/**
197
* Creates an array of array values not included in the other given arrays
198
*/
199
function difference(array, ...values);
200
201
/**
202
* Creates an array of unique values that are included in all given arrays
203
*/
204
function intersection(...arrays);
205
206
/**
207
* Creates an array with all falsey values removed
208
*/
209
function compact(array);
210
```
211
212
### Type Checking
213
214
Utility functions for checking data types and object characteristics.
215
216
```javascript { .api }
217
/**
218
* Checks if value is classified as an Array object
219
*/
220
function isArray(value);
221
222
/**
223
* Checks if value is the language type of Object
224
*/
225
function isObject(value);
226
227
/**
228
* Checks if value is an empty object, collection, map, or set
229
*/
230
function isEmpty(value);
231
232
/**
233
* Performs a deep comparison between two values to determine if they are equivalent
234
*/
235
function isEqual(value, other);
236
237
/**
238
* Checks if value is classified as a String primitive or object
239
*/
240
function isString(value);
241
242
/**
243
* Checks if value is classified as a Number primitive or object
244
*/
245
function isNumber(value);
246
```
247
248
### Function Utilities
249
250
Higher-order functions for controlling function execution, caching, and composition.
251
252
```javascript { .api }
253
/**
254
* Creates a debounced function that delays invoking func until after wait milliseconds
255
*/
256
function debounce(func, wait, options);
257
258
/**
259
* Creates a throttled function that only invokes func at most once per every wait milliseconds
260
*/
261
function throttle(func, wait, options);
262
263
/**
264
* Creates a function that memoizes the result of func
265
*/
266
function memoize(func, resolver);
267
268
/**
269
* Creates a function that is restricted to invoking func once
270
*/
271
function once(func);
272
```
273
274
### String Manipulation
275
276
Functions for transforming, formatting, and analyzing strings.
277
278
```javascript { .api }
279
/**
280
* Converts string to camel case
281
*/
282
function camelCase(string);
283
284
/**
285
* Converts string to kebab case
286
*/
287
function kebabCase(string);
288
289
/**
290
* Converts the first character of string to upper case and the remaining to lower case
291
*/
292
function capitalize(string);
293
294
/**
295
* Removes leading and trailing whitespace or specified characters from string
296
*/
297
function trim(string, chars);
298
```
299
300
### Mathematical Operations
301
302
Utility functions for mathematical calculations and number operations.
303
304
```javascript { .api }
305
/**
306
* Adds two numbers
307
*/
308
function add(augend, addend);
309
310
/**
311
* Subtract two numbers
312
*/
313
function subtract(minuend, subtrahend);
314
315
/**
316
* Computes the maximum value of array
317
*/
318
function max(array);
319
320
/**
321
* Computes the minimum value of array
322
*/
323
function min(array);
324
325
/**
326
* Computes the sum of the values in array
327
*/
328
function sum(array);
329
```
330
331
## Common Usage Patterns
332
333
### Chaining Operations
334
```javascript
335
import _ from "lodash";
336
337
const result = _([1, 2, 3, 4, 5])
338
.map(n => n * 2)
339
.filter(n => n > 5)
340
.value();
341
// [6, 8, 10]
342
```
343
344
### Working with Nested Data
345
```javascript
346
import { get, set, has } from "lodash";
347
348
const data = {
349
users: [
350
{ id: 1, profile: { name: "Alice", settings: { theme: "dark" } } },
351
{ id: 2, profile: { name: "Bob" } }
352
]
353
};
354
355
// Safe property access
356
const aliceTheme = get(data, "users[0].profile.settings.theme", "light");
357
const bobTheme = get(data, "users[1].profile.settings.theme", "light");
358
359
// Setting nested properties
360
set(data, "users[1].profile.settings.theme", "dark");
361
362
// Checking property existence
363
const hasSettings = has(data, "users[0].profile.settings");
364
```
365
366
### Data Transformation Pipelines
367
```javascript
368
import { map, filter, groupBy, sortBy } from "lodash";
369
370
const orders = [
371
{ id: 1, status: "completed", amount: 100, customer: "Alice" },
372
{ id: 2, status: "pending", amount: 200, customer: "Bob" },
373
{ id: 3, status: "completed", amount: 150, customer: "Alice" }
374
];
375
376
// Transform and group data
377
const completedOrdersByCustomer = groupBy(
378
filter(orders, { status: "completed" }),
379
"customer"
380
);
381
382
// Calculate totals
383
const customerTotals = map(completedOrdersByCustomer, (orders, customer) => ({
384
customer,
385
total: sum(map(orders, "amount")),
386
orderCount: orders.length
387
}));
388
```
389
390
## Performance Considerations
391
392
- **Modular Imports**: Import only needed functions to reduce bundle size
393
- **Memoization**: Use `memoize` for expensive computations
394
- **Batch Operations**: Prefer single operations over multiple iterations
395
- **Chain Optimization**: Use lazy evaluation with lodash chains for better performance on large datasets