How to use the ary function from lodash

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

lodash.ary is a function that creates a new function that limits the number of arguments passed to the provided function.

19
20
21
22
23
24
25
26
27
28
module.exports.add                 = _.add;
module.exports.addContrib          = _.addContrib;
module.exports.after               = _.after;
module.exports.always              = _.always;
module.exports.arity               = _.arity;
module.exports.ary                 = _.ary;
module.exports.assign              = _.assign;
module.exports.assignIn            = _.assignIn;
module.exports.assignInWith        = _.assignInWith;
module.exports.assignWith          = _.assignWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

4
5
6
7
8
9
10
11
12
13
14
// parseInt("1100", 2) => 12


module.exports = {
  sort: arr => _.sortBy(arr, x=>x),
  // fixes headache in past due to arr.map(parseInt) applying parseInt(element, index, array)
  // Ooh this would be same as _.ary(parseInt, 1)
  int: s => parseInt(s, 10),
  // faster to type and return starting value. I would use log except it looks too much like Math.log
  print: e => {console.log(e); return e},
  clog: console.log,
fork icon1
star icon4
watch icon0

+ 2 other calls in file

How does lodash.ary work?

The lodash.ary function creates a new function that wraps the given function, limiting the arguments passed to it to the specified arity (number of expected arguments) and discarding any additional arguments beyond that.

293
294
295
296
297
298
299
300
301
302
303
304
305
// Function


const after = _.after(2, () => console.log('hello'));
after();


const ary = _.ary(Math.max, 1);
console.log(ary(1, 2, 3)); // => 1


const before = _.before(2, () => console.log('hello'));
before();
fork icon0
star icon4
watch icon0

+ 15 other calls in file

63
64
65
66
67
68
69
70
71
72

    return this.channel.assertExchange(
        exchange.name,
        exchange.type,
        exchange.opts,
        _.ary(cb, 1)
    );
},

(cb) => {
fork icon25
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
const _ = require("lodash");

function myFunc(a, b, c) {
  console.log(a, b, c);
}

const myFuncTwo = _.ary(myFunc, 2);
myFuncTwo("one", "two", "three");
// Output: "one two undefined"

In the example, the myFunc function takes three parameters but we only want to pass two parameters to it. We can use lodash.ary to create a new function that only accepts two parameters and then call that new function.

Other functions in lodash

Sorted by popularity

function icon

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