Bug can at anywhere,Can you find it?
由array撰写的日志
2组 json diff 对比
十二 2nd
/**
*@jsonDataHandler.diff(json1,json2)
*/
Array.prototype.compareTo = function(compareAry) {
if (this.length === compareAry.length) {
var i;
for (i = 0; i < compareAry.length; i+=1) {
if (Object.isArray(this[i]) === true) {
if (this[i].compareTo(compareAry[i]) === false) {
return false;
}
continue;
}
else if (this[i] !== compareAry[i]) {
return false;
}
}
return true;
}
return false;
};
(function() {
var _toString = Object.prototype.toString;
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
function keys(object) {
var results = [];
for (var property in object)
results.push(property);
return results;
}
function isArray(object) {
return _toString.call(object) == "[object Array]";
}
extend(Object, {
extend: extend,
keys: keys,
isArray: isArray
});
})();
(function() {
var arrayProto = Array.prototype;
function indexOf(item, i) {
i || (i = 0);
var length = this.length;
if (i < 0) i = length + i;
for (; i < length; i++)
if (this[i] === item) return i;
return -1;
}
if (!arrayProto.indexOf) arrayProto.indexOf = indexOf;
})();
var jsonDataHandler = {
merge: function(j1, j2) {
if (typeof this.merging === "undefined" || this.merging === 0) {
this.mergeCyclicCheck = [];
this.merging = 0;
}
this.merging += 1;
if (typeof j1 === "undefined") {
j1 = {};
}
if (typeof j2 === "undefined") {
j2 = {};
}
if (typeof this.mergeCyclicCheck === "undefined") {
this.mergeCyclicCheck = [];
}
var key;
for (key in j2) if (j2.hasOwnProperty(key)) {
if (typeof j1[key] === "undefined") {
j1[key] = j2[key];
}
else {
if (typeof j2[key] === "object") {
if (this.mergeCyclicCheck.indexOf(j1[key]) >= 0) {
break;
}
this.merge(j1[key], j2[key]);
this.mergeCyclicCheck.push(j1[key]);
}
else {
j1[key] = j2[key];
}
}
}
this.merging -= 1;
},
diff: function(j1, j2) {
if (typeof this.diffing === "undefined" || this.diffing === 0) {
this.diffCyclicCheck = [];
this.diffing = 0;
}
var diffRes = {};
this.diffing += 1;
if (typeof j1 === "undefined") {
j1 = {};
}
if (typeof j2 === "undefined") {
j2 = {};
}
if (typeof this.diffCyclicCheck === "undefined") {
this.diffCyclicCheck = [];
}
var key, bDiff;
for (key in j2) if (j2.hasOwnProperty(key)) {
bDiff = false;
if (typeof j1[key] === "undefined" || typeof j1[key] != typeof j2[key]) {
bDiff = true;
}
else if (j1[key] !== j2[key]) {
if (typeof j2[key] === "object") {
if (this.diffCyclicCheck.indexOf(j2[key]) >= 0) {
break;
}
else if (Object.isArray(j2[key])) {
if (j1[key].length !== j2[key].length || j1[key] !== j2[key]) {
if (j2[key].compareTo(j1[key]) === false) {
bDiff = true;
}
}
}
else if (typeof j1[key] === "object") {
var dR = this.diff(j1[key], j2[key]);
if (Object.keys(dR).length > 0) {
diffRes[key] = dR;
}
}
else {
bDiff = true;
}
this.diffCyclicCheck.push(j2[key]);
}
else if (j1[key] !== j2[key]) {
bDiff = true;
}
}
if (bDiff) {
diffRes[key] = j2[key];
}
}
for (key in j1) if (j1.hasOwnProperty(key)) {
bDiff = false;
if (typeof j2[key] === "undefined") {
diffRes[key] = j1[key];
}
}
this.diffing -= 1;
return diffRes;
}
};
2组Array diff 对比
十二 2nd
2组array 进行对比,取其差异部分。
/**
* @array_diff(new,old)
*/
function array_diff(){var arr1=arguments[0],retArr={};var k1='',i=1,k='',arr={};arr1keys:for(k1 in arr1){for(i=1;i<arguments.length;i++){arr=arguments[i];for(k in arr){if(arr[k]===arr1[k1]){continue arr1keys;}}
retArr[k1]=arr1[k1];}}
return retArr;}