UNPKG

jsonlogic-formula

Version:

JsonLogic parser to and from spreadsheet formula syntax

4 lines (3 loc) 3.84 kB
var k=/[&;,=><+\-)\*\/]/,A=", ",E="V",O="F",R="O";function h(r,e=""){return r==null?"null":Array.isArray(r)?!e&&r.length>1?`[${r.map(t=>h(t)).join(",")}]`:r.map(t=>h(t)).join(e):typeof r=="string"?JSON.stringify(r):r.toString()}var U="@VAR";function _(r,e,t){return{$key:U,variable:r,leftSheet:e,rightSheet:t,toString(){return`${E}(${JSON.stringify(this.variable)}, ${JSON.stringify(this.leftSheet)}, ${JSON.stringify(this.rightSheet)})`}}}var j="@FN";function C(r,e){return{$key:j,name:r,args:e,toString(){return`${O}('${this.name}', ${h(this.args,A)})`}}}var x="@OP";function $(r,e,t){return{$key:x,operator:r,left:e,right:t,toString(){return`${R}('${this.operator}', ${h(this.left,A)}, ${h(this.right,A)})`}}}function J(r){let e=r.split(/("[^"]*"|'[^']*'|\#[A-Z\/0_]+[\!?]?|\[[^\]]*\]|[\w\.]+\s*\(|>=|<=|<>|[!&:;,+\-\/*()=<>%^\s{}@])/).filter(l=>!!l),t=0,s=0,g=!1,p=!1,i=!1,c=0;return f().filter(l=>l!==",");function f(l){let o=[],n;for(;n=e[t];){let T=e[t-1],F=e[t+1],a=n.length,m=g;if(g=!1,s+=a,p=n==="!"?p:T===" ",i&&c===1&&n!=="}"){c=2,o.push(f(/}/));continue}if(n[0]==='"'&&n[a-1]==='"'){t++,m?o.push(`${o.pop()}"${n.slice(1,-1)}`):o.push(n.slice(1,-1)),g=!0;continue}if(n[0]==='"'&&n[a-1]!=='"')throw new Error(`Unclosed quote (") at ${s-a+1}`);if(n[0]==="'"&&n[a-1]==="'"){t++;continue}if(l!=null&&l.test(n))return o;if(n===" "||n===` `||n==="\r"||n===" "){t++;continue}if(n==="("){if(t++,o.push(...f()),e[t-1]!==")")throw new Error(`Unclosed parenthesis "(" at ${s}`);continue}if(i&&n===";")return c=1,t++,o;if(i&&n===","){c=2,t++;continue}if(n==="{"){if(i)throw new Error("Nested arrays are not supported");i=!0,c=1,t++,o.push(f(/}/));continue}if(n==="}"&&c===2){c=1;continue}if(n==="}"){i=!1,c=0,t++;continue}if(a>1&&n.charAt(a-1)==="("){t++;let u=C(n.slice(0,-1).trim().toUpperCase(),f().filter(N=>N!==","));if(o.push(u),e[t-1]!==")")throw new Error(`Unclosed parenthesis "(" at ${s}`);continue}if(n===")")return t++,o;if(n==="%"){t++;let u=$(n,o.pop());o.push(u);continue}if(n==="*"||n==="/"||n==="&"){t++;let u=$(n,o.pop(),f(k));o.push(u);continue}if(n==="+"||n==="-"||n==="^"){t++;let u=o[o.length-1];if(u===","||u===";"||u===""||u==null){let w=$(n,"0",f(k));o.push(w);continue}let N=$(n,o.pop(),f(/[;,=><+\-^&)]/));o.push(N);continue}if(n==="="||n==="<>"||n==="<="||n===">="||n==="<"||n===">"){t++;let u=$(n,o.pop(),f(/[\;\,=><)]/));o.push(u);continue}if(/^[0-9.][0-9.,]*$/.test(n)&&T!==":"&&F!==":"){t++,o.push(Number(n.replace(/,/g,".")));continue}if(n.toUpperCase()==="TRUE"){t++,o.push(!0);continue}if(n.toUpperCase()==="FALSE"){t++,o.push(!1);continue}if(n.toUpperCase()==="NULL"){t++,o.push(null);continue}if(/^[A-Za-z_\\][\w.]*$/.test(n)){let u=_(n);o.push(u),t++;continue}o.push(n),t++}return o}}function L(r,...e){return r==="="?{"===":e}:r==="<>"?{"!==":e}:r==="&"?{cat:e}:{[r]:e}}function V(r,...e){return r==="NOT"?e[0]["!"]?{"!!":e[0]["!"]}:{"!":e}:{[r.toLowerCase()]:e}}function M(r){return{var:r}}function P(r){return new Function(E,O,R,"return "+h(J(r)))(M,V,L)}var S={"==":"=","===":"=","!=":"<>","!==":"<>",CAT:"&"};function y(r,e=0){if(Array.isArray(r))return e>0?`{${r.map(t=>y(t,e+1)).join(";")}}`:r.flatMap(t=>y(t,e+1));if(typeof r=="object"&&r){let t=Object.keys(r);if(t.length===1){let s=t[0].toUpperCase();if(s==="VAR"){let i=r[t[0]];return typeof i=="string"?i||"root":y(i,e+1)}let g=y(r[t[0]],0),p=[].concat(g);if(/^[!=><]+$/.test(s)||s==="CAT"){let i=S[s]||s,c=p.slice(0,2),f=p.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=S[s]||s;return p.length===1?`-${p.join(i)}`:p.join(i)}return`${s}(${p.join(",")})`}return JSON.stringify(r)}return typeof r=="boolean"?String(r).toUpperCase():JSON.stringify(r)}export{P as formulaToJsonlogic,y as jsonlogicToFormula}; //# sourceMappingURL=index.mjs.map