diff --git a/lib/discrepances.js b/lib/discrepances.js index 1ad21fc..07d53a7 100644 --- a/lib/discrepances.js +++ b/lib/discrepances.js @@ -30,6 +30,7 @@ var bestGlobals = require('best-globals'); var datetime = bestGlobals.datetime; var timeInterval = bestGlobals.timeInterval; var constructorName = bestGlobals.constructorName; +var changing = bestGlobals.changing; function getType(variable) { if(null === variable) { return 'null'; } @@ -49,8 +50,6 @@ function compare(resultArray, a, b, index) { resultArray.push(diff || null); } -function setIfDiff(obj, key, val) { if(val) { obj[key] = val; } } - function timeStr(dt) { return datetime(dt).toYmdHmsM().substr(11); } function compareStrings(a,b) { @@ -120,16 +119,36 @@ function compareDates(a, b) { return {difference:res.join(''), values:[a,b]}; } +function setIfDiff(obj, key, val) { if(val) { obj[key] = val; } } + +// function compareOrdered(aKeys, ) function compareObjects(a, b, opts) { var rv = {}; var aKeys = Object.keys(a); var bKeys = Object.keys(b); - aKeys.forEach(function(key) { - setIfDiff(rv, key, !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key])); - }); - bKeys.forEach(function(key) { - setIfDiff(rv, key, !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key])); - }); + if(opts.unordered) { + aKeys.forEach(function(key) { + setIfDiff(rv, key, !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key])); + }); + bKeys.forEach(function(key) { + setIfDiff(rv, key, !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key])); + }); + } else { + var diffs = [], keys=[], values=[]; + aKeys.forEach(function(key,index) { + var bKey = bKeys[index]; + if(key !== bKey) { + diffs.push(index); + keys.push([key, bKey]); + values.push(discrepances(a[key], b[bKey])); + } + }); + if(diffs.length) { + rv.differences = diffs; + rv.keys = keys; + rv.values = values; + } + } return Object.keys(rv).length ? {object:rv} : null; } @@ -138,7 +157,7 @@ function isSimpleValue(classType) { } discrepances = function discrepances(a, b, opts){ - var opts = opts || {}; + var opts = changing({unordered:true}, opts||{}); if(a === b){ return null; } var typeA = getType(a); var typeB = getType(b); diff --git a/test/test.js b/test/test.js index 07142bc..8656076 100644 --- a/test/test.js +++ b/test/test.js @@ -49,13 +49,23 @@ describe("discrepances", function(){ {a: undefined , b:false , expect:{types:['undefined' , 'boolean'], values:[undefined, false] }}, {a: new Example({uno:1}) , b: new Example({uno:1}) , expect: null }, {a: new Example({uno:1}) , b: {uno:1} , expect: {classes:['Example', 'Object'] } }, - {a: new Example({uno:1}) , b: new Example({uno:2}) , expect: {"object":{"uno":discrepances(1,2)}} }, + {a: new Example({uno:1}) , b: new Example({uno:2}) , expect: {object:{"uno":discrepances(1,2)}} }, {a: {0:1, length:1} , b: {0:1,1:2,length:2} , expect:{object:{1:{onlyRight:2}, length:discrepances(1,2)}}}, - {a: {last:'Simpson', name:'Bart'} , b:{last:'Simpson', name:'Lisa'} , expect:{"object":{"name":discrepances("Bart","Lisa")}} }, - {a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:null, opts:{unordered:true}}, - //{a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:{differences:} }, - {a: {name:'Hommer', age:40} , b:{name:'Hommer'} , expect:{"object":{"age":{"onlyLeft":40}}}}, - {a: {name:'Hommer'} , b:{name:'Hommer', age:40} , expect:{"object":{"age":{"onlyRight":40}}}}, + {a: {last:'Simpson', name:'Bart'} , b:{last:'Simpson', name:'Lisa'} , expect:{object:{"name":discrepances("Bart","Lisa")}} }, + {a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:null}, + { a:{one:'un', two:'dos'}, + b:{two:'dos', one:'un'}, + expect:{ + object:{ + differences:[0,1], + keys:[['one','two'] , ['two','one']], + values:[discrepances('un','dos'), discrepances('dos','un')] + } + }, + opts:{unordered:false} + }, + {a: {name:'Hommer', age:40} , b:{name:'Hommer'} , expect:{object:{"age":{"onlyLeft":40}}}}, + {a: {name:'Hommer'} , b:{name:'Hommer', age:40} , expect:{object:{"age":{"onlyRight":40}}}}, ]; // esto es para evitar que values:[] tenga fechas distintas a 'a' y 'b' var dateFixtures = [