New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rulesenginejs

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rulesenginejs - npm Package Compare versions

Comparing version 1.7.3 to 1.8.0

dist/deferred.min.js

2

dist/rulesengine.min.js

@@ -1,1 +0,1 @@

!function(e,t){if("function"==typeof define&&define.amd)define(t);else{var n=t();"object"!=typeof module?e[n.name]=n:module.exports=n}}(this,function(){var e;"undefined"==typeof $&&require("jsdom-no-contextify").env("",function(t,n){t?console.error(t):($=require("jquery")(n),e=$)});var t=function(t){this.facts={},this.rules=[],this.rulesMap={},this.evaluatedRules={},this.events={},this.isEvaluatingFlg=!1,void 0!==t&&(e=t)};return t.prototype.constructor=t,t.prototype.updateFacts=function(t,n){var r=e.Deferred();if("object"==typeof t)return this.evaluatedRules={},this.facts=t,this.run().always(function(){r.resolve()}),r},t.prototype.getFacts=function(e){var t=JSON.parse(JSON.stringify(this.facts));if(void 0===e||"string"!=typeof e)return t;for(var n=e.split("."),r=0;r<n.length;r++)if(void 0===(t=t[n[r]]))return;return t},t.prototype.addRule=function(t,n,r){if("string"==typeof t){if("function"!=typeof n?n=function(){return e.Deferred().resolve()}:void 0===(n(this.facts)||{}).then&&(n=function(t){return function(n){var r=e.Deferred();return t(n)?setTimeout(r.resolve,0):setTimeout(r.reject,0),r}}(n)),(Array.isArray(r)||"object"!=typeof r)&&(r={}),void 0===r.priority&&(r.priority=9),Array.isArray(r.events)||(void 0!==r.events?r.events=[r.events]:r.events=[]),void 0===this.rulesMap[t]&&this.rules.push(t),Array.isArray(r.events))for(var i=0;i<r.events.length;i++)"string"==typeof r.events[i]&&this.addEvent(r.events[i]);else"string"==typeof r.events&&this.addEvent(r.events);void 0===this.events[t]&&(this.addEvent(t),Object.defineProperty(this.events[t],"_auto_generated_",{value:!0})),r.events.push(t),this.rulesMap[t]={name:t,events:r.events,test:n,priority:r.priority,conditions:r.conditions}}},t.prototype.addRules=function(e){if(!Array.isArray(e))return!1;for(var t=0;t<e.length;t++)Array.isArray(e[t])&&this.addRule.apply(this,e[t])},t.prototype.removeRule=function(e){for(var t=0;t<this.rules.length;t++)this.rules[t]===e&&this.rules.splice(t,1);delete this.rulesMap[e],this.events[e]&&!0===this.events[e]._auto_generated_&&delete this.events[e]},t.prototype.addEvent=function(e){this.events[e]={bound:{}}},t.prototype.addEvents=function(e){if(!Array.isArray(e))return!1;for(var t=0;t<e.length;t++)Array.isArray(e[t])?this.addEvent.apply(this,e[t]):this.addEvent.call(this,e[t])},t.prototype.removeEvent=function(e){delete this.events[e]},t.prototype.emit=function(e,t){if(t)return void 0!==this.events[e].bound._evaluation_event&&(this.events[e].bound._evaluation_event(this.facts),!0);for(var n in this.events[e].bound)this.events[e].bound[n](this.facts);return!1},t.prototype.on=function(e,t,n){this.events[e].bound[t]=n},t.prototype.off=function(e,t){void 0!==t?delete this.events[e].bound[t]:this.events[e].bound={}},t.prototype.run=function(){var t=!1,n=this;n.rules.sort(function(e,t){return n.rulesMap[e].priority>n.rulesMap[t].priority?1:n.rulesMap[e].priority<n.rulesMap[t].priority?-1:0});var r=function(s){var o=e.Deferred();if(t)return o.resolve();if(void 0===s)return o.resolve();var a=[];if(void 0!==s.all)s.all.forEach(function(e){a.push(r(e))}),e.when.apply(e,a).done(function(){o.resolve()}).fail(function(){o.reject()});else if(void 0!==s.any)s.any.forEach(function(t){a.push(function(){var n=e.Deferred();return r(t).done(function(){n.reject()}).fail(function(){n.resolve()}),n}())}),e.when.apply(e,a).done(function(){o.reject()}).fail(function(){o.resolve()});else{var u=s,v=!1;"string"!=typeof u?o.reject():("!"===u.charAt(0)&&(v=!0,u=u.slice(1)),void 0!==n.rulesMap[u]&&!1===v?i(n.rulesMap[u]).done(function(){o.resolve()}).fail(function(){o.reject()}):void 0!==n.rulesMap[u]&&!0===v?i(n.rulesMap[u]).done(function(){o.reject()}).fail(function(){o.resolve()}):o.reject())}return o},i=function(i){var s=e.Deferred();return t?s.resolve():void 0===i?s.resolve():!0===n.evaluatedRules[i.name]?s.resolve():!1===n.evaluatedRules[i.name]?s.reject():void 0!==n.evaluatedRules[i.name]&&"function"==typeof n.evaluatedRules[i.name].always?(n.evaluatedRules[i.name].done(function(){s.resolve()}).fail(function(){s.reject()}),s):(n.evaluatedRules[i.name]=s,void 0!==i.conditions?r(i.conditions).done(function(){i.test(n.facts).done(function(){n.evaluatedRules[i.name]=!0;for(var e=0;e<i.events.length;e++)!0===n.emit(i.events[e],n.isEvaluatingFlg)&&(t=!0);s.resolve(),n.evaluatedRules[i.name]=!0}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),n.evaluatedRules[i.name]=!1,s.reject(),n.evaluatedRules[i.name]=!1})}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),s.reject(),n.evaluatedRules[i.name]=!1}):i.test(n.facts).done(function(){for(var e=0;e<i.events.length;e++)!0===n.emit(i.events[e],n.isEvaluatingFlg)&&(t=!0);s.resolve(),n.evaluatedRules[i.name]=!0}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),s.reject(),n.evaluatedRules[i.name]=!1}),s)};if(0===Object.keys(n.rulesMap).length)return e.Deferred().resolve();var s=e.Deferred(),o=function(e,t){t(e).always(function(){e<n.rules.length?o(e+1,t):s.resolve()})};return o(0,function(e){return i(n.rulesMap[n.rules[e]])}),s},t.prototype.evaluate=function(t,n){var r,i=e.Deferred(),s=this.facts,o=JSON.stringify(this.evaluatedRules),a=this;return void 0!==n&&(this.facts=n),this.rulesMap[t]&&!0===this.events[t]._auto_generated_&&(r=this.rulesMap[t].priority,this.rulesMap[t].priority=-1/0),this.isEvaluatingFlg=!0,this.evaluatedRules={},this.on(t,"_evaluation_event",function(e){a.off(t,"_evaluation_event"),a.facts=s,a.evaluatedRules=JSON.parse(o),void 0!==r&&(a.rulesMap[t].priority=r),a.isEvaluatingFlg=!1,i.resolve()}),this.run().always(function(){a.off(t,"_evaluation_event"),a.facts=s,a.evaluatedRules=JSON.parse(o),void 0!==r&&(a.rulesMap[t].priority=r),a.isEvaluatingFlg=!1,i.reject()}),i},t});
!function(e,t){if("function"==typeof define&&define.amd)define(t);else{var n=t();"object"!=typeof module?e[n.name]=n:module.exports=n}}(this,function(){var e;try{"undefined"==typeof $&&require("jsdom-no-contextify").env("",function(t,n){t?console.error(t):($=require("jquery")(n),e=$)})}catch(t){try{e=require("./Deferred.js")}catch(t){e=require("./deferred.min.js")}}var t=function(t){this.facts={},this.rules=[],this.rulesMap={},this.evaluatedRules={},this.events={},this.isEvaluatingFlg=!1,void 0!==t&&(e=t)};return t.prototype.constructor=t,t.prototype.updateFacts=function(t,n){var r=e.Deferred();if("object"==typeof t)return this.evaluatedRules={},this.facts=t,this.run().always(function(){r.resolve()}),r},t.prototype.getFacts=function(e){var t=JSON.parse(JSON.stringify(this.facts));if(void 0===e||"string"!=typeof e)return t;for(var n=e.split("."),r=0;r<n.length;r++)if(void 0===(t=t[n[r]]))return;return t},t.prototype.addRule=function(t,n,r){if("string"==typeof t){if("function"!=typeof n?n=function(){return e.Deferred().resolve()}:void 0===(n(this.facts)||{}).then&&(n=function(t){return function(n){var r=e.Deferred();return t(n)?setTimeout(r.resolve,0):setTimeout(r.reject,0),r}}(n)),(Array.isArray(r)||"object"!=typeof r)&&(r={}),void 0===r.priority&&(r.priority=9),Array.isArray(r.events)||(void 0!==r.events?r.events=[r.events]:r.events=[]),void 0===this.rulesMap[t]&&this.rules.push(t),Array.isArray(r.events))for(var i=0;i<r.events.length;i++)"string"==typeof r.events[i]&&this.addEvent(r.events[i]);else"string"==typeof r.events&&this.addEvent(r.events);void 0===this.events[t]&&(this.addEvent(t),Object.defineProperty(this.events[t],"_auto_generated_",{value:!0})),r.events.push(t),this.rulesMap[t]={name:t,events:r.events,test:n,priority:r.priority,conditions:r.conditions}}},t.prototype.addRules=function(e){if(!Array.isArray(e))return!1;for(var t=0;t<e.length;t++)Array.isArray(e[t])&&this.addRule.apply(this,e[t])},t.prototype.removeRule=function(e){for(var t=0;t<this.rules.length;t++)this.rules[t]===e&&this.rules.splice(t,1);delete this.rulesMap[e],this.events[e]&&!0===this.events[e]._auto_generated_&&delete this.events[e]},t.prototype.addEvent=function(e){this.events[e]={bound:{}}},t.prototype.addEvents=function(e){if(!Array.isArray(e))return!1;for(var t=0;t<e.length;t++)Array.isArray(e[t])?this.addEvent.apply(this,e[t]):this.addEvent.call(this,e[t])},t.prototype.removeEvent=function(e){delete this.events[e]},t.prototype.emit=function(e,t){if(t)return void 0!==this.events[e].bound._evaluation_event&&(this.events[e].bound._evaluation_event(this.facts),!0);for(var n in this.events[e].bound)this.events[e].bound[n](this.facts);return!1},t.prototype.on=function(e,t,n){this.events[e].bound[t]=n},t.prototype.off=function(e,t){void 0!==t?delete this.events[e].bound[t]:this.events[e].bound={}},t.prototype.run=function(){var t=!1,n=this;n.rules.sort(function(e,t){return n.rulesMap[e].priority>n.rulesMap[t].priority?1:n.rulesMap[e].priority<n.rulesMap[t].priority?-1:0});var r=function(s){var o=e.Deferred();if(t)return o.resolve();if(void 0===s)return o.resolve();var a=[];if(void 0!==s.all)s.all.forEach(function(e){a.push(r(e))}),e.when.apply(e,a).done(function(){o.resolve()}).fail(function(){o.reject()});else if(void 0!==s.any)s.any.forEach(function(t){a.push(function(){var n=e.Deferred();return r(t).done(function(){n.reject()}).fail(function(){n.resolve()}),n}())}),e.when.apply(e,a).done(function(){o.reject()}).fail(function(){o.resolve()});else{var u=s,v=!1;"string"!=typeof u?o.reject():("!"===u.charAt(0)&&(v=!0,u=u.slice(1)),void 0!==n.rulesMap[u]&&!1===v?i(n.rulesMap[u]).done(function(){o.resolve()}).fail(function(){o.reject()}):void 0!==n.rulesMap[u]&&!0===v?i(n.rulesMap[u]).done(function(){o.reject()}).fail(function(){o.resolve()}):o.reject())}return o},i=function(i){var s=e.Deferred();return t?s.resolve():void 0===i?s.resolve():!0===n.evaluatedRules[i.name]?s.resolve():!1===n.evaluatedRules[i.name]?s.reject():void 0!==n.evaluatedRules[i.name]&&"function"==typeof n.evaluatedRules[i.name].always?(n.evaluatedRules[i.name].done(function(){s.resolve()}).fail(function(){s.reject()}),s):(n.evaluatedRules[i.name]=s,void 0!==i.conditions?r(i.conditions).done(function(){i.test(n.facts).done(function(){n.evaluatedRules[i.name]=!0;for(var e=0;e<i.events.length;e++)!0===n.emit(i.events[e],n.isEvaluatingFlg)&&(t=!0);s.resolve(),n.evaluatedRules[i.name]=!0}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),n.evaluatedRules[i.name]=!1,s.reject(),n.evaluatedRules[i.name]=!1})}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),s.reject(),n.evaluatedRules[i.name]=!1}):i.test(n.facts).done(function(){for(var e=0;e<i.events.length;e++)!0===n.emit(i.events[e],n.isEvaluatingFlg)&&(t=!0);s.resolve(),n.evaluatedRules[i.name]=!0}).fail(function(){void 0!==((n.events[i.name]||{}).bound||{})._evaluation_event&&(t=!0),s.reject(),n.evaluatedRules[i.name]=!1}),s)};if(0===Object.keys(n.rulesMap).length)return e.Deferred().resolve();var s=e.Deferred(),o=function(e,t){t(e).always(function(){e<n.rules.length?o(e+1,t):s.resolve()})};return o(0,function(e){return i(n.rulesMap[n.rules[e]])}),s},t.prototype.evaluate=function(t,n){var r,i=e.Deferred(),s=this.facts,o=JSON.stringify(this.evaluatedRules),a=this;return void 0!==n&&(this.facts=n),this.rulesMap[t]&&!0===this.events[t]._auto_generated_&&(r=this.rulesMap[t].priority,this.rulesMap[t].priority=-1/0),this.isEvaluatingFlg=!0,this.evaluatedRules={},this.on(t,"_evaluation_event",function(e){a.off(t,"_evaluation_event"),a.facts=s,a.evaluatedRules=JSON.parse(o),void 0!==r&&(a.rulesMap[t].priority=r),a.isEvaluatingFlg=!1,i.resolve()}),this.run().always(function(){a.off(t,"_evaluation_event"),a.facts=s,a.evaluatedRules=JSON.parse(o),void 0!==r&&(a.rulesMap[t].priority=r),a.isEvaluatingFlg=!1,i.reject()}),i},t});
{
"name": "rulesenginejs",
"version": "1.7.3",
"version": "1.8.0",
"description": "A simple front-end rules engine",

@@ -10,3 +10,4 @@ "main": "dist/rulesengine.min.js",

"debug": "./node_modules/mocha/bin/mocha --inspect-brk test.js",
"push": "npm run build && npm publish && git push origin master"
"push": "npm run build && npm publish && git push origin master",
"build_deferred": "./node_modules/minify/bin/minify.js ./src/Deferred.js > ./dist/deferred.min.js"
},

