Upgrade to lodash v4 (#660)

* npm install lodash-2

Someone handily published a lodash-2 v4.17.4 - it is exactly the same as lodash v4.17.4, so it is safe to use during the migration.

* use lodash-2 in tests

* update tests to split utils vs lodash

* remove Utils.nextTick usage

Utils.nextTick with a single argument is the same as process.nextTick

* lowercase utils

Because it seems that this is the coding style in this repo

* upgrade lodash in grunt/*

* keep lodash-2 as a dev dep for now

* use lodash-2 in scripts

* use snakeCase from utils

It was a mistake in my previous commit to not update this usage

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* split utils vs lodash in scripts/generate

Also use lodash-2 where it is easy to do so

* use utils.get until lodash upgrade

* remove lodash.isempty; lodash-2 now used in prod (in src/lib/apis/ code)

* unbundle lodash from utils

* upgrade to lodash 4

* remove lodash.get and lodash.trimEnd

* clean out unused code

* clean out unused code

* fix a breaking change listed under "notable changes" rather than under "breaking changes"...
This commit is contained in:
Dominykas Blyžė
2018-05-14 21:37:23 +03:00
committed by Spencer
parent 4cd2b3e506
commit f1de944809
67 changed files with 355 additions and 394 deletions

View File

@ -248,12 +248,12 @@ describe('Connection Pool', function () {
connection.setStatus('dead');
expect(_.size(clock.timers)).to.eql(1);
var id = _(clock.timers).keys().first();
var id = _(clock.timers).keys().head();
// it re-dies
connection.setStatus('dead');
expect(_.size(clock.timers)).to.eql(1);
expect(_(clock.timers).keys().first()).to.not.eql(id);
expect(_(clock.timers).keys().head()).to.not.eql(id);
});
it('does nothing when a connection is re-alive', function () {

View File

@ -2,7 +2,8 @@ describe('File Logger', function () {
var Log = require('../../../src/lib/log');
var FileLogger = require('../../../src/lib/loggers/file');
var once = require('events').EventEmitter.prototype.once;
var _ = require('../../../src/lib/utils');
var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var parentLog;
var logger;
var expect = require('expect.js');
@ -15,7 +16,7 @@ describe('File Logger', function () {
afterEach(function () {
parentLog.close();
logger && _.clearWriteStreamBuffer(logger.stream);
logger && utils.clearWriteStreamBuffer(logger.stream);
});
function makeLogger(parent, levels) {

View File

@ -4,7 +4,8 @@ describe('Stream Logger', function () {
var MockWritableStream = require('../../mocks/writable_stream');
var once = require('events').EventEmitter.prototype.once;
var stream = new MockWritableStream();
var _ = require('../../../src/lib/utils');
var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var expect = require('expect.js');
var parentLog;
@ -19,7 +20,7 @@ describe('Stream Logger', function () {
afterEach(function () {
parentLog.close();
_.clearWriteStreamBuffer(stream);
utils.clearWriteStreamBuffer(stream);
});
function makeLogger(parent, levels) {

View File

@ -285,7 +285,6 @@ describe('Transport Class', function () {
describe('randomizeHosts options', function () {
it('calls _.shuffle be default', function () {
var _ = require('../../../src/lib/utils');
stub(Transport.connectionPools.main.prototype, 'setHosts');
stub(_, 'shuffle');
var trans = new Transport({
@ -295,7 +294,6 @@ describe('Transport Class', function () {
expect(_.shuffle.callCount).to.eql(1);
});
it('skips the call to _.shuffle when false', function () {
var _ = require('../../../src/lib/utils');
stub(Transport.connectionPools.main.prototype, 'setHosts');
stub(_, 'shuffle');
var trans = new Transport({

View File

@ -1,4 +1,5 @@
var _ = require('../../../src/lib/utils');
var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var expect = require('expect.js');
var stub = require('../../utils/auto_release_stub').make();
@ -13,59 +14,59 @@ describe('Utils', function () {
};
it('likes arrays of strings', function () {
expect(_.isArrayOfStrings(thing.is)).to.be(true);
expect(utils.isArrayOfStrings(thing.is)).to.be(true);
});
it('dislikes when there is even one non string', function () {
// notice a string in the array
thing.is.push(thing.not || ' not ');
expect(_.isArrayOfStrings(thing.is)).to.be(false);
expect(utils.isArrayOfStrings(thing.is)).to.be(false);
});
});
describe('#isNumeric', function () {
it('likes integer literals', function () {
expect(_.isNumeric('-10')).to.be(true);
expect(_.isNumeric('0')).to.be(true);
expect(_.isNumeric('5')).to.be(true);
expect(_.isNumeric(-16)).to.be(true);
expect(_.isNumeric(0)).to.be(true);
expect(_.isNumeric(32)).to.be(true);
expect(_.isNumeric('040')).to.be(true);
expect(_.isNumeric('0xFF')).to.be(true);
expect(_.isNumeric(0xFFF)).to.be(true);
expect(utils.isNumeric('-10')).to.be(true);
expect(utils.isNumeric('0')).to.be(true);
expect(utils.isNumeric('5')).to.be(true);
expect(utils.isNumeric(-16)).to.be(true);
expect(utils.isNumeric(0)).to.be(true);
expect(utils.isNumeric(32)).to.be(true);
expect(utils.isNumeric('040')).to.be(true);
expect(utils.isNumeric('0xFF')).to.be(true);
expect(utils.isNumeric(0xFFF)).to.be(true);
});
it('likes float literals', function () {
expect(_.isNumeric('-1.6')).to.be(true);
expect(_.isNumeric('4.536')).to.be(true);
expect(_.isNumeric(-2.6)).to.be(true);
expect(_.isNumeric(3.1415)).to.be(true);
expect(_.isNumeric(8e5)).to.be(true);
expect(_.isNumeric('123e-2')).to.be(true);
expect(utils.isNumeric('-1.6')).to.be(true);
expect(utils.isNumeric('4.536')).to.be(true);
expect(utils.isNumeric(-2.6)).to.be(true);
expect(utils.isNumeric(3.1415)).to.be(true);
expect(utils.isNumeric(8e5)).to.be(true);
expect(utils.isNumeric('123e-2')).to.be(true);
});
it('dislikes non-numeric stuff', function () {
expect(_.isNumeric('')).to.be(false);
expect(_.isNumeric(' ')).to.be(false);
expect(_.isNumeric('\t\t')).to.be(false);
expect(_.isNumeric('abcdefghijklm1234567890')).to.be(false);
expect(_.isNumeric('xabcdefx')).to.be(false);
expect(_.isNumeric(true)).to.be(false);
expect(_.isNumeric(false)).to.be(false);
expect(_.isNumeric('bcfed5.2')).to.be(false);
expect(_.isNumeric('7.2acdgs')).to.be(false);
expect(_.isNumeric(undefined)).to.be(false);
expect(_.isNumeric(null)).to.be(false);
expect(_.isNumeric(NaN)).to.be(false);
expect(_.isNumeric(Infinity)).to.be(false);
expect(_.isNumeric(Number.POSITIVE_INFINITY)).to.be(false);
expect(_.isNumeric(Number.NEGATIVE_INFINITY)).to.be(false);
expect(_.isNumeric(new Date(2009, 1, 1))).to.be(false);
expect(_.isNumeric([])).to.be(false);
expect(_.isNumeric([1, 2, 3, 4])).to.be(false);
expect(_.isNumeric({})).to.be(false);
expect(_.isNumeric(function () {})).to.be(false);
expect(utils.isNumeric('')).to.be(false);
expect(utils.isNumeric(' ')).to.be(false);
expect(utils.isNumeric('\t\t')).to.be(false);
expect(utils.isNumeric('abcdefghijklm1234567890')).to.be(false);
expect(utils.isNumeric('xabcdefx')).to.be(false);
expect(utils.isNumeric(true)).to.be(false);
expect(utils.isNumeric(false)).to.be(false);
expect(utils.isNumeric('bcfed5.2')).to.be(false);
expect(utils.isNumeric('7.2acdgs')).to.be(false);
expect(utils.isNumeric(undefined)).to.be(false);
expect(utils.isNumeric(null)).to.be(false);
expect(utils.isNumeric(NaN)).to.be(false);
expect(utils.isNumeric(Infinity)).to.be(false);
expect(utils.isNumeric(Number.POSITIVE_INFINITY)).to.be(false);
expect(utils.isNumeric(Number.NEGATIVE_INFINITY)).to.be(false);
expect(utils.isNumeric(new Date(2009, 1, 1))).to.be(false);
expect(utils.isNumeric([])).to.be(false);
expect(utils.isNumeric([1, 2, 3, 4])).to.be(false);
expect(utils.isNumeric({})).to.be(false);
expect(utils.isNumeric(function () {})).to.be(false);
});
});
@ -82,20 +83,20 @@ describe('Utils', function () {
},
function (name, unit) {
it('likes ' + name, function () {
expect(_.isInterval('1' + unit)).to.be(true);
expect(utils.isInterval('1' + unit)).to.be(true);
});
it('likes decimal ' + name, function () {
expect(_.isInterval('1.5' + unit)).to.be(true);
expect(utils.isInterval('1.5' + unit)).to.be(true);
});
});
it('dislikes more than one unit', function () {
expect(_.isInterval('1my')).to.be(false);
expect(utils.isInterval('1my')).to.be(false);
});
it('dislikes spaces', function () {
expect(_.isInterval('1 m')).to.be(false);
expect(utils.isInterval('1 m')).to.be(false);
});
});
});
@ -105,108 +106,88 @@ describe('Utils', function () {
describe('#camelCase', function () {
it('find spaces, underscores, and other natural word breaks', function () {
expect(_.camelCase('Neil Patrick.Harris-is_a.dog')).to.eql('neilPatrickHarrisIsADog');
expect(utils.camelCase('Neil Patrick.Harris-is_a.dog')).to.eql('neilPatrickHarrisIsADog');
});
it('ignores abreviations', function () {
expect(_.camelCase('Json_parser')).to.eql('jsonParser');
expect(utils.camelCase('Json_parser')).to.eql('jsonParser');
});
it('handles leading _', function () {
expect(_.camelCase('_thing_one_')).to.eql('_thingOne');
expect(utils.camelCase('_thing_one_')).to.eql('_thingOne');
});
it('works on numbers', function () {
expect(_.camelCase('version 1.0')).to.eql('version10');
expect(utils.camelCase('version 1.0')).to.eql('version10');
});
});
describe('#studlyCase', function () {
it('find spaces, underscores, and other natural word breaks', function () {
expect(_.studlyCase('Neil Patrick.Harris-is_a.dog')).to.eql('NeilPatrickHarrisIsADog');
expect(utils.studlyCase('Neil Patrick.Harris-is_a.dog')).to.eql('NeilPatrickHarrisIsADog');
});
it('ignores abreviations', function () {
expect(_.studlyCase('Json_parser')).to.eql('JsonParser');
expect(utils.studlyCase('Json_parser')).to.eql('JsonParser');
});
it('handles leading _', function () {
expect(_.studlyCase('_thing_one_')).to.eql('_ThingOne');
expect(utils.studlyCase('_thing_one_')).to.eql('_ThingOne');
});
it('works on numbers', function () {
expect(_.studlyCase('version 1.0')).to.eql('Version10');
expect(utils.studlyCase('version 1.0')).to.eql('Version10');
});
});
describe('#snakeCase', function () {
it('find spaces, underscores, and other natural word breaks', function () {
expect(_.snakeCase('Neil Patrick.Harris-is_a.dog')).to.eql('neil_patrick_harris_is_a_dog');
expect(utils.snakeCase('Neil Patrick.Harris-is_a.dog')).to.eql('neil_patrick_harris_is_a_dog');
});
it('ignores abreviations', function () {
expect(_.snakeCase('Json_parser')).to.eql('json_parser');
expect(utils.snakeCase('Json_parser')).to.eql('json_parser');
});
it('handles leading _', function () {
expect(_.snakeCase('_thing_one_')).to.eql('_thing_one');
expect(utils.snakeCase('_thing_one_')).to.eql('_thing_one');
});
it('works on numbers', function () {
expect(_.snakeCase('version 1.0')).to.eql('version_1_0');
});
});
describe('#toLowerString', function () {
it('transforms normal strings', function () {
expect(_.toLowerString('PASTA')).to.eql('pasta');
});
it('ignores long form empty vals (null, false, undef)', function () {
expect(_.toLowerString(null)).to.eql('');
expect(_.toLowerString(false)).to.eql('');
expect(_.toLowerString(void 0)).to.eql('');
});
it('uses the objects own toString', function () {
expect(_.toLowerString(['A', 'B'])).to.eql('a,b');
});
it('sorta kinda works on objects', function () {
expect(_.toLowerString({ a: 'thing' })).to.eql('[object object]');
expect(utils.snakeCase('version 1.0')).to.eql('version_1_0');
});
});
describe('#toUpperString', function () {
it('transforms normal strings', function () {
expect(_.toUpperString('PASTA')).to.eql('PASTA');
expect(utils.toUpperString('PASTA')).to.eql('PASTA');
});
it('ignores long form empty vals (null, false, undef)', function () {
expect(_.toUpperString(null)).to.eql('');
expect(_.toUpperString(false)).to.eql('');
expect(_.toUpperString(void 0)).to.eql('');
expect(utils.toUpperString(null)).to.eql('');
expect(utils.toUpperString(false)).to.eql('');
expect(utils.toUpperString(void 0)).to.eql('');
});
it('uses the objects own toString', function () {
expect(_.toUpperString(['A', 'B'])).to.eql('A,B');
expect(utils.toUpperString(['A', 'B'])).to.eql('A,B');
});
it('sorta kinda works on objects', function () {
expect(_.toUpperString({ a: 'thing' })).to.eql('[OBJECT OBJECT]');
expect(utils.toUpperString({ a: 'thing' })).to.eql('[OBJECT OBJECT]');
});
});
describe('#repeat', function () {
it('repeats strings', function () {
expect(_.repeat(' ', 5)).to.eql(' ');
expect(_.repeat('foobar', 2)).to.eql('foobarfoobar');
expect(utils.repeat(' ', 5)).to.eql(' ');
expect(utils.repeat('foobar', 2)).to.eql('foobarfoobar');
});
});
describe('#ucfirst', function () {
it('only capitalized the first letter, lowercases everything else', function () {
expect(_.ucfirst('ALGER')).to.eql('Alger');
expect(utils.ucfirst('ALGER')).to.eql('Alger');
});
});
@ -219,14 +200,14 @@ describe('Utils', function () {
var obj = {
foo: 'bar'
};
expect(_.deepMerge(obj, { bar: 'baz' })).to.eql(obj);
expect(utils.deepMerge(obj, { bar: 'baz' })).to.eql(obj);
});
it('concats arrays', function () {
var obj = {
foo: ['bax', 'boz']
};
_.deepMerge(obj, { foo: ['boop'] });
utils.deepMerge(obj, { foo: ['boop'] });
expect(obj.foo).to.have.length(3);
});
@ -234,7 +215,7 @@ describe('Utils', function () {
var obj = {
foo: ['stop', 'foo', 'stahp']
};
_.deepMerge(obj, { foo: 'string' });
utils.deepMerge(obj, { foo: 'string' });
expect(obj.foo).to.have.length(3);
});
@ -245,7 +226,7 @@ describe('Utils', function () {
foo: ['bax', 'boz']
}
};
_.deepMerge(obj, { bax: { foo: ['poo'] } });
utils.deepMerge(obj, { bax: { foo: ['poo'] } });
expect(obj.bax.foo).to.have.length(3);
});
@ -254,25 +235,25 @@ describe('Utils', function () {
describe('#createArray', function () {
it('accepts an array of things and simply returns a copy of it', function () {
var inp = [{ a: 1 }, 'pizza'];
var out = _.createArray(inp);
var out = utils.createArray(inp);
expect(out).to.eql(inp);
expect(out).to.not.be(inp);
});
it('accepts a primitive value and calls the the transform function', function (done) {
_.createArray('str', function (val) {
utils.createArray('str', function (val) {
expect(val).to.be('str');
done();
});
});
it('wraps any non-array in an array', function () {
expect(_.createArray({})).to.eql([{}]);
expect(_.createArray('')).to.eql(['']);
expect(_.createArray(123)).to.eql([123]);
expect(_.createArray(/abc/)).to.eql([/abc/]);
expect(_.createArray(false)).to.eql([false]);
expect(utils.createArray({})).to.eql([{}]);
expect(utils.createArray('')).to.eql(['']);
expect(utils.createArray(123)).to.eql([123]);
expect(utils.createArray(/abc/)).to.eql([/abc/]);
expect(utils.createArray(false)).to.eql([false]);
});
it('returns false when the transform function returns undefined', function () {
expect(_.createArray(['str', 1], function (val) {
expect(utils.createArray(['str', 1], function (val) {
if (_.isString(val)) {
return {
val: val
@ -290,21 +271,21 @@ describe('Utils', function () {
var config = {
func: function () {}
};
expect(_.funcEnum(config, 'func', {}, 'toString'))
expect(utils.funcEnum(config, 'func', {}, 'toString'))
.to.be(config.func);
});
it('tests if the value at key in object is undefined, returns the option at key default if so', function () {
var config = {
func: undefined
};
expect(_.funcEnum(config, 'func', {}, 'toString'))
expect(utils.funcEnum(config, 'func', {}, 'toString'))
.to.be(Object.prototype.toString);
});
it('tests if the value at key in object is a string, returns the option at that key if so', function () {
var config = {
'config key name': 'toString'
};
expect(_.funcEnum(config, 'config key name', { toString: 'pizza' }, 'toJSON'))
expect(utils.funcEnum(config, 'config key name', { toString: 'pizza' }, 'toJSON'))
.to.be('pizza');
});
it('throws an informative error if the selection if invalid', function () {
@ -313,15 +294,15 @@ describe('Utils', function () {
};
expect(function () {
_.funcEnum(config, 'config', {});
utils.funcEnum(config, 'config', {});
}).to.throwError(/expected a function/i);
expect(function () {
_.funcEnum(config, 'config', { main: 'default' }, 'main');
utils.funcEnum(config, 'config', { main: 'default' }, 'main');
}).to.throwError(/expected a function or main/i);
expect(function () {
_.funcEnum(config, 'config', { main: 'default', other: 'default' }, 'main');
utils.funcEnum(config, 'config', { main: 'default', other: 'default' }, 'main');
}).to.throwError(/expected a function or one of main, other/i);
});
});
@ -337,7 +318,7 @@ describe('Utils', function () {
stub(func, method);
var args = _.map(new Array(i), function (val, i) { return i; });
_.applyArgs(func, null, args);
utils.applyArgs(func, null, args);
expect(func[method].callCount).to.eql(1);
if (method === 'apply') {
@ -354,7 +335,7 @@ describe('Utils', function () {
var args = _.map(new Array(argCount), function (val, i) { return i; });
var expected = args.slice(slice);
_.applyArgs(func, null, args, slice);
utils.applyArgs(func, null, args, slice);
expect(func[method].callCount).to.eql(1);
if (method === 'apply') {
@ -368,24 +349,24 @@ describe('Utils', function () {
describe('#getUnwrittenFromStream', function () {
it('ignores things that do not have writableState', function () {
expect(_.getUnwrittenFromStream()).to.be(undefined);
expect(_.getUnwrittenFromStream(false)).to.be(undefined);
expect(_.getUnwrittenFromStream([])).to.be(undefined);
expect(_.getUnwrittenFromStream({})).to.be(undefined);
expect(utils.getUnwrittenFromStream()).to.be(undefined);
expect(utils.getUnwrittenFromStream(false)).to.be(undefined);
expect(utils.getUnwrittenFromStream([])).to.be(undefined);
expect(utils.getUnwrittenFromStream({})).to.be(undefined);
});
if (require('stream').Writable) {
var MockWritableStream = require('../../mocks/writable_stream');
it('ignores empty stream', function () {
var stream = new MockWritableStream();
expect(_.getUnwrittenFromStream(stream)).to.be('');
expect(utils.getUnwrittenFromStream(stream)).to.be('');
});
it('returns only what is in the buffer', function () {
var stream = new MockWritableStream();
stream.write('hot');
stream.write('dog');
expect(_.getUnwrittenFromStream(stream)).to.be('dog');
expect(utils.getUnwrittenFromStream(stream)).to.be('dog');
});
}
});