From 7f9d1f38721dad7ee679a6b8c3c958e9ef11104b Mon Sep 17 00:00:00 2001 From: Michael Cheng Date: Sun, 8 Mar 2026 09:43:42 +0800 Subject: [PATCH 1/2] feat: A naive replacement of both lodash methods. --- dist/index.d.ts | 2 +- dist/index.js | 28 ++++++++++++++++------------ dist/node-rules.min.js | 2 +- lib/index.ts | 11 ++++++++--- package-lock.json | 24 ------------------------ package.json | 4 ---- 6 files changed, 26 insertions(+), 45 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index c20eba5..5de8a79 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -41,4 +41,4 @@ declare class RuleEngine { prioritize(priority: number, filter?: Record): void; } -export { RuleEngine }; +export { API, Fact, Options, Rule, RuleEngine }; diff --git a/dist/index.js b/dist/index.js index 1d2ffac..b2893b1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,4 +1,4 @@ -"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __defProp = Object.defineProperty; +"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); @@ -6,8 +6,12 @@ var __publicField = (obj, key, value) => { }; // lib/index.ts -var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep); -var _lodashisequal = require('lodash.isequal'); var _lodashisequal2 = _interopRequireDefault(_lodashisequal); +var cloneDeep = (fact) => { + return JSON.parse(JSON.stringify(fact)); +}; +var isEqual = (lastSession, session) => { + return JSON.stringify(lastSession) === JSON.stringify(session); +}; var RuleEngine = class { constructor(rules, options) { __publicField(this, "rules", []); @@ -52,13 +56,13 @@ var RuleEngine = class { execute(fact, callback) { const thisHolder = this; let complete = false; - const session = _lodashclonedeep2.default.call(void 0, fact); - let lastSession = _lodashclonedeep2.default.call(void 0, fact); + const session = cloneDeep(fact); + let lastSession = cloneDeep(fact); let rules = this.activeRules; const matchPath = []; const ignoreFactChanges = this.ignoreFactChanges; function FnRuleLoop(x) { - const API = { + const API2 = { rule: () => rules[x], when: (outcome) => { if (outcome) { @@ -66,11 +70,11 @@ var RuleEngine = class { _consequence.ruleRef = rules[x].id || rules[x].name || `index_${x}`; thisHolder.nextTick(() => { matchPath.push(_consequence.ruleRef); - _consequence.call(session, API, session); + _consequence.call(session, API2, session); }); } else { thisHolder.nextTick(() => { - API.next(); + API2.next(); }); } }, @@ -80,10 +84,10 @@ var RuleEngine = class { return FnRuleLoop(0); }, next: () => { - if (!ignoreFactChanges && !_lodashisequal2.default.call(void 0, lastSession, session)) { - lastSession = _lodashclonedeep2.default.call(void 0, session); + if (!ignoreFactChanges && !isEqual(lastSession, session)) { + lastSession = cloneDeep(session); thisHolder.nextTick(() => { - API.restart(); + API2.restart(); }); } else { thisHolder.nextTick(() => { @@ -95,7 +99,7 @@ var RuleEngine = class { rules = thisHolder.activeRules; if (x < rules.length && !complete) { const _rule = rules[x].condition; - _rule.call(session, API, session); + _rule.call(session, API2, session); } else { thisHolder.nextTick(() => { session.matchPath = matchPath; diff --git a/dist/node-rules.min.js b/dist/node-rules.min.js index 5273f31..6726c53 100644 --- a/dist/node-rules.min.js +++ b/dist/node-rules.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).NodeRules=t.NodeRules||{})}(this,(function(t){"use strict";var e="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function r(){throw new Error("setTimeout has not been defined")}function n(){throw new Error("clearTimeout has not been defined")}var o=r,i=n;function u(t){if(o===setTimeout)return setTimeout(t,0);if((o===r||!o)&&setTimeout)return o=setTimeout,setTimeout(t,0);try{return o(t,0)}catch(e){try{return o.call(null,t,0)}catch(e){return o.call(this,t,0)}}}"function"==typeof e.setTimeout&&(o=setTimeout),"function"==typeof e.clearTimeout&&(i=clearTimeout);var a,c=[],s=!1,f=-1;function l(){s&&a&&(s=!1,a.length?c=a.concat(c):f=-1,c.length&&h())}function h(){if(!s){var t=u(l);s=!0;for(var e=c.length;e;){for(a=c,c=[];++f1)for(var r=1;r-1},kt.prototype.set=function(t,e){var r=this.__data__,n=Ft(r,t);return n<0?r.push([t,e]):r[n][1]=e,this},St.prototype.clear=function(){this.__data__={hash:new zt,map:new(vt||kt),string:new zt}},St.prototype.delete=function(t){return Ut(this,t).delete(t)},St.prototype.get=function(t){return Ut(this,t).get(t)},St.prototype.has=function(t){return Ut(this,t).has(t)},St.prototype.set=function(t,e){return Ut(this,t).set(t,e),this},Et.prototype.clear=function(){this.__data__=new kt},Et.prototype.delete=function(t){return this.__data__.delete(t)},Et.prototype.get=function(t){return this.__data__.get(t)},Et.prototype.has=function(t){return this.__data__.has(t)},Et.prototype.set=function(t,e){var n=this.__data__;if(n instanceof kt){var o=n.__data__;if(!vt||o.length-1&&t%1==0&&t-1&&t%1==0&&t<=o}(t.length)&&!Kt(t)}var Jt=ht||function(){return!1};function Kt(t){var e=Qt(t)?rt.call(t):"";return e==c||e==s}function Qt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function Xt(t){return Ht(t)?Rt(t):function(t){if(!Vt(t))return pt(t);var e=[];for(var r in Object(t))et.call(t,r)&&"constructor"!=r&&e.push(r);return e}(t)}t.exports=function(t){return $t(t,!0,!0)}}({get exports(){return S},set exports(t){S=t}},S);var E={};function R(t){return t&&t.__esModule?t:{default:t}}!function(t,e){var r=200,n="__lodash_hash_undefined__",o=1,i=2,u=9007199254740991,a="[object Arguments]",c="[object Array]",s="[object AsyncFunction]",f="[object Boolean]",l="[object Date]",h="[object Error]",p="[object Function]",_="[object GeneratorFunction]",v="[object Map]",y="[object Number]",d="[object Null]",b="[object Object]",g="[object Promise]",j="[object Proxy]",w="[object RegExp]",m="[object Set]",O="[object String]",A="[object Symbol]",T="[object Undefined]",x="[object WeakMap]",k="[object ArrayBuffer]",S="[object DataView]",E=/^\[object .+?Constructor\]$/,R=/^(?:0|[1-9]\d*)$/,P={};P["[object Float32Array]"]=P["[object Float64Array]"]=P["[object Int8Array]"]=P["[object Int16Array]"]=P["[object Int32Array]"]=P["[object Uint8Array]"]=P["[object Uint8ClampedArray]"]=P["[object Uint16Array]"]=P["[object Uint32Array]"]=!0,P[a]=P[c]=P[k]=P[f]=P[S]=P[l]=P[h]=P[p]=P[v]=P[y]=P[b]=P[w]=P[m]=P[O]=P[x]=!1;var F="object"==typeof z&&z&&z.Object===Object&&z,$="object"==typeof self&&self&&self.Object===Object&&self,M=F||$||Function("return this")(),I=e&&!e.nodeType&&e,L=I&&t&&!t.nodeType&&t,U=L&&L.exports===I,B=U&&F.process,C=function(){try{return B&&B.binding&&B.binding("util")}catch(t){}}(),D=C&&C.isTypedArray;function N(t,e){for(var r=-1,n=null==t?0:t.length;++rs))return!1;var l=a.get(t);if(l&&a.get(e))return l==e;var h=-1,p=!0,_=r&i?new kt:void 0;for(a.set(t,e),a.set(e,t);++h-1},xt.prototype.set=function(t,e){var r=this.__data__,n=Rt(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this},zt.prototype.clear=function(){this.size=0,this.__data__={hash:new Tt,map:new(pt||xt),string:new Tt}},zt.prototype.delete=function(t){var e=Bt(this,t).delete(t);return this.size-=e?1:0,e},zt.prototype.get=function(t){return Bt(this,t).get(t)},zt.prototype.has=function(t){return Bt(this,t).has(t)},zt.prototype.set=function(t,e){var r=Bt(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this},kt.prototype.add=kt.prototype.push=function(t){return this.__data__.set(t,n),this},kt.prototype.has=function(t){return this.__data__.has(t)},St.prototype.clear=function(){this.__data__=new xt,this.size=0},St.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},St.prototype.get=function(t){return this.__data__.get(t)},St.prototype.has=function(t){return this.__data__.has(t)},St.prototype.set=function(t,e){var n=this.__data__;if(n instanceof xt){var o=n.__data__;if(!pt||o.length-1&&t%1==0&&t-1&&t%1==0&&t<=u}function Xt(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function Yt(t){return null!=t&&"object"==typeof t}var Zt=D?function(t){return function(e){return t(e)}}(D):function(t){return Yt(t)&&Qt(t.length)&&!!P[Pt(t)]};function te(t){return null!=(e=t)&&Qt(e.length)&&!Kt(e)?Et(t):It(t);var e}t.exports=function(t,e){return $t(t,e)}}({get exports(){return E},set exports(t){E=t}},E),Object.defineProperty(k,"__esModule",{value:!0});var P=Object.defineProperty,F=(t,e,r)=>(((t,e,r)=>{e in t?P(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r})(t,"symbol"!=typeof e?e+"":e,r),r),$=R(S),M=R(E),I=k.RuleEngine=class{constructor(t,e){F(this,"rules",[]),F(this,"activeRules",[]),F(this,"ignoreFactChanges",!1),t&&this.register(t),e&&(this.ignoreFactChanges=e.ignoreFactChanges||!1)}init(){this.rules=[],this.activeRules=[]}register(t){Array.isArray(t)?this.rules.push(...t):null!==t&&"object"==typeof t&&this.rules.push(t),this.sync()}sync(){this.activeRules=this.rules.filter((t=>{if(void 0===t.on&&(t.on=!0),!0===t.on)return t})),this.activeRules.sort(((t,e)=>t.priority&&e.priority?e.priority-t.priority:0))}execute(t,e){const r=this;let n=!1;const o=$.default.call(void 0,t);let i=$.default.call(void 0,t),u=this.activeRules;const a=[],c=this.ignoreFactChanges;!function t(s){const f={rule:()=>u[s],when:t=>{if(t){const t=u[s].consequence;t.ruleRef=u[s].id||u[s].name||`index_${s}`,r.nextTick((()=>{a.push(t.ruleRef),t.call(o,f,o)}))}else r.nextTick((()=>{f.next()}))},restart:()=>t(0),stop:()=>(n=!0,t(0)),next:()=>{c||M.default.call(void 0,i,o)?r.nextTick((()=>t(s+1))):(i=$.default.call(void 0,o),r.nextTick((()=>{f.restart()})))}};if(u=r.activeRules,s{o.matchPath=a,e(o)}))}(0)}nextTick(t){(null==x?void 0:p)?null==x||p(t):setTimeout(t,0)}findRules(t){return void 0===t?this.rules:(Object.keys(t).forEach((e=>void 0===t[e]&&delete t[e])),this.rules.filter((e=>Object.keys(t).some((r=>t[r]===e[r])))))}turn(t,e){const r=this.findRules(e);for(let e=0,n=r.length;e1)for(var n=1;n(((e,t,n)=>{t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n})(e,"symbol"!=typeof t?t+"":t,n),n),_=e=>JSON.parse(JSON.stringify(e)),A=(e,t)=>JSON.stringify(e)===JSON.stringify(t),F=j.RuleEngine=class{constructor(e,t){C(this,"rules",[]),C(this,"activeRules",[]),C(this,"ignoreFactChanges",!1),e&&this.register(e),t&&(this.ignoreFactChanges=t.ignoreFactChanges||!1)}init(){this.rules=[],this.activeRules=[]}register(e){Array.isArray(e)?this.rules.push(...e):null!==e&&"object"==typeof e&&this.rules.push(e),this.sync()}sync(){this.activeRules=this.rules.filter((e=>{if(void 0===e.on&&(e.on=!0),!0===e.on)return e})),this.activeRules.sort(((e,t)=>e.priority&&t.priority?t.priority-e.priority:0))}execute(e,t){const n=this;let r=!1;const i=_(e);let o=_(e),s=this.activeRules;const u=[],l=this.ignoreFactChanges;!function e(c){const a={rule:()=>s[c],when:e=>{if(e){const e=s[c].consequence;e.ruleRef=s[c].id||s[c].name||`index_${c}`,n.nextTick((()=>{u.push(e.ruleRef),e.call(i,a,i)}))}else n.nextTick((()=>{a.next()}))},restart:()=>e(0),stop:()=>(r=!0,e(0)),next:()=>{l||A(o,i)?n.nextTick((()=>e(c+1))):(o=_(i),n.nextTick((()=>{a.restart()})))}};if(s=n.activeRules,c{i.matchPath=u,t(i)}))}(0)}nextTick(e){(null==O?void 0:d)?null==O||d(e):setTimeout(e,0)}findRules(e){return void 0===e?this.rules:(Object.keys(e).forEach((t=>void 0===e[t]&&delete e[t])),this.rules.filter((t=>Object.keys(e).some((n=>e[n]===t[n])))))}turn(e,t){const n=this.findRules(t);for(let t=0,r=n.length;t { + return JSON.parse(JSON.stringify(fact)); +} + +const isEqual = (lastSession: Fact, session: Fact): boolean => { + return JSON.stringify(lastSession) === JSON.stringify(session); +} + export {Rule, Options, Fact, API} export class RuleEngine { diff --git a/package-lock.json b/package-lock.json index adec319..1df9c52 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,10 +8,6 @@ "name": "node-rules", "version": "9.2.0", "license": "MIT", - "dependencies": { - "lodash.clonedeep": "^4.5.0", - "lodash.isequal": "^4.5.0" - }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", "@rollup/plugin-node-resolve": "^15.0.1", @@ -3613,16 +3609,6 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, - "node_modules/lodash.clonedeep": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", - "integrity": "sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==" - }, - "node_modules/lodash.isequal": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", - "integrity": "sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==" - }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -11042,16 +11028,6 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, - "lodash.clonedeep": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", - "integrity": "sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==" - }, - "lodash.isequal": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", - "integrity": "sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==" - }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", diff --git a/package.json b/package.json index 3ea0087..0c40cbe 100755 --- a/package.json +++ b/package.json @@ -6,10 +6,6 @@ "type": "git", "url": "https://github.com/mithunsatheesh/node-rules" }, - "dependencies": { - "lodash.clonedeep": "^4.5.0", - "lodash.isequal": "^4.5.0" - }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", "@rollup/plugin-node-resolve": "^15.0.1", From faad8fc88d9b57a18b29b3615a6bf1f400d849ab Mon Sep 17 00:00:00 2001 From: Michael Cheng Date: Sun, 8 Mar 2026 11:04:00 +0800 Subject: [PATCH 2/2] feat: Switch to ES-Toolkit - Change moduleResolution to node. - Fix RollUp issue by excluding the problematic module. --- dist/index.js | 15 ++++------ dist/node-rules.min.js | 2 +- lib/index.ts | 9 +----- package-lock.json | 68 +++++++++++------------------------------- package.json | 5 ++-- rollup.config.mjs | 5 +++- tsconfig.json | 3 +- 7 files changed, 34 insertions(+), 73 deletions(-) diff --git a/dist/index.js b/dist/index.js index b2893b1..52f77c3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6,12 +6,7 @@ var __publicField = (obj, key, value) => { }; // lib/index.ts -var cloneDeep = (fact) => { - return JSON.parse(JSON.stringify(fact)); -}; -var isEqual = (lastSession, session) => { - return JSON.stringify(lastSession) === JSON.stringify(session); -}; +var _compat = require('es-toolkit/compat'); var RuleEngine = class { constructor(rules, options) { __publicField(this, "rules", []); @@ -56,8 +51,8 @@ var RuleEngine = class { execute(fact, callback) { const thisHolder = this; let complete = false; - const session = cloneDeep(fact); - let lastSession = cloneDeep(fact); + const session = _compat.cloneDeep.call(void 0, fact); + let lastSession = _compat.cloneDeep.call(void 0, fact); let rules = this.activeRules; const matchPath = []; const ignoreFactChanges = this.ignoreFactChanges; @@ -84,8 +79,8 @@ var RuleEngine = class { return FnRuleLoop(0); }, next: () => { - if (!ignoreFactChanges && !isEqual(lastSession, session)) { - lastSession = cloneDeep(session); + if (!ignoreFactChanges && !_compat.isEqual.call(void 0, lastSession, session)) { + lastSession = _compat.cloneDeep.call(void 0, session); thisHolder.nextTick(() => { API2.restart(); }); diff --git a/dist/node-rules.min.js b/dist/node-rules.min.js index 6726c53..677c766 100644 --- a/dist/node-rules.min.js +++ b/dist/node-rules.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).NodeRules=e.NodeRules||{})}(this,(function(e){"use strict";var t="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function n(){throw new Error("setTimeout has not been defined")}function r(){throw new Error("clearTimeout has not been defined")}var i=n,o=r;function s(e){if(i===setTimeout)return setTimeout(e,0);if((i===n||!i)&&setTimeout)return i=setTimeout,setTimeout(e,0);try{return i(e,0)}catch(t){try{return i.call(null,e,0)}catch(t){return i.call(this,e,0)}}}"function"==typeof t.setTimeout&&(i=setTimeout),"function"==typeof t.clearTimeout&&(o=clearTimeout);var u,l=[],c=!1,a=-1;function f(){c&&u&&(c=!1,u.length?l=u.concat(l):a=-1,l.length&&h())}function h(){if(!c){var e=s(f);c=!0;for(var t=l.length;t;){for(u=l,l=[];++a1)for(var n=1;n(((e,t,n)=>{t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n})(e,"symbol"!=typeof t?t+"":t,n),n),_=e=>JSON.parse(JSON.stringify(e)),A=(e,t)=>JSON.stringify(e)===JSON.stringify(t),F=j.RuleEngine=class{constructor(e,t){C(this,"rules",[]),C(this,"activeRules",[]),C(this,"ignoreFactChanges",!1),e&&this.register(e),t&&(this.ignoreFactChanges=t.ignoreFactChanges||!1)}init(){this.rules=[],this.activeRules=[]}register(e){Array.isArray(e)?this.rules.push(...e):null!==e&&"object"==typeof e&&this.rules.push(e),this.sync()}sync(){this.activeRules=this.rules.filter((e=>{if(void 0===e.on&&(e.on=!0),!0===e.on)return e})),this.activeRules.sort(((e,t)=>e.priority&&t.priority?t.priority-e.priority:0))}execute(e,t){const n=this;let r=!1;const i=_(e);let o=_(e),s=this.activeRules;const u=[],l=this.ignoreFactChanges;!function e(c){const a={rule:()=>s[c],when:e=>{if(e){const e=s[c].consequence;e.ruleRef=s[c].id||s[c].name||`index_${c}`,n.nextTick((()=>{u.push(e.ruleRef),e.call(i,a,i)}))}else n.nextTick((()=>{a.next()}))},restart:()=>e(0),stop:()=>(r=!0,e(0)),next:()=>{l||A(o,i)?n.nextTick((()=>e(c+1))):(o=_(i),n.nextTick((()=>{a.restart()})))}};if(s=n.activeRules,c{i.matchPath=u,t(i)}))}(0)}nextTick(e){(null==O?void 0:d)?null==O||d(e):setTimeout(e,0)}findRules(e){return void 0===e?this.rules:(Object.keys(e).forEach((t=>void 0===e[t]&&delete e[t])),this.rules.filter((t=>Object.keys(e).some((n=>e[n]===t[n])))))}turn(e,t){const n=this.findRules(t);for(let t=0,r=n.length;t1)for(var i=1;i(((e,r,i)=>{r in e?Xs(e,r,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[r]=i})(e,"symbol"!=typeof r?r+"":r,i),i),ea=Qs,ra=R.RuleEngine=class{constructor(e,r){Ys(this,"rules",[]),Ys(this,"activeRules",[]),Ys(this,"ignoreFactChanges",!1),e&&this.register(e),r&&(this.ignoreFactChanges=r.ignoreFactChanges||!1)}init(){this.rules=[],this.activeRules=[]}register(e){Array.isArray(e)?this.rules.push(...e):null!==e&&"object"==typeof e&&this.rules.push(e),this.sync()}sync(){this.activeRules=this.rules.filter((e=>{if(void 0===e.on&&(e.on=!0),!0===e.on)return e})),this.activeRules.sort(((e,r)=>e.priority&&r.priority?r.priority-e.priority:0))}execute(e,r){const i=this;let t=!1;const s=ea.cloneDeep.call(void 0,e);let a=ea.cloneDeep.call(void 0,e),n=this.activeRules;const u=[],o=this.ignoreFactChanges;!function e(l){const c={rule:()=>n[l],when:e=>{if(e){const e=n[l].consequence;e.ruleRef=n[l].id||n[l].name||`index_${l}`,i.nextTick((()=>{u.push(e.ruleRef),e.call(s,c,s)}))}else i.nextTick((()=>{c.next()}))},restart:()=>e(0),stop:()=>(t=!0,e(0)),next:()=>{o||ea.isEqual.call(void 0,a,s)?i.nextTick((()=>e(l+1))):(a=ea.cloneDeep.call(void 0,s),i.nextTick((()=>{c.restart()})))}};if(n=i.activeRules,l{s.matchPath=u,r(s)}))}(0)}nextTick(e){(null==x?void 0:q)?null==x||q(e):setTimeout(e,0)}findRules(e){return void 0===e?this.rules:(Object.keys(e).forEach((r=>void 0===e[r]&&delete e[r])),this.rules.filter((r=>Object.keys(e).some((i=>e[i]===r[i])))))}turn(e,r){const i=this.findRules(r);for(let r=0,t=i.length;r { - return JSON.parse(JSON.stringify(fact)); -} - -const isEqual = (lastSession: Fact, session: Fact): boolean => { - return JSON.stringify(lastSession) === JSON.stringify(session); -} - export {Rule, Options, Fact, API} export class RuleEngine { diff --git a/package-lock.json b/package-lock.json index 1df9c52..d378c32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,13 +8,14 @@ "name": "node-rules", "version": "9.2.0", "license": "MIT", + "dependencies": { + "es-toolkit": "^1.45.1" + }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", "@rollup/plugin-node-resolve": "^15.0.1", "@rollup/plugin-terser": "^0.4.0", "@types/jest": "^27.0.0", - "@types/lodash.clonedeep": "^4.5.6", - "@types/lodash.isequal": "^4.5.5", "@types/node": "^16.3.3", "jest": "^27.0.6", "jest-cli": "^27.0.6", @@ -1218,30 +1219,6 @@ "pretty-format": "^27.0.0" } }, - "node_modules/@types/lodash": { - "version": "4.14.191", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz", - "integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==", - "dev": true - }, - "node_modules/@types/lodash.clonedeep": { - "version": "4.5.7", - "resolved": "https://registry.npmjs.org/@types/lodash.clonedeep/-/lodash.clonedeep-4.5.7.tgz", - "integrity": "sha512-ccNqkPptFIXrpVqUECi60/DFxjNKsfoQxSQsgcBJCX/fuX1wgyQieojkcWH/KpE3xzLoWN/2k+ZeGqIN3paSvw==", - "dev": true, - "dependencies": { - "@types/lodash": "*" - } - }, - "node_modules/@types/lodash.isequal": { - "version": "4.5.6", - "resolved": "https://registry.npmjs.org/@types/lodash.isequal/-/lodash.isequal-4.5.6.tgz", - "integrity": "sha512-Ww4UGSe3DmtvLLJm2F16hDwEQSv7U0Rr8SujLUA2wHI2D2dm8kPu6Et+/y303LfjTIwSBKXB/YTUcAKpem/XEg==", - "dev": true, - "dependencies": { - "@types/lodash": "*" - } - }, "node_modules/@types/node": { "version": "16.18.14", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.14.tgz", @@ -2017,6 +1994,16 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/es-toolkit": { + "version": "1.45.1", + "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.45.1.tgz", + "integrity": "sha512-/jhoOj/Fx+A+IIyDNOvO3TItGmlMKhtX8ISAHKE90c4b/k1tqaqEZ+uUqfpU8DMnW5cgNJv606zS55jGvza0Xw==", + "license": "MIT", + "workspaces": [ + "docs", + "benchmarks" + ] + }, "node_modules/esbuild": { "version": "0.12.29", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.12.29.tgz", @@ -9203,30 +9190,6 @@ "pretty-format": "^27.0.0" } }, - "@types/lodash": { - "version": "4.14.191", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz", - "integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==", - "dev": true - }, - "@types/lodash.clonedeep": { - "version": "4.5.7", - "resolved": "https://registry.npmjs.org/@types/lodash.clonedeep/-/lodash.clonedeep-4.5.7.tgz", - "integrity": "sha512-ccNqkPptFIXrpVqUECi60/DFxjNKsfoQxSQsgcBJCX/fuX1wgyQieojkcWH/KpE3xzLoWN/2k+ZeGqIN3paSvw==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, - "@types/lodash.isequal": { - "version": "4.5.6", - "resolved": "https://registry.npmjs.org/@types/lodash.isequal/-/lodash.isequal-4.5.6.tgz", - "integrity": "sha512-Ww4UGSe3DmtvLLJm2F16hDwEQSv7U0Rr8SujLUA2wHI2D2dm8kPu6Et+/y303LfjTIwSBKXB/YTUcAKpem/XEg==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, "@types/node": { "version": "16.18.14", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.14.tgz", @@ -9815,6 +9778,11 @@ "is-arrayish": "^0.2.1" } }, + "es-toolkit": { + "version": "1.45.1", + "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.45.1.tgz", + "integrity": "sha512-/jhoOj/Fx+A+IIyDNOvO3TItGmlMKhtX8ISAHKE90c4b/k1tqaqEZ+uUqfpU8DMnW5cgNJv606zS55jGvza0Xw==" + }, "esbuild": { "version": "0.12.29", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.12.29.tgz", diff --git a/package.json b/package.json index 0c40cbe..af0dade 100755 --- a/package.json +++ b/package.json @@ -6,13 +6,14 @@ "type": "git", "url": "https://github.com/mithunsatheesh/node-rules" }, + "dependencies": { + "es-toolkit": "^1.45.1" + }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", "@rollup/plugin-node-resolve": "^15.0.1", "@rollup/plugin-terser": "^0.4.0", "@types/jest": "^27.0.0", - "@types/lodash.clonedeep": "^4.5.6", - "@types/lodash.isequal": "^4.5.5", "@types/node": "^16.3.3", "jest": "^27.0.6", "jest-cli": "^27.0.6", diff --git a/rollup.config.mjs b/rollup.config.mjs index a6e771f..0599076 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -6,7 +6,9 @@ import terser from "@rollup/plugin-terser"; export default [ { input: "dist/index.js", - plugins: [nodeResolve(), commonjs(), globals(), terser()], + plugins: [nodeResolve(), commonjs({ + exclude: ['node_modules/es-toolkit/dist/compat/predicate/isNative.js', 'node_modules/es-toolkit/**'], + }), globals(), terser()], output: { name: "NodeRules", format: "umd", @@ -16,3 +18,4 @@ export default [ }, }, ]; + diff --git a/tsconfig.json b/tsconfig.json index 5f2c939..f3f70de 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ ], "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "moduleResolution": "node" } } \ No newline at end of file