Skip to main content

Util methods

Explore the list of methods in utils library.


This documentation offers an in-depth overview of Actioner's utils methods, categorically presenting their functionalities, parameters, returns, and usage examples, serving as a valuable reference while designing workflows.

Info

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

Overview

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

Function binding and composition

  • bindAll: Binds methods of an object to the object itself, overwriting the existing methods.
  • flow: Creates a function that returns the result of invoking a series of functions, passing the result of one function as an argument to the next.
  • over: Creates a function that invokes multiple functions and returns an array of their results.

Data transformation and manipulation

  • cond: Creates a function that returns the first truthy result from a series of functions.
  • conforms: Creates a function that checks if an object conforms to a given source object.
  • method: Creates a function that invokes the method at a given path of an object.
  • property: Creates a function that returns the value at a given path of an object.
  • iteratee: Converts a value into a function that returns the value.

Value handling and defaulting

  • constant: Creates a function that always returns the same value.
  • defaultTo: Checks if a value is undefined or null and returns a default value if so.

Methods reference utils library

utils.bindAll(object, methodNames)

Binds methods of object to the object, overwriting the existing methods.

Parameters

  • object (Object): The object to bind methods to.
  • methodNames (Array|string): The method names to bind.

Returns

  • (Object): Returns the object.

Example

const newUser = {
name: 'Alice',
sayHello() {
context.users.push(`Hello, ${this.name}!`);
}
};

utils.bindAll(newUser, ['sayHello']);
newUser.sayHello(); // Result: "Hello, Alice!"

utils.cond(pairs)

Creates a function that returns the result of the first predicate that returns a truthy value.

Parameters

  • pairs (Array): The predicate-function pairs.

Returns

  • (Function): Returns the new composite function.

Example

const checkValue = utils.cond([
[x => x === 1, () => 'Value is 1'],
[x => x === 2, () => 'Value is 2'],
[utils.stubTrue, () => 'Value is something else']
]);

checkValue(1); // Result: "Value is 1"
checkValue(2); // Result: "Value is 2"
checkValue(3); // Result: "Value is something else"

utils.conforms(source)

Creates a function that checks if object conforms to source.

Parameters

  • source (Object): The source object.

Returns

  • (Function): Returns the new function.

Example

const newUser = {
name: 'Alice',
age: 30
};

const isAlice = utils.conforms({ name: 'Alice' });
isAlice(newUser); // Result: true

utils.constant(value)

Creates a function that returns value.

Parameters

  • value (*): The value to return.

Returns

  • (Function): Returns the new constant function.

Example

