Function cond

  • 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.

    Type Parameters

    • R = any

    Parameters

    • pairs: [((...args) => boolean), ((...args) => R)][][]

      The predicate-function pairs.

    Returns R

    Returns the new composite function.

    Since

    5.12.0

    Example

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

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

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

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