UNPKG

@openmrs/esm-expression-evaluator

Version:

Utilities for evaluating user-defined expressions

2 lines 32.9 kB
System.register([],(function(e,t){return{execute:function(){e((()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{compile:()=>U,evaluate:()=>S,evaluateAsBoolean:()=>I,evaluateAsBooleanAsync:()=>j,evaluateAsNumber:()=>M,evaluateAsNumberAsync:()=>N,evaluateAsType:()=>L,evaluateAsTypeAsync:()=>B,evaluateAsync:()=>R,extractVariableNames:()=>Z,jsep:()=>i});class r{static get version(){return"1.3.9"}static toString(){return"JavaScript Expression Parser (JSEP) v"+r.version}static addUnaryOp(e){return r.max_unop_len=Math.max(e.length,r.max_unop_len),r.unary_ops[e]=1,r}static addBinaryOp(e,t,n){return r.max_binop_len=Math.max(e.length,r.max_binop_len),r.binary_ops[e]=t,n?r.right_associative.add(e):r.right_associative.delete(e),r}static addIdentifierChar(e){return r.additional_identifier_chars.add(e),r}static addLiteral(e,t){return r.literals[e]=t,r}static removeUnaryOp(e){return delete r.unary_ops[e],e.length===r.max_unop_len&&(r.max_unop_len=r.getMaxKeyLen(r.unary_ops)),r}static removeAllUnaryOps(){return r.unary_ops={},r.max_unop_len=0,r}static removeIdentifierChar(e){return r.additional_identifier_chars.delete(e),r}static removeBinaryOp(e){return delete r.binary_ops[e],e.length===r.max_binop_len&&(r.max_binop_len=r.getMaxKeyLen(r.binary_ops)),r.right_associative.delete(e),r}static removeAllBinaryOps(){return r.binary_ops={},r.max_binop_len=0,r}static removeLiteral(e){return delete r.literals[e],r}static removeAllLiterals(){return r.literals={},r}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(e){return new r(e).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(e){return r.binary_ops[e]||0}static isIdentifierStart(e){return e>=65&&e<=90||e>=97&&e<=122||e>=128&&!r.binary_ops[String.fromCharCode(e)]||r.additional_identifier_chars.has(String.fromCharCode(e))}static isIdentifierPart(e){return r.isIdentifierStart(e)||r.isDecimalDigit(e)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(e,t){if(r.hooks[e]){const n={context:this,node:t};return r.hooks.run(e,n),n.node}return t}searchHook(e){if(r.hooks[e]){const t={context:this};return r.hooks[e].find((function(e){return e.call(t.context,t),t.node})),t.node}}gobbleSpaces(){let e=this.code;for(;e===r.SPACE_CODE||e===r.TAB_CODE||e===r.LF_CODE||e===r.CR_CODE;)e=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const e=this.gobbleExpressions(),t=1===e.length?e[0]:{type:r.COMPOUND,body:e};return this.runHook("after-all",t)}gobbleExpressions(e){let t,n,i=[];for(;this.index<this.expr.length;)if(t=this.code,t===r.SEMCOL_CODE||t===r.COMMA_CODE)this.index++;else if(n=this.gobbleExpression())i.push(n);else if(this.index<this.expr.length){if(t===e)break;this.throwError('Unexpected "'+this.char+'"')}return i}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let e=this.expr.substr(this.index,r.max_binop_len),t=e.length;for(;t>0;){if(r.binary_ops.hasOwnProperty(e)&&(!r.isIdentifierStart(this.code)||this.index+e.length<this.expr.length&&!r.isIdentifierPart(this.expr.charCodeAt(this.index+e.length))))return this.index+=t,e;e=e.substr(0,--t)}return!1}gobbleBinaryExpression(){let e,t,n,i,o,s,a,c,l;if(s=this.gobbleToken(),!s)return s;if(t=this.gobbleBinaryOp(),!t)return s;for(o={value:t,prec:r.binaryPrecedence(t),right_a:r.right_associative.has(t)},a=this.gobbleToken(),a||this.throwError("Expected expression after "+t),i=[s,o,a];t=this.gobbleBinaryOp();){if(n=r.binaryPrecedence(t),0===n){this.index-=t.length;break}o={value:t,prec:n,right_a:r.right_associative.has(t)},l=t;const c=e=>o.right_a&&e.right_a?n>e.prec:n<=e.prec;for(;i.length>2&&c(i[i.length-2]);)a=i.pop(),t=i.pop().value,s=i.pop(),e={type:r.BINARY_EXP,operator:t,left:s,right:a},i.push(e);e=this.gobbleToken(),e||this.throwError("Expected expression after "+l),i.push(o,e)}for(c=i.length-1,e=i[c];c>1;)e={type:r.BINARY_EXP,operator:i[c-1].value,left:i[c-2],right:e},c-=2;return e}gobbleToken(){let e,t,n,i;if(this.gobbleSpaces(),i=this.searchHook("gobble-token"),i)return this.runHook("after-token",i);if(e=this.code,r.isDecimalDigit(e)||e===r.PERIOD_CODE)return this.gobbleNumericLiteral();if(e===r.SQUOTE_CODE||e===r.DQUOTE_CODE)i=this.gobbleStringLiteral();else if(e===r.OBRACK_CODE)i=this.gobbleArray();else{for(t=this.expr.substr(this.index,r.max_unop_len),n=t.length;n>0;){if(r.unary_ops.hasOwnProperty(t)&&(!r.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!r.isIdentifierPart(this.expr.charCodeAt(this.index+t.length)))){this.index+=n;const e=this.gobbleToken();return e||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:r.UNARY_EXP,operator:t,argument:e,prefix:!0})}t=t.substr(0,--n)}r.isIdentifierStart(e)?(i=this.gobbleIdentifier(),r.literals.hasOwnProperty(i.name)?i={type:r.LITERAL,value:r.literals[i.name],raw:i.name}:i.name===r.this_str&&(i={type:r.THIS_EXP})):e===r.OPAREN_CODE&&(i=this.gobbleGroup())}return i?(i=this.gobbleTokenProperty(i),this.runHook("after-token",i)):this.runHook("after-token",!1)}gobbleTokenProperty(e){this.gobbleSpaces();let t=this.code;for(;t===r.PERIOD_CODE||t===r.OBRACK_CODE||t===r.OPAREN_CODE||t===r.QUMARK_CODE;){let n;if(t===r.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==r.PERIOD_CODE)break;n=!0,this.index+=2,this.gobbleSpaces(),t=this.code}this.index++,t===r.OBRACK_CODE?((e={type:r.MEMBER_EXP,computed:!0,object:e,property:this.gobbleExpression()}).property||this.throwError('Unexpected "'+this.char+'"'),this.gobbleSpaces(),t=this.code,t!==r.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):t===r.OPAREN_CODE?e={type:r.CALL_EXP,arguments:this.gobbleArguments(r.CPAREN_CODE),callee:e}:(t===r.PERIOD_CODE||n)&&(n&&this.index--,this.gobbleSpaces(),e={type:r.MEMBER_EXP,computed:!1,object:e,property:this.gobbleIdentifier()}),n&&(e.optional=!0),this.gobbleSpaces(),t=this.code}return e}gobbleNumericLiteral(){let e,t,n="";for(;r.isDecimalDigit(this.code);)n+=this.expr.charAt(this.index++);if(this.code===r.PERIOD_CODE)for(n+=this.expr.charAt(this.index++);r.isDecimalDigit(this.code);)n+=this.expr.charAt(this.index++);if(e=this.char,"e"===e||"E"===e){for(n+=this.expr.charAt(this.index++),e=this.char,"+"!==e&&"-"!==e||(n+=this.expr.charAt(this.index++));r.isDecimalDigit(this.code);)n+=this.expr.charAt(this.index++);r.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+n+this.char+")")}return t=this.code,r.isIdentifierStart(t)?this.throwError("Variable names cannot start with a number ("+n+this.char+")"):(t===r.PERIOD_CODE||1===n.length&&n.charCodeAt(0)===r.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:r.LITERAL,value:parseFloat(n),raw:n}}gobbleStringLiteral(){let e="";const t=this.index,n=this.expr.charAt(this.index++);let i=!1;for(;this.index<this.expr.length;){let t=this.expr.charAt(this.index++);if(t===n){i=!0;break}if("\\"===t)switch(t=this.expr.charAt(this.index++),t){case"n":e+="\n";break;case"r":e+="\r";break;case"t":e+="\t";break;case"b":e+="\b";break;case"f":e+="\f";break;case"v":e+="\v";break;default:e+=t}else e+=t}return i||this.throwError('Unclosed quote after "'+e+'"'),{type:r.LITERAL,value:e,raw:this.expr.substring(t,this.index)}}gobbleIdentifier(){let e=this.code,t=this.index;for(r.isIdentifierStart(e)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(e=this.code,r.isIdentifierPart(e));)this.index++;return{type:r.IDENTIFIER,name:this.expr.slice(t,this.index)}}gobbleArguments(e){const t=[];let n=!1,i=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let o=this.code;if(o===e){n=!0,this.index++,e===r.CPAREN_CODE&&i&&i>=t.length&&this.throwError("Unexpected token "+String.fromCharCode(e));break}if(o===r.COMMA_CODE){if(this.index++,i++,i!==t.length)if(e===r.CPAREN_CODE)this.throwError("Unexpected token ,");else if(e===r.CBRACK_CODE)for(let e=t.length;e<i;e++)t.push(null)}else if(t.length!==i&&0!==i)this.throwError("Expected comma");else{const e=this.gobbleExpression();e&&e.type!==r.COMPOUND||this.throwError("Expected comma"),t.push(e)}}return n||this.throwError("Expected "+String.fromCharCode(e)),t}gobbleGroup(){this.index++;let e=this.gobbleExpressions(r.CPAREN_CODE);if(this.code===r.CPAREN_CODE)return this.index++,1===e.length?e[0]:!!e.length&&{type:r.SEQUENCE_EXP,expressions:e};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:r.ARRAY_EXP,elements:this.gobbleArguments(r.CBRACK_CODE)}}}const n=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(r,{hooks:n,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(r),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},right_associative:new Set,additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),r.max_unop_len=r.getMaxKeyLen(r.unary_ops),r.max_binop_len=r.getMaxKeyLen(r.binary_ops);const i=e=>new r(e).parse(),o=Object.getOwnPropertyNames(class{});Object.getOwnPropertyNames(r).filter((e=>!o.includes(e)&&void 0===i[e])).forEach((e=>{i[e]=r[e]})),i.Jsep=r;var s={name:"ternary",init(e){e.hooks.add("after-expression",(function(t){if(t.node&&this.code===e.QUMARK_CODE){this.index++;const r=t.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const i=this.gobbleExpression();if(i||this.throwError("Expected expression"),t.node={type:"ConditionalExpression",test:r,consequent:n,alternate:i},r.operator&&e.binary_ops[r.operator]<=.9){let n=r;for(;n.right.operator&&e.binary_ops[n.right.operator]<=.9;)n=n.right;t.node.test=n.right,n.right=t.node,t.node=r}}else this.throwError("Expected :")}}))}};i.plugins.register(s);const a="ArrowFunctionExpression";var c={name:"arrow",init(e){function t(r){r&&(Object.values(r).forEach((e=>{e&&"object"==typeof e&&t(e)})),"=>"===r.operator&&(r.type=a,r.params=r.left?[r.left]:null,r.body=r.right,r.params&&r.params[0].type===e.SEQUENCE_EXP&&(r.params=r.params[0].expressions),delete r.left,delete r.right,delete r.operator))}e.addBinaryOp("=>",.1,!0),e.hooks.add("gobble-expression",(function(t){if(this.gobbleSpaces(),this.code===e.OPAREN_CODE){const r=this.index;if(this.index++,this.gobbleSpaces(),this.code===e.CPAREN_CODE){this.index++;const e=this.gobbleBinaryOp();if("=>"===e){const r=this.gobbleBinaryExpression();return r||this.throwError("Expected expression after "+e),void(t.node={type:a,params:null,body:r})}}this.index=r}})),e.hooks.add("after-expression",(function(e){t(e.node)}))}},l={name:"new",init(e){e.addUnaryOp("new"),e.hooks.add("after-token",(function(t){const r=t.node;if(r&&"new"===r.operator){r.argument&&[e.CALL_EXP,e.MEMBER_EXP].includes(r.argument.type)||this.throwError("Expected new function()"),t.node=r.argument;let n=t.node;for(;n.type===e.MEMBER_EXP||n.type===e.CALL_EXP&&n.callee.type===e.MEMBER_EXP;)n=n.type===e.MEMBER_EXP?n.object:n.callee.object;n.type="NewExpression"}}))}},u={name:"numbers",init(e){function t(e){return"x"===e||"X"===e?(this.index+=2,[[48,57],[65,70],[97,102]]):"b"===e||"B"===e?(this.index+=2,[[48,49]]):"o"===e||"O"===e||e>="0"&&e<="7"?(this.index+=e<="7"?1:2,[[48,55]]):null}function r(e){return"x"===e||"X"===e?16:"b"===e||"B"===e?2:8}function n(e,t){return 95===e||t.some((([t,r])=>e>=t&&e<=r))}function i(t){const r=this.index;let n="";const i=()=>{for(;e.isDecimalDigit(this.code)||95===this.code;)95===this.code?this.index++:n+=this.expr.charAt(this.index++)};i(),this.code===e.PERIOD_CODE&&(n+=this.expr.charAt(this.index++),i());let o=this.char;"e"!==o&&"E"!==o||(n+=this.expr.charAt(this.index++),o=this.char,"+"!==o&&"-"!==o||(n+=this.expr.charAt(this.index++)),i(),e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+n+this.char+")"));const s=this.code;if(e.isIdentifierStart(s))this.throwError("Variable names cannot start with a number ("+n+this.char+")");else if(s===e.PERIOD_CODE)return n.length>1&&this.throwError(`Unexpected period ${JSON.stringify({chCode:s,number:n},null,2)}`),void(this.index=r);t.node={type:e.LITERAL,value:parseFloat(n),raw:this.expr.substring(r,this.index)}}e.hooks.add("gobble-token",(function(o){if(48===this.code){const s=this.index,a=this.expr.charAt(this.index+1),c=t.call(this,a);if(!c)return;let l="";for(;n(this.code,c);)95===this.code?this.index++:l+=this.expr.charAt(this.index++);if(e.isIdentifierPart(this.code)){if(e.isDecimalDigit(this.code)&&e.isDecimalDigit(a.charCodeAt(0)))return this.index=s+1,void i.call(this,o);this.throwError("unexpected char within number")}o.node={type:e.LITERAL,value:parseInt(l,r(a)),raw:this.expr.substring(s,this.index)}}else(e.isDecimalDigit(this.code)||this.code===e.PERIOD_CODE)&&i.call(this,o)}))}},p={name:"regex",init(e){e.hooks.add("gobble-token",(function(t){if(47===this.code){const r=++this.index;let n=!1;for(;this.index<this.expr.length;){if(47===this.code&&!n){const n=this.expr.slice(r,this.index);let i,o="";for(;++this.index<this.expr.length;){const e=this.code;if(!(e>=97&&e<=122||e>=65&&e<=90||e>=48&&e<=57))break;o+=this.char}try{i=new RegExp(n,o)}catch(e){this.throwError(e.message)}return t.node={type:e.LITERAL,value:i,raw:this.expr.slice(r-1,this.index)},t.node=this.gobbleTokenProperty(t.node),t.node}this.code===e.OBRACK_CODE?n=!0:n&&this.code===e.CBRACK_CODE&&(n=!1),this.index+=92===this.code?2:1}this.throwError("Unclosed Regex")}}))}},h={name:"ternary",init(e){e.hooks.add("after-expression",(function(t){if(t.node&&this.code===e.QUMARK_CODE){this.index++;const r=t.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const i=this.gobbleExpression();if(i||this.throwError("Expected expression"),t.node={type:"ConditionalExpression",test:r,consequent:n,alternate:i},r.operator&&e.binary_ops[r.operator]<=.9){let n=r;for(;n.right.operator&&e.binary_ops[n.right.operator]<=.9;)n=n.right;t.node.test=n.right,n.right=t.node,t.node=r}}else this.throwError("Expected :")}}))}},d={name:"jsepTemplateLiteral",init(e){function t(e,t=!0){if(96===this.code){const r={type:"TemplateLiteral",quasis:[],expressions:[]};let n="",i="",o=!1;const s=this.expr.length,a=()=>r.quasis.push({type:"TemplateElement",value:{raw:i,cooked:n},tail:o});for(;this.index<s;){let s=this.expr.charAt(++this.index);if("`"===s)return this.index+=1,o=!0,a(),e.node=r,t&&(e.node=this.gobbleTokenProperty(e.node)),e.node;if("$"===s&&"{"===this.expr.charAt(this.index+1))this.index+=2,a(),i="",n="",r.expressions.push(...this.gobbleExpressions(125)),125!==this.code&&this.throwError("unclosed ${");else if("\\"===s)switch(i+=s,s=this.expr.charAt(++this.index),i+=s,s){case"n":n+="\n";break;case"r":n+="\r";break;case"t":n+="\t";break;case"b":n+="\b";break;case"f":n+="\f";break;case"v":n+="\v";break;default:n+=s}else n+=s,i+=s}this.throwError("Unclosed `")}}e.hooks.add("gobble-token",t),e.hooks.add("after-token",(function(r){if((r.node.type===e.IDENTIFIER||r.node.type===e.MEMBER_EXP)&&96===this.code)return r.node={type:"TaggedTemplateExpression",tag:r.node,quasi:t.bind(this)(r,!1)},r.node=this.gobbleTokenProperty(r.node),r.node}))}};function f(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var b,y,x={Array,Boolean,Symbol,Infinity:1/0,NaN:NaN,Math,Number,BigInt,String,RegExp,JSON,isFinite,isNaN,parseFloat,parseInt,decodeURI,encodeURI,encodeURIComponent,Object:{__proto__:void 0,assign:Object.assign.bind(null),fromEntries:Object.fromEntries.bind(null),hasOwn:Object.hasOwn.bind(null),keys:Object.keys.bind(null),is:Object.is.bind(null),values:Object.values.bind(null)}},g=(b=function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable})))),n.forEach((function(t){f(e,t,r[t])}))}return e}({},x),y=null!=(y={Promise})?y:{},Object.getOwnPropertyDescriptors?Object.defineProperties(b,Object.getOwnPropertyDescriptors(y)):function(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r.push.apply(r,n)}return r}(Object(y)).forEach((function(e){Object.defineProperty(b,e,Object.getOwnPropertyDescriptor(y,e))})),b);function E(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function m(e,t,r,n,i,o,s){try{var a=e[o](s),c=a.value}catch(e){return void r(e)}a.done?t(c):Promise.resolve(c).then(n,i)}function v(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function s(e){m(o,n,i,s,a,"next",e)}function a(e){m(o,n,i,s,a,"throw",e)}s(void 0)}))}}function O(e,t,r){return O=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var i=new(Function.bind.apply(e,n));return r&&A(i,r.prototype),i},O.apply(null,arguments)}function _(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function w(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}function C(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable})))),n.forEach((function(t){_(e,t,r[t])}))}return e}function A(e,t){return A=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},A(e,t)}function D(e){return function(e){if(Array.isArray(e))return E(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return E(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?E(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function P(e,t){var r,n,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,n=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}function S(e){return L(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},X)}function R(e){return k.apply(this,arguments)}function k(){return k=v((function(e){var t=arguments;return P(this,(function(r){return[2,B(e,t.length>1&&void 0!==t[1]?t[1]:{},X)]}))})),k.apply(this,arguments)}function I(e){return L(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},K)}function j(e){return B(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},K)}function M(e){return L(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},F)}function N(e){return B(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},F)}function L(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2?arguments[2]:void 0;if(!("string"==typeof e||"object"==typeof e&&e&&"type"in e))throw"Unknown expression type ".concat(e,". Expressions must either be a string or pre-compiled string.");null==t&&(t={});var n=function(e){return Q(e,x)}(t),o=q("string"==typeof e?i(e):e,n);if(r(o))return o;throw{type:"Invalid result",message:"string"==typeof e?"The expression ".concat(e," did not produce a valid result"):"The expression did not produce a valid result"}}function B(e){return T.apply(this,arguments)}function T(){return T=v((function(e){var t,r,n,o=arguments;return P(this,(function(s){return t=o.length>1&&void 0!==o[1]?o[1]:{},r=o.length>2?o[2]:void 0,"string"==typeof e||"object"==typeof e&&e&&"type"in e?(null==t&&(t={}),n=function(e){return Q(e,g)}(t),[2,Promise.resolve(q("string"==typeof e?i(e):e,n)).then((function(t){if(r(t))return t;throw{type:"Invalid result",message:"string"==typeof e?"The expression ".concat(e," did not produce a valid result"):"The expression did not produce a valid result"}}))]):[2,Promise.reject("Unknown expression type ".concat(e,". Expressions must either be a string or pre-compiled string."))]}))})),T.apply(this,arguments)}function U(e){return i(e)}function X(e){return"string"==typeof e||"number"==typeof e||"boolean"==typeof e||null==e||w(e,Date)}function K(e){return"boolean"==typeof e}function F(e){return"number"==typeof e}function q(e,t){switch(e.type){case"UnaryExpression":return function(e,t){var r=q(e.argument,t);switch(e.operator){case"+":return+r;case"-":return-r;case"~":return~r;case"!":return!r;default:throw"Expression evaluator does not support operator '".concat(e.operator,"''")}}(e,t);case"BinaryExpression":return function(e,t){var r=q(e.left,t),n=q(e.right,t);switch(e.operator){case"+":return r+n;case"-":return r-n;case"*":return r*n;case"/":return r/n;case"%":return r%n;case"**":return Math.pow(r,n);case"==":return r==n;case"===":return r===n;case"!=":return r!=n;case"!==":return r!==n;case">":return r>n;case">=":return r>=n;case"<":return r<n;case"<=":return r<=n;case"in":return r in n;case"&&":return r&&n;case"||":return r||n;case"??":return null!=r?r:n;default:throw"Expression evaluator does not support operator '".concat(e.operator,"' operator")}}(e,t);case"ConditionalExpression":return function(e,t){var r=q(e.test,t);return q(r?e.consequent:e.alternate,t)}(e,t);case"CallExpression":return function(e,t){var r,n=null===(r=e.arguments)||void 0===r?void 0:r.map(V(t)),i=q(e.callee,t);if(!i)throw"No function named ".concat($(e.callee)," is defined in this context");if("function"!=typeof i)throw"".concat($(e.callee)," is not a function");return i.apply(void 0,D(n))}(e,t);case"ArrowFunctionExpression":return function(e,t){var r,n,i,o=null!==(n=null===(r=e.params)||void 0===r?void 0:r.map((function(e){if("Identifier"===e.type)return e.name;throw"Cannot handle parameter of type ".concat(e.type)})))&&void 0!==n?n:[];return function(){for(var r=arguments.length,n=new Array(r),i=0;i<r;i++)n[i]=arguments[i];if(n.length<o.length)throw"Required argument(s) ".concat(o.slice(n.length,-1).join(", ")," were not provided");var s=Object.fromEntries(o.reduce((function(e,t,r){var i=n[r];return Y(i)&&e.push([t,i]),e}),[]));return q(e.body,t.addVariables(s))}.bind(null!==(i=t.thisObj)&&void 0!==i?i:null)}(e,t);case"MemberExpression":return function(e,t){var r=q(e.object,t);if(void 0===r)switch(e.object.type){case"Identifier":var n=H(e.object,t);throw ReferenceError("ReferenceError: ".concat(n," is not defined"));case"MemberExpression":var i=H(e.property,t);throw TypeError("TypeError: cannot read properties of undefined (reading '".concat(i,"')"));default:throw"VisitMemberExpression does not support operator '".concat(e.object.type,"' type")}var o,s=r;if("string"==typeof r)s=String.prototype;else if("number"==typeof r)s=Number.prototype;else if("function"==typeof r);else if("object"!=typeof r)throw"VisitMemberExpression does not support member access on type ".concat(void 0===r?"undefined":function(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}(r));switch(t.thisObj=s,e.property.type){case"Identifier":case"MemberExpression":o=q(e.property,t);break;default:var a=q(e.property,t);if(void 0===a)throw{type:"Illegal property access",message:"No property was supplied to the property access"};J(a),o=r[a]}return"function"==typeof o?o.bind(r):o}(e,t);case"ArrayExpression":return function(e,t){var r;return null===(r=e.elements)||void 0===r?void 0:r.map(V(t))}(e,t);case"SequenceExpression":return function(e,t){var r=e.expressions.map(V(t));return r[r.length-1]}(e,t);case"NewExpression":return function(e,t){if(!e.callee||"Identifier"!==e.callee.type)throw e.callee?"new must be called with either Date or RegExp":'Could not handle "new" without a specified class';var r,n=null===(r=e.arguments)||void 0===r?void 0:r.map(V(t));switch(e.callee.name){case"Date":return O(Date,D(n));case"RegExp":return O(RegExp,D(n));default:throw"Cannot instantiate object of type ".concat(e.callee.name)}}(e,t);case"Literal":return function(e,t){return J(e.value),e.value}(e);case"Identifier":return function(e,t){J(e.name);var r=t.thisObj;if(r&&("object"==typeof r||"function"==typeof r)&&e.name in r){var n=r[e.name];return J(n),n}if(t.variables&&e.name in t.variables){var i=t.variables[e.name];return J(i),i}return e.name in t.globals?t.globals[e.name]:void 0}(e,t);case"TemplateLiteral":return function(e,t){var r,n,i,o,s=null!==(i=null===(r=e.expressions)||void 0===r?void 0:r.map(V(t)))&&void 0!==i?i:[],a=null!==(o=null===(n=e.quasis)||void 0===n?void 0:n.map(V(t)))&&void 0!==o?o:[];return a.filter((function(e){return!e.tail})).map((function(e){return e.value})).join("")+s.join("")+a.filter((function(e){return e.tail})).map((function(e){return e.value})).join("")}(e,t);case"TemplateElement":return function(e,t){return{value:e.cooked,tail:e.tail}}(e);default:throw"Expression evaluator does not support expression of type '".concat(e.type,"'")}}function H(e,t){switch(e.type){case"Literal":return e.value;case"Identifier":return e.name;case"MemberExpression":return H(e.property,t);default:throw"VisitExpressionName does not support expression of type '".concat(e.type,"'")}}function Q(e,t){var r={thisObj:void 0,variables:C({},e),globals:C({},t),addVariables:function(e){return this.variables=C({},this.variables,e),this}};return r.addVariables.bind(r),r}function V(e){return function(t){return null===t?null:q(t,e)}}function J(e){if("__proto__"===e||"prototype"===e||"constructor"===e)throw{type:"Illegal property access",message:"Cannot access the ".concat(e," property of objects")}}function Y(e){if("string"==typeof e||"number"==typeof e||"boolean"==typeof e||"function"==typeof e||null===e||w(e,RegExp))return!0;if("object"==typeof e){var t=!0,r=!1,n=void 0;try{for(var i,o=Object.keys(e)[Symbol.iterator]();!(t=(i=o.next()).done);t=!0)if(!Y(e[i.value]))return!1}catch(e){r=!0,n=e}finally{try{t||null==o.return||o.return()}finally{if(r)throw n}}return!0}if(Array.isArray(e)){var s=!0,a=!1,c=void 0;try{for(var l,u=e[Symbol.iterator]();!(s=(l=u.next()).done);s=!0)if(!Y(l.value))return!1}catch(e){a=!0,c=e}finally{try{s||null==u.return||u.return()}finally{if(a)throw c}}}return!1}function $(e){return e?"MemberExpression"===e.type?null===(t=e.property)||void 0===t?void 0:t.name:e.name:null;var t}function G(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function z(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function W(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable})))),n.forEach((function(t){z(e,t,r[t])}))}return e}function Z(e){if(!("string"==typeof e||"object"==typeof e&&e&&"type"in e))throw"Unknown expression type ".concat(e,". Expressions must either be a string or pre-compiled string.");var t,r={globals:W({},g),isLocalExpression:!1,variables:new Set};return ee("string"==typeof e?i(e):e,r),function(e){if(Array.isArray(e))return G(e)}(t=r.variables)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(t)||function(e,t){if(e){if("string"==typeof e)return G(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?G(e,t):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function ee(e,t){switch(e.type){case"UnaryExpression":return function(e,t){return ee(e.argument,t)}(e,t);case"BinaryExpression":return function(e,t){return[ee(e.left,t),ee(e.right,t)].filter(Boolean)}(e,t);case"ConditionalExpression":return function(e,t){return[ee(e.consequent,t),ee(e.test,t),ee(e.alternate,t)].filter(Boolean)}(e,t);case"CallExpression":return function(e,t){var r,n=ee(e.callee,t);return null===(r=e.arguments)||void 0===r||r.map(te(t)),n}(e,t);case"ArrowFunctionExpression":return function(e,t){var r,n,i=W({},t);i.isLocalExpression=!0;var o,s=null!==(n=null===(r=e.params)||void 0===r?void 0:r.map(te(i)))&&void 0!==n?n:[],a=null!==(o=ee(e.body,i))&&void 0!==o?o:[];if(a&&Array.isArray(a)){var c=!0,l=!1,u=void 0;try{for(var p,h=a[Symbol.iterator]();!(c=(p=h.next()).done);c=!0){var d=p.value;s.includes(d)||t.variables.add(d)}}catch(e){l=!0,u=e}finally{try{c||null==h.return||h.return()}finally{if(l)throw u}}}}(e,t);case"MemberExpression":return function(e,t){ee(e.object,t);var r=W({},t);r.isLocalExpression=!0,ee(e.property,r)}(e,t);case"ArrayExpression":return function(e,t){var r;null===(r=e.elements)||void 0===r||r.map(te(t))}(e,t);case"SequenceExpression":return function(e,t){var r;null===(r=e.expressions)||void 0===r||r.map(te(t))}(e,t);case"NewExpression":return function(e,t){var r;null===(r=e.arguments)||void 0===r||r.map(te(t))}(e,t);case"Literal":case"TemplateElement":return;case"Identifier":return function(e,t){if(!(e.name in t.globals)){if(t.isLocalExpression)return e.name;t.variables.add(e.name)}}(e,t);case"TemplateLiteral":return function(e,t){var r,n;null===(r=e.expressions)||void 0===r||r.map(te(t)),null===(n=e.quasis)||void 0===n||n.map(te(t))}(e,t);default:throw"Expression evaluator does not support expression of type '".concat(e.type,"'")}}function te(e){return function(t){return null===t?null:ee(t,e)}}return i.plugins.register(c),i.plugins.register(l),i.plugins.register(u),i.plugins.register(p),i.plugins.register(h),i.plugins.register(d),i.addBinaryOp("in",7),i.addBinaryOp("??",1),t})())}}})); //# sourceMappingURL=openmrs-esm-expression-evaluator.js.map