Replace underscore by lodash

Summary:As far as we agreed to merge `rnpm` into react-native core, we need to align our dependencies to exclude duplications. One of the steps forward would be to use the same utilities library. According to the thread on fb, everybody is fine with replacing underscore by lodash (which we use internally for rnpm).

So, here we go!

cc mkonicek davidaurelio grabbou

**Test plan**
```
$ npm test
```
![image](https://cloud.githubusercontent.com/assets/2273613/13173972/ee34c922-d700-11e5-971b-68ff7322b6d6.png)

**Code formatting**

Changes are aligned with the current [style guide](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#style-guide).
Closes https://github.com/facebook/react-native/pull/6030

Differential Revision: D3016271

Pulled By: davidaurelio

fb-gh-sync-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45
shipit-source-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45
This commit is contained in:
Kureev Alexey 2016-03-09 03:08:25 -08:00 committed by Facebook Github Bot 3
parent f2483eb1ee
commit db3a00d58c
14 changed files with 82 additions and 85 deletions

View File

@ -8,7 +8,7 @@
*/
'use strict';
var _ = require('underscore');
var _ = require('lodash');
var wordwrap = require('wordwrap');
var HORIZONTAL_LINE = '\u2500';
@ -45,7 +45,7 @@ var BOTTOM_RIGHT = '\u2518';
function formatBanner(message, options) {
options = options || {};
_.defaults(options, {
chalkFunction: _.identity,
chalkFunction: (fn) => fn,
width: 80,
marginLeft: 0,
marginRight: 0,
@ -71,7 +71,7 @@ function formatBanner(message, options) {
var left = spaces(marginLeft) + VERTICAL_LINE + spaces(paddingLeft);
var right = spaces(paddingRight) + VERTICAL_LINE + spaces(marginRight);
var bodyLines = _.flatten([
var bodyLines = _.flattenDeep([
arrayOf('', paddingTop),
body.split('\n'),
arrayOf('', paddingBottom),
@ -88,7 +88,7 @@ function formatBanner(message, options) {
spaces(marginRight);
var bottom = spaces(marginLeft) + BOTTOM_LEFT + horizontalBorderLine +
BOTTOM_RIGHT + spaces(marginRight);
return _.flatten([top, bodyLines, bottom]).join('\n');
return _.flattenDeep([top, bodyLines, bottom]).join('\n');
}
function spaces(number) {

113
npm-shrinkwrap.json generated
View File

@ -2167,9 +2167,9 @@
}
},
"flow-bin": {
"version": "0.21.0",
"from": "flow-bin@>=0.21.0 <0.22.0",
"resolved": "https://registry.npmjs.org/flow-bin/-/flow-bin-0.21.0.tgz",
"version": "0.22.1",
"from": "flow-bin@latest",
"resolved": "https://registry.npmjs.org/flow-bin/-/flow-bin-0.22.1.tgz",
"dependencies": {
"bin-wrapper": {
"version": "2.1.3",
@ -2330,7 +2330,7 @@
},
"is-absolute": {
"version": "0.1.7",
"from": "is-absolute@>=0.1.4 <0.2.0",
"from": "is-absolute@>=0.1.7 <0.2.0",
"resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-0.1.7.tgz",
"dependencies": {
"is-relative": {
@ -2417,7 +2417,7 @@
"dependencies": {
"ansi-regex": {
"version": "0.2.1",
"from": "ansi-regex@>=0.2.0 <0.3.0",
"from": "ansi-regex@>=0.2.1 <0.3.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-0.2.1.tgz"
}
}
@ -2429,7 +2429,7 @@
"dependencies": {
"ansi-regex": {
"version": "0.2.1",
"from": "ansi-regex@>=0.2.0 <0.3.0",
"from": "ansi-regex@>=0.2.1 <0.3.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-0.2.1.tgz"
}
}
@ -2508,7 +2508,7 @@
},
"is-absolute": {
"version": "0.1.7",
"from": "is-absolute@>=0.1.4 <0.2.0",
"from": "is-absolute@>=0.1.7 <0.2.0",
"resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-0.1.7.tgz",
"dependencies": {
"is-relative": {
@ -2585,7 +2585,7 @@
},
"is-absolute": {
"version": "0.1.7",
"from": "is-absolute@>=0.1.5 <0.2.0",
"from": "is-absolute@>=0.1.7 <0.2.0",
"resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-0.1.7.tgz",
"dependencies": {
"is-relative": {
@ -3224,7 +3224,7 @@
"dependencies": {
"ansi-regex": {
"version": "0.2.1",
"from": "ansi-regex@>=0.2.0 <0.3.0",
"from": "ansi-regex@>=0.2.1 <0.3.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-0.2.1.tgz"
}
}
@ -3236,7 +3236,7 @@
"dependencies": {
"ansi-regex": {
"version": "0.2.1",
"from": "ansi-regex@>=0.2.0 <0.3.0",
"from": "ansi-regex@>=0.2.1 <0.3.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-0.2.1.tgz"
}
}
@ -3250,7 +3250,7 @@
},
"lpad-align": {
"version": "1.1.0",
"from": "lpad-align@>=1.0.1 <2.0.0",
"from": "lpad-align@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/lpad-align/-/lpad-align-1.1.0.tgz",
"dependencies": {
"lpad": {
@ -3324,9 +3324,9 @@
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.5.2.tgz",
"dependencies": {
"glob": {
"version": "7.0.0",
"version": "7.0.3",
"from": "glob@>=7.0.0 <8.0.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.0.0.tgz"
"resolved": "https://registry.npmjs.org/glob/-/glob-7.0.3.tgz"
}
}
}
@ -3539,16 +3539,16 @@
"from": "boom@^2.8.x",
"resolved": "https://registry.npmjs.org/boom/-/boom-2.10.1.tgz"
},
"brace-expansion": {
"version": "1.1.1",
"from": "brace-expansion@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.1.tgz"
},
"caseless": {
"version": "0.11.0",
"from": "caseless@~0.11.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.11.0.tgz"
},
"brace-expansion": {
"version": "1.1.1",
"from": "brace-expansion@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.1.tgz"
},
"chalk": {
"version": "1.1.1",
"from": "chalk@^1.1.1",
@ -3639,16 +3639,16 @@
"from": "generate-function@^2.0.0",
"resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.0.0.tgz"
},
"generate-object-property": {
"version": "1.2.0",
"from": "generate-object-property@^1.1.0",
"resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz"
},
"graceful-fs": {
"version": "4.1.2",
"from": "graceful-fs@4.1",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.2.tgz"
},
"generate-object-property": {
"version": "1.2.0",
"from": "generate-object-property@^1.1.0",
"resolved": "https://registry.npmjs.org/generate-object-property/-/generate-object-property-1.2.0.tgz"
},
"graceful-readlink": {
"version": "1.0.1",
"from": "graceful-readlink@>= 1.0.0",
@ -3684,11 +3684,6 @@
"from": "http-signature@~0.11.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-0.11.0.tgz"
},
"inherits": {
"version": "2.0.1",
"from": "inherits@*",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
},
"ini": {
"version": "1.3.4",
"from": "ini@~1.3.0",
@ -3699,21 +3694,26 @@
"from": "is-my-json-valid@^2.12.2",
"resolved": "https://registry.npmjs.org/is-my-json-valid/-/is-my-json-valid-2.12.2.tgz"
},
"inherits": {
"version": "2.0.1",
"from": "inherits@*",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
},
"is-property": {
"version": "1.0.2",
"from": "is-property@^1.0.0",
"resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz"
},
"isstream": {
"version": "0.1.2",
"from": "isstream@~0.1.2",
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz"
},
"isarray": {
"version": "0.0.1",
"from": "isarray@0.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz"
},
"isstream": {
"version": "0.1.2",
"from": "isstream@~0.1.2",
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz"
},
"json-stringify-safe": {
"version": "5.0.1",
"from": "json-stringify-safe@~5.0.1",
@ -3729,16 +3729,16 @@
"from": "lodash._basetostring@^3.0.0",
"resolved": "https://registry.npmjs.org/lodash._basetostring/-/lodash._basetostring-3.0.1.tgz"
},
"lodash.pad": {
"version": "3.1.1",
"from": "lodash.pad@^3.0.0",
"resolved": "https://registry.npmjs.org/lodash.pad/-/lodash.pad-3.1.1.tgz"
},
"lodash._createpadding": {
"version": "3.6.1",
"from": "lodash._createpadding@^3.0.0",
"resolved": "https://registry.npmjs.org/lodash._createpadding/-/lodash._createpadding-3.6.1.tgz"
},
"lodash.pad": {
"version": "3.1.1",
"from": "lodash.pad@^3.0.0",
"resolved": "https://registry.npmjs.org/lodash.pad/-/lodash.pad-3.1.1.tgz"
},
"lodash.padleft": {
"version": "3.1.1",
"from": "lodash.padleft@^3.0.0",
@ -3774,16 +3774,16 @@
"from": "mkdirp@>=0.3.0 <0.4.0||>=0.4.0 <0.5.0||>=0.5.0 <0.6.0",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz"
},
"npmlog": {
"version": "1.2.1",
"from": "npmlog@~1.2.0",
"resolved": "https://registry.npmjs.org/npmlog/-/npmlog-1.2.1.tgz"
},
"node-uuid": {
"version": "1.4.3",
"from": "node-uuid@~1.4.3",
"resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.3.tgz"
},
"npmlog": {
"version": "1.2.1",
"from": "npmlog@~1.2.0",
"resolved": "https://registry.npmjs.org/npmlog/-/npmlog-1.2.1.tgz"
},
"oauth-sign": {
"version": "0.8.0",
"from": "oauth-sign@~0.8.0",
@ -3839,31 +3839,31 @@
"from": "string_decoder@~0.10.x",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz"
},
"strip-ansi": {
"version": "3.0.0",
"from": "strip-ansi@^3.0.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.0.tgz"
},
"stringstream": {
"version": "0.0.5",
"from": "stringstream@~0.0.4",
"resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz"
},
"strip-ansi": {
"version": "3.0.0",
"from": "strip-ansi@^3.0.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.0.tgz"
},
"strip-json-comments": {
"version": "0.1.3",
"from": "strip-json-comments@0.1.x",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-0.1.3.tgz"
},
"supports-color": {
"version": "2.0.0",
"from": "supports-color@^2.0.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz"
},
"tar": {
"version": "2.2.1",
"from": "tar@~2.2.0",
"resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz"
},
"supports-color": {
"version": "2.0.0",
"from": "supports-color@^2.0.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz"
},
"tough-cookie": {
"version": "2.2.0",
"from": "tough-cookie@~2.2.0",
@ -5147,7 +5147,7 @@
},
"lodash": {
"version": "3.10.1",
"from": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz",
"from": "lodash@>=3.10.1 <4.0.0",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz"
},
"lodash._baseassign": {
@ -6349,11 +6349,6 @@
"from": "uglify-to-browserify@>=1.0.0 <1.1.0",
"resolved": "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz"
},
"underscore": {
"version": "1.8.3",
"from": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz"
},
"user-home": {
"version": "1.1.1",
"from": "https://registry.npmjs.org/user-home/-/user-home-1.1.1.tgz",

View File

@ -151,6 +151,7 @@
"json-stable-stringify": "^1.0.1",
"json5": "^0.4.0",
"jstransform": "^11.0.3",
"lodash": "^3.10.1",
"mkdirp": "^0.5.1",
"module-deps": "^3.9.1",
"node-fetch": "^1.3.3",
@ -169,7 +170,6 @@
"stacktrace-parser": "^0.1.3",
"temp": "0.8.3",
"uglify-js": "^2.6.2",
"underscore": "^1.8.3",
"wordwrap": "^1.0.0",
"worker-farm": "^1.3.1",
"ws": "^0.8.0",

View File

@ -14,7 +14,6 @@ require('fast-path').replace();
useGracefulFs();
var debug = require('debug');
var omit = require('underscore').omit;
var Activity = require('./src/Activity');
exports.createServer = createServer;
@ -118,6 +117,16 @@ function createNonPersistentServer(options) {
return createServer(options);
}
function omit(obj, blacklistedKeys) {
return Object.keys(obj).reduce((clone, key) => {
if (blacklistedKeys.indexOf(key) === -1) {
clone[key] = obj[key];
}
return clone;
}, {});
}
// we need to listen on a socket as soon as a server is created, but only once.
// This file also serves as entry point when spawning a socket server; in that
// case we need to start the server immediately.

View File

@ -8,7 +8,7 @@
*/
'use strict';
const _ = require('underscore');
const _ = require('lodash');
const base64VLQ = require('./base64-vlq');
const BundleBase = require('./BundleBase');
const ModuleTransport = require('../lib/ModuleTransport');
@ -143,7 +143,7 @@ class Bundle extends BundleBase {
if (options.excludeSource) {
if (map.sourcesContent && map.sourcesContent.length) {
map = _.extend({}, map, {sourcesContent: []});
map = Object.assign({}, map, {sourcesContent: []});
}
}

View File

@ -8,7 +8,6 @@
*/
'use strict';
const _ = require('underscore');
const ModuleTransport = require('../lib/ModuleTransport');
class BundleBase {
@ -66,7 +65,7 @@ class BundleBase {
return this._source;
}
this._source = _.pluck(this._modules, 'code').join('\n');
this._source = this._modules.map((module) => module.code).join('\n');
return this._source;
}

View File

@ -8,7 +8,6 @@
*/
'use strict';
const _ = require('underscore');
const BundleBase = require('./BundleBase');
const ModuleTransport = require('../lib/ModuleTransport');

View File

@ -11,7 +11,7 @@
jest
.setMock('worker-farm', () => () => undefined)
.dontMock('node-haste/node_modules/throat')
.dontMock('underscore')
.dontMock('lodash')
.dontMock('../../lib/ModuleTransport')
.setMock('uglify-js')
.dontMock('../');

View File

@ -10,4 +10,4 @@
// Bug with Jest because we're going to the node_modules that is a sibling
// of what jest thinks our root (the dir with the package.json) should be.
module.exports = require.requireActual('underscore');
module.exports = require.requireActual('lodash');

View File

@ -8,16 +8,13 @@
*/
'use strict';
jest.dontMock('../')
.dontMock('underscore');
jest.dontMock('../');
jest.mock('path');
const Promise = require('promise');
const Resolver = require('../');
const path = require('path');
const _ = require('underscore');
let DependencyGraph = jest.genMockFn();
jest.setMock('node-haste', DependencyGraph);
@ -126,9 +123,9 @@ describe('Resolver', function() {
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies[result.dependencies.length - 1]).toBe(module);
expect(_.pluck(DependencyGraph.prototype.createPolyfill.mock.calls, 0)).toEqual([
{ file: 'polyfills/polyfills.js',
id: 'polyfills/polyfills.js',
expect(DependencyGraph.prototype.createPolyfill.mock.calls.map((call) => call[0])).toEqual([
{ id: 'polyfills/polyfills.js',
file: 'polyfills/polyfills.js',
dependencies: []
},
{ id: 'polyfills/console.js',

View File

@ -11,7 +11,7 @@
jest.setMock('worker-farm', function() { return () => {}; })
.dontMock('node-haste/node_modules/throat')
.dontMock('os')
.dontMock('underscore')
.dontMock('lodash')
.dontMock('path')
.dontMock('url')
.setMock('timers', { setImmediate: (fn) => setTimeout(fn, 0) })

View File

@ -15,7 +15,7 @@ const getPlatformExtension = require('node-haste').getPlatformExtension;
const Bundler = require('../Bundler');
const Promise = require('promise');
const _ = require('underscore');
const _ = require('lodash');
const declareOpts = require('../lib/declareOpts');
const path = require('path');
const url = require('url');
@ -492,7 +492,7 @@ class Server {
return true;
}).join('.') + '.js';
const sourceMapUrlObj = _.clone(urlObj);
const sourceMapUrlObj = Object.assign({}, urlObj);
sourceMapUrlObj.pathname = pathname.replace(/\.bundle$/, '.map');
// try to get the platform from the url

View File

@ -10,7 +10,6 @@
jest.setMock('uglify-js')
.mock('child_process')
.dontMock('underscore')
.dontMock('../');
var SocketInterface = require('../');

View File

@ -11,7 +11,6 @@
const Promise = require('promise');
const SocketClient = require('./SocketClient');
const SocketServer = require('./SocketServer');
const _ = require('underscore');
const crypto = require('crypto');
const debug = require('debug')('ReactNativePackager:SocketInterface');
const fs = require('fs');
@ -99,7 +98,7 @@ function createServer(resolve, reject, options, sockPath) {
const log = fs.openSync(logPath, 'a');
// Enable server debugging by default since it's going to a log file.
const env = _.clone(process.env);
const env = Object.assign({}, process.env);
env.DEBUG = 'ReactNativePackager:SocketServer';
// We have to go through the main entry point to make sure