From 1dc85446954c34012c78edffd8dd5057f56856ed Mon Sep 17 00:00:00 2001 From: "Diego F.(EW7)" Date: Tue, 4 Oct 2016 10:55:32 -0300 Subject: [PATCH] Arrays/Objects compared --- lib/discrepances.js | 15 ++++++--------- test/test.js | 4 +++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/discrepances.js b/lib/discrepances.js index 224a3d8..5292f16 100644 --- a/lib/discrepances.js +++ b/lib/discrepances.js @@ -72,15 +72,17 @@ discrepances = function discrepances(a, b){ if(a.length !== b.length) { rv.length = discrepances(a.length,b.length); } - return {array:rv}; + return rv.length ? {array:rv} : null; } else if(classA==='Object') { Object.keys(a).forEach(function(key) { - rv[key] = !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key]); + var r = !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key]); + if(r) { rv[key] = r; } }); Object.keys(b).forEach(function(key) { - rv[key] = !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]); + var r = !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]); + if(r) { rv[key] = r; } }); - return {object:rv}; + return Object.keys(rv).length ? {object:rv} : null; } } } @@ -90,11 +92,6 @@ discrepances = function discrepances(a, b){ } else { return {types:[typeA,typeB], values:[a,b]} } - // var rta=''; - // if(def && def.typeof && typeof obj !== def.typeof){ - // rta+='typeof '+JSON.stringify(obj)+' !== '+JSON.stringify(def.typeof); - // } - // return rta || null; } return discrepances; diff --git a/test/test.js b/test/test.js index ba02964..959c92c 100644 --- a/test/test.js +++ b/test/test.js @@ -15,8 +15,9 @@ describe("discrepances", function(){ {a:fechaActual , b:/a/ , expected: {classes:['Date' , 'RegExp'], values:[fechaActual, /a/] }}, {skip:true, a:new Date(2011,1,3), b:new Date(2011,1,4) , expected: {difference:'a definir', values:[new Date(2011,1,3), new Date(2011,1,4)] }}, {a:[1,2,3,4,5] , b:[1,2,33,4,5,6] , expected: {array:{length:discrepances(5,6), 2:discrepances(3,33)} }}, + {a:[1,2,3,4,5] , b:[1,2,3,4,5] , expected: null }, {a:{x:1, y:2} , b:{y:3, z:{zz:3}} , expected: {object:{x:{onlyLeft:1}, y:discrepances(2,3), z:{onlyRight:{zz:3}}} }}, - {skip:true, a:{x:1, y:2, z:[3]} , b:{x:1, y:2, z:[3]} , expected: null }, + {a:{x:1, y:2, z:[3]} , b:{x:1, y:2, z:[3]} , expected: null }, ].forEach(function(fixture){ if(fixture.skip) { delete fixture.skip; @@ -24,6 +25,7 @@ describe("discrepances", function(){ return true; } it("fixture: "+JSON.stringify(fixture), function(){ + //console.log("RES", discrepances(fixture.a, fixture.b)); console.log("EXP", fixture.expected); expect(discrepances(fixture.a, fixture.b)).to.eql(fixture.expected); expect(JSON.stringify(discrepances(fixture.a, fixture.b))).to.eql(JSON.stringify(fixture.expected)); expect(JSON4all.stringify(discrepances(fixture.a, fixture.b))).to.eql(JSON4all.stringify(fixture.expected));