recursive-merge
Advanced tools
Comparing version 1.1.3 to 1.2.0
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var merge, | ||
var isArray, isObject, isScalar, merge, mergeArray, mergeObject, _type, | ||
__slice = [].slice; | ||
_type = Object.prototype.toString; | ||
isScalar = function(variable) { | ||
var _ref; | ||
return ((_ref = _type.call(variable)) !== '[object Array]' && _ref !== '[object Object]') || variable === null; | ||
}; | ||
isObject = function(variable) { | ||
return variable !== null && _type.call(variable) === '[object Object]'; | ||
}; | ||
isArray = function(variable) { | ||
return _type.call(variable) === '[object Array]'; | ||
}; | ||
merge = function(left, right) { | ||
var i, leftType, name, rightType, type, value, valueType, _i, _len; | ||
type = Object.prototype.toString; | ||
leftType = type.call(left); | ||
rightType = type.call(right); | ||
var leftType, rightType; | ||
if (isScalar(left) || isScalar(right)) { | ||
throw new Error('Can not merge scalar objects.'); | ||
} | ||
leftType = _type.call(left); | ||
rightType = _type.call(right); | ||
if (leftType !== rightType) { | ||
@@ -16,31 +33,55 @@ throw new Error('Can not merge ' + leftType + ' with ' + rightType + '.'); | ||
case '[object Array]': | ||
for (i = _i = 0, _len = right.length; _i < _len; i = ++_i) { | ||
value = right[i]; | ||
valueType = type.call(value); | ||
if ((valueType === '[object Array]' || valueType === '[object Object]') && value !== null) { | ||
left[i] = merge(left[i], value); | ||
} else { | ||
left.push(value); | ||
} | ||
} | ||
break; | ||
return mergeArray(left, right); | ||
case '[object Object]': | ||
for (name in right) { | ||
value = right[name]; | ||
if (right.hasOwnProperty(name) && (name !== '__proto__')) { | ||
valueType = type.call(value); | ||
if (typeof left[name] === 'undefined' || left[name] === null) { | ||
left[name] = value; | ||
} else if (valueType === '[object Array]' || valueType === '[object Object]') { | ||
left[name] = merge(left[name], value); | ||
} | ||
} | ||
} | ||
break; | ||
return mergeObject(left, right); | ||
default: | ||
throw new Error('Can not merge ' + leftType + ' objects.'); | ||
} | ||
}; | ||
mergeArray = function(left, right) { | ||
var add, i, leftValue, rightValue, value, _i, _j, _len, _len1; | ||
add = []; | ||
for (i = _i = 0, _len = right.length; _i < _len; i = ++_i) { | ||
rightValue = right[i]; | ||
leftValue = left[i]; | ||
if ((isObject(leftValue) && isObject(rightValue)) || (isArray(leftValue) && isArray(rightValue))) { | ||
left[i] = merge(leftValue, rightValue); | ||
} else if (isObject(rightValue)) { | ||
add.push(merge({}, rightValue)); | ||
} else if (isArray(rightValue)) { | ||
add.push(merge([], rightValue)); | ||
} else { | ||
add.push(rightValue); | ||
} | ||
} | ||
for (_j = 0, _len1 = add.length; _j < _len1; _j++) { | ||
value = add[_j]; | ||
left.push(value); | ||
} | ||
return left; | ||
}; | ||
mergeObject = function(left, right) { | ||
var key, mergeWith, value; | ||
for (key in right) { | ||
value = right[key]; | ||
if (right.hasOwnProperty(key) && (key !== '__proto__')) { | ||
if (isScalar(value)) { | ||
if (!left.hasOwnProperty(key)) { | ||
left[key] = value; | ||
} | ||
} else { | ||
if (left.hasOwnProperty(key)) { | ||
left[key] = merge(left[key], value); | ||
} else { | ||
mergeWith = isObject(value) ? {} : []; | ||
left[key] = merge(mergeWith, value); | ||
} | ||
} | ||
} | ||
} | ||
return left; | ||
}; | ||
module.exports = function() { | ||
@@ -47,0 +88,0 @@ var left, r, right, _i, _len; |
{ | ||
"name": "recursive-merge", | ||
"description": "Recursive merge tool for arrays and objects", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -31,4 +31,4 @@ "name": "David Kudera", | ||
"scripts": { | ||
"test": "cd ./test; echo \"Testing in node:\"; mocha ./node/index.js --reporter spec; cd ./browser; echo \"Testing in browser:\"; mocha-phantomjs ./index.html;" | ||
"test": "cd ./test; echo \"Testing in node:\"; mocha ./node/index.js --reporter spec; cd ./browser; echo \"Testing in browser:\"; simq build; mocha-phantomjs ./index.html;" | ||
} | ||
} |
@@ -43,2 +43,5 @@ # Recursive merge | ||
* 1.2.0 | ||
+ Rewritten | ||
* 1.1.2 - 1.1.3 | ||
@@ -45,0 +48,0 @@ + Bugs in IE8 |
@@ -174,10 +174,27 @@ /** Generated by SimQ **/ | ||
(function() { | ||
var merge, | ||
var isArray, isObject, isScalar, merge, mergeArray, mergeObject, _type, | ||
__slice = [].slice; | ||
_type = Object.prototype.toString; | ||
isScalar = function(variable) { | ||
var _ref; | ||
return ((_ref = _type.call(variable)) !== '[object Array]' && _ref !== '[object Object]') || variable === null; | ||
}; | ||
isObject = function(variable) { | ||
return variable !== null && _type.call(variable) === '[object Object]'; | ||
}; | ||
isArray = function(variable) { | ||
return _type.call(variable) === '[object Array]'; | ||
}; | ||
merge = function(left, right) { | ||
var i, leftType, name, rightType, type, value, valueType, _i, _len; | ||
type = Object.prototype.toString; | ||
leftType = type.call(left); | ||
rightType = type.call(right); | ||
var leftType, rightType; | ||
if (isScalar(left) || isScalar(right)) { | ||
throw new Error('Can not merge scalar objects.'); | ||
} | ||
leftType = _type.call(left); | ||
rightType = _type.call(right); | ||
if (leftType !== rightType) { | ||
@@ -188,31 +205,55 @@ throw new Error('Can not merge ' + leftType + ' with ' + rightType + '.'); | ||
case '[object Array]': | ||
for (i = _i = 0, _len = right.length; _i < _len; i = ++_i) { | ||
value = right[i]; | ||
valueType = type.call(value); | ||
if ((valueType === '[object Array]' || valueType === '[object Object]') && value !== null) { | ||
left[i] = merge(left[i], value); | ||
} else { | ||
left.push(value); | ||
} | ||
} | ||
break; | ||
return mergeArray(left, right); | ||
case '[object Object]': | ||
for (name in right) { | ||
value = right[name]; | ||
if (right.hasOwnProperty(name) && (name !== '__proto__')) { | ||
valueType = type.call(value); | ||
if (typeof left[name] === 'undefined' || left[name] === null) { | ||
left[name] = value; | ||
} else if (valueType === '[object Array]' || valueType === '[object Object]') { | ||
left[name] = merge(left[name], value); | ||
} | ||
} | ||
} | ||
break; | ||
return mergeObject(left, right); | ||
default: | ||
throw new Error('Can not merge ' + leftType + ' objects.'); | ||
} | ||
}; | ||
mergeArray = function(left, right) { | ||
var add, i, leftValue, rightValue, value, _i, _j, _len, _len1; | ||
add = []; | ||
for (i = _i = 0, _len = right.length; _i < _len; i = ++_i) { | ||
rightValue = right[i]; | ||
leftValue = left[i]; | ||
if ((isObject(leftValue) && isObject(rightValue)) || (isArray(leftValue) && isArray(rightValue))) { | ||
left[i] = merge(leftValue, rightValue); | ||
} else if (isObject(rightValue)) { | ||
add.push(merge({}, rightValue)); | ||
} else if (isArray(rightValue)) { | ||
add.push(merge([], rightValue)); | ||
} else { | ||
add.push(rightValue); | ||
} | ||
} | ||
for (_j = 0, _len1 = add.length; _j < _len1; _j++) { | ||
value = add[_j]; | ||
left.push(value); | ||
} | ||
return left; | ||
}; | ||
mergeObject = function(left, right) { | ||
var key, mergeWith, value; | ||
for (key in right) { | ||
value = right[key]; | ||
if (right.hasOwnProperty(key) && (key !== '__proto__')) { | ||
if (isScalar(value)) { | ||
if (!left.hasOwnProperty(key)) { | ||
left[key] = value; | ||
} | ||
} else { | ||
if (left.hasOwnProperty(key)) { | ||
left[key] = merge(left[key], value); | ||
} else { | ||
mergeWith = isObject(value) ? {} : []; | ||
left[key] = merge(mergeWith, value); | ||
} | ||
} | ||
} | ||
} | ||
return left; | ||
}; | ||
module.exports = function() { | ||
@@ -257,14 +298,20 @@ var left, r, right, _i, _len; | ||
return merge('', ''); | ||
}).to["throw"](Error, 'Can not merge [object String] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
expect(function() { | ||
return merge(1, 1); | ||
}).to["throw"](Error, 'Can not merge [object Number] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
return expect(function() { | ||
return merge(true, true); | ||
}).to["throw"](Error, 'Can not merge [object Boolean] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
}); | ||
it('should return merged arrays', function() { | ||
expect(merge([1, 2, 3], [1, 2, 3, 4, 5])).to.be.eql([1, 2, 3, 1, 2, 3, 4, 5]); | ||
expect(merge([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]])).to.be.eql([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]); | ||
expect(merge([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])).to.be.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); | ||
it('should return merged simple arrays', function() { | ||
return expect(merge([1, 1, 2, 3], [3, 4, 4, 5], [10, 9, 8, 1])).to.be.eql([1, 1, 2, 3, 3, 4, 4, 5, 10, 9, 8, 1]); | ||
}); | ||
it('should return merged advanced arrays', function() { | ||
return expect(merge([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]])).to.be.eql([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]); | ||
}); | ||
it('should return merged more than two arrays', function() { | ||
return expect(merge([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])).to.be.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); | ||
}); | ||
it('should return merged arrays with objects', function() { | ||
return expect(merge([ | ||
@@ -293,4 +340,4 @@ { | ||
}); | ||
return it('should return merged objects', function() { | ||
expect(merge({ | ||
it('should return merged simple objects', function() { | ||
return expect(merge({ | ||
hello: 'world' | ||
@@ -303,3 +350,5 @@ }, { | ||
}); | ||
expect(merge({ | ||
}); | ||
it('should return merged advanced objects', function() { | ||
return expect(merge({ | ||
one: { | ||
@@ -338,3 +387,5 @@ two: 2, | ||
}); | ||
expect(merge({ | ||
}); | ||
it('should return merged more than two objects', function() { | ||
return expect(merge({ | ||
one: 1 | ||
@@ -353,2 +404,4 @@ }, { | ||
}); | ||
}); | ||
return it('should return merged objects with arrays', function() { | ||
return expect(merge({ | ||
@@ -390,3 +443,3 @@ one: [1] | ||
"description": "Recursive merge tool for arrays and objects", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -426,3 +479,3 @@ "name": "David Kudera", | ||
}); | ||
require.__setStats({"/lib/Merge.js":{"atime":1385388465000,"mtime":1385388458000,"ctime":1385388458000},"/test/browser/tests/Merge.coffee":{"atime":1385387157000,"mtime":1385387105000,"ctime":1385387105000},"/package.json":{"atime":1385388477000,"mtime":1385388471000,"ctime":1385388471000}}); | ||
require.__setStats({"/lib/Merge.js":{"atime":1385409968000,"mtime":1385409966000,"ctime":1385409966000},"/test/browser/tests/Merge.coffee":{"atime":1385410275000,"mtime":1385410273000,"ctime":1385410273000},"/package.json":{"atime":1385410221000,"mtime":1385410220000,"ctime":1385410220000}}); | ||
require.version = '5.1.2'; | ||
@@ -429,0 +482,0 @@ |
@@ -18,14 +18,20 @@ // Generated by CoffeeScript 1.6.3 | ||
return merge('', ''); | ||
}).to["throw"](Error, 'Can not merge [object String] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
expect(function() { | ||
return merge(1, 1); | ||
}).to["throw"](Error, 'Can not merge [object Number] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
return expect(function() { | ||
return merge(true, true); | ||
}).to["throw"](Error, 'Can not merge [object Boolean] objects.'); | ||
}).to["throw"](Error, 'Can not merge scalar objects.'); | ||
}); | ||
it('should return merged arrays', function() { | ||
expect(merge([1, 2, 3], [1, 2, 3, 4, 5])).to.be.eql([1, 2, 3, 1, 2, 3, 4, 5]); | ||
expect(merge([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]])).to.be.eql([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]); | ||
expect(merge([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])).to.be.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); | ||
it('should return merged simple arrays', function() { | ||
return expect(merge([1, 1, 2, 3], [3, 4, 4, 5], [10, 9, 8, 1])).to.be.eql([1, 1, 2, 3, 3, 4, 4, 5, 10, 9, 8, 1]); | ||
}); | ||
it('should return merged advanced arrays', function() { | ||
return expect(merge([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]])).to.be.eql([[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]); | ||
}); | ||
it('should return merged more than two arrays', function() { | ||
return expect(merge([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])).to.be.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); | ||
}); | ||
it('should return merged arrays with objects', function() { | ||
return expect(merge([ | ||
@@ -54,4 +60,4 @@ { | ||
}); | ||
return it('should return merged objects', function() { | ||
expect(merge({ | ||
it('should return merged simple objects', function() { | ||
return expect(merge({ | ||
hello: 'world' | ||
@@ -64,3 +70,5 @@ }, { | ||
}); | ||
expect(merge({ | ||
}); | ||
it('should return merged advanced objects', function() { | ||
return expect(merge({ | ||
one: { | ||
@@ -99,3 +107,5 @@ two: 2, | ||
}); | ||
expect(merge({ | ||
}); | ||
it('should return merged more than two objects', function() { | ||
return expect(merge({ | ||
one: 1 | ||
@@ -114,2 +124,4 @@ }, { | ||
}); | ||
}); | ||
return it('should return merged objects with arrays', function() { | ||
return expect(merge({ | ||
@@ -116,0 +128,0 @@ one: [1] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
29767
687
56