Skip to main content

Function methods

Explore the list of methods in functionUtils library.


This document outlines the functionality of functionUtils library, detailing a wide range of methods that enhance and manipulate JavaScript functions, complete with descriptions and usage examples.

Info

functionUtils library uses Lodash's Function methods. For full details of how to use these methods, refer to Lodash documentation.

Overview

The functionUtils library offers a diverse range of methods. Here's the list of some of the most common methods:

Function manipulation

  • after: Creates a function that invokes func only after it's been called n or more times.
  • before: Creates a function that invokes func only before it's been called n times.
  • once: Creates a function that invokes func only once, no matter how many times it's called.

Function arity and argument control

  • ary: Creates a function that accepts up to n arguments, ignoring additional arguments.
  • curry: Creates a curried function with a specified arity, allowing partial application of arguments.

Methods reference in functionUtils library

functionUtils.after(n, func)

Creates a function that invokes func only after it has been called n or more times.

Parameters

  • n (number): The number of calls before the func is invoked.
  • func (Function): The function to delay execution for.

Returns

(Function): The new debounced function.

Example

const add = (a, b) => a + b;
const afterTwoCalls = functionUtils.after(2, add);

const result1 = afterTwoCalls(3, 4); // No result
const result2 = afterTwoCalls(5, 6); // Result: 11 (3 + 4)
const result3 = afterTwoCalls(7, 8); // Result: 13 (5 + 6)

functionUtils.ary(func, n)

Creates a function that limits the number of arguments passed to func to n.

Parameters

  • func (Function): The function to cap arguments for.
  • n (number): The maximum number of arguments allowed.

Returns

(Function): The new function.

Example

const sum = (a, b, c) => a + b + c;
const sumWithTwoArgs = functionUtils.ary(sum, 2);
sumWithTwoArgs(1, 2, 3); // Result: 6

functionUtils.before(n, func)

Creates a function that invokes func only if it has not been called n or more times.

Parameters

  • n (number): The maximum number of calls allowed.
  • func (Function): The function to restrict execution for.

Returns

(Function): The new function.

Example

const subtract = (a, b) => a - b;
const beforeThreeCalls = functionUtils.before(3, subtract);

const result1 = beforeThreeCalls(10, 5); // Result: 5 (10 - 5)
const result2 = beforeThreeCalls(8, 3); // Result: 5 (8 - 3)
const result3 = beforeThreeCalls(6, 2); // Result: 4 (6 - 2)
const result4 = beforeThreeCalls(4, 1); // No further result

functionUtils.bind(func, thisArg, ...partials)

Creates a new function that, when called, has its this value set to thisArg, and prepends any additional arguments to those provided during invocation.

Parameters

  • func (Function): The function to bind.
  • thisArg (*): The value to set as the this binding.
  • ...partials (*): The arguments to prepend to the bound function.

Returns

(Function): The new bound function.

Example

const greet = functionUtils.bind((name, message) => `${message}, ${name}!`, null, 'Hello');
greet('Alice'); // Result: "Hello, Alice!"

functionUtils.bindKey(object, key, ...partials)

Creates a function that binds the method at key of object to object and prepends any additional arguments to those provided during invocation.

Parameters

  • object (Object): The object containing the method.
  • key (string): The key of the method.
  • ...partials (*): The arguments to prepend to the bound function.

Returns

(Function): The new bound function.

Example

const obj = {
greeting: 'Hello',
greet(name) {
return `${this.greeting}, ${name}!`;
}
};

const boundGreet = functionUtils.bindKey(obj, 'greet', 'Alice');
boundGreet(); // Result: "Hello, Alice!"

functionUtils.curry(func, arity)

Creates a function that accepts up to arity arguments, ignoring any additional arguments.

Parameters

  • func (Function): The function to curry.
  • arity (number): The arity of the curried function.

Returns

(Function): The new curried function.

Example

const sum = (a, b, c) => a + b + c;
const curriedSum = functionUtils.curry(sum, 2);
curriedSum(1, 2, 3); // Result: 3 (ignores the third argument)
curriedSum(1)(2, 3); // Result: 3 (ignores the third argument)

