Skip to content

Commit

Permalink
Date/RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 4, 2016
1 parent 789479b commit e9963c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ function getType(variable) {
return typeof variable;
}

function getClass(variable) {
if(variable instanceof Date) { return 'Date'; }
if(variable instanceof RegExp) { return 'RegExp'; }
if(variable instanceof Array) { return 'Array'; }
if(variable instanceof Object) { return 'Object'; }
throw new Error('undetected class type');
}
discrepances = function discrepances(a, b){
if(a === b){
return null;
}
var typeA = getType(a);
var typeB = getType(b);
//console.log("typeA", typeA, "typeB", typeB)
if(typeA === typeB) {
if(typeA==='object' || typeB==='object') {
var classA = getClass(a);
var classB = getClass(b);
if(classA !== classB) {
return {classes:[classA,classB], values:[a,b]};
}
}
if(typeA === 'number') {
return {difference:a-b, values:[a,b]};
}
Expand Down
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ var JSON4all = require('json4all');
var expect = require('expect.js');
var discrepances = require('../lib/discrepances.js');

var fechaActual = new Date();

describe("discrepances", function(){
[
{a:4 , b:4 , expected: null },
{a:4 , b:5 , expected: {difference:-1 , values:[4, 5] }},
{a:"4" , b:4 , expected: {types:['string', 'number'], values:["4", 4] }},
{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/] }},
{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)} }},
{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}}} }},
Expand Down

0 comments on commit e9963c5

Please sign in to comment.