- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.4.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
index.md docs/
1# Lodash23Lodash 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.45## Package Information67- **Package Name**: lodash8- **Package Type**: npm9- **Language**: JavaScript10- **Installation**: `npm install lodash`1112## Core Imports1314```javascript15// ES Modules - Full library16import _ from "lodash";1718// ES Modules - Named imports (recommended)19import { forEach, map, filter, get, set } from "lodash";2021// ES Modules - Category imports22import { chunk, compact, flatten } from "lodash/array";23import { camelCase, kebabCase } from "lodash/string";24```2526For CommonJS:2728```javascript29// CommonJS - Full library30const _ = require("lodash");3132// CommonJS - Named destructuring33const { forEach, map, filter, get, set } = require("lodash");34```3536## Basic Usage3738```javascript39import { map, filter, get, debounce, camelCase } from "lodash";4041// Array operations42const numbers = [1, 2, 3, 4, 5];43const doubled = map(numbers, n => n * 2);44const evens = filter(numbers, n => n % 2 === 0);4546// Object operations47const user = { profile: { name: "Alice", age: 30 } };48const name = get(user, "profile.name"); // "Alice"49const missing = get(user, "profile.email", "No email"); // "No email"5051// Function utilities52const debouncedSave = debounce(() => {53console.log("Saving...");54}, 300);5556// String utilities57const title = camelCase("hello world"); // "helloWorld"58```5960## Architecture6162Lodash is built around several key design principles:6364- **Functional Programming**: Pure functions without side effects65- **Modularity**: Each function is available as a separate module66- **Performance**: Optimized for speed and memory efficiency67- **Consistency**: Uniform API across all utilities68- **Iteratee Support**: Functions accept various iteratee formats (functions, objects, strings)6970The library is organized into distinct categories, each focusing on specific data types and operations.7172## Capabilities7374### Collection Operations7576Core iteration and transformation functions for arrays and objects.7778```javascript { .api }79/**80* Iterates over elements of collection and invokes iteratee for each element81*/82function forEach(collection, iteratee);8384/**85* Creates an array of values by running each element through iteratee86*/87function map(collection, iteratee);8889/**90* Iterates over elements, returning an array of all elements predicate returns truthy for91*/92function filter(collection, predicate);9394/**95* Reduces collection to a value which is the accumulated result of running each element through iteratee96*/97function reduce(collection, iteratee, accumulator);9899/**100* Iterates over elements, returning the first element predicate returns truthy for101*/102function find(collection, predicate);103104/**105* Checks if predicate returns truthy for any element of collection106*/107function some(collection, predicate);108109/**110* Checks if predicate returns truthy for all elements of collection111*/112function every(collection, predicate);113114/**115* Checks if value is in collection116*/117function includes(collection, value, fromIndex);118```119120### Object Manipulation121122Functions for working with object properties, nested data, and object transformations.123124```javascript { .api }125/**126* Gets the value at path of object127*/128function get(object, path, defaultValue);129130/**131* Sets the value at path of object132*/133function set(object, path, value);134135/**136* Checks if path is a direct property of object137*/138function has(object, path);139140/**141* Creates an object composed of the picked object properties142*/143function pick(object, ...paths);144145/**146* Creates an object composed of properties that are not omitted147*/148function omit(object, ...paths);149150/**151* Recursively merges own and inherited enumerable string keyed properties152*/153function merge(object, ...sources);154155/**156* Assigns own enumerable string keyed properties of source objects to destination object157*/158function assign(object, ...sources);159160/**161* Creates an array of the own enumerable property names of object162*/163function keys(object);164165/**166* Creates an array of the own enumerable string keyed property values of object167*/168function values(object);169170/**171* Iterates over own enumerable string keyed properties of an object172*/173function forOwn(object, iteratee);174```175176### Array Operations177178Specialized functions for array manipulation, filtering, and transformation.179180```javascript { .api }181/**182* Creates an array of elements split into groups the length of size183*/184function chunk(array, size);185186/**187* Flattens array a single level deep188*/189function flatten(array);190191/**192* Creates a duplicate-free version of an array193*/194function uniq(array);195196/**197* Creates an array of array values not included in the other given arrays198*/199function difference(array, ...values);200201/**202* Creates an array of unique values that are included in all given arrays203*/204function intersection(...arrays);205206/**207* Creates an array with all falsey values removed208*/209function compact(array);210```211212### Type Checking213214Utility functions for checking data types and object characteristics.215216```javascript { .api }217/**218* Checks if value is classified as an Array object219*/220function isArray(value);221222/**223* Checks if value is the language type of Object224*/225function isObject(value);226227/**228* Checks if value is an empty object, collection, map, or set229*/230function isEmpty(value);231232/**233* Performs a deep comparison between two values to determine if they are equivalent234*/235function isEqual(value, other);236237/**238* Checks if value is classified as a String primitive or object239*/240function isString(value);241242/**243* Checks if value is classified as a Number primitive or object244*/245function isNumber(value);246```247248### Function Utilities249250Higher-order functions for controlling function execution, caching, and composition.251252```javascript { .api }253/**254* Creates a debounced function that delays invoking func until after wait milliseconds255*/256function debounce(func, wait, options);257258/**259* Creates a throttled function that only invokes func at most once per every wait milliseconds260*/261function throttle(func, wait, options);262263/**264* Creates a function that memoizes the result of func265*/266function memoize(func, resolver);267268/**269* Creates a function that is restricted to invoking func once270*/271function once(func);272```273274### String Manipulation275276Functions for transforming, formatting, and analyzing strings.277278```javascript { .api }279/**280* Converts string to camel case281*/282function camelCase(string);283284/**285* Converts string to kebab case286*/287function kebabCase(string);288289/**290* Converts the first character of string to upper case and the remaining to lower case291*/292function capitalize(string);293294/**295* Removes leading and trailing whitespace or specified characters from string296*/297function trim(string, chars);298```299300### Mathematical Operations301302Utility functions for mathematical calculations and number operations.303304```javascript { .api }305/**306* Adds two numbers307*/308function add(augend, addend);309310/**311* Subtract two numbers312*/313function subtract(minuend, subtrahend);314315/**316* Computes the maximum value of array317*/318function max(array);319320/**321* Computes the minimum value of array322*/323function min(array);324325/**326* Computes the sum of the values in array327*/328function sum(array);329```330331## Common Usage Patterns332333### Chaining Operations334```javascript335import _ from "lodash";336337const result = _([1, 2, 3, 4, 5])338.map(n => n * 2)339.filter(n => n > 5)340.value();341// [6, 8, 10]342```343344### Working with Nested Data345```javascript346import { get, set, has } from "lodash";347348const data = {349users: [350{ id: 1, profile: { name: "Alice", settings: { theme: "dark" } } },351{ id: 2, profile: { name: "Bob" } }352]353};354355// Safe property access356const aliceTheme = get(data, "users[0].profile.settings.theme", "light");357const bobTheme = get(data, "users[1].profile.settings.theme", "light");358359// Setting nested properties360set(data, "users[1].profile.settings.theme", "dark");361362// Checking property existence363const hasSettings = has(data, "users[0].profile.settings");364```365366### Data Transformation Pipelines367```javascript368import { map, filter, groupBy, sortBy } from "lodash";369370const 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];375376// Transform and group data377const completedOrdersByCustomer = groupBy(378filter(orders, { status: "completed" }),379"customer"380);381382// Calculate totals383const customerTotals = map(completedOrdersByCustomer, (orders, customer) => ({384customer,385total: sum(map(orders, "amount")),386orderCount: orders.length387}));388```389390## Performance Considerations391392- **Modular Imports**: Import only needed functions to reduce bundle size393- **Memoization**: Use `memoize` for expensive computations394- **Batch Operations**: Prefer single operations over multiple iterations395- **Chain Optimization**: Use lazy evaluation with lodash chains for better performance on large datasets