Home

function: cond


cond(pairs: Array): function

Creates a function that iterates over `pairs` and invokes the corresponding function of the first predicate to return truthy. The predicate-function pairs are invoked with the `this` binding and arguments of the created function.

Example(s):

const func = cond([
  [matches({ 'a': 1 }),         constant('matches A')],
  [conforms({ 'b': isNumber }), constant('matches B')],
  [stubTrue,                    constant('no match')]
])

func({ 'a': 1, 'b': 2 })
// => 'matches A'

func({ 'a': 0, 'b': 1 })
// => 'matches B'

func({ 'a': '1', 'b': '2' })
// => 'no match'

Params:

Name Type Attribute Description
pairs The predicate-function pairs.

Returns:

function

  {
    "comment": "/**\r\n * Creates a function that iterates over `pairs` and invokes the corresponding\r\n * function of the first predicate to return truthy. The predicate-function\r\n * pairs are invoked with the `this` binding and arguments of the created\r\n * function.\r\n *\r\n * @since 4.0.0\r\n * @category Util\r\n * @param {Array} pairs The predicate-function pairs.\r\n * @returns {Function} Returns the new composite function.\r\n * @example\r\n *\r\n * const func = cond([\r\n *   [matches({ 'a': 1 }),         constant('matches A')],\r\n *   [conforms({ 'b': isNumber }), constant('matches B')],\r\n *   [stubTrue,                    constant('no match')]\r\n * ])\r\n *\r\n * func({ 'a': 1, 'b': 2 })\r\n * // => 'matches A'\r\n *\r\n * func({ 'a': 0, 'b': 1 })\r\n * // => 'matches B'\r\n *\r\n * func({ 'a': '1', 'b': '2' })\r\n * // => 'no match'\r\n */",
    "meta": {
        "range": [
            855,
            1337
        ],
        "filename": "cond.js",
        "lineno": 30,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100000744",
            "name": "cond",
            "type": "FunctionDeclaration",
            "paramnames": [
                "pairs"
            ]
        },
        "vars": {
            "length": "cond~length",
            "pairs": "cond~pairs",
            "": null
        }
    },
    "description": "Creates a function that iterates over `pairs` and invokes the corresponding\rfunction of the first predicate to return truthy. The predicate-function\rpairs are invoked with the `this` binding and arguments of the created\rfunction.",
    "since": "4.0.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Util",
            "value": "Util"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "Array"
                ]
            },
            "description": "The predicate-function pairs.",
            "name": "pairs"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "Returns the new composite function."
        }
    ],
    "examples": [
        "const func = cond([\r  [matches({ 'a': 1 }),         constant('matches A')],\r  [conforms({ 'b': isNumber }), constant('matches B')],\r  [stubTrue,                    constant('no match')]\r])\r\rfunc({ 'a': 1, 'b': 2 })\r// => 'matches A'\r\rfunc({ 'a': 0, 'b': 1 })\r// => 'matches B'\r\rfunc({ 'a': '1', 'b': '2' })\r// => 'no match'"
    ],
    "name": "cond",
    "longname": "cond",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000083",
    "___s": true,
    "filepath": "cond.js"
}