Skip to content

Commit

Permalink
Comparing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 4, 2016
1 parent 5b0ec76 commit 52b034b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 15 additions & 5 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@
var discrepances = {};
/*jshint +W004 */

discrepances = function discrepances(obj, def){
var rta='';
if(def && def.typeof && typeof obj !== def.typeof){
rta+='typeof '+JSON.stringify(obj)+' !== '+JSON.stringify(def.typeof);
discrepances = function discrepances(a, b){
if(a === b){
return null;
}
return rta || null;
var typeA = typeof a;
var typeB = typeof b;
if(typeA === typeB) {
if(typeA === 'number') {
return {difference:a-b, 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
9 changes: 5 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var discrepances = require('../lib/discrepances.js');
describe("discrepances", function(){
[
{a:4 , b:4 , expected: null },
{skip:true, a:4 , b:5 , expected: {difference:-1 , values:[4, 5] }},
{a:4 , b:5 , expected: {difference:-1 , values:[4, 5] }},
{skip:true, a:"4" , b:4 , expected: {types:['string', 'number'], values:["4", 4] }},
{skip:true, a:null , b:0 , expected: {types:['null' , 'number'], values:[null, 0] }},
{skip:true, a:new Date() , b:/a/ , expected: {class:['Date' , 'RegExp'], values:[new Date(), /a/] }},
Expand All @@ -17,13 +17,14 @@ describe("discrepances", function(){
{skip:true, 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("controls discrepances via fixture: "+JSON.stringify(fixture), function(){
it("fixture: "+JSON.stringify(fixture), function(){
expect(discrepances(fixture.a, fixture.b)).to.eql(fixture.expected);
expect(JSON.stringify(discrepances(fixture.a, fixture.structure))).to.eql(JSON.stringify(fixture.expected));
expect(JSON4all.stringify(discrepances(fixture.a, fixture.structure))).to.eql(JSON4all.stringify(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));
});
});
});

0 comments on commit 52b034b

Please sign in to comment.