UNPKG

rules-engine-lib

Version:

A Ts rules engine lib

2 lines (1 loc) 2.98 kB
var T=(r,f)=>{if(typeof f!=="string"&&!Array.isArray(f))return;if(typeof f==="string"&&f.indexOf(".")===-1&&Object.hasOwn(r,f))return r[f];let m=Array.isArray(f)?f:f.split("."),w=0,g=r;while(w<m.length){let O=m[w];if(!g||typeof g!=="object"||!Object.hasOwn(g,O))return;g=g[O],w++}return g},N=T;var h=(r)=>{try{let f=r.replace(/'/g,'"');return JSON.parse(f)}catch(f){return r}},x=h;class j{conditions;options;constructor(r,f){if(r==null)throw new Error("Conditions can't be null or undefined");this.conditions=r,this.options=f??{caseSensitive:!1,modifyDataset:!1}}run(r){if(r==null)return;if(typeof r!="object")return;let f=this.sortByWeights();for(let m of f){let w=this.conditions[m],g=!1;if(w.IF)g=this.executeIF(w.IF,r);if(!this.options.modifyDataset)return g?w.THEN:w.OTHERWISE;g?this.modifyData(w.THEN,r):this.modifyData(w.OTHERWISE??{},r)}}modifyData(r,f){for(let m in r){let w=r[m],g=m.split("."),O=f;g.forEach((J,A)=>{if(A<g.length-1)O=O[J];else O[J]=w})}}executeIF(r,f){let m=0;for(let w in r)if(r!=null){let g=this.executeLogicalOperation(!["OR","AND"].includes(w)?r:r[w],f,w=="OR"?"OR":"AND");m=w=="OR"?m|g:1&g}return m===1}executeLogicalOperation(r,f,m){if(Object.keys(r).length===0)return 0;let w=m==="AND"?1:0;for(let g in r){let O=r[g],J=x(g),A=N(f,J);if(typeof O==="object"&&O!==null){if(O.between)w&=this.executeBetween(O.between,A);if(O.contains)w&=this.executeContains(O.contains,A);if(O.greaterThan)w&=this.executeGreaterThan(O.greaterThan,A);if(O.lessThan)w&=this.executeLessThan(O.lessThan,A);if(O.in)w&=this.executeIncludes(O.in,A);if(O.includes)w&=this.executeIncludes(O.includes,A);if(O.matches)w&=this.executeMatches(O.matches,A);if(O.not)w&=this.executeNot(O.not,A)}else switch(this.options.caseSensitive){case!0:w=m==="AND"?w&+(O===A):w|+(O!==A);break;default:w=m==="AND"?w&+(O==A):w|+(O!=A);break}}return w}executeBetween([r,f],m){if(m==null)return 0;if(typeof m=="number"&&m>r&&m<f)return 1;return 0}executeContains(r,f){if(f==null)return 0;if(typeof f=="string"&&f.includes(r))return 1;if(Array.isArray(f)&&f.includes(r))return 1;return 0}executeIncludes(r,f){if(f==null)return 0;if(typeof f=="string"&&r.includes(f))return 1;return 0}executeGreaterThan(r,f){if(f==null)return 0;if(typeof f=="number"&&f>r)return 1;return 0}executeLessThan(r,f){if(f==null)return 0;if(typeof f=="number"&&f<r)return 1;return 0}executeMatches(r,f){if(f==null)return 0;if(typeof f=="string"&&r.test(f))return 1;return 0}executeNot(r,f){if(f==null)return 0;if(typeof r=="object"&&r!=null&&r.in)return this.executeIncludes(r.in,f)==0?1:0;if(typeof r=="object"&&r!=null&&r.includes)return this.executeIncludes(r.includes,f)==0?1:0;if(typeof f=="string"&&typeof r=="string"&&(this.options.caseSensitive?r!==f:r!=f))return 1;return 0}sortByWeights(){let r=[];for(let f in this.conditions)if(Object.hasOwn(this.conditions,f))r.push({...this.conditions[f],id:f});return r.sort((f,m)=>{return(f.WEIGHT??0)-(m.WEIGHT??0)}).map((f)=>f.id)}}var q=j;var F=q;export{F as default};