Lodash is a JavaScript utility library that can greatly improve your productivity.
_.find, _.clone, _.cloneDeep, _.assign, _.isArray, _.map, _.forEach
_.isEmpty(null); //true
_.isEmpty({}); //true
_.isEmpty([]); // true
_.isEmpty([1]); // false
_.size([2,3]); //2
_.size({a: 1, b: [2,3], c: {}}); //3
_.times(5, (i) => {
console.log(i); // 0, 1, 2, 3, 4
})
_.uniqueId(); // "1"
_.uniqueId('MyPrefix_'); // "MyPrefix_2"
require('./myotherfile');
_.uniqueId(); // "3"
///// myotherfile.js
_.uniqueId(); // "2"
var ownerArr = [{
"owner": "Colin",
"pets": [{"name":"dog1"}, {"name": "dog2"}]
}, {
"owner": "John",
"pets": [{"name":"dog3"}, {"name": "dog4"}]
}];
_.map(ownerArr, 'pets[0].name').map(console.log); // dog1, dog3
_.map(ownerArr, 'pets[2].name').map(console.log); // [undefined, undefined], no exceptions
_.get(ownerArr[0], 'pets[0].name'); //dog1
_.get(ownerArr[2], 'pets[0].name', 'no dogs found'); //no dogs found
_.set(ownerArr[0], 'pets[0].name', 'dogx'); // now ownerArr[0].pets is [{"name":"dogx"}, {"name": "dog2"}]
_.set(ownerArr[2], 'pets[0].name', 'dogy'); // no exceptions, ownerArr keeps its original value
var mycar = {'name': 'colin', 'car': 'suzuki', 'age': 17};
_.omit(mycar, ['car', 'age']); // {"name": "colin"}
_.pick(mycar, ['car', 'age']); // {"car": "suzuki", "age": 17};
_.isError(_.attempt(JSON.parse.bind(null, 'x'))); // true, no exception
_.isError(_.attempt(JSON.parse.bind(null, '{}'))); // false, no exception
_.defaults({ 'host': 'www.google.com' }, { 'port': 80 }, { 'protocol': 'http' }); // {"host": "www.google.com", "port": 80, "protocol": "http"}
scrollBar.onscroll(_.throttle(onScrollFunc, 400)); // throttle ensures the onScrollFunc is called approximately 400ms in between
submitButton.onclick(_.debounce(onSubmitFunc, 400, {
'leading': true,
'trailing': false
})); // debounce ensures the onSubmitFunc is not called multiple times within 400ms
Lodash: 10 Javascript Utility Functions That You Should Probably Stop Rewriting Underscore debounce vs throttling