rules-engine-lib
Version:
A Ts rules engine lib
2 lines (1 loc) • 3.47 kB
JavaScript
var{defineProperty:N,getOwnPropertyNames:Q,getOwnPropertyDescriptor:b}=Object,y=Object.prototype.hasOwnProperty;var x=new WeakMap,z=(r)=>{var f=x.get(r),O;if(f)return f;if(f=N({},"__esModule",{value:!0}),r&&typeof r==="object"||typeof r==="function")Q(r).map((w)=>!y.call(f,w)&&N(f,w,{get:()=>r[w],enumerable:!(O=b(r,w))||O.enumerable}));return x.set(r,f),f};var B=(r,f)=>{for(var O in f)N(r,O,{get:f[O],enumerable:!0,configurable:!0,set:(w)=>f[O]=()=>w})};var F={};B(F,{default:()=>G});module.exports=z(F);var C=(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 O=Array.isArray(f)?f:f.split("."),w=0,g=r;while(w<O.length){let m=O[w];if(!g||typeof g!=="object"||!Object.hasOwn(g,m))return;g=g[m],w++}return g},j=C;var D=(r)=>{try{let f=r.replace(/'/g,'"');return JSON.parse(f)}catch(f){return r}},q=D;class T{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 O of f){let w=this.conditions[O],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 O in r){let w=r[O],g=O.split("."),m=f;g.forEach((J,A)=>{if(A<g.length-1)m=m[J];else m[J]=w})}}executeIF(r,f){let O=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");O=w=="OR"?O|g:1&g}return O===1}executeLogicalOperation(r,f,O){if(Object.keys(r).length===0)return 0;let w=O==="AND"?1:0;for(let g in r){let m=r[g],J=q(g),A=j(f,J);if(typeof m==="object"&&m!==null){if(m.between)w&=this.executeBetween(m.between,A);if(m.contains)w&=this.executeContains(m.contains,A);if(m.greaterThan)w&=this.executeGreaterThan(m.greaterThan,A);if(m.lessThan)w&=this.executeLessThan(m.lessThan,A);if(m.in)w&=this.executeIncludes(m.in,A);if(m.includes)w&=this.executeIncludes(m.includes,A);if(m.matches)w&=this.executeMatches(m.matches,A);if(m.not)w&=this.executeNot(m.not,A)}else switch(this.options.caseSensitive){case!0:w=O==="AND"?w&+(m===A):w|+(m!==A);break;default:w=O==="AND"?w&+(m==A):w|+(m!=A);break}}return w}executeBetween([r,f],O){if(O==null)return 0;if(typeof O=="number"&&O>r&&O<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,O)=>{return(f.WEIGHT??0)-(O.WEIGHT??0)}).map((f)=>f.id)}}var h=T;var G=h;