- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.6.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
function.md docs/
1# Function Utilities23Function composition, currying, debouncing, throttling, and other functional programming utilities. These methods help create more flexible and performant functions.45## Capabilities67### Function Control89Control when and how often functions execute.1011```javascript { .api }12/**13* Creates a debounced function that delays invoking `func` until after `wait`14* milliseconds have elapsed since the last time the debounced function was invoked.15*16* @param {Function} func The function to debounce17* @param {number} [wait=0] The number of milliseconds to delay18* @param {Object} [options] The options object19* @returns {Function} Returns the new debounced function20*/21function debounce(func: Function, wait?: number, options?: Object): Function;2223/**24* Creates a throttled function that only invokes `func` at most once per25* every `wait` milliseconds.26*27* @param {Function} func The function to throttle28* @param {number} [wait=0] The number of milliseconds to throttle invocations to29* @param {Object} [options] The options object30* @returns {Function} Returns the new throttled function31*/32function throttle(func: Function, wait?: number, options?: Object): Function;33```3435**Usage Examples:**3637```javascript38import { debounce, throttle } from "lodash";3940// Debounce - waits for pause in calls41const debouncedSave = debounce(saveFunction, 1000);42// Will only call saveFunction 1 second after the last call4344// Throttle - limits call frequency45const throttledScroll = throttle(onScroll, 100);46// Will call onScroll at most once every 100ms4748// Debounce with options49const debouncedSearch = debounce(search, 300, {50leading: true, // invoke on leading edge51trailing: false // don't invoke on trailing edge52});53```5455### Function Caching5657Cache function results for performance.5859```javascript { .api }60/**61* Creates a function that memoizes the result of `func`. If `resolver` is62* provided, it determines the cache key for storing the result based on the63* arguments provided to the memoized function.64*65* @param {Function} func The function to have its output memoized66* @param {Function} [resolver] The function to resolve the cache key67* @returns {Function} Returns the new memoized function68*/69function memoize(func: Function, resolver?: Function): Function;70```7172**Usage Examples:**7374```javascript75import { memoize } from "lodash";7677// Basic memoization78const expensive = memoize(function(n) {79console.log('Computing...');80return n * n;81});8283expensive(5); // logs "Computing...", returns 2584expensive(5); // returns 25 (cached, no log)8586// Custom resolver87const memoizedGet = memoize(get, function(obj, path) {88return obj.id + '.' + path;89});90```9192### Function Currying9394Transform functions to accept arguments partially.9596```javascript { .api }97/**98* Creates a function that accepts arguments of `func` and either invokes99* `func` returning its result, if at least `arity` number of arguments have100* been provided, or returns a function that accepts the remaining `func` arguments.101*102* @param {Function} func The function to curry103* @param {number} [arity=func.length] The arity of `func`104* @returns {Function} Returns the new curried function105*/106function curry(func: Function, arity?: number): Function;107108/**109* This method is like `curry` except that arguments are applied to `func`110* in the manner of `partialRight` instead of `partial`.111*112* @param {Function} func The function to curry113* @param {number} [arity=func.length] The arity of `func`114* @returns {Function} Returns the new curried function115*/116function curryRight(func: Function, arity?: number): Function;117```118119**Usage Examples:**120121```javascript122import { curry, curryRight } from "lodash";123124// Basic currying125const add = (a, b, c) => a + b + c;126const curriedAdd = curry(add);127128curriedAdd(1)(2)(3); // 6129curriedAdd(1, 2)(3); // 6130curriedAdd(1)(2, 3); // 6131132// Curry right (arguments applied right-to-left)133const divide = (a, b) => a / b;134const curriedRightDivide = curryRight(divide);135curriedRightDivide(2)(10); // 10 / 2 = 5136```137138### Partial Application139140Create functions with pre-filled arguments.141142```javascript { .api }143/**144* Creates a function that invokes `func` with `partials` prepended to the145* arguments it receives.146*147* @param {Function} func The function to partially apply arguments to148* @param {...*} [partials] The arguments to be partially applied149* @returns {Function} Returns the new partially applied function150*/151function partial(func: Function, ...partials: any[]): Function;152153/**154* This method is like `partial` except that partially applied arguments155* are appended to the arguments it receives.156*157* @param {Function} func The function to partially apply arguments to158* @param {...*} [partials] The arguments to be partially applied159* @returns {Function} Returns the new partially applied function160*/161function partialRight(func: Function, ...partials: any[]): Function;162```163164**Usage Examples:**165166```javascript167import { partial, partialRight } from "lodash";168169const greet = (greeting, name) => `${greeting} ${name}`;170171// Partial application from left172const sayHello = partial(greet, 'Hello');173sayHello('World'); // "Hello World"174175// Partial application from right176const greetJohn = partialRight(greet, 'John');177greetJohn('Hi'); // "Hi John"178```179180### Function Binding181182Bind functions to specific contexts.183184```javascript { .api }185/**186* Creates a function that invokes `func` with the `this` binding of `thisArg`187* and `partials` prepended to the arguments it receives.188*189* @param {Function} func The function to bind190* @param {*} thisArg The `this` binding of `func`191* @param {...*} [partials] The arguments to be partially applied192* @returns {Function} Returns the new bound function193*/194function bind(func: Function, thisArg: any, ...partials: any[]): Function;195196/**197* Binds methods of an object to the object itself, overwriting the existing method.198*199* @param {Object} object The object to bind and assign the bound methods to200* @param {...(string|string[])} methodNames The object method names to bind201* @returns {Object} Returns `object`202*/203function bindAll(object: Object, ...methodNames: (string|string[])[]): Object;204205/**206* Creates a function that invokes the method at `object[key]` with `partials`207* prepended to the arguments it receives.208*209* @param {Object} object The object to invoke the method on210* @param {string} key The key of the method211* @param {...*} [partials] The arguments to be partially applied212* @returns {Function} Returns the new bound function213*/214function bindKey(object: Object, key: string, ...partials: any[]): Function;215```216217### Function Composition218219Combine multiple functions into single functions.220221```javascript { .api }222/**223* Creates a function that is the composition of the provided functions,224* where each successive invocation is supplied the return value of the previous.225*226* @param {...(Function|Function[])} [funcs] The functions to invoke227* @returns {Function} Returns the new composite function228*/229function flow(...funcs: (Function|Function[])[]): Function;230231/**232* This method is like `flow` except that it creates a function that invokes233* the given functions from right to left.234*235* @param {...(Function|Function[])} [funcs] The functions to invoke236* @returns {Function} Returns the new composite function237*/238function flowRight(...funcs: (Function|Function[])[]): Function;239```240241**Usage Examples:**242243```javascript244import { flow, flowRight } from "lodash";245246const add = x => x + 1;247const multiply = x => x * 2;248const square = x => x * x;249250// Left-to-right composition251const transform = flow(add, multiply, square);252transform(2); // ((2 + 1) * 2)² = 36253254// Right-to-left composition255const transform2 = flowRight(square, multiply, add);256transform2(2); // ((2 + 1) * 2)² = 36 (same result)257```258259### Function Constraints260261Limit how functions can be called.262263```javascript { .api }264/**265* Creates a function that is restricted to invoking `func` once. Repeat calls266* to the function return the value of the first invocation.267*268* @param {Function} func The function to restrict269* @returns {Function} Returns the new restricted function270*/271function once(func: Function): Function;272273/**274* Creates a function that invokes `func`, with up to `n` arguments,275* ignoring any additional arguments.276*277* @param {Function} func The function to cap arguments for278* @param {number} [n=func.length] The arity cap279* @returns {Function} Returns the new capped function280*/281function ary(func: Function, n?: number): Function;282283/**284* Creates a function that accepts up to one argument, ignoring any285* additional arguments.286*287* @param {Function} func The function to cap arguments for288* @returns {Function} Returns the new capped function289*/290function unary(func: Function): Function;291```292293### Function Timing294295Control the timing of function execution.296297```javascript { .api }298/**299* Creates a function that invokes `func` only after it's called more than300* `n` times.301*302* @param {number} n The number of calls before `func` is invoked303* @param {Function} func The function to restrict304* @returns {Function} Returns the new restricted function305*/306function after(n: number, func: Function): Function;307308/**309* Creates a function that invokes `func`, with the `this` binding and arguments310* of the created function, while it's called less than `n` times.311*312* @param {number} n The number of calls at which `func` is no longer invoked313* @param {Function} func The function to restrict314* @returns {Function} Returns the new restricted function315*/316function before(n: number, func: Function): Function;317318/**319* Defers invoking the `func` until the current call stack has cleared.320*321* @param {Function} func The function to defer322* @param {...*} [args] The arguments to invoke `func` with323* @returns {number} Returns the timer id324*/325function defer(func: Function, ...args: any[]): number;326327/**328* Invokes `func` after `wait` milliseconds.329*330* @param {Function} func The function to delay331* @param {number} wait The number of milliseconds to delay invocation332* @param {...*} [args] The arguments to invoke `func` with333* @returns {number} Returns the timer id334*/335function delay(func: Function, wait: number, ...args: any[]): number;336```337338### Function Transformation339340Transform functions in various ways.341342```javascript { .api }343/**344* Creates a function that negates the result of the predicate `func`.345*346* @param {Function} predicate The predicate to negate347* @returns {Function} Returns the new negated function348*/349function negate(predicate: Function): Function;350351/**352* Creates a function that invokes `func` with arguments reversed.353*354* @param {Function} func The function to flip arguments for355* @returns {Function} Returns the new flipped function356*/357function flip(func: Function): Function;358359/**360* Creates a function that invokes `func` with its arguments transformed.361*362* @param {Function} func The function to wrap363* @param {...(Function|Function[])} [transforms] The argument transforms364* @returns {Function} Returns the new function365*/366function overArgs(func: Function, ...transforms: (Function|Function[])[]): Function;367368/**369* Creates a function that invokes `func` with arguments arranged according370* to the specified `indexes`.371*372* @param {Function} func The function to rearrange arguments for373* @param {...(number|number[])} indexes The arranged argument indexes374* @returns {Function} Returns the new function375*/376function rearg(func: Function, ...indexes: (number|number[])[]): Function;377```378379### Function Wrapping380381Wrap functions with additional behavior.382383```javascript { .api }384/**385* Creates a function that provides `value` to `wrapper` as its first386* argument. Any additional arguments provided to the function are appended387* to those provided to the `wrapper`.388*389* @param {*} value The value to wrap390* @param {Function} [wrapper] The wrapper function391* @returns {Function} Returns the new function392*/393function wrap(value: any, wrapper?: Function): Function;394395/**396* Creates a function that invokes `func` with the `this` binding of the397* created function and an array of arguments much like `Function#apply`.398*399* @param {Function} func The function to spread arguments over400* @param {number} [start=0] The start position of the spread401* @returns {Function} Returns the new function402*/403function spread(func: Function, start?: number): Function;404405/**406* Creates a function that invokes `func` with the `this` binding of the407* created function and arguments from `start` and beyond provided as408* an array.409*410* @param {Function} func The function to apply a rest parameter to411* @param {number} [start=func.length-1] The start position of the rest parameter412* @returns {Function} Returns the new function413*/414function rest(func: Function, start?: number): Function;415```