What is lodash.cond?
The lodash.cond package is a utility function from the Lodash library that allows you to create a function that iterates over pairs of predicates and functions, invoking the function of the first predicate to return truthy. This is useful for creating conditional logic in a functional programming style.
Conditional Function Execution
This feature allows you to create a function that executes different logic based on the input. The function `func` will return 'one' if the input is 1, 'two' if the input is 2, and 'default' for any other input.
const _ = require('lodash.cond');
const func = _.cond([
[x => x === 1, () => 'one'],
[x => x === 2, () => 'two'],
[() => true, () => 'default']
]);
console.log(func(1)); // 'one'
console.log(func(2)); // 'two'
console.log(func(3)); // 'default'