forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Migrating
jdalton edited this page Sep 20, 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
_.compose
is Lodash_.flowRight
- Underscore
_.contains
is Lodash_.includes
- Underscore
_.each
doesn’t allow exiting by returningfalse
- Underscore
_.findWhere
is Lodash_.find
- Underscore
_.flatten
is deep by default while Lodash is shallow - Underscore
_.invoke
is Lodash_.invokeMap
- Underscore
_.mapObject
is Lodash_.mapValues
- Underscore
_.pluck
is Lodash_.map
- Underscore
_.uniq
by aniteratee
is Lodash_.uniqBy
- Underscore
_.where
is Lodash_.filter
- Underscore
_.isFinite
doesn’t align withNumber.isFinite
(e.g._.isFinite('1')
returnstrue
in Underscore butfalse
in Lodash) - Underscore
_.matches
shorthand doesn’t support deep comparisons
(e.g._.filter(objects, { 'a': { 'b': 'c' } })
) - Underscore ≥ 1.7 & Lodash have changed their
_.template
syntax to_.template(string, option)(data)
- Lodash
_.memoize
caches areMap
like objects` - Lodash supports implicit chaining, lazy chaining, & shortcut fusion
- Lodash split its overloaded
_.head
,_.last
,_.rest
, &_.initial
out 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'
}
}
};