Skip to content

Commit

Permalink
Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 4, 2016
1 parent e9963c5 commit 5b89315
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function getClass(variable) {
if(variable instanceof Object) { return 'Object'; }
throw new Error('undetected class type');
}

function compare(result, a, b, index) {
var diff = discrepances(a[index], b[index]);
result.push(diff || null);
}

discrepances = function discrepances(a, b){
if(a === b){
return null;
Expand All @@ -49,8 +55,25 @@ discrepances = function discrepances(a, b){
if(typeA==='object' || typeB==='object') {
var classA = getClass(a);
var classB = getClass(b);
//console.log("classA", classA, "classB", classB)
if(classA !== classB) {
return {classes:[classA,classB], values:[a,b]};
} else {
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};
}
}
}
if(typeA === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("discrepances", function(){
{a:null , b:0 , expected: {types:['null' , 'number'], values:[null, 0] }},
{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)] }},
{skip:true, 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,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}}} }},
{skip:true, a:{x:1, y:2, z:[3]} , b:{x:1, y:2, z:[3]} , expected: null },
].forEach(function(fixture){
Expand Down

0 comments on commit 5b89315

Please sign in to comment.