From b6b93d1e53a10b368e0428f721c8538317717ed4 Mon Sep 17 00:00:00 2001 From: al66 Date: Wed, 17 May 2017 21:02:01 +0200 Subject: [PATCH] correction checking dates --- lib/cond.js | 19 ++++++++++--------- test/test.js | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/cond.js b/lib/cond.js index abf3627..f5b288e 100644 --- a/lib/cond.js +++ b/lib/cond.js @@ -45,7 +45,7 @@ var evalCond = function( tokens, cond ) { next = true; }); // date at start - token.val.replace(/^,?(\d{2}[./-]\d{2}[./-]\d{4}$)(.*)/g, function( match, $low, $rest, offset, string ){ + token.val.replace(/^,?(\d{2}[./-]\d{2}[./-]\d{4})(.*)/g, function( match, $low, $rest, offset, string ){ if ($rest && $rest.length > 0) tokens.push({"sign":token.sign, "val":$rest}); if (!token.method) token.method = "EQ"; if ($low.length > 0 ) { @@ -96,7 +96,7 @@ var compiler = function( condStr) { while (tokens && tokens.length>0) { evalCond(tokens, cond); }; - //console.log('cond:', cond); + console.log('cond:', cond); return cond; }; @@ -115,20 +115,21 @@ var check = function(data, cond) { var compare = function(single) { any = true; - if (single.date) data = Date.parse(data); + let value = data; + if (single.date) value = Date.parse(data); switch (single.method) { case 'EQ': - return data == single.low; + return value == single.low; case 'GT': - return data > single.low; + return value > single.low; case 'GE': - return data >= single.low; + return value >= single.low; case 'LT': - return data < single.low; + return value < single.low; case 'LE': - return data <= single.low; + return value <= single.low; case 'BT': - return (data >= single.low && data <= single.high); + return (value >= single.low && value <= single.high); default: return false; } diff --git a/test/test.js b/test/test.js index 1cad35f..7d07caa 100644 --- a/test/test.js +++ b/test/test.js @@ -74,17 +74,37 @@ var testDigits = [ ] ] ]; + var testDates = [ ['< 01.01.2017', [ [new Date(Date.parse('12.12.2015')), true], [new Date(Date.parse('05.03.2019')), false] ] ], + ['01.01.2017', [ + [new Date(Date.parse('01.01.2017')), true], + ['01.01.2017', true] + ] + ], + ['[01.01.2017..01.01.2018],01.01.2019,[03.01.2019..01.03.2019]', [ + [new Date(Date.parse('12.12.2015')), false], + [new Date(Date.parse('01.01.2017')), true], + [new Date(Date.parse('05.06.2017')), true], + [new Date(Date.parse('01.02.2018')), false], + ['01.01.2019', true], + [new Date(Date.parse('01.01.2019')), true] + ] + ], ['> 11/03/2017', [ [new Date(Date.parse('12/01/2017')), true], [new Date(Date.parse('10/04/2017')), false] ] ], + ['>=11/03/2017', [ + [new Date(Date.parse('11/03/2017')), true], + [new Date(Date.parse('11/02/2017')), false] + ] + ], ['11/03/2017', [ [new Date(Date.parse('12/01/2017')), false], ['11/03/2017', true] @@ -147,7 +167,6 @@ var testStrings = [ ] ]; - /* * Test Digits */