functionUtils.curryRight(func, arity)

This method is like functionUtils.curry except that arguments are applied to the function from right to left.

Parameters

  • func (Function): The function to curry.
  • arity (number): The arity of the curried function.

Returns

(Function): The new curried function.

Example

const subtract = (a, b) => a - b;
const curriedSubtract = functionUtils.curryRight(subtract, 2);
curriedSubtract(5, 3); // Result: -2
curriedSubtract(5)(3); // Result: -2

functionUtils.debounce(func, wait, options)

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.

Parameters

  • func (Function): The function to debounce.
  • wait (number): The number of milliseconds to delay.
  • options (Object): The options object.

Returns

(Function): The new debounced function.

Example

const add = (a, b) => a + b;
const debouncedAdd = functionUtils.debounce(add, 300);

const result1 = debouncedAdd(2, 3); // undefined
const result2 = debouncedAdd(4, 5); // undefined

// Wait for 300 milliseconds
const result3 = debouncedAdd(6, 7); // Result: 13 (4 + 9)

functionUtils.defer(func, ...args)

Defers invoking func until the current call stack has cleared.

Parameters

  • func (Function): The function to defer.
  • ...args (*): The arguments to pass to the function.

Returns

(number): The timer id.```

functionUtils.delay(func, wait, ...args)

Invokes func after wait milliseconds.

Parameters

  • func (Function): The function to delay.
  • wait (number): The number of milliseconds to wait.
  • ...args (*): The arguments to pass to the function.

Returns

(number): The timer id.

Example

const add = (a, b) => a + b;
functionUtils.delay(add, 1000, 3, 4);

// Wait for 1000 milliseconds
// The result is 7

functionUtils.flip(func)

Creates a function that flips the order of the first two arguments.

Parameters

  • func (Function): The function to flip arguments for.

Returns

(Function): The new function.

Example

const subtract = (a, b) => a - b;
const flippedSubtract = functionUtils.flip(subtract);
flippedSubtract(3, 5); // Result: 2 (flipped order)

functionUtils.memoize(func, resolver)

Creates a function that memoizes the result of func. The resolver determines the cache key.

Parameters

  • func (Function): The function to memoize.
  • resolver (Function): The function to resolve the cache key.

Returns

(Function): The new memoized function.

Example

const add = (a, b) => a + b;
const resolver = (...args) => args.join(',');
const memoizedAdd = functionUtils.memoize(add, resolver);

const result1 = memoizedAdd(2, 3); // Result: (computes)
const result2 = memoizedAdd(2, 3); // Result: (cached result)

// Even though the function is called again with the same arguments, it uses the cached result
result1; // Result: 5
result2; // Result: 5

functionUtils.negate(predicate)

Creates a function that negates the result of the predicate function.

Parameters

  • predicate (Function): The function to negate.

Returns

(Function): The new function.

Example

const isEven = num => num % 2 === 0;
const isOdd = functionUtils.negate(isEven);
isOdd(3); // Result: true
isOdd(4); // Result: false

functionUtils.once(func)

Creates a function that invokes func only once. Subsequent calls return the result of the initial call.

Parameters

  • func (Function): The function to restrict to a single call.

Returns

(Function): The new function.

Example

const greetOnce = functionUtils.once(() => "Hello, world!");
greetOnce(); // Result: "Hello, world!"
greetOnce(); // Result: (cached result)

functionUtils.overArgs(func, ...transforms)

Creates a function that invokes func with its arguments transformed.

Parameters

  • func (Function): The function to wrap.
  • ...transforms (Function): The functions to transform arguments.

Returns

(Function): The new function.

Example

const sum = (a, b, c) => a + b + c;
const overArgsSum = functionUtils.overArgs(sum, [Math.sqrt, Math.square, Math.cbrt]);
overArgsSum(4, 9, 8); // Result: 3 + 81 + 2 (arguments transformed)

functionUtils.partial(func, ...partials)

Creates a new function that partially applies the provided arguments to func.

Parameters

  • func (Function): The function to partially apply arguments to.
  • ...partials (*): The arguments to partially apply.

Returns

(Function): The new partially applied function.

Example

const greet = (greeting, name) => `${greeting}, ${name}!`;
const greetHello = functionUtils.partial(greet, "Hello");
greetHello("Alice"); // Result: "Hello, Alice!"

functionUtils.partialRight(func, ...partials)

This method is like functionUtils.partial except that partial arguments are applied from right to left.

Parameters

  • func (Function): The function to partially apply arguments to.
  • ...partials (*): The arguments to partially apply.

Returns

(Function): The new partially applied function.

Example

const subtract = (a, b) => a - b;
const subtractFrom10 = functionUtils.partialRight(subtract, 10);
subtractFrom10(3); // Result: 7 (10 - 3)

functionUtils.rearg(func, indexes)

Creates a function that invokes func with arguments arranged according to the specified indexes.

Parameters

  • func (Function): The function to rearrange arguments for.
  • indexes (Array): The arranged argument indexes.

Returns

(Function): The new function.

Example

const formatName = (firstName, lastName) => `${lastName}, ${firstName}`;
const rearrangedName = functionUtils.rearg(formatName, [1, 0]);
rearrangedName("Alice", "Smith"); // Result: "Smith, Alice"

functionUtils.rest(func, start)

Creates a function that invokes func with the this binding and arguments of the created function, while providing the remaining arguments to the func starting from the start index.

Parameters

  • func (Function): The function to apply the rest arguments to.
  • start (number): The start index of the rest arguments.

Returns

(Function): The new function.

Example

const sum = (...args) => args.reduce((total, num) => total + num, 0);
const sumFrom2 = functionUtils.rest(sum, 2);
sumFrom2(1, 2, 3, 4); // Result: 3 + 4 = 7

functionUtils.spread(func, start)

Creates a function that invokes func with the this binding and an array of arguments.

Parameters

  • func (Function): The function to spread the array of arguments to.
  • start (number): The start index of the spread arguments.

Returns

(Function): The new function.

Example

const sum = (a, b, c) => a + b + c;
const spreadSum = functionUtils.spread(sum);
spreadSum([1, 2, 3]); // Result: 1 + 2 + 3 = 6

functionUtils.throttle(func, wait, options)

Creates a throttled function that only invokes func at most once per every wait milliseconds.

Parameters

  • func (Function): The function to throttle.
  • wait (number): The number of milliseconds to throttle.
  • options (Object): The options object.

Returns

(Function): The new throttled function.

Example

const add = (a, b) => a + b;
const throttledAdd = functionUtils.throttle(add, 1000);

// Call the throttled function
const result1 = throttledAdd(2, 3); // Result: 5
const result2 = throttledAdd(4, 5); // No output
const result3 = throttledAdd(6, 7); // No output

// Only the first call within the throttle interval produces output

functionUtils.unary(func)

Creates a function that accepts only one argument and ignores additional arguments.

Parameters

  • func (Function): The function to restrict to a single argument.

Returns

(Function): The new function.

Example

const greet = name => `Hello, ${name}!`;
const greetUnary = functionUtils.unary(greet);
greetUnary("Alice", "Bob"); // Result: "Hello, Alice!"

functionUtils.wrap(func, wrapper)

Creates a wrapped function that invokes wrapper with the func as the first argument and any additional arguments provided.

Parameters

  • func (Function): The function to wrap.
  • wrapper (Function): The wrapper function.

Returns

(Function): The new wrapped function.

Example

const wrapWithPrefixAndSuffix = (func, ...args) => {
const prefix = "Prefix: ";
const suffix = " Suffix";
const result = func(...args);
return `${prefix}${result}${suffix}`;
};

const capitalize = (text) => text.toUpperCase();
const wrappedCapitalize = functionUtils.wrap(capitalize, wrapWithPrefixAndSuffix);
const wrappedResult = wrappedCapitalize("hello"); // Result: "Prefix: HELLO Suffix"