@@ -13,0 +14,0 @@ "author": "tetratorus",

@@ -16,13 +16,23 @@ (function(scope, factory) {

var jQuery;
if (typeof $ === 'undefined') {
var jsdom = require('jsdom-no-contextify');
jsdom.env('', function(err, window) {
if (err) {
console.error(err);
return;
}
$ = require('jquery')(window);
jQuery = $;
});
try {
if (typeof $ === 'undefined') {
debugger;
var jsdom = require('jsdom-no-contextify');
jsdom.env('', function(err, window) {
if (err) {
console.error(err);
return;
}
$ = require('jquery')(window);
jQuery = $;
});
}
} catch (e) {
try {
jQuery = require('./Deferred.js');
} catch (e) {
jQuery = require('./deferred.min.js');
}
}
//***************************************************************//

@@ -320,3 +330,3 @@ // Rules Engine //

context.evaluatedRules[rule.name] = deferred;
// check if conditions are satisfied first
// check conditions
if (rule.conditions !== undefined) {

@@ -323,0 +333,0 @@ evaluateConditions(rule.conditions)

@@ -5,13 +5,18 @@ var chai = require('chai');

var RulesEngine = require('./src/RulesEngine.js');
var jsdom = require('jsdom-no-contextify');
var $;
jsdom.env('', function(err, window) {
if (err) {
console.error(err);
return;
}
$ = require('jquery')(window);
});
var jsdom, $;
try {
jsdom = require('jsdom-no-contextify');
jsdom.env('', function(err, window) {
if (err) {
console.error(err);
return;
}
$ = require('jquery')(window);
});
} catch (e) {
$ = 'MODULE NOT FOUND';
}
/** Tests */

@@ -294,3 +299,3 @@ describe('RulesEngine', function() {

return r.run();
}).always(function() {
}).done(function() {
assert.isAtMost(count, 3);

@@ -312,3 +317,2 @@ done();

it('should prioritize and exit early during individual rule/event evaluation', function(done) {
debugger;
var r = new RulesEngine();

@@ -329,3 +333,3 @@ r.addRules([

done();
})
});
});

@@ -383,2 +387,3 @@ });

it('should accept a promise library in place of jQuery', function(done) {
if ($ === 'MODULE_NOT_FOUND') return this.skip();
var temp = $.Deferred;

@@ -389,3 +394,3 @@ var flag = false;

return temp.apply($, arguments);
}
};
var r = new RulesEngine($);

@@ -421,2 +426,75 @@ r.addEvents(['testEvent', 'event1', 'event2', 'event3']);

});
it('should be correctly evaluate async rules', function(done) {
if ($ === 'MODULE_NOT_FOUND') return this.skip();
var r = new RulesEngine();
r.addRule('testRule', function(facts) {
var deferred = $.Deferred();
setTimeout(function() {
if (facts.testFact) {
deferred.resolve();
} else {
deferred.reject();
}
}, 2000);
return deferred;
});
var flag = false;
r.on('testRule', 'handler', function() {
flag = true;
done();
});
r.updateFacts({testFact: true}).done(function() {
if (!flag) assert.isOk(false);
});
});
it('should resolve all evaluated rules to boolean after run is complete', function(done) {
if ($ === 'MODULE_NOT_FOUND') return this.skip();
var r = new RulesEngine();
r.addRule('rule1', function(facts) {
var deferred = $.Deferred();
setTimeout(function() {
if (facts.testFact) {
deferred.resolve();
} else {
deferred.reject();
}
}, 2000);
return deferred;
});
r.addRule('testRule', function(facts) { return facts.testFact; }, {
events: 'testEvent',
conditions: {
all: [{
any: ['rule2',
{
all: ['rule3', {
any: ['rule4', 'rule5']
}, {
any: ['rule6', 'rule7']
}]
}
]
},
'rule1'
]
}
});
r.addRule('rule2', function(facts) { return false; });
r.addRule('rule3', function(facts) { return true; });
r.addRule('rule4', function(facts) { return true; });
r.addRule('rule5', function(facts) { return false; });
r.addRule('rule6', function(facts) { return true; });
r.addRule('rule7', function(facts) { return false; });
debugger;
r.evaluate('testRule', {testFact: true})
.done(function() {
for (var i = 0; i < r.evaluatedRules.length; i++) {
assert.typeOf(r.evaluatedRules[i], 'string');
}
done();
})
.fail(function() {
assert.isOk(false);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc