From c5a4d27d6adbe43748955f817580e79978b49c0a Mon Sep 17 00:00:00 2001 From: Jonathan Muller Date: Tue, 23 Aug 2016 12:12:53 +0200 Subject: [PATCH 1/2] Handle RegExp as a valid matching value --- README.md | 6 ++++++ lib/rules-engine.js | 7 +++++-- tests/simple-usage-spec.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ffdab1..6b5b598 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rules-engine.js b/lib/rules-engine.js index f927596..6bfd863 100644 --- a/lib/rules-engine.js +++ b/lib/rules-engine.js @@ -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 diff --git a/tests/simple-usage-spec.js b/tests/simple-usage-spec.js index b8f4a89..5fddb5d 100644 --- a/tests/simple-usage-spec.js +++ b/tests/simple-usage-spec.js @@ -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){ From 1a6f157532c93447624fce5cf781812334c42d00 Mon Sep 17 00:00:00 2001 From: Jonathan Muller Date: Tue, 23 Aug 2016 15:27:06 +0200 Subject: [PATCH 2/2] Bump version to 0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eadaf3e..3452356 100644 --- a/package.json +++ b/package.json @@ -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": {