Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
PuKoren committed Aug 23, 2016
2 parents d542563 + 1a6f157 commit e7ef2bc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ var rules = [
{'page': {val: '*', should: true}}
];
```
or
```js
var rules = [
{'page': {val: /.+/, should: true}}
];
```

User viewed any page BUT page 3:
```js
Expand Down
7 changes: 5 additions & 2 deletions lib/rules-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ function checkRule(ev, specs) {
return;
}
if (evk === spk) { //if the key if the event is the key of the spec ('views' === 'views')
if (typeof sp.val === 'object') { //if the specifications value is an array ('Contains' clause)
if (sp.val.length === undefined) {
//if the specifications value is an array ('Contains' clause), regexp or object ($ operators)
if (typeof sp.val === 'object') {
if (sp.val instanceof RegExp) {
sp.matched = sp.val.test(event.val) === sp.should;
} else if (sp.val.length === undefined) {
sp.matched = checkComputedRule(sp.val, event.val) === sp.should;
} else {
sp.matched = (_.includes(sp.val, event.val) === sp.should) || (sp.should?undefined:false); //or clause get undefined if not found
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rules-engine",
"version": "0.1.5",
"version": "0.2.0",
"description": "A node.js module to check if an event array matches some specifications. Usefull when you want to compare many rules with many events and check if the final output is true/false.",
"main": "index.js",
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions tests/simple-usage-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@ describe('rule engine usage', function() {
});
});

it('should match a simple regexp rule', function(done) {
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: /abcd/, should: true}}])
.then(function(res){
expect(res).toBe(true);
})
.then(done)
.catch(done);
});

it('should match a simple regexp rule with should:false', function(done) {
rEngine.apply([{'view': { val: 'abc' }}], [{'view': {val: /abcd/, should: false}}])
.then(function(res){
expect(res).toBe(true);
})
.then(done)
.catch(done);
});

it('should reject a simple regexp rule', function(done) {
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: /dcba/, should: true}}])
.then(function(res){
expect(res).not.toBe(true);
})
.then(done)
.catch(done);
});

it('should reject a regexp rule, specified as string', function(done) {
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: '/abcd/', should: true}}])
.then(function(res){
expect(res).not.toBe(true);
})
.then(done)
.catch(done);
});

it('should match when found on OR clause', function(done) {
rEngine.apply([{'view': { val: 1 }}], [{'view': {val: [0, 1, 2], should: true}}])
.then(function(res){
Expand Down

0 comments on commit e7ef2bc

Please sign in to comment.