Repository navigation
Migrating
jdalton edited this page Sep 19, 2016
·
1 revision
When migrating from Underscore to Lodash there are some differences to be aware of.
The eslint-plugin-lodash
may help migrate.
- Underscore
_.composeis Lodash_.flowRight - Underscore
_.containsis Lodash_.includes - Underscore
_.eachdoesn’t allow exiting by returningfalse - Underscore
_.findWhereis Lodash_.find - Underscore
_.flattenis deep by default while Lodash is shallow - Underscore
_.invokeis Lodash_.invokeMap - Underscore
_.mapObjectis Lodash_.mapValues - Underscore
_.pluckis Lodash_.map - Underscore
_.uniqby aniterateeis Lodash_.uniqBy - Underscore
_.whereis Lodash_.filter - Underscore
_.isFinitedoesn’t align withNumber.isFinite
(e.g._.isFinite('1')returnstruein Underscore butfalsein Lodash) - Underscore
_.matchesshorthand doesn’t support deep comparisons
(e.g._.filter(objects, { 'a': { 'b': 'c' } })) - Underscore ≥ 1.7 & Lodash have changed their
_.templatesyntax to_.template(string, option)(data) - Lodash
_.memoizecaches areMaplike objects` - Lodash supports implicit chaining, lazy chaining, & shortcut fusion
- Lodash split its overloaded
_.head,_.last,_.rest, &_.initialout into
_.take,_.takeRight,_.drop, &_.dropRight
(i.e._.head(array, 2)in Underscore is_.take(array, 2)in Lodash)
Lodash works great with Backbone. It’s even run against Backone’s unit tests on every commit.
You can replace Underscore with Lodash in Backbone by using an AMD loader, browserify, or webpack.
Note: Lodash v4 works with Backbone ≥ v1.3.0.
- Using AMD “paths” configuration
require({
'paths' {
'backbone': 'path/to/backbone',
'jquery': 'path/to/jquery',
'underscore': 'path/to/lodash'
}
}, ['backbone'], function(Backbone) {
// use Backbone
});- Using the aliasify transform for browserify by adding a section to your package.json
{
"browserify": {
"transform": ["aliasify"]
},
"aliasify": {
"aliases": {
"underscore": "lodash"
}
}
}and executing browserify with the aliasify parameter
$ browserify entry.js --global-transform aliasify -o out.js- Using the resolve.alias configuration in webpack
module.exports = {
'resolve': {
'alias': {
'underscore': 'absolute/path/to/lodash'
}
}
};