Skip to content

Commit e7ef2bc

Browse files
committed
Merge branch 'release/0.2.0'
2 parents d542563 + 1a6f157 commit e7ef2bc

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ var rules = [
7171
{'page': {val: '*', should: true}}
7272
];
7373
```
74+
or
75+
```js
76+
var rules = [
77+
{'page': {val: /.+/, should: true}}
78+
];
79+
```
7480

7581
User viewed any page BUT page 3:
7682
```js

lib/rules-engine.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ function checkRule(ev, specs) {
8282
return;
8383
}
8484
if (evk === spk) { //if the key if the event is the key of the spec ('views' === 'views')
85-
if (typeof sp.val === 'object') { //if the specifications value is an array ('Contains' clause)
86-
if (sp.val.length === undefined) {
85+
//if the specifications value is an array ('Contains' clause), regexp or object ($ operators)
86+
if (typeof sp.val === 'object') {
87+
if (sp.val instanceof RegExp) {
88+
sp.matched = sp.val.test(event.val) === sp.should;
89+
} else if (sp.val.length === undefined) {
8790
sp.matched = checkComputedRule(sp.val, event.val) === sp.should;
8891
} else {
8992
sp.matched = (_.includes(sp.val, event.val) === sp.should) || (sp.should?undefined:false); //or clause get undefined if not found

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rules-engine",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"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.",
55
"main": "index.js",
66
"scripts": {

tests/simple-usage-spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@ describe('rule engine usage', function() {
2626
});
2727
});
2828

29+
it('should match a simple regexp rule', function(done) {
30+
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: /abcd/, should: true}}])
31+
.then(function(res){
32+
expect(res).toBe(true);
33+
})
34+
.then(done)
35+
.catch(done);
36+
});
37+
38+
it('should match a simple regexp rule with should:false', function(done) {
39+
rEngine.apply([{'view': { val: 'abc' }}], [{'view': {val: /abcd/, should: false}}])
40+
.then(function(res){
41+
expect(res).toBe(true);
42+
})
43+
.then(done)
44+
.catch(done);
45+
});
46+
47+
it('should reject a simple regexp rule', function(done) {
48+
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: /dcba/, should: true}}])
49+
.then(function(res){
50+
expect(res).not.toBe(true);
51+
})
52+
.then(done)
53+
.catch(done);
54+
});
55+
56+
it('should reject a regexp rule, specified as string', function(done) {
57+
rEngine.apply([{'view': { val: 'abcd' }}], [{'view': {val: '/abcd/', should: true}}])
58+
.then(function(res){
59+
expect(res).not.toBe(true);
60+
})
61+
.then(done)
62+
.catch(done);
63+
});
64+
2965
it('should match when found on OR clause', function(done) {
3066
rEngine.apply([{'view': { val: 1 }}], [{'view': {val: [0, 1, 2], should: true}}])
3167
.then(function(res){

0 commit comments

Comments
 (0)