remove deepMerge util
This commit is contained in:
@ -11,34 +11,6 @@ var utils = {};
|
||||
|
||||
utils.inherits = nodeUtils.inherits;
|
||||
|
||||
/**
|
||||
* Recursively merge two objects, walking into each object and concating arrays. If both to and from have a value at a
|
||||
* key, but the values' types don't match to's value is left unmodified. Only Array and Object values are merged - that
|
||||
* it to say values with a typeof "object"
|
||||
*
|
||||
* @param {Object} to - Object to merge into (no cloning, the original object
|
||||
* is modified)
|
||||
* @param {Object} from - Object to pull changed from
|
||||
* @return {Object} - returns the modified to value
|
||||
*/
|
||||
utils.deepMerge = function (to, from) {
|
||||
_.each(from, function (fromVal, key) {
|
||||
switch (typeof to[key]) {
|
||||
case 'undefined':
|
||||
to[key] = from[key];
|
||||
break;
|
||||
case 'object':
|
||||
if (_.isArray(to[key]) && _.isArray(from[key])) {
|
||||
to[key] = to[key].concat(from[key]);
|
||||
}
|
||||
else if (_.isPlainObject(to[key]) && _.isPlainObject(from[key])) {
|
||||
utils.deepMerge(to[key], from[key]);
|
||||
}
|
||||
}
|
||||
});
|
||||
return to;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test if a value is an array and its contents are string type
|
||||
*
|
||||
|
||||
@ -193,45 +193,6 @@ describe('Utils', function () {
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('#deepMerge', function () {
|
||||
|
||||
it('returns the same object that was passed', function () {
|
||||
var obj = {
|
||||
foo: 'bar'
|
||||
};
|
||||
expect(utils.deepMerge(obj, { bar: 'baz' })).to.eql(obj);
|
||||
});
|
||||
|
||||
it('concats arrays', function () {
|
||||
var obj = {
|
||||
foo: ['bax', 'boz']
|
||||
};
|
||||
utils.deepMerge(obj, { foo: ['boop'] });
|
||||
expect(obj.foo).to.have.length(3);
|
||||
});
|
||||
|
||||
it('wont merge values of different types', function () {
|
||||
var obj = {
|
||||
foo: ['stop', 'foo', 'stahp']
|
||||
};
|
||||
utils.deepMerge(obj, { foo: 'string' });
|
||||
expect(obj.foo).to.have.length(3);
|
||||
});
|
||||
|
||||
it('works recursively', function () {
|
||||
var obj = {
|
||||
foo: 'bar',
|
||||
bax: {
|
||||
foo: ['bax', 'boz']
|
||||
}
|
||||
};
|
||||
utils.deepMerge(obj, { bax: { foo: ['poo'] } });
|
||||
expect(obj.bax.foo).to.have.length(3);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#createArray', function () {
|
||||
it('accepts an array of things and simply returns a copy of it', function () {
|
||||
var inp = [{ a: 1 }, 'pizza'];
|
||||
|
||||
Reference in New Issue
Block a user