Comparing version 0.5.1 to 0.5.2
{ | ||
"name": "corridor", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "JSON -> HTML -> JSON data corridor", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1031,3 +1031,3 @@ /** | ||
strategy = defaults.merge, | ||
i, ii, key, tmp; | ||
i, ii, key, otherLength, tmp, type; | ||
@@ -1039,14 +1039,15 @@ if (opts && 'merge' in opts) { | ||
if (arraylike(other)) { | ||
otherLength = getLength(other); | ||
if (toString.call(obj) === '[object Array]') { | ||
if (strategy === 'concat') { | ||
for (i = 0, ii = other.length; i < ii; i++) { | ||
for (i = 0; i < otherLength; i++) { | ||
obj.push(other[i]); | ||
} | ||
} else if (strategy === 'extend') { | ||
for (i = 0, ii = other.length; i < ii; i++) { | ||
for (i = 0; i < otherLength; i++) { | ||
obj[i] = merge(obj[i], other[i], opts); | ||
} | ||
} else { | ||
if (!obj.length || other.length > 1) { | ||
for (i = 0, ii = other.length; i < ii; i++) { | ||
if (!obj.length || otherLength > 1) { | ||
for (i = 0; i < otherLength; i++) { | ||
obj.push(other[i]); | ||
@@ -1063,5 +1064,10 @@ } | ||
} else { | ||
for (i = 0, ii = other.length; i < ii; i++) { | ||
if (i in obj && typeof obj[i] === 'object' && obj[i] !== null) { | ||
obj[i] = merge(obj[i], other[i], opts); | ||
for (i = 0; i < otherLength; i++) { | ||
if (i in obj && typeof obj[i] === 'object' && obj[i] !== null && typeof other[i] === 'object') { | ||
type = toString.call(other[i]); | ||
if (type === '[object Array]' || type === '[object Object]') { | ||
obj[i] = merge(obj[i], other[i], opts); | ||
} else { | ||
obj[i] = other[i]; | ||
} | ||
} else { | ||
@@ -1081,4 +1087,9 @@ obj[i] = other[i]; | ||
for (key in other) { | ||
if (key in obj && typeof obj[key] === 'object' && obj[key] !== null) { | ||
obj[key] = merge(obj[key], other[key], opts); | ||
if (key in obj && typeof obj[key] === 'object' && obj[key] !== null && typeof other[key] === 'object') { | ||
type = toString.call(other[key]); | ||
if (type === '[object Array]' || type === '[object Object]') { | ||
obj[key] = merge(obj[key], other[key], opts); | ||
} else { | ||
obj[key] = other[key]; | ||
} | ||
} else { | ||
@@ -1178,2 +1189,24 @@ obj[key] = other[key]; | ||
}, | ||
/** | ||
* Determine the length of an arraylike object by either returning its length | ||
* property or counting its keys. | ||
* @param {mixed} obj The object whose length is to be determined. | ||
* @param {number} The length of the arraylike object. | ||
*/ | ||
getLength = arraylike.getLength = function(obj) { | ||
if ('length' in obj) { | ||
return obj.length; | ||
} | ||
var length = 0, k; | ||
for (k in obj) { | ||
length++; | ||
} | ||
return length; | ||
}, | ||
@@ -1180,0 +1213,0 @@ /** |
@@ -18,2 +18,6 @@ /** | ||
},{ | ||
obj: new String('hi'), | ||
expected: false, | ||
reason: 'a wrapped primitive value is not array-like' | ||
},{ | ||
obj: ['hi'], | ||
@@ -20,0 +24,0 @@ expected: true, |
@@ -30,2 +30,7 @@ /** | ||
reason: 'nested conflicting objects should have their keys take precidence' | ||
},{ | ||
obj: { "person": { "name": "original" } }, | ||
other: { "person": { "name": new String("other") } }, | ||
expected: { "person": { "name": "other" } }, | ||
reason: 'wrapped primitives should be coerced to native primitives' | ||
}]; | ||
@@ -92,3 +97,3 @@ | ||
exports['corridor.merge(mismatch)'] = function(test) { | ||
exports['corridor.merge(arraylike)'] = function(test) { | ||
@@ -103,4 +108,30 @@ var | ||
expected: ['a', 'b'], | ||
reason: 'arraylike objects should behave like arrays for merging' | ||
reason: 'arraylike objects should contribute to arrays when merging' | ||
},{ | ||
obj: {"a":"b", 0:{"foo":"bar"}}, | ||
other: {0:{"foo":"baz"}, 1:"c"}, | ||
expected: {"a":"b", 0:{"foo":"baz"}, 1:"c"}, | ||
reason: 'arraylike objects should merge into objects like objects' | ||
}]; | ||
test.expect(suite.length); | ||
for (var i = 0, ii = suite.length; i < ii; i++) { | ||
(function(data){ | ||
var actual = corridor.merge(data.obj, data.other); | ||
test.equals(JSON.stringify(actual), JSON.stringify(data.expected), data.reason); | ||
})(suite[i]); | ||
} | ||
test.done(); | ||
}; | ||
exports['corridor.merge(mismatch)'] = function(test) { | ||
var | ||
corridor = require('../src/corridor.js'), | ||
suite = [{ | ||
obj: {"foo":"bar"}, | ||
@@ -111,2 +142,7 @@ other: ["baz"], | ||
},{ | ||
obj: {0:[]}, | ||
other: ["baz"], | ||
expected: {0:"baz"}, | ||
reason: 'an array should contribute numeric keys to an object, and overwrite on type mismatch' | ||
},{ | ||
obj: ['baz'], | ||
@@ -121,2 +157,27 @@ other: {"foo":"bar"}, | ||
reason: 'merging a non-array-like sub-key into an array should objectify the array' | ||
},{ | ||
obj: {"foo":[]}, | ||
other: {"foo":"bar"}, | ||
expected: {"foo":"bar"}, | ||
reason: 'a primitive value should replace an array when mismatched in an object' | ||
},{ | ||
obj: {"foo":[]}, | ||
other: {"foo":new String("bar")}, | ||
expected: {"foo":"bar"}, | ||
reason: 'a wrapped primitive value should replace an array when mismatched in an object' | ||
},{ | ||
obj: {"foo":{"baz":5}}, | ||
other: {"foo":"bar"}, | ||
expected: {"foo":"bar"}, | ||
reason: 'a primitive value should replace an object when mismatched in an object' | ||
},{ | ||
obj: {"foo":"bar"}, | ||
other: {"foo":[]}, | ||
expected: {"foo":[]}, | ||
reason: 'an array value should replace a primitive when mismatched in an object' | ||
},{ | ||
obj: {"foo":"bar"}, | ||
other: {"foo":{"baz":5}}, | ||
expected: {"foo":{"baz":5}}, | ||
reason: 'an object should replace a primitive value when mismatched in an object' | ||
}]; | ||
@@ -123,0 +184,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171870
2098