Lodash
| |
Original author(s) | John-David Dalton |
---|---|
Initial release | April 23, 2012[1] |
Stable release |
4.17.5
/ February 3, 2018[2] |
Written in | JavaScript |
Type | JavaScript library |
License | MIT |
Website |
lodash |
Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm.
History
Lodash draws most of its ideas from Underscore.js and now receives maintenance from the original contributors to Underscore.js.
Summary
Lodash is a JavaScript library that helps programmers write more concise and easier to maintain JavaScript.
It can be broken down into several main areas:
- Utilities - for simplifying common programming tasks such as determining type as well as simplifying math operations.
- Function - simplifying binding, decorating, constraining, throttling, debouncing, currying, and changing the pointer.
- String - conversion functions for performing basic string operations, such as trimming, converting to uppercase, camel case, etc.
- Array - creating, splitting, combining, modifying, and compressing
- Collection - iterating, sorting, filtering, splitting, and building
- Object - accessing, extending, merging, defaults, and transforming
- Seq - chaining, wrapping, filtering, and testing.
It has had multiple releases, so all functions may not be available in all implementations. For example, _.chunk has only been available since version 3.0.0.
Content
The most up to date version of the function definitions are available from lodash.com/docs
Utilities:
Lodash has a group of general purpose utilities for simplifying common programming tasks.
Lang:
- Contains a group of
_.is
functions useful for determining type for example_.isUndefined
,_.isString
,_.isNumber
,_.isArray
, etc. can be used to elegantly replace more common ways of determining type such asvar == null
. - It also contains a group of
_.to
functions useful for converting one type to another for example_.toArray
,_.toInteger
,_.toNumber
,_.toString
.
Math, Date, Number, and Util:
- Contains a group of functions for simplifying dealing with standard math, date and number operations. For example, _.floor, _.max, _.min, _.round, _.clamp, and _.now.
- Util also contains the
_.bindAll
function useful for a more object-oriented programming style as it causes all methods belonging to an object to have the same context.
Properties and methods:
- Contains a small group of functions for dealing with templateSettings
- Contains version information via
_.VERSION
, useful when code needs to run with different versions of Lodash.
For example, a SpiderMonkey JavaScript implementation may be at an earlier version of Lodash than the latest one found in a major browser.
Function:
- Lodash also contains a group of functions that target functional programming.
- In addition to the _.bindAll function mentioned in Util section above, the function group contains _.bind that replaces the standard JavaScript
call()
,apply()
orbind()
. - lazy binding via _.bindKey, decorating via _.partial, _.partialRight, and _.wrap
- caching via _.memoize
- limiting call counts via _.once, _.before, and _.after
- controlling function timing via _.throttle, _.debounce, _.defer, and _.delay
- currying via _.curry and _.curryRight
String:
- Lodash contains a group of function specifically for dealing with standard string operations:
- Character change via _.lowerCase, _.toLower, _.upperCase, _.toUpper, _.capitalize, _.deburr, _.camelCase, _.kebabCase, _.upperFirst, _.escape, _.replace, etc.
- String Conversion via _.pag, _.padEnd, _.padStart, _.trim, _.trimEnd, _.trimStart, _.truncate, _.words, _.split, _.repeat, etc.
Arrays:
- Array truncation and splitting via _.slice, _.remove, _.compact, _.difference, _.differenceBy, _.differenceWith, _.chunk, _.drop, _.intersection, _.pull, _.remove, etc.
- Array combination via _.union, _.join, _.fill, _.concat, _.union, _.intersection, _.xor, etc.
- Array modification via _.reverse, _.flatten, and _.flattenDeep
- Array creation and compression via _.fromPairs, _.zip, _.unzip, etc.
Collections:
- All of the functions for dealing with collections can be applied to arrays, as collections are a superset of arrays.
- Iteration - One of the most powerful features of Lodash is its support for iterating over collections.
- _.forEach can be used to iterate over a collection injecting both the item and the index into the called function. For example:
_.forEach(items, function(item, index) { // do stuff here; } ); will call the function passing in a single item and the index repeatedly for the whole collection.
- iterated in reverse - _.forEachRight
- searching - _.find, _.findLast and _.some
- sorting via - _.sortBy, _.orderBy, _.groupBy
- filtering via - _.filter, _.reject, _.every, _.includes, and _.partition
- randomization via - _.sample, _.sampleSize, and _.shuffle
- converting via - _.map, _.reduce, _.reduceRight
Objects:
- Objects are a superset of collections, arrays, strings, and functions, so all the Lodash functions in this section apply to them.
- creating, merging and adding via - _.create (instead of new), _.invoke, _.clone, _.assign, _.merge, _.defaults, _.pick, _.omit, etc.
- key / value - _.findKey, _.findLastKey, _.get, _.keys, etc.
- iteration - _.mapValues, _.mapKeys, _.forIn, _.forOwn, etc.
- finding and calling Functions via - _.functions, _.result
- object modification via - _.toPairs _.set, _.invert, etc.
- in addition to the
_.map
touched in collections, Lodash supports a group of functions useful for transforming one object into another including:
_.pick, _.omit, _.assingIn, _.transform, _.mapValues, etc.
Seq:
- Lodash supports chaining function calls to make them more streamline by passing the results up the chain to the calling function.
- The underscore before an object is used to create an implicit chain in Lodash. For example, to count the number of unique suits in my hand of cards.
var myHand = ['club', 'diamond', 'diamond', 'club', 'spade'];
_(myHand)
.uniq()
.size();
// 3
- The same thing can be accomplished explicitly using the _.chain + _.value functions,
var myHand = ['club', 'diamond', 'diamond', 'club', 'spade'];
_.chain(myHand)
.uniq()
.size()
.value();
// 3
- The seq group also supports tapping into value in the chain via
_.tap
and_.thru
- Sequence Chaining is an elegant way of creating complex Lodash functions from a group of simple ones. Chains can be used to combine, filter, test and reduce objects, collections and arrays to get the desired results with a minimum amount of coding.