Skip to content

Commit

Permalink
Object/Object
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 4, 2016
1 parent 5b89315 commit db23d79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ discrepances = function discrepances(a, b){
if(classA !== classB) {
return {classes:[classA,classB], values:[a,b]};
} else {
var rv = {};
if(classA==='Array') {
var res=[];
var max = Math.min(a.length, b.length);
for(var i_ab=0; i_ab<max; ++i_ab){
compare(res, a, b, i_ab);
}
var rv = {};
res.forEach(function(r,index) {
if(r) { rv[index] = r; }
});
if(a.length !== b.length) {
rv.length = discrepances(a.length,b.length);
}
return {array:rv};
} else if(classA==='Object') {
Object.keys(a).forEach(function(key) {
rv[key] = !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key]);
});
Object.keys(b).forEach(function(key) {
rv[key] = !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]);
});
return {object:rv};
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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)} }},
{skip:true, a:{x:1, y:2} , b:{y:3, z:{zz:3}} , expected: {object:{x:{onlyLeft:1}, y:discrepances(2,3), z:{onlyRight:{zz:3}}} }},
{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 },
].forEach(function(fixture){
if(fixture.skip) {
Expand Down

0 comments on commit db23d79

Please sign in to comment.