Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from al66/checkcond_date
Browse files Browse the repository at this point in the history
added date checks
  • Loading branch information
al66 committed May 17, 2017
2 parents cbaf0a0 + b6b93d1 commit 6bbe53f
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 12 deletions.
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

[![NPM](https://nodei.co/npm/checkcond.png)](https://nodei.co/npm/checkcond/)

check value against condition string
Checks digit, date or string value against condition string.

For usage stand alone or e.g. in decision tables -> [DMN Notation](http://www.omg.org/spec/DMN/1.1/PDF/).

## Usage
```js
Expand Down Expand Up @@ -87,21 +89,65 @@ let result = checkDirect(x, condString); // => true
x=12 => false
x=13 => true
```
### Check dates
`'< 01/01/2017'`.
```
x='10/12/2015' => true
x='05/03/2017' => false
```
`'> 03.11.2017'`.
```
x='12/01/2017' => true
x='10/04/2017' => false
```
`'[01/01/2017..01/01/2018]'`.
```
x='10/12/2015' => false
x='01/01/2017' => true
x='05/06/2017' => true
x='01/01/2018' => true
x='01/01/2019' => false
x='05/03/2019' => false
```
`'not([01/01/2017..01/01/2018])'`.
```
x='10/12/2015' => true
x='01/01/2017' => false
x='05/06/2017' => false
x='01/01/2018' => false
x='01/01/2019' => true
x='05/03/2019' => true
```

### Check strings against arrays
`"'AL','FGH','NO'"`
```
x='AL' => true
x='AF' => false
```
`"AL,FGH,NO"` works also
```
x='AL' => true
x='AF' => false
```
`"not('AL','FGH','NO')"`
```
x='AL' => false
x='AF' => true
```
`"not(AL,FGH,NO)"`
```
x='AL' => false
x='AF' => true
```
`"!('AL','FGH','NO')"`
```
x='AL' => false
x='AF' => true
```


### Other interesting implementation I found
[js-feel](https://www.npmjs.com/package/js-feel).


35 changes: 28 additions & 7 deletions lib/cond.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ var evalCond = function( tokens, cond ) {
next = true;
});
if (next) return;
// interval with dates
token.val.replace(/^,? *\[(\d{2}[./-]\d{2}[./-]\d{4})..(\d{2}[./-]\d{2}[./-]\d{4})\](.*)/g,
function( match, $low, $high, $rest, offset, string ){
if ($rest && $rest.length > 0) tokens.push({"sign":token.sign, "val":$rest});
if ($low.length > 0 && $high.length > 0 ) {
cond.push({"sign":token.sign, "method":"BT", "low": Date.parse($low), "high": Date.parse($high), "date": true });
};
next = true;
});
// date at start
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 ) {
cond.push({"sign":token.sign, "method":token.method, "low": Date.parse($low) , "date": true });
};
next = true;
});
if (next) return;
// interval with digits
token.val.replace(/^,? *\[(-?\d+(?:,\d+)*(?:\.\d+(?:e\d+)?)?)..(-?\d+(?:,\d+)*(?:\.\d+(?:e\d+)?)?)\](.*)/g,
function( match, $low, $high, $rest, offset, string ){
Expand Down Expand Up @@ -77,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;
};

Expand All @@ -96,19 +115,21 @@ var check = function(data, cond) {

var compare = function(single) {
any = true;
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;
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "checkcond",
"version": "0.0.3",
"version": "0.0.4",
"description": "check value against condition string",
"main": "index.js",
"scripts": {
Expand All @@ -14,7 +14,9 @@
"keywords": [
"condition",
"eval",
"check"
"check",
"DMN",
"decision"
],
"author": "Andreas Leinen <andreas.leinen@imicros.de> (www.imicros.de)",
"license": "MIT",
Expand Down
89 changes: 87 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,82 @@ 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]
]
],
['[01.01.2017..01.01.2018]', [
[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.01.2018')), true],
[new Date(Date.parse('01.01.2019')), false],
[new Date(Date.parse('05.03.2019')), false]
]
],
['!([01.01.2017..01.01.2018])', [
[new Date(Date.parse('12.12.2015')), true],
[new Date(Date.parse('01.01.2017')), false],
[new Date(Date.parse('05.06.2017')), false],
[new Date(Date.parse('01.01.2018')), false],
[new Date(Date.parse('01.01.2019')), true],
[new Date(Date.parse('05.03.2019')), true]
]
]
];
var testStrings = [
["'AL','FGH','NO'",[
['AL', true],
['AF', false]
]
],
["AL,FGH,NO",[
['AL', true],
['AF', false]
]
],
["not('AL','FGH','NO')",[
['AL', false],
['AF', true]
]
],
["not(AL,FGH,NO)",[
['AL', false],
['AF', true]
]
],
["not ('AL','FGH','NO')",[
['AL', false],
['AF', true]
Expand All @@ -102,7 +167,6 @@ var testStrings = [
]
];


/*
* Test Digits
*/
Expand All @@ -125,7 +189,28 @@ describe('Check Digits', function() {
});
}
});

/*
* Test Dates
*/
describe('Check Dates', function() {
for(let i = 0; i < testDates.length; i++) {
let t = testDates[i];
let condString = t[0];
describe(condString, function() {
let values = t[1];
for (let n = 0; n < values.length; n++) {
let v = values[n];
let val = v[0];
let expected = v[1];
it('X='+val+' => '+expected, function(done){
let cond = compiler(condString);
expect(check(val, cond)).to.be.equals(expected);
done();
});
}
});
}
});
/*
* Test Strings
*/
Expand Down

0 comments on commit 6bbe53f

Please sign in to comment.