Lodash utility library exported as ES6 modules for modern JavaScript applications with tree-shaking support.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
General utility functions for common programming patterns including identity, constant functions, flow control, and stub functions.
/**
* Returns the first argument it receives
* @param {*} value - Any value
* @returns {*} Returns value
*/
function identity(value);
/**
* Creates a function that returns value
* @param {*} value - The value to return from the new function
* @returns {Function} Returns the new constant function
*/
function constant(value);
/**
* A method that returns undefined
* @returns {undefined} Returns undefined
*/
function noop();
/**
* Generates a unique ID
* @param {string} prefix - The value to prefix the ID with
* @returns {string} Returns the unique ID
*/
function uniqueId(prefix);/**
* Creates an array of numbers progressing from start up to, but not including, end
* @param {number} start - The start of the range
* @param {number} end - The end of the range
* @param {number} step - The value to increment or decrement by
* @returns {Array} Returns the range of numbers
*/
function range(start, end, step);
/**
* Like range but populates the array in descending order
* @param {number} start - The start of the range
* @param {number} end - The end of the range
* @param {number} step - The value to increment or decrement by
* @returns {Array} Returns the range of numbers
*/
function rangeRight(start, end, step);/**
* Invokes the iteratee n times, returning an array of the results
* @param {number} n - The number of times to invoke iteratee
* @param {Function} iteratee - The function invoked per iteration
* @returns {Array} Returns the array of results
*/
function times(n, iteratee);/**
* A method that returns a new empty array
* @returns {Array} Returns the new empty array
*/
function stubArray();
/**
* A method that returns false
* @returns {boolean} Returns false
*/
function stubFalse();
/**
* A method that returns a new empty object
* @returns {Object} Returns the new empty object
*/
function stubObject();
/**
* A method that returns an empty string
* @returns {string} Returns the empty string
*/
function stubString();
/**
* A method that returns true
* @returns {boolean} Returns true
*/
function stubTrue();/**
* Attempts to invoke func, returning either the result or the caught error object
* @param {Function} func - The function to attempt
* @param {...*} args - The arguments to invoke func with
* @returns {*} Returns the func result or error object
*/
function attempt(func, ...args);
/**
* Returns defaultValue if value is NaN, null, or undefined
* @param {*} value - The value to check
* @param {*} defaultValue - The default value
* @returns {*} Returns the resolved value
*/
function defaultTo(value, defaultValue);/**
* Creates a function that invokes func with arguments converted to iteratee
* @param {*} func - The value to convert to a callback
* @returns {Function} Returns the callback
*/
function iteratee(func);
/**
* Creates a function that performs a partial deep comparison between a source object
* @param {Object} source - The object of property values to match
* @returns {Function} Returns the new spec function
*/
function matches(source);
/**
* Creates a function that performs a partial deep comparison between the value at path and srcValue
* @param {Array|string} path - The path of the property to get
* @param {*} srcValue - The value to match
* @returns {Function} Returns the new spec function
*/
function matchesProperty(path, srcValue);
/**
* Creates a function that returns the argument at index n
* @param {number} n - The index of the argument to return
* @returns {Function} Returns the new pass-thru function
*/
function nthArg(n);
/**
* Creates a function that returns the value at path of a given object
* @param {Array|string} path - The path of the property to get
* @returns {Function} Returns the new accessor function
*/
function property(path);
/**
* Creates a function that returns the value at path of object
* @param {Object} object - The object to query
* @returns {Function} Returns the new accessor function
*/
function propertyOf(object);import {
identity, constant, range, times, uniqueId,
attempt, defaultTo, matches, property, nthArg
} from "lodash-es";
// Identity function (useful as default iteratee)
console.log([1, 2, 3].map(identity)); // [1, 2, 3]
// Constant function
const getTrue = constant(true);
console.log(getTrue()); // true
console.log([1, 2, 3].map(getTrue)); // [true, true, true]
// Generate ranges
console.log(range(4)); // [0, 1, 2, 3]
console.log(range(1, 5)); // [1, 2, 3, 4]
console.log(range(0, 20, 5)); // [0, 5, 10, 15]
// Generate arrays with times
const squares = times(5, n => n * n); // [0, 1, 4, 9, 16]
// Generate unique IDs
console.log(uniqueId()); // '1'
console.log(uniqueId('contact_')); // 'contact_2'
console.log(uniqueId('user_')); // 'user_3'
// Safe function execution
const result = attempt(JSON.parse, '{"valid": true}');
console.log(result); // { valid: true }
const errorResult = attempt(JSON.parse, 'invalid json');
console.log(errorResult); // Error object
// Default values
console.log(defaultTo(undefined, 'default')); // 'default'
console.log(defaultTo(null, 'default')); // 'default'
console.log(defaultTo('value', 'default')); // 'value'
// Create predicate functions
const users = [
{ name: 'Alice', age: 25, active: true },
{ name: 'Bob', age: 30, active: false }
];
const isActive = matches({ active: true });
console.log(users.filter(isActive)); // [{ name: 'Alice', age: 25, active: true }]
// Property accessor functions
const getName = property('name');
console.log(users.map(getName)); // ['Alice', 'Bob']
// Get nth argument
const getSecond = nthArg(1);
console.log(getSecond('a', 'b', 'c')); // 'b'