Skip to content

Commit

Permalink
Arrays/Objects compared
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 4, 2016
1 parent db23d79 commit 1dc8544
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 6 additions & 9 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ 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;
it('skipped fixture: '+JSON.stringify(fixture));
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));
Expand Down

0 comments on commit 1dc8544

Please sign in to comment.