const alwaysTrue = utils.constant(true);
(alwaysTrue(); // Result: true

utils.flow(...funcs)

Creates a function that returns the result of invoking the given functions.

Parameters

  • ...funcs (Function): The functions to invoke.

Returns

  • (Function): Returns the new composite function.

Example

const add = (a, b) => a + b;
const double = x => x * 2;
const addThenDouble = utils.flow(add, double);

addThenDouble(2, 3); // Result: (2 + 3) * 2 = 10

utils.flowRight(...funcs)

This method is like utils.flow except that it invokes functions from right to left.

Parameters

  • ...funcs (Function): The functions to invoke.

Returns

  • (Function): Returns the new composite function.

Example

const subtract = (a, b) => a - b;
const double = x => x * 2;
const subtractThenDouble = utils.flowRight(subtract, double);

subtractThenDouble(5, 2); // Result: (5 - 2) * 2 = 6

utils.iteratee(value)

Creates a function that returns the value.

Parameters

  • value (*): The value to convert to an iteratee.

Returns

  • (Function): Returns the new iteratee.

Example

const iteratee = utils.iteratee({ name: 'Alice' });
iteratee({ name: 'Alice' }); // Result: { name: 'Alice' }

utils.matches(source)

Creates a function that performs a partial deep comparison between the given object and source.

Parameters

  • source (Object): The object of property values to match.

Returns

  • (Function): Returns the new function.

Example

const isMatch = utils.matches({ a: 1 });
isMatch({ a: 1, b: 2 }); // Result: true

utils.matchesProperty(path, srcValue)

Creates a function that performs a partial deep comparison between the value at path of object and srcValue.

Parameters

  • path (Array|string): The path of the property to get. srcValue (*): The value to compare.

Returns

  • (Function): Returns the new function.

Example

const isAgeEqual30 = utils.matchesProperty('age', 30);
isAgeEqual30({ name: 'Alice', age: 30 }); // Result: true

utils.method(object, path, ...args)

Invokes the method at path of object.

Parameters

  • object (Object): The object to query.
  • path (Array|string): The path of the method to invoke.
  • ...args (*): The arguments to invoke the method with.

Returns

  • (*): Returns the result of the invoked method.

Example

const newUser = {
name: 'Alice',
greet(message) {
return `${message}, ${this.name}!`;
}
};

utils.method(newUser, 'greet', 'Hello'); // Result: "Hello, Alice!"

utils.methodOf(object, ...args)

The opposite of utils.method; this method creates a function that invokes the method at a given path of a object.

Parameters

  • object (Object): The object to query.
  • ...args (*): The arguments to invoke the method with.

Returns

  • (Function): Returns the new function.

Example

const greet = utils.methodOf({
name: 'Alice',
greet(message) {
return `${message}, ${this.name}!`;
}
});

greet('Hello'); // Result: "Hello, Alice!"

utils.mixin([options={}])

Adds all own methods from source objects to the destination object.

Parameters

  • [options={}] (Object): The options object.

Returns

  • (Object): Returns the destination object.

Example

const obj = { name: 'Alice' };
utils.mixin({
capitalizeName() {
this.name = this.name.toUpperCase();
}
});

obj.capitalizeName();
obj.name; // Result: "ALICE"

utils.nthArg(n)

Creates a function that gets the argument at index n.

Parameters

  • n (number): The index of the argument to retrieve.

Returns

  • (Function): Returns the new function.

Example

const getSecondArg = utils.nthArg(1);
getSecondArg('a', 'b', 'c'); // Result: "b"

utils.over(...iteratees)

Creates a function that invokes each provided function with the arguments it receives and returns the results.

Parameters

  • ...iteratees (Function): The functions to invoke.

Returns

  • (Function): Returns the new composite function.

Example

const square = x => x * x;
const double = x => x * 2;
const overDoubleAndSquare = utils.over(double, square);

overDoubleAndSquare(5); // Result: [10, 25]

utils.overEvery(...predicates)

Creates a function that checks if all of the provided predicates return truthy when invoked with the arguments it receives.

Parameters

  • ...predicates (Function): The predicates to check.

Returns

  • (Function): Returns the new composite function.

Example

const isEven = num => num % 2 === 0;
const isPositive = num => num > 0;
const overEveryCheck = utils.overEvery(isEven, isPositive);

overEveryCheck(4); // Result: true (even and positive)
overEveryCheck(-2); // Result: false (not positive)

utils.overSome(...predicates)

Creates a function that checks if any of the provided predicates return truthy when invoked with the arguments it receives.

Parameters

  • ...predicates (Function): The predicates to check.

Returns

  • (Function): Returns the new composite function.

Example

const isEven = num => num % 2 === 0;
const isPositive = num => num > 0;
const overSomeCheck = utils.overSome(isEven, isPositive);

overSomeCheck(4); // Result: true (even)
overSomeCheck(-2); // Result: true (positive)
overSomeCheck(-3); // Result: false (neither even nor positive)

utils.property(path)

Creates a function that returns the value at path of a given object.

Parameters

  • path (Array|string): The path of the property to get.

Returns

  • (Function): Returns the new function.

Example

const getUser = utils.property('newUser.name');
getUser({ newUser: { name: 'Alice' } }); // Result: "Alice"

utils.propertyOf(object)

The opposite of utils.property; this method creates a function that returns the value at a given path of a object.

Parameters

  • object (Object): The object to query.

Returns

  • (Function): Returns the new function.

Example

const newUser = {
name: 'Alice',
address: {
city: 'Wonderland'
}
};

const getCity = utils.propertyOf(newUser)('address.city');
getCity; // Result: "Wonderland"

utils.range([start=0], end, [step=1])

Creates an array of numbers progressing from start up to, but not including, end.

Parameters

  • [start=0] (number): The start of the range.
  • end (number): The end of the range.
  • [step=1] (number): The value to increment or decrement by.

Returns

  • (Array): Returns the new array.

Example

utils.range(1, 5); // Result: [1, 2, 3, 4]
utils.range(0, 10, 2); // Result: [0, 2, 4, 6, 8]

utils.rangeRight([start=0], end, [step=1])

This method is like utils.range except that it populates values in a descending order.

Parameters

  • [start=0] (number): The start of the range.
  • end (number): The end of the range.
  • [step=1] (number): The value to increment or decrement by.

Returns

  • (Array): Returns the new array.

Example

utils.rangeRight(1, 5); // Result: [4, 3, 2, 1]
utils.rangeRight(0, 10, 2); // Result: [8, 6, 4, 2, 0]

utils.runInContext([context=lodash])

Creates a new lodash function using the specified context.

Parameters

  • [context=lodash] (Object): The context object.

Returns

  • (Function): Returns the new lodash function.

Example

const customLodash = utils.runInContext({
map: (array, iteratee) => array.map(iteratee).join('-')
});

customLodash.map([1, 2, 3], x => x * 2); // Result: "2-4-6"

utils.toPath(value)

Converts value to a property path array.

Parameters

  • value (*): The value to convert.

Returns

  • (Array): Returns the new property path array.

Example

utils.toPath('a.b.c'); // Result: ['a', 'b', 'c']
utils.toPath(['a', 'b', 'c']); // Result: ['a', 'b', 'c']
utils.toPath('a[0].b.c'); // Result: ['a', '0', 'b', 'c']

utils.attempt(func, ...args)

Attempts to invoke func, returning either the result or the caught error object.

Parameters

  • func (Function): The function to attempt. ...args (*): The arguments to invoke the function with.

Returns

  • (*): Returns the result or the caught error object.

Example

const divide = (a, b) => a / b;
utils.attempt(divide, 10, 2); // Result: 5 (result)
utils.attempt(divide, 10, 0); // Result: Error (caught error object)

utils.defaultTo(value, defaultValue)

Checks value to determine if it is NaN, null, or undefined, and returns defaultValue in such cases.

Parameters

  • value (*): The value to check.
  • defaultValue (*): The default value.

Returns

  • (*): Returns the resolved value.

Example

utils.defaultTo(null, 'Default'); // Result: "Default"
utils.defaultTo(42, 'Default'); // Result: 42

utils.identity(value)

This method returns the first argument it receives.

Parameters

  • value (*): Any value.

Returns

  • (*): Returns the input value.

Example

utils.identity('Hello'); // Result: "Hello"

utils.stubArray()

This method returns an empty array.

Returns

  • (Array): An empty array.

Example

utils.stubArray(); // Result: []

utils.stubFalse()

This method returns false.

Returns

  • (boolean): Always returns false.

Example

utils.stubFalse(); // Result: false

utils.stubObject()

This method returns an empty object.

Returns

  • (Object): An empty object.

Example

utils.stubObject(); // Result: {}

utils.stubString()

This method returns an empty string.

Returns

  • (string): An empty string.

Example

utils.stubString(); // Result: ""

utils.stubTrue()

This method returns true.

Returns

  • (boolean): Always returns true.

Example

utils.stubTrue(); // Result: true

utils.noConflict()

This method restores the value of lodash back to its original value.

Returns

  • (Object): Returns the lodash object.

Example

const lodash = utils.noConflict();
_) // Result: undefined

