jsonlogic-formula
Version:
JsonLogic parser to and from spreadsheet formula syntax
4 lines (3 loc) • 4.3 kB
JavaScript
;var A=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var J=(t,e)=>{for(var r in e)A(t,r,{get:e[r],enumerable:!0})},L=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let p of C(e))!x.call(t,p)&&p!==r&&A(t,p,{get:()=>e[p],enumerable:!(s=j(e,p))||s.enumerable});return t};var V=t=>L(A({},"__esModule",{value:!0}),t);var D={};J(D,{formulaToJsonlogic:()=>F,jsonlogicToFormula:()=>$});module.exports=V(D);var E=/[&;,=><+\-)\*\/]/,T=", ",O="V",R="F",S="O";function g(t,e=""){return t==null?"null":Array.isArray(t)?!e&&t.length>1?`[${t.map(r=>g(r)).join(",")}]`:t.map(r=>g(r)).join(e):typeof t=="string"?JSON.stringify(t):t.toString()}var M="@VAR";function P(t,e,r){return{$key:M,variable:t,leftSheet:e,rightSheet:r,toString(){return`${O}(${JSON.stringify(this.variable)}, ${JSON.stringify(this.leftSheet)}, ${JSON.stringify(this.rightSheet)})`}}}var d="@FN";function I(t,e){return{$key:d,name:t,args:e,toString(){return`${R}('${this.name}', ${g(this.args,T)})`}}}var b="@OP";function y(t,e,r){return{$key:b,operator:t,left:e,right:r,toString(){return`${S}('${this.operator}', ${g(this.left,T)}, ${g(this.right,T)})`}}}function B(t){let e=t.split(/("[^"]*"|'[^']*'|\#[A-Z\/0_]+[\!?]?|\[[^\]]*\]|[\w\.]+\s*\(|>=|<=|<>|[!&:;,+\-\/*()=<>%^\s{}@])/).filter(a=>!!a),r=0,s=0,p=!1,l=!1,i=!1,c=0;return f().filter(a=>a!==",");function f(a){let o=[],n;for(;n=e[r];){let k=e[r-1],w=e[r+1],h=n.length,U=p;if(p=!1,s+=h,l=n==="!"?l:k===" ",i&&c===1&&n!=="}"){c=2,o.push(f(/}/));continue}if(n[0]==='"'&&n[h-1]==='"'){r++,U?o.push(`${o.pop()}"${n.slice(1,-1)}`):o.push(n.slice(1,-1)),p=!0;continue}if(n[0]==='"'&&n[h-1]!=='"')throw new Error(`Unclosed quote (") at ${s-h+1}`);if(n[0]==="'"&&n[h-1]==="'"){r++;continue}if(a!=null&&a.test(n))return o;if(n===" "||n===`
`||n==="\r"||n===" "){r++;continue}if(n==="("){if(r++,o.push(...f()),e[r-1]!==")")throw new Error(`Unclosed parenthesis "(" at ${s}`);continue}if(i&&n===";")return c=1,r++,o;if(i&&n===","){c=2,r++;continue}if(n==="{"){if(i)throw new Error("Nested arrays are not supported");i=!0,c=1,r++,o.push(f(/}/));continue}if(n==="}"&&c===2){c=1;continue}if(n==="}"){i=!1,c=0,r++;continue}if(h>1&&n.charAt(h-1)==="("){r++;let u=I(n.slice(0,-1).trim().toUpperCase(),f().filter(N=>N!==","));if(o.push(u),e[r-1]!==")")throw new Error(`Unclosed parenthesis "(" at ${s}`);continue}if(n===")")return r++,o;if(n==="%"){r++;let u=y(n,o.pop());o.push(u);continue}if(n==="*"||n==="/"||n==="&"){r++;let u=y(n,o.pop(),f(E));o.push(u);continue}if(n==="+"||n==="-"||n==="^"){r++;let u=o[o.length-1];if(u===","||u===";"||u===""||u==null){let _=y(n,"0",f(E));o.push(_);continue}let N=y(n,o.pop(),f(/[;,=><+\-^&)]/));o.push(N);continue}if(n==="="||n==="<>"||n==="<="||n===">="||n==="<"||n===">"){r++;let u=y(n,o.pop(),f(/[\;\,=><)]/));o.push(u);continue}if(/^[0-9.][0-9.,]*$/.test(n)&&k!==":"&&w!==":"){r++,o.push(Number(n.replace(/,/g,".")));continue}if(n.toUpperCase()==="TRUE"){r++,o.push(!0);continue}if(n.toUpperCase()==="FALSE"){r++,o.push(!1);continue}if(n.toUpperCase()==="NULL"){r++,o.push(null);continue}if(/^[A-Za-z_\\][\w.]*$/.test(n)){let u=P(n);o.push(u),r++;continue}o.push(n),r++}return o}}function K(t,...e){return t==="="?{"===":e}:t==="<>"?{"!==":e}:t==="&"?{cat:e}:{[t]:e}}function W(t,...e){return t==="NOT"?e[0]["!"]?{"!!":e[0]["!"]}:{"!":e}:{[t.toLowerCase()]:e}}function z(t){return{var:t}}function F(t){return new Function(O,R,S,"return "+g(B(t)))(z,W,K)}var m={"==":"=","===":"=","!=":"<>","!==":"<>",CAT:"&"};function $(t,e=0){if(Array.isArray(t))return e>0?`{${t.map(r=>$(r,e+1)).join(";")}}`:t.flatMap(r=>$(r,e+1));if(typeof t=="object"&&t){let r=Object.keys(t);if(r.length===1){let s=r[0].toUpperCase();if(s==="VAR"){let i=t[r[0]];return typeof i=="string"?i||"root":$(i,e+1)}let p=$(t[r[0]],0),l=[].concat(p);if(/^[!=><]+$/.test(s)||s==="CAT"){let i=m[s]||s,c=l.slice(0,2),f=l.slice(1,3);return f.length>1?`AND(${c.join(i)},${f.join(i)})`:i==="!"?`NOT(${c})`:i==="!!"?`NOT(NOT(${c}))`:c.join(i)}if(/^[+\-*\/%]+$/.test(s)){let i=m[s]||s;return l.length===1?`-${l.join(i)}`:l.join(i)}return`${s}(${l.join(",")})`}return JSON.stringify(t)}return typeof t=="boolean"?String(t).toUpperCase():JSON.stringify(t)}
//# sourceMappingURL=index.js.map