Skip to content

Commit

Permalink
Nueva salida para diffs de objetos
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Nov 1, 2016
1 parent 6144a87 commit d2be252
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
16 changes: 10 additions & 6 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,23 @@ function compareObjects(a, b, opts) {
setIfDiff(rv, key, !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]));
});
} else {
var diffs = [], keys=[], values=[];
var diffs = [];
aKeys.forEach(function(key,index) {
var dif = {pos:index};
var bKey = bKeys[index];
if(key !== bKey) {
diffs.push(index);
keys.push([key, bKey]);
values.push(discrepances(a[key], b[bKey]));
dif.keys = [key, bKey];
var vdif = discrepances(a[key], b[bKey]);
if(vdif) { dif.values = vdif; }
} else {
setIfDiff(dif, 'values', discrepances(a[key], b[key]));
}
if(dif.keys || dif.values) {
diffs.push(dif);
}
});
if(diffs.length) {
rv.differences = diffs;
rv.keys = keys;
rv.values = values;
}
}
return Object.keys(rv).length ? {object:rv} : null;
Expand Down
25 changes: 19 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ describe("discrepances", function(){
b:{two:'dos', one:'un'},
expect:{
object:{
differences:[0,1],
keys:[['one','two'] , ['two','one']],
values:[discrepances('un','dos'), discrepances('dos','un')]
differences:[
{pos:0, keys:['one','two'], values:discrepances('un','dos')},
{pos:1, keys:['two','one'], values:discrepances('dos','un')},
]
}
},
opts:{unordered:false}
Expand All @@ -69,14 +70,26 @@ describe("discrepances", function(){
b:{one:'un', zwei:'dos'},
expect:{
object:{
differences:[1],
keys:[['two','zwei']],
values:[null]
differences:[
{pos:1, keys:['two','zwei']},
]
}
},
opts:{unordered:false}
},
{a: [{a:'A', b:'B', c:'C'}] , b:[{a:'a', b:'B', c:'C'}] , expect:{array:{object:{a:discrepances('A','a')} }} },
{ a:{one:'un', two:'dos'},
b:{one:'ein', zwei:'dos'},
expect:{
object:{
differences:[
{pos:0, values:discrepances('un','ein')},
{pos:1, keys:['two','zwei']},
]
}
},
opts:{unordered:false}
},
];
// esto es para evitar que values:[] tenga fechas distintas a 'a' y 'b'
var dateFixtures = [
Expand Down

0 comments on commit d2be252

Please sign in to comment.