utils.noop()

This method returns undefined.

Returns

  • (undefined): Always returns undefined.

Example

utils.noop(); // Result: undefined

utils.now()

This method returns the current timestamp.

Returns

  • (number): The current timestamp.

Example

utils.now(); // Result: 1667012345678 (current timestamp)

utils.runInContext([context=lodash])

Creates a new lodash function using the specified context.

Parameters

  • [context=lodash] (Object): The context object.

Returns

  • (Function): Returns the new lodash function.

Example

const customLodash = utils.runInContext({
map: (array, iteratee) => array.map(iteratee).join('-')
});

customLodash.map([1, 2, 3], x => x * 2); // Result: "2-4-6"

utils.times(n, iteratee)

Invokes the iteratee n times, returning an array of the results.

Parameters

  • n (number): The number of times to invoke iteratee.
  • iteratee (Function): The function to invoke.

Returns

  • (Array): Returns an array of the results.

Example

const square = x => x * x;
utils.times(3, square); // Result: [0, 1, 4]

utils.uniqueId([prefix=''])

Generates a unique ID. If a prefix is provided, it is added to the start of the ID.

Parameters

  • [prefix=''] (string): The prefix to add to the ID.

Returns

  • (string): Returns the unique ID.

Example

utils.uniqueId('user_'); // Result: "user_1"
utils.uniqueId(); // Result: "2"