How to use the cond function from lodash

Find comprehensive JavaScript lodash.cond code examples handpicked from public code repositorys.

lodash.cond creates a function that iterates over an array of [predicate, transform] pairs and returns the result of the first transform that returns truthy for the corresponding predicate.

60
61
62
63
64
65
66
67
68
69
module.exports.collate             = _.collate;
module.exports.compact             = _.compact;
module.exports.comparator          = _.comparator;
module.exports.complement          = _.complement;
module.exports.concat              = _.concat;
module.exports.cond                = _.cond;
module.exports.conforms            = _.conforms;
module.exports.conjoin             = _.conjoin;
module.exports.cons                = _.cons;
module.exports.constant            = _.constant;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

883
884
885
886
887
888
889
890
891
892
893
894
console.log(attempt); // => [Error: Divide by zero.]


const bindAll = _.bindAll(object, ['increment', 'square']);
console.log(bindAll); // => { 'increment': [Function: increment], 'square': [Function: square] }


const cond = _.cond([
    [_.matches({ 'a': 1 }),           _.constant('matches A')],
    [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
    [_.stubTrue,                      _.constant('no match')]
]);
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.cond work?

lodash.cond creates a function that iterates over an array of conditions and returns the corresponding result of the first condition that is truthy, similar to a switch statement. Each condition in the array is an array itself containing a predicate function and a corresponding function that is executed when the predicate returns a truthy value. If no conditions are truthy, an optional default function can be provided to return a default value.

199
200
201
202
203
204
205
206
207
208

  return `${value}`
},

// give us some user-friendly "types"
stringifyFriendlyTypeof: _.cond(USER_FRIENDLY_TYPE_DETECTORS),

stringify (values) {
  // if we already have an array
  // then nest it again so that
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const lodash = require("lodash");

const numberType = (value) => typeof value === "number";
const stringType = (value) => typeof value === "string";

const getType = lodash.cond([
  [numberType, lodash.constant("number")],
  [stringType, lodash.constant("string")],
  [lodash.stubTrue, lodash.constant("unknown")],
]);

console.log(getType(42)); // output: number
console.log(getType("hello")); // output: string
console.log(getType(true)); // output: unknown

In this example, lodash.cond is used to create a function getType that takes a value and returns a string indicating its type. The function getType is created by passing an array of conditions, each of which is a two-element array where the first element is a predicate function that checks if the value matches the condition and the second element is a function that returns the corresponding type string. If none of the conditions match, the last function, lodash.stubTrue, is called and returns the "unknown" type string.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)