@metrichor/jmespath
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -1,16 +0,2 @@ | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function t(){for(var t=0,e=0,r=arguments.length;e<r;e++)t+=arguments[e].length;var n=Array(t),i=0;for(e=0;e<r;e++)for(var s=arguments[e],o=0,a=s.length;o<a;o++,i++)n[i]=s[o];return n}var e,r,n=function(t){return null!==t&&"[object Object]"===Object.prototype.toString.call(t)},i=function(t,e){if(t===e)return!0;if(typeof t!=typeof e)return!1;if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(var r=0;r<t.length;r+=1)if(!i(t[r],e[r]))return!1;return!0}if(n(t)&&n(e)){var s=Object.entries(t),o=new Set(Object.keys(e));if(s.length!==o.size)return!1;for(var a=0,u=s;a<u.length;a++){var c=u[a],h=c[0],T=c[1];if(!i(T,e[h]))return!1;o.delete(h)}return 0===o.size}return!1},s=function(t){if(""===t||!1===t||null==t)return!0;if(Array.isArray(t)&&0===t.length)return!0;if(n(t)){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}return!1},o=function(t){return t>="0"&&t<="9"||"-"===t};!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(r||(r={}));var a,u,c=((e={"(":r.TOK_LPAREN,")":r.TOK_RPAREN,"*":r.TOK_STAR,",":r.TOK_COMMA,".":r.TOK_DOT,":":r.TOK_COLON,"@":r.TOK_CURRENT}).$=r.TOK_ROOT,e["]"]=r.TOK_RBRACKET,e["{"]=r.TOK_LBRACE,e["}"]=r.TOK_RBRACE,e),h={"!":!0,"<":!0,"=":!0,">":!0},T={"\t":!0,"\n":!0,"\r":!0," ":!0},_=new(function(){function t(){this._current=0}return t.prototype.tokenize=function(t){var e,n,i,s,a=[];for(this._current=0;this._current<t.length;)if((s=t[this._current])>="a"&&s<="z"||s>="A"&&s<="Z"||"_"===s)e=this._current,n=this.consumeUnquotedIdentifier(t),a.push({start:e,type:r.TOK_UNQUOTEDIDENTIFIER,value:n});else if(void 0!==c[t[this._current]])a.push({start:this._current,type:c[t[this._current]],value:t[this._current]}),this._current+=1;else if(o(t[this._current]))i=this.consumeNumber(t),a.push(i);else if("["===t[this._current])i=this.consumeLBracket(t),a.push(i);else if('"'===t[this._current])e=this._current,n=this.consumeQuotedIdentifier(t),a.push({start:e,type:r.TOK_QUOTEDIDENTIFIER,value:n});else if("'"===t[this._current])e=this._current,n=this.consumeRawStringLiteral(t),a.push({start:e,type:r.TOK_LITERAL,value:n});else if("`"===t[this._current]){e=this._current;var u=this.consumeLiteral(t);a.push({start:e,type:r.TOK_LITERAL,value:u})}else if(void 0!==h[t[this._current]])(i=this.consumeOperator(t))&&a.push(i);else if(void 0!==T[t[this._current]])this._current+=1;else if("&"===t[this._current])e=this._current,this._current+=1,"&"===t[this._current]?(this._current+=1,a.push({start:e,type:r.TOK_AND,value:"&&"})):a.push({start:e,type:r.TOK_EXPREF,value:"&"});else{if("|"!==t[this._current]){var _=new Error("Unknown character: "+t[this._current]);throw _.name="LexerError",_}e=this._current,this._current+=1,"|"===t[this._current]?(this._current+=1,a.push({start:e,type:r.TOK_OR,value:"||"})):a.push({start:e,type:r.TOK_PIPE,value:"|"})}return a},t.prototype.consumeUnquotedIdentifier=function(t){var e,r=this._current;for(this._current+=1;this._current<t.length&&((e=t[this._current])>="a"&&e<="z"||e>="A"&&e<="Z"||e>="0"&&e<="9"||"_"===e);)this._current+=1;return t.slice(r,this._current)},t.prototype.consumeQuotedIdentifier=function(t){var e=this._current;this._current+=1;for(var r=t.length;'"'!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&'"'!==t[n+1]?n+=1:n+=2,this._current=n}return this._current+=1,JSON.parse(t.slice(e,this._current))},t.prototype.consumeRawStringLiteral=function(t){var e=this._current;this._current+=1;for(var r=t.length;"'"!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&"'"!==t[n+1]?n+=1:n+=2,this._current=n}return this._current+=1,t.slice(e+1,this._current-1).replace("\\'","'")},t.prototype.consumeNumber=function(t){var e=this._current;this._current+=1;for(var n=t.length;o(t[this._current])&&this._current<n;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:r.TOK_NUMBER}},t.prototype.consumeLBracket=function(t){var e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:r.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:r.TOK_FLATTEN,value:"[]"}):{start:e,type:r.TOK_LBRACKET,value:"["}},t.prototype.consumeOperator=function(t){var e=this._current,n=t[e];return this._current+=1,"!"===n?"="===t[this._current]?(this._current+=1,{start:e,type:r.TOK_NE,value:"!="}):{start:e,type:r.TOK_NOT,value:"!"}:"<"===n?"="===t[this._current]?(this._current+=1,{start:e,type:r.TOK_LTE,value:"<="}):{start:e,type:r.TOK_LT,value:"<"}:">"===n?"="===t[this._current]?(this._current+=1,{start:e,type:r.TOK_GTE,value:">="}):{start:e,type:r.TOK_GT,value:">"}:"="===n&&"="===t[this._current]?(this._current+=1,{start:e,type:r.TOK_EQ,value:"=="}):void 0},t.prototype.consumeLiteral=function(t){this._current+=1;for(var e=this._current,r=t.length;"`"!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&"`"!==t[n+1]?n+=1:n+=2,this._current=n}var i=t.slice(e,this._current).trimLeft();i=i.replace("\\`","`");var s=this.looksLikeJSON(i)?JSON.parse(i):JSON.parse('"'+i+'"');return this._current+=1,s},t.prototype.looksLikeJSON=function(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0]))try{return JSON.parse(t),!0}catch(t){return!1}return!1},t}()),E=((a={})[r.TOK_EOF]=0,a[r.TOK_UNQUOTEDIDENTIFIER]=0,a[r.TOK_QUOTEDIDENTIFIER]=0,a[r.TOK_RBRACKET]=0,a[r.TOK_RPAREN]=0,a[r.TOK_COMMA]=0,a[r.TOK_RBRACE]=0,a[r.TOK_NUMBER]=0,a[r.TOK_CURRENT]=0,a[r.TOK_EXPREF]=0,a[r.TOK_ROOT]=0,a[r.TOK_PIPE]=1,a[r.TOK_OR]=2,a[r.TOK_AND]=3,a[r.TOK_EQ]=5,a[r.TOK_GT]=5,a[r.TOK_LT]=5,a[r.TOK_GTE]=5,a[r.TOK_LTE]=5,a[r.TOK_NE]=5,a[r.TOK_FLATTEN]=9,a[r.TOK_STAR]=20,a[r.TOK_FILTER]=21,a[r.TOK_DOT]=40,a[r.TOK_NOT]=45,a[r.TOK_LBRACE]=50,a[r.TOK_LBRACKET]=55,a[r.TOK_LPAREN]=60,a),l=new(function(){function e(){this.index=0,this.tokens=[]}return e.prototype.parse=function(t){this.loadTokens(t),this.index=0;var e=this.expression(0);if(this.lookahead(0)!==r.TOK_EOF){var n=this.lookaheadToken(0);this.errorToken(n,"Unexpected token type: "+n.type+", value: "+n.value)}return e},e.prototype.loadTokens=function(e){this.tokens=t(_.tokenize(e),[{type:r.TOK_EOF,value:"",start:e.length}])},e.prototype.expression=function(t){var e=this.lookaheadToken(0);this.advance();for(var r=this.nud(e),n=this.lookahead(0);t<E[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r},e.prototype.lookahead=function(t){return this.tokens[this.index+t].type},e.prototype.lookaheadToken=function(t){return this.tokens[this.index+t]},e.prototype.advance=function(){this.index+=1},e.prototype.nud=function(t){var e,n;switch(t.type){case r.TOK_LITERAL:return{type:"Literal",value:t.value};case r.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case r.TOK_QUOTEDIDENTIFIER:var i={type:"Field",name:t.value};if(this.lookahead(0)===r.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return i;case r.TOK_NOT:return{type:"NotExpression",children:[e=this.expression(E.Not)]};case r.TOK_STAR:return{type:"ValueProjection",children:[{type:"Identity"},e=this.lookahead(0)===r.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(E.Star)]};case r.TOK_FILTER:return this.led(t.type,{type:"Identity"});case r.TOK_LBRACE:return this.parseMultiselectHash();case r.TOK_FLATTEN:return{type:"Projection",children:[{type:r.TOK_FLATTEN,children:[{type:"Identity"}]},e=this.parseProjectionRHS(E.Flatten)]};case r.TOK_LBRACKET:return this.lookahead(0)===r.TOK_NUMBER||this.lookahead(0)===r.TOK_COLON?(e=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},e)):this.lookahead(0)===r.TOK_STAR&&this.lookahead(1)===r.TOK_RBRACKET?(this.advance(),this.advance(),{children:[{type:"Identity"},e=this.parseProjectionRHS(E.Star)],type:"Projection"}):this.parseMultiselectList();case r.TOK_CURRENT:return{type:r.TOK_CURRENT};case r.TOK_ROOT:return{type:r.TOK_ROOT};case r.TOK_EXPREF:return{type:"ExpressionReference",children:[n=this.expression(E.Expref)]};case r.TOK_LPAREN:for(var s=[];this.lookahead(0)!==r.TOK_RPAREN;)this.lookahead(0)===r.TOK_CURRENT?(n={type:r.TOK_CURRENT},this.advance()):n=this.expression(0),s.push(n);return this.match(r.TOK_RPAREN),s[0];default:this.errorToken(t)}},e.prototype.led=function(t,e){var n;switch(t){case r.TOK_DOT:var i=E.Dot;return this.lookahead(0)!==r.TOK_STAR?{type:"Subexpression",children:[e,n=this.parseDotRHS(i)]}:(this.advance(),{type:"ValueProjection",children:[e,n=this.parseProjectionRHS(i)]});case r.TOK_PIPE:return n=this.expression(E.Pipe),{type:r.TOK_PIPE,children:[e,n]};case r.TOK_OR:return{type:"OrExpression",children:[e,n=this.expression(E.Or)]};case r.TOK_AND:return{type:"AndExpression",children:[e,n=this.expression(E.And)]};case r.TOK_LPAREN:for(var s=e.name,o=[],a=void 0;this.lookahead(0)!==r.TOK_RPAREN;)this.lookahead(0)===r.TOK_CURRENT?(a={type:r.TOK_CURRENT},this.advance()):a=this.expression(0),this.lookahead(0)===r.TOK_COMMA&&this.match(r.TOK_COMMA),o.push(a);return this.match(r.TOK_RPAREN),{name:s,type:"Function",children:o};case r.TOK_FILTER:var u=this.expression(0);return this.match(r.TOK_RBRACKET),{type:"FilterProjection",children:[e,n=this.lookahead(0)===r.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(E.Filter),u]};case r.TOK_FLATTEN:return{type:"Projection",children:[{type:r.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(E.Flatten)]};case r.TOK_EQ:case r.TOK_NE:case r.TOK_GT:case r.TOK_GTE:case r.TOK_LT:case r.TOK_LTE:return this.parseComparator(e,t);case r.TOK_LBRACKET:var c=this.lookaheadToken(0);return c.type===r.TOK_NUMBER||c.type===r.TOK_COLON?(n=this.parseIndexExpression(),this.projectIfSlice(e,n)):(this.match(r.TOK_STAR),this.match(r.TOK_RBRACKET),{type:"Projection",children:[e,n=this.parseProjectionRHS(E.Star)]});default:return this.errorToken(this.lookaheadToken(0))}},e.prototype.match=function(t){if(this.lookahead(0)!==t){var e=this.lookaheadToken(0);this.errorToken(e,"Expected "+t+", got: "+e.type)}else this.advance()},e.prototype.errorToken=function(t,e){void 0===e&&(e="");var r=new Error(e||"Invalid token ("+t.type+'): "'+t.value+'"');throw r.name="ParserError",r},e.prototype.parseIndexExpression=function(){if(this.lookahead(0)===r.TOK_COLON||this.lookahead(1)===r.TOK_COLON)return this.parseSliceExpression();var t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(r.TOK_RBRACKET),t},e.prototype.projectIfSlice=function(t,e){var r={type:"IndexExpression",children:[t,e]};return"Slice"===e.type?{children:[r,this.parseProjectionRHS(E.Star)],type:"Projection"}:r},e.prototype.parseSliceExpression=function(){for(var t=[null,null,null],e=0,n=this.lookahead(0);n!==r.TOK_RBRACKET&&e<3;){if(n===r.TOK_COLON)e+=1,this.advance();else if(n===r.TOK_NUMBER)t[e]=this.lookaheadToken(0).value,this.advance();else{var i=this.lookaheadToken(0);this.errorToken(i,"Syntax error, unexpected token: "+i.value+"("+i.type+")")}n=this.lookahead(0)}return this.match(r.TOK_RBRACKET),{children:t,type:"Slice"}},e.prototype.parseComparator=function(t,e){return{type:"Comparator",name:e,children:[t,this.expression(E[e])]}},e.prototype.parseDotRHS=function(t){var e=this.lookahead(0);if([r.TOK_UNQUOTEDIDENTIFIER,r.TOK_QUOTEDIDENTIFIER,r.TOK_STAR].includes(e))return this.expression(t);if(e===r.TOK_LBRACKET)return this.match(r.TOK_LBRACKET),this.parseMultiselectList();if(e===r.TOK_LBRACE)return this.match(r.TOK_LBRACE),this.parseMultiselectHash();var n=this.lookaheadToken(0);this.errorToken(n,"Syntax error, unexpected token: "+n.value+"("+n.type+")")},e.prototype.parseProjectionRHS=function(t){if(E[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===r.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===r.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===r.TOK_DOT)return this.match(r.TOK_DOT),this.parseDotRHS(t);var e=this.lookaheadToken(0);this.errorToken(e,"Syntax error, unexpected token: "+e.value+"("+e.type+")")},e.prototype.parseMultiselectList=function(){for(var t=[];this.lookahead(0)!==r.TOK_RBRACKET;){var e=this.expression(0);if(t.push(e),this.lookahead(0)===r.TOK_COMMA&&(this.match(r.TOK_COMMA),this.lookahead(0)===r.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(r.TOK_RBRACKET),{type:"MultiSelectList",children:t}},e.prototype.parseMultiselectHash=function(){for(var t,e,n,i=[],s=[r.TOK_UNQUOTEDIDENTIFIER,r.TOK_QUOTEDIDENTIFIER];;){if(t=this.lookaheadToken(0),!s.includes(t.type))throw new Error("Expecting an identifier token, got: "+t.type);if(e=t.value,this.advance(),this.match(r.TOK_COLON),n=this.expression(0),i.push({value:n,type:"KeyValuePair",name:e}),this.lookahead(0)===r.TOK_COMMA)this.match(r.TOK_COMMA);else if(this.lookahead(0)===r.TOK_RBRACE){this.match(r.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:i}},e}());!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING"}(u||(u={}));var p=function(){function e(e){var r,i=this;this.TYPE_NAME_TABLE=((r={})[u.TYPE_NUMBER]="number",r[u.TYPE_ANY]="any",r[u.TYPE_STRING]="string",r[u.TYPE_ARRAY]="array",r[u.TYPE_OBJECT]="object",r[u.TYPE_BOOLEAN]="boolean",r[u.TYPE_EXPREF]="expression",r[u.TYPE_NULL]="null",r[u.TYPE_ARRAY_NUMBER]="Array<number>",r[u.TYPE_ARRAY_STRING]="Array<string>",r),this.functionAbs=function(t){var e=t[0];return Math.abs(e)},this.functionAvg=function(t){for(var e=t[0],r=0,n=0;n<e.length;n+=1)r+=e[n];return r/e.length},this.functionCeil=function(t){var e=t[0];return Math.ceil(e)},this.functionContains=function(t){var e=t[0],r=t[1];return e.includes(r)},this.functionEndsWith=function(t){var e=t[0],r=t[1];return e.includes(r,e.length-r.length)},this.functionFloor=function(t){var e=t[0];return Math.floor(e)},this.functionJoin=function(t){var e=t[0];return t[1].join(e)},this.functionKeys=function(t){var e=t[0];return Object.keys(e)},this.functionLength=function(t){var e=t[0];return n(e)?Object.keys(e).length:e.length},this.functionMap=function(t){if(!i._interpreter)return[];for(var e=[],r=i._interpreter,n=t[0],s=t[1],o=0;o<s.length;o+=1)e.push(r.visit(n,s[o]));return e},this.functionMax=function(t){var e=t[0];if(!e.length)return null;if(i.getTypeName(e[0])===u.TYPE_NUMBER)return Math.max.apply(Math,e);for(var r=e,n=r[0],s=1;s<r.length;s+=1)n.localeCompare(r[s])<0&&(n=r[s]);return n},this.functionMaxBy=function(t){for(var e,r,n=t[1],s=t[0],o=i.createKeyFunction(n,[u.TYPE_NUMBER,u.TYPE_STRING]),a=-1/0,c=0;c<s.length;c+=1)void 0!==(r=o&&o(s[c]))&&r>a&&(a=r,e=s[c]);return e},this.functionMerge=function(t){for(var e={},r=0;r<t.length;r+=1){var n=t[r];e=Object.assign(e,n)}return e},this.functionMin=function(t){var e=t[0];if(!e.length)return null;if(i.getTypeName(e[0])===u.TYPE_NUMBER)return Math.min.apply(Math,e);for(var r=e,n=r[0],s=1;s<r.length;s+=1)r[s].localeCompare(n)<0&&(n=r[s]);return n},this.functionMinBy=function(t){for(var e,r,n=t[1],s=t[0],o=i.createKeyFunction(n,[u.TYPE_NUMBER,u.TYPE_STRING]),a=1/0,c=0;c<s.length;c+=1)void 0!==(r=o&&o(s[c]))&&r<a&&(a=r,e=s[c]);return e},this.functionNotNull=function(t){for(var e=0;e<t.length;e+=1)if(i.getTypeName(t[e])!==u.TYPE_NULL)return t[e];return null},this.functionReverse=function(t){var e=t[0];if(i.getTypeName(e)===u.TYPE_STRING){for(var r=e,n="",s=r.length-1;s>=0;s-=1)n+=r[s];return n}var o=e.slice(0);return o.reverse(),o},this.functionSort=function(e){return t(e[0]).sort()},this.functionSortBy=function(t){if(!i._interpreter)return[];var e=t[0].slice(0);if(0===e.length)return e;var r=i._interpreter,n=t[1],s=i.getTypeName(r.visit(n,e[0]));if(void 0!==s&&![u.TYPE_NUMBER,u.TYPE_STRING].includes(s))throw new Error("TypeError: unexpected type ("+i.TYPE_NAME_TABLE[s]+")");for(var o=[],a=0;a<e.length;a+=1)o.push([a,e[a]]);o.sort((function(t,e){var o=r.visit(n,t[1]),a=r.visit(n,e[1]);if(i.getTypeName(o)!==s)throw new Error("TypeError: expected ("+i.TYPE_NAME_TABLE[s]+"), received "+i.TYPE_NAME_TABLE[i.getTypeName(o)]);if(i.getTypeName(a)!==s)throw new Error("TypeError: expected ("+i.TYPE_NAME_TABLE[s]+"), received "+i.TYPE_NAME_TABLE[i.getTypeName(a)]);return o>a?1:o<a?-1:t[0]-e[0]}));for(var c=0;c<o.length;c+=1)e[c]=o[c][1];return e},this.functionStartsWith=function(t){var e=t[0],r=t[1];return e.startsWith(r)},this.functionSum=function(t){return t[0].reduce((function(t,e){return t+e}),0)},this.functionToArray=function(t){var e=t[0];return i.getTypeName(e)===u.TYPE_ARRAY?e:[e]},this.functionToNumber=function(t){var e,r=t[0],n=i.getTypeName(r);return n===u.TYPE_NUMBER?r:n!==u.TYPE_STRING||(e=+r,isNaN(e))?null:e},this.functionToString=function(t){var e=t[0];return i.getTypeName(e)===u.TYPE_STRING?e:JSON.stringify(e)},this.functionType=function(t){var e=t[0];switch(i.getTypeName(e)){case u.TYPE_NUMBER:return"number";case u.TYPE_STRING:return"string";case u.TYPE_ARRAY:return"array";case u.TYPE_OBJECT:return"object";case u.TYPE_BOOLEAN:return"boolean";case u.TYPE_EXPREF:return"expref";case u.TYPE_NULL:return"null";default:return}},this.functionValues=function(t){var e=t[0];return Object.values(e)},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[u.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[u.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[u.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[u.TYPE_STRING,u.TYPE_ARRAY]},{types:[u.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[u.TYPE_STRING]},{types:[u.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[u.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[u.TYPE_STRING]},{types:[u.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[u.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[u.TYPE_STRING,u.TYPE_ARRAY,u.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[u.TYPE_EXPREF]},{types:[u.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[u.TYPE_ARRAY_NUMBER,u.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[u.TYPE_ARRAY]},{types:[u.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[u.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[u.TYPE_ARRAY_NUMBER,u.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[u.TYPE_ARRAY]},{types:[u.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[u.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[u.TYPE_STRING,u.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[u.TYPE_ARRAY_STRING,u.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[u.TYPE_ARRAY]},{types:[u.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[u.TYPE_STRING]},{types:[u.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[u.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[u.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[u.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[u.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[u.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[u.TYPE_OBJECT]}]}},this._interpreter=e}return e.prototype.registerFunction=function(t,e,r){if(t in this.functionTable)throw new Error("Function already defined: "+t+"()");this.functionTable[t]={_func:e.bind(this),_signature:r}},e.prototype.callFunction=function(t,e){var r=this.functionTable[t];if(void 0===r)throw new Error("Unknown function: "+t+"()");return this.validateArgs(t,e,r._signature),r._func.call(this,e)},e.prototype.validateInputSignatures=function(t,e){for(var r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error("ArgumentError: "+t+"() 'variadic' argument "+(r+1)+" must occur last")},e.prototype.validateArgs=function(t,e,r){var n,i,s,o=this;this.validateInputSignatures(t,r);var a,u,c,h=r.filter((function(t){var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,T=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,_=e.length<h,E=e.length>r.length,l=_&&(!T&&h>1||T)?"at least ":"";if(T&&_||!T&&(_||E))throw s=r.length>1,new Error("ArgumentError: "+t+"() takes "+l+h+" argument"+(s?"s":"")+" but received "+e.length);for(var p=0;p<r.length;p+=1){c=!1,a=r[p].types,u=this.getTypeName(e[p]);var f=void 0;for(f=0;f<a.length;f+=1)if(void 0!==u&&this.typeMatches(u,a[f],e[p])){c=!0;break}if(!c&&void 0!==u){var R=a.map((function(t){return o.TYPE_NAME_TABLE[t]})).join(" | ");throw new Error("TypeError: "+t+"() expected argument "+(p+1)+" to be type ("+R+") but received type "+this.TYPE_NAME_TABLE[u]+" instead.")}}},e.prototype.typeMatches=function(t,e,r){if(e===u.TYPE_ANY)return!0;if(e!==u.TYPE_ARRAY_STRING&&e!==u.TYPE_ARRAY_NUMBER&&e!==u.TYPE_ARRAY)return t===e;if(e===u.TYPE_ARRAY)return t===u.TYPE_ARRAY;if(t===u.TYPE_ARRAY){var n=void 0;e===u.TYPE_ARRAY_NUMBER?n=u.TYPE_NUMBER:e===u.TYPE_ARRAY_STRING&&(n=u.TYPE_STRING);for(var i=0;i<r.length;i+=1){var s=this.getTypeName(r[i]);if(void 0!==s&&void 0!==n&&!this.typeMatches(s,n,r[i]))return!1}return!0}return!1},e.prototype.getTypeName=function(t){switch(Object.prototype.toString.call(t)){case"[object String]":return u.TYPE_STRING;case"[object Number]":return u.TYPE_NUMBER;case"[object Array]":return u.TYPE_ARRAY;case"[object Boolean]":return u.TYPE_BOOLEAN;case"[object Null]":return u.TYPE_NULL;case"[object Object]":return t.jmespathType===r.TOK_EXPREF?u.TYPE_EXPREF:u.TYPE_OBJECT;default:return}},e.prototype.createKeyFunction=function(t,e){var r=this;if(this._interpreter){var n=this._interpreter;return function(i){var s=n.visit(t,i);if(!e.includes(r.getTypeName(s))){var o="TypeError: expected one of ("+e.map((function(t){return r.TYPE_NAME_TABLE[t]})).join(" | ")+"), received "+r.TYPE_NAME_TABLE[r.getTypeName(s)];throw new Error(o)}return s}}},e}(),f=new(function(){function e(){this._rootValue=null,this.runtime=new p(this)}return e.prototype.search=function(t,e){return this._rootValue=e,this.visit(t,e)},e.prototype.visit=function(e,o){var a,u,c,h,T,_,E,l,p,f;switch(e.type){case"Field":return null===o?null:n(o)?void 0===(_=o[e.name])?null:_:null;case"Subexpression":for(c=this.visit(e.children[0],o),p=1;p<e.children.length;p+=1)if(null===(c=this.visit(e.children[1],c)))return null;return c;case"IndexExpression":return E=this.visit(e.children[0],o),this.visit(e.children[1],E);case"Index":if(!Array.isArray(o))return null;var R=e.value;return R<0&&(R=o.length+R),void 0===(c=o[R])&&(c=null),c;case"Slice":if(!Array.isArray(o))return null;var O=t(e.children),y=this.computeSliceParams(o.length,O),v=y[0],A=y[1],d=y[2];if(c=[],d>0)for(p=v;p<A;p+=d)c.push(o[p]);else for(p=v;p>A;p+=d)c.push(o[p]);return c;case"Projection":if(f=this.visit(e.children[0],o),!Array.isArray(f))return null;for(l=[],p=0;p<f.length;p+=1)null!==(u=this.visit(e.children[1],f[p]))&&l.push(u);return l;case"ValueProjection":if(f=this.visit(e.children[0],o),!n(f))return null;l=[];var N=Object.values(f);for(p=0;p<N.length;p+=1)null!==(u=this.visit(e.children[1],N[p]))&&l.push(u);return l;case"FilterProjection":if(f=this.visit(e.children[0],o),!Array.isArray(f))return null;var K=[],P=[];for(p=0;p<f.length;p+=1)a=this.visit(e.children[2],f[p]),s(a)||K.push(f[p]);for(var Y=0;Y<K.length;Y+=1)null!==(u=this.visit(e.children[1],K[Y]))&&P.push(u);return P;case"Comparator":switch(h=this.visit(e.children[0],o),T=this.visit(e.children[1],o),e.name){case r.TOK_EQ:c=i(h,T);break;case r.TOK_NE:c=!i(h,T);break;case r.TOK_GT:c=h>T;break;case r.TOK_GTE:c=h>=T;break;case r.TOK_LT:c=h<T;break;case r.TOK_LTE:c=h<=T;break;default:throw new Error("Unknown comparator: "+e.name)}return c;case r.TOK_FLATTEN:var g=this.visit(e.children[0],o);if(!Array.isArray(g))return null;var L=[];for(p=0;p<g.length;p+=1)u=g[p],Array.isArray(u)?L=t(L,u):L.push(u);return L;case"Identity":return o;case"MultiSelectList":if(null===o)return null;for(l=[],p=0;p<e.children.length;p+=1)l.push(this.visit(e.children[p],o));return l;case"MultiSelectHash":if(null===o)return null;l={};var I=void 0;for(p=0;p<e.children.length;p+=1)l[(I=e.children[p]).name]=this.visit(I.value,o);return l;case"OrExpression":return a=this.visit(e.children[0],o),s(a)&&(a=this.visit(e.children[1],o)),a;case"AndExpression":return h=this.visit(e.children[0],o),s(h)?h:this.visit(e.children[1],o);case"NotExpression":return h=this.visit(e.children[0],o),s(h);case"Literal":return e.value;case r.TOK_PIPE:return E=this.visit(e.children[0],o),this.visit(e.children[1],E);case r.TOK_CURRENT:return o;case r.TOK_ROOT:return this._rootValue;case"Function":var m=[];for(Y=0;Y<e.children.length;Y+=1)m.push(this.visit(e.children[Y],o));return this.runtime.callFunction(e.name,m);case"ExpressionReference":var k=e.children[0];return k.jmespathType=r.TOK_EXPREF,k;default:throw new Error("Unknown node type: "+e.type)}},e.prototype.computeSliceParams=function(t,e){var r=e[0],n=e[1],i=e[2];if(null===i)i=1;else if(0===i){var s=new Error("Invalid slice, step cannot be 0");throw s.name="RuntimeError",s}var o=i<0;return[r=null===r?o?t-1:0:this.capSliceRange(t,r,i),n=null===n?o?-1:t:this.capSliceRange(t,n,i),i]},e.prototype.capSliceRange=function(t,e,r){var n=e;return n<0?(n+=t)<0&&(n=r<0?-1:0):n>=t&&(n=r<0?t-1:t),n},e}()),R=u.TYPE_ANY,O=u.TYPE_ARRAY,y=u.TYPE_ARRAY_NUMBER,v=u.TYPE_ARRAY_STRING,A=u.TYPE_BOOLEAN,d=u.TYPE_EXPREF,N=u.TYPE_NULL,K=u.TYPE_NUMBER,P=u.TYPE_OBJECT,Y=u.TYPE_STRING;function g(t){return l.parse(t)}function L(t){return _.tokenize(t)}var I=function(t,e,r){f.runtime.registerFunction(t,e,r)};function m(t,e){var r=l.parse(e);return f.search(r,t)}var k=f,S={compile:g,registerFunction:I,search:m,tokenize:L,TreeInterpreter:k,TYPE_ANY:R,TYPE_ARRAY_NUMBER:y,TYPE_ARRAY_STRING:v,TYPE_ARRAY:O,TYPE_BOOLEAN:A,TYPE_EXPREF:d,TYPE_NULL:N,TYPE_NUMBER:K,TYPE_OBJECT:P,TYPE_STRING:Y};export default S;export{R as TYPE_ANY,O as TYPE_ARRAY,y as TYPE_ARRAY_NUMBER,v as TYPE_ARRAY_STRING,A as TYPE_BOOLEAN,d as TYPE_EXPREF,N as TYPE_NULL,K as TYPE_NUMBER,P as TYPE_OBJECT,Y as TYPE_STRING,k as TreeInterpreter,g as compile,S as jmespath,I as registerFunction,m as search,L as tokenize}; | ||
const t=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),e=(r,n)=>{if(r===n)return!0;if(typeof r!=typeof n)return!1;if(Array.isArray(r)&&Array.isArray(n)){if(r.length!==n.length)return!1;for(let t=0;t<r.length;t+=1)if(!e(r[t],n[t]))return!1;return!0}if(t(r)&&t(n)){const t=Object.entries(r),i=new Set(Object.keys(n));if(t.length!==i.size)return!1;for(const[r,s]of t){if(!e(s,n[r]))return!1;i.delete(r)}return 0===i.size}return!1},r=e=>{if(""===e||!1===e||null==e)return!0;if(Array.isArray(e)&&0===e.length)return!0;if(t(e)){for(const t in e)if(e.hasOwnProperty(t))return!1;return!0}return!1},n=t=>t>="0"&&t<="9"||"-"===t;var i;!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(i||(i={}));const s={"(":i.TOK_LPAREN,")":i.TOK_RPAREN,"*":i.TOK_STAR,",":i.TOK_COMMA,".":i.TOK_DOT,":":i.TOK_COLON,"@":i.TOK_CURRENT,$:i.TOK_ROOT,"]":i.TOK_RBRACKET,"{":i.TOK_LBRACE,"}":i.TOK_RBRACE},o={"!":!0,"<":!0,"=":!0,">":!0},h={"\t":!0,"\n":!0,"\r":!0," ":!0};const c=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,c,u;for(this._current=0;this._current<t.length;)if((a=t[this._current])>="a"&&a<="z"||a>="A"&&a<="Z"||"_"===a)r=this._current,c=this.consumeUnquotedIdentifier(t),e.push({start:r,type:i.TOK_UNQUOTEDIDENTIFIER,value:c});else if(void 0!==s[t[this._current]])e.push({start:this._current,type:s[t[this._current]],value:t[this._current]}),this._current+=1;else if(n(t[this._current]))u=this.consumeNumber(t),e.push(u);else if("["===t[this._current])u=this.consumeLBracket(t),e.push(u);else if('"'===t[this._current])r=this._current,c=this.consumeQuotedIdentifier(t),e.push({start:r,type:i.TOK_QUOTEDIDENTIFIER,value:c});else if("'"===t[this._current])r=this._current,c=this.consumeRawStringLiteral(t),e.push({start:r,type:i.TOK_LITERAL,value:c});else if("`"===t[this._current]){r=this._current;const n=this.consumeLiteral(t);e.push({start:r,type:i.TOK_LITERAL,value:n})}else if(void 0!==o[t[this._current]])u=this.consumeOperator(t),u&&e.push(u);else if(void 0!==h[t[this._current]])this._current+=1;else if("&"===t[this._current])r=this._current,this._current+=1,"&"===t[this._current]?(this._current+=1,e.push({start:r,type:i.TOK_AND,value:"&&"})):e.push({start:r,type:i.TOK_EXPREF,value:"&"});else{if("|"!==t[this._current]){const e=new Error(`Unknown character: ${t[this._current]}`);throw e.name="LexerError",e}r=this._current,this._current+=1,"|"===t[this._current]?(this._current+=1,e.push({start:r,type:i.TOK_OR,value:"||"})):e.push({start:r,type:i.TOK_PIPE,value:"|"})}var a;return e}consumeUnquotedIdentifier(t){const e=this._current;for(this._current+=1;this._current<t.length&&((r=t[this._current])>="a"&&r<="z"||r>="A"&&r<="Z"||r>="0"&&r<="9"||"_"===r);)this._current+=1;var r;return t.slice(e,this._current)}consumeQuotedIdentifier(t){const e=this._current;this._current+=1;const r=t.length;for(;'"'!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&'"'!==t[e+1]?e+=1:e+=2,this._current=e}return this._current+=1,JSON.parse(t.slice(e,this._current))}consumeRawStringLiteral(t){const e=this._current;this._current+=1;const r=t.length;for(;"'"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"'"!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;return t.slice(e+1,this._current-1).replace("\\'","'")}consumeNumber(t){const e=this._current;this._current+=1;const r=t.length;for(;n(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:i.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:i.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:i.TOK_FLATTEN,value:"[]"}):{start:e,type:i.TOK_LBRACKET,value:"["}}consumeOperator(t){const e=this._current,r=t[e];return this._current+=1,"!"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_NE,value:"!="}):{start:e,type:i.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_LTE,value:"<="}):{start:e,type:i.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_GTE,value:">="}):{start:e,type:i.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_EQ,value:"=="}):void 0}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimLeft();n=n.replace("\\`","`");const i=this.looksLikeJSON(n)?JSON.parse(n):JSON.parse(`"${n}"`);return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0]))try{return JSON.parse(t),!0}catch(t){return!1}return!1}},u={[i.TOK_EOF]:0,[i.TOK_UNQUOTEDIDENTIFIER]:0,[i.TOK_QUOTEDIDENTIFIER]:0,[i.TOK_RBRACKET]:0,[i.TOK_RPAREN]:0,[i.TOK_COMMA]:0,[i.TOK_RBRACE]:0,[i.TOK_NUMBER]:0,[i.TOK_CURRENT]:0,[i.TOK_EXPREF]:0,[i.TOK_ROOT]:0,[i.TOK_PIPE]:1,[i.TOK_OR]:2,[i.TOK_AND]:3,[i.TOK_EQ]:5,[i.TOK_GT]:5,[i.TOK_LT]:5,[i.TOK_GTE]:5,[i.TOK_LTE]:5,[i.TOK_NE]:5,[i.TOK_FLATTEN]:9,[i.TOK_STAR]:20,[i.TOK_FILTER]:21,[i.TOK_DOT]:40,[i.TOK_NOT]:45,[i.TOK_LBRACE]:50,[i.TOK_LBRACKET]:55,[i.TOK_LPAREN]:60};const a=new class{constructor(){this.index=0,this.tokens=[]}parse(t){this.loadTokens(t),this.index=0;const e=this.expression(0);if(this.lookahead(0)!==i.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Unexpected token type: ${t.type}, value: ${t.value}`)}return e}loadTokens(t){this.tokens=[...c.tokenize(t),{type:i.TOK_EOF,value:"",start:t.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<u[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r}lookahead(t){return this.tokens[this.index+t].type}lookaheadToken(t){return this.tokens[this.index+t]}advance(){this.index+=1}nud(t){let e,r,n;switch(t.type){case i.TOK_LITERAL:return{type:"Literal",value:t.value};case i.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case i.TOK_QUOTEDIDENTIFIER:const s={type:"Field",name:t.value};if(this.lookahead(0)===i.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return s;case i.TOK_NOT:return r=this.expression(u.Not),{type:"NotExpression",children:[r]};case i.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===i.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(u.Star),{type:"ValueProjection",children:[e,r]};case i.TOK_FILTER:return this.led(t.type,{type:"Identity"});case i.TOK_LBRACE:return this.parseMultiselectHash();case i.TOK_FLATTEN:return e={type:i.TOK_FLATTEN,children:[{type:"Identity"}]},r=this.parseProjectionRHS(u.Flatten),{type:"Projection",children:[e,r]};case i.TOK_LBRACKET:return this.lookahead(0)===i.TOK_NUMBER||this.lookahead(0)===i.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},r)):this.lookahead(0)===i.TOK_STAR&&this.lookahead(1)===i.TOK_RBRACKET?(this.advance(),this.advance(),r=this.parseProjectionRHS(u.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case i.TOK_CURRENT:return{type:i.TOK_CURRENT};case i.TOK_ROOT:return{type:i.TOK_ROOT};case i.TOK_EXPREF:return n=this.expression(u.Expref),{type:"ExpressionReference",children:[n]};case i.TOK_LPAREN:const o=[];for(;this.lookahead(0)!==i.TOK_RPAREN;)this.lookahead(0)===i.TOK_CURRENT?(n={type:i.TOK_CURRENT},this.advance()):n=this.expression(0),o.push(n);return this.match(i.TOK_RPAREN),o[0];default:this.errorToken(t)}}led(t,e){let r;switch(t){case i.TOK_DOT:const n=u.Dot;return this.lookahead(0)!==i.TOK_STAR?(r=this.parseDotRHS(n),{type:"Subexpression",children:[e,r]}):(this.advance(),r=this.parseProjectionRHS(n),{type:"ValueProjection",children:[e,r]});case i.TOK_PIPE:return r=this.expression(u.Pipe),{type:i.TOK_PIPE,children:[e,r]};case i.TOK_OR:return r=this.expression(u.Or),{type:"OrExpression",children:[e,r]};case i.TOK_AND:return r=this.expression(u.And),{type:"AndExpression",children:[e,r]};case i.TOK_LPAREN:const s=e.name,o=[];let h;for(;this.lookahead(0)!==i.TOK_RPAREN;)this.lookahead(0)===i.TOK_CURRENT?(h={type:i.TOK_CURRENT},this.advance()):h=this.expression(0),this.lookahead(0)===i.TOK_COMMA&&this.match(i.TOK_COMMA),o.push(h);this.match(i.TOK_RPAREN);return{name:s,type:"Function",children:o};case i.TOK_FILTER:const c=this.expression(0);return this.match(i.TOK_RBRACKET),r=this.lookahead(0)===i.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(u.Filter),{type:"FilterProjection",children:[e,r,c]};case i.TOK_FLATTEN:return{type:"Projection",children:[{type:i.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(u.Flatten)]};case i.TOK_EQ:case i.TOK_NE:case i.TOK_GT:case i.TOK_GTE:case i.TOK_LT:case i.TOK_LTE:return this.parseComparator(e,t);case i.TOK_LBRACKET:const a=this.lookaheadToken(0);return a.type===i.TOK_NUMBER||a.type===i.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(i.TOK_STAR),this.match(i.TOK_RBRACKET),r=this.parseProjectionRHS(u.Star),{type:"Projection",children:[e,r]});default:return this.errorToken(this.lookaheadToken(0))}}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===i.TOK_COLON||this.lookahead(1)===i.TOK_COLON)return this.parseSliceExpression();const t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(i.TOK_RBRACKET),t}projectIfSlice(t,e){const r={type:"IndexExpression",children:[t,e]};return"Slice"===e.type?{children:[r,this.parseProjectionRHS(u.Star)],type:"Projection"}:r}parseSliceExpression(){const t=[null,null,null];let e=0,r=this.lookahead(0);for(;r!==i.TOK_RBRACKET&&e<3;){if(r===i.TOK_COLON)e+=1,this.advance();else if(r===i.TOK_NUMBER)t[e]=this.lookaheadToken(0).value,this.advance();else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}r=this.lookahead(0)}return this.match(i.TOK_RBRACKET),{children:t,type:"Slice"}}parseComparator(t,e){return{type:"Comparator",name:e,children:[t,this.expression(u[e])]}}parseDotRHS(t){const e=this.lookahead(0);if([i.TOK_UNQUOTEDIDENTIFIER,i.TOK_QUOTEDIDENTIFIER,i.TOK_STAR].includes(e))return this.expression(t);if(e===i.TOK_LBRACKET)return this.match(i.TOK_LBRACKET),this.parseMultiselectList();if(e===i.TOK_LBRACE)return this.match(i.TOK_LBRACE),this.parseMultiselectHash();const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseProjectionRHS(t){if(u[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===i.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===i.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===i.TOK_DOT)return this.match(i.TOK_DOT),this.parseDotRHS(t);const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error, unexpected token: ${e.value}(${e.type})`)}parseMultiselectList(){const t=[];for(;this.lookahead(0)!==i.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===i.TOK_COMMA&&(this.match(i.TOK_COMMA),this.lookahead(0)===i.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(i.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[i.TOK_UNQUOTEDIDENTIFIER,i.TOK_QUOTEDIDENTIFIER];let r,n,s;for(;;){if(r=this.lookaheadToken(0),!e.includes(r.type))throw new Error(`Expecting an identifier token, got: ${r.type}`);if(n=r.value,this.advance(),this.match(i.TOK_COLON),s=this.expression(0),t.push({value:s,type:"KeyValuePair",name:n}),this.lookahead(0)===i.TOK_COMMA)this.match(i.TOK_COMMA);else if(this.lookahead(0)===i.TOK_RBRACE){this.match(i.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}};var T;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING"}(T||(T={}));class _{constructor(e){this.TYPE_NAME_TABLE={[T.TYPE_NUMBER]:"number",[T.TYPE_ANY]:"any",[T.TYPE_STRING]:"string",[T.TYPE_ARRAY]:"array",[T.TYPE_OBJECT]:"object",[T.TYPE_BOOLEAN]:"boolean",[T.TYPE_EXPREF]:"expression",[T.TYPE_NULL]:"null",[T.TYPE_ARRAY_NUMBER]:"Array<number>",[T.TYPE_ARRAY_STRING]:"Array<string>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{let e=0;for(let r=0;r<t.length;r+=1)e+=t[r];return e/t.length},this.functionCeil=([t])=>Math.ceil(t),this.functionContains=t=>{const[e,r]=t;return e.includes(r)},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFloor=([t])=>Math.floor(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([e])=>t(e)?Object.keys(e).length:e.length,this.functionMap=t=>{if(!this._interpreter)return[];const e=[],r=this._interpreter,n=t[0],i=t[1];for(let t=0;t<i.length;t+=1)e.push(r.visit(n,i[t]));return e},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===T.TYPE_NUMBER)return Math.max(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)r.localeCompare(e[t])<0&&(r=e[t]);return r},this.functionMaxBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[T.TYPE_NUMBER,T.TYPE_STRING]);let i,s,o=-1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s>o&&(o=s,i=r[t]);return i},this.functionMerge=t=>{let e={};for(let r=0;r<t.length;r+=1){const n=t[r];e=Object.assign(e,n)}return e},this.functionMin=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===T.TYPE_NUMBER)return Math.min(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)e[t].localeCompare(r)<0&&(r=e[t]);return r},this.functionMinBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[T.TYPE_NUMBER,T.TYPE_STRING]);let i,s,o=1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s<o&&(o=s,i=r[t]);return i},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==T.TYPE_NULL)return t[e];return null},this.functionReverse=([t])=>{if(this.getTypeName(t)===T.TYPE_STRING){const e=t;let r="";for(let t=e.length-1;t>=0;t-=1)r+=e[t];return r}const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>[...t].sort(),this.functionSortBy=t=>{if(!this._interpreter)return[];const e=t[0].slice(0);if(0===e.length)return e;const r=this._interpreter,n=t[1],i=this.getTypeName(r.visit(n,e[0]));if(void 0!==i&&![T.TYPE_NUMBER,T.TYPE_STRING].includes(i))throw new Error(`TypeError: unexpected type (${this.TYPE_NAME_TABLE[i]})`);const s=[];for(let t=0;t<e.length;t+=1)s.push([t,e[t]]);s.sort(((t,e)=>{const s=r.visit(n,t[1]),o=r.visit(n,e[1]);if(this.getTypeName(s)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(s)]}`);if(this.getTypeName(o)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(o)]}`);return s>o?1:s<o?-1:t[0]-e[0]}));for(let t=0;t<s.length;t+=1)e[t]=s[t][1];return e},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===T.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===T.TYPE_NUMBER?t:e!==T.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===T.TYPE_STRING?t:JSON.stringify(t),this.functionType=([t])=>{switch(this.getTypeName(t)){case T.TYPE_NUMBER:return"number";case T.TYPE_STRING:return"string";case T.TYPE_ARRAY:return"array";case T.TYPE_OBJECT:return"object";case T.TYPE_BOOLEAN:return"boolean";case T.TYPE_EXPREF:return"expref";case T.TYPE_NULL:return"null";default:return}},this.functionValues=([t])=>Object.values(t),this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[T.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[T.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[T.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY]},{types:[T.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[T.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[T.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY,T.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[T.TYPE_EXPREF]},{types:[T.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[T.TYPE_ARRAY_NUMBER,T.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[T.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[T.TYPE_ARRAY_NUMBER,T.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[T.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[T.TYPE_ARRAY_STRING,T.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[T.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[T.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[T.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[T.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[T.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[T.TYPE_OBJECT]}]}},this._interpreter=e}registerFunction(t,e,r){if(t in this.functionTable)throw new Error(`Function already defined: ${t}()`);this.functionTable[t]={_func:e.bind(this),_signature:r}}callFunction(t,e){const r=this.functionTable[t];if(void 0===r)throw new Error(`Unknown function: ${t}()`);return this.validateArgs(t,e,r._signature),r._func.call(this,e)}validateInputSignatures(t,e){for(let r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error(`ArgumentError: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,h=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,c=e.length<o,u=e.length>r.length,a=c&&(!h&&o>1||h)?"at least ":"";if(h&&c||!h&&(c||u))throw s=r.length>1,new Error(`ArgumentError: ${t}() takes ${a}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`TypeError: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===T.TYPE_ANY)return!0;if(e!==T.TYPE_ARRAY_STRING&&e!==T.TYPE_ARRAY_NUMBER&&e!==T.TYPE_ARRAY)return t===e;if(e===T.TYPE_ARRAY)return t===T.TYPE_ARRAY;if(t===T.TYPE_ARRAY){let t;e===T.TYPE_ARRAY_NUMBER?t=T.TYPE_NUMBER:e===T.TYPE_ARRAY_STRING&&(t=T.TYPE_STRING);for(let e=0;e<r.length;e+=1){const n=this.getTypeName(r[e]);if(void 0!==n&&void 0!==t&&!this.typeMatches(n,t,r[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return T.TYPE_STRING;case"[object Number]":return T.TYPE_NUMBER;case"[object Array]":return T.TYPE_ARRAY;case"[object Boolean]":return T.TYPE_BOOLEAN;case"[object Null]":return T.TYPE_NULL;case"[object Object]":return t.jmespathType===i.TOK_EXPREF?T.TYPE_EXPREF:T.TYPE_OBJECT;default:return}}createKeyFunction(t,e){if(!this._interpreter)return;const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`TypeError: expected one of (${e.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(i)]}`;throw new Error(t)}return i}}}const l=new class{constructor(){this._rootValue=null,this.runtime=new _(this)}search(t,e){return this._rootValue=e,this.visit(t,e)}visit(n,s){let o,h,c,u,a,T,_,l,E,R,p;switch(n.type){case"Field":return null===s?null:t(s)?(T=s[n.name],void 0===T?null:T):null;case"Subexpression":for(c=this.visit(n.children[0],s),R=1;R<n.children.length;R+=1)if(c=this.visit(n.children[1],c),null===c)return null;return c;case"IndexExpression":return _=this.visit(n.children[0],s),l=this.visit(n.children[1],_),l;case"Index":if(!Array.isArray(s))return null;let O=n.value;return O<0&&(O=s.length+O),c=s[O],void 0===c&&(c=null),c;case"Slice":if(!Array.isArray(s))return null;const f=[...n.children],A=this.computeSliceParams(s.length,f),[d,N,K]=A;if(c=[],K>0)for(R=d;R<N;R+=K)c.push(s[R]);else for(R=d;R>N;R+=K)c.push(s[R]);return c;case"Projection":if(p=this.visit(n.children[0],s),!Array.isArray(p))return null;for(E=[],R=0;R<p.length;R+=1)h=this.visit(n.children[1],p[R]),null!==h&&E.push(h);return E;case"ValueProjection":if(p=this.visit(n.children[0],s),!t(p))return null;E=[];const y=Object.values(p);for(R=0;R<y.length;R+=1)h=this.visit(n.children[1],y[R]),null!==h&&E.push(h);return E;case"FilterProjection":if(p=this.visit(n.children[0],s),!Array.isArray(p))return null;const P=[],Y=[];for(R=0;R<p.length;R+=1)o=this.visit(n.children[2],p[R]),r(o)||P.push(p[R]);for(let t=0;t<P.length;t+=1)h=this.visit(n.children[1],P[t]),null!==h&&Y.push(h);return Y;case"Comparator":switch(u=this.visit(n.children[0],s),a=this.visit(n.children[1],s),n.name){case i.TOK_EQ:c=e(u,a);break;case i.TOK_NE:c=!e(u,a);break;case i.TOK_GT:c=u>a;break;case i.TOK_GTE:c=u>=a;break;case i.TOK_LT:c=u<a;break;case i.TOK_LTE:c=u<=a;break;default:throw new Error(`Unknown comparator: ${n.name}`)}return c;case i.TOK_FLATTEN:const g=this.visit(n.children[0],s);if(!Array.isArray(g))return null;let v=[];for(R=0;R<g.length;R+=1)h=g[R],Array.isArray(h)?v=[...v,...h]:v.push(h);return v;case"Identity":return s;case"MultiSelectList":if(null===s)return null;for(E=[],R=0;R<n.children.length;R+=1)E.push(this.visit(n.children[R],s));return E;case"MultiSelectHash":if(null===s)return null;let L;for(E={},R=0;R<n.children.length;R+=1)L=n.children[R],E[L.name]=this.visit(L.value,s);return E;case"OrExpression":return o=this.visit(n.children[0],s),r(o)&&(o=this.visit(n.children[1],s)),o;case"AndExpression":return u=this.visit(n.children[0],s),r(u)?u:this.visit(n.children[1],s);case"NotExpression":return u=this.visit(n.children[0],s),r(u);case"Literal":return n.value;case i.TOK_PIPE:return _=this.visit(n.children[0],s),this.visit(n.children[1],_);case i.TOK_CURRENT:return s;case i.TOK_ROOT:return this._rootValue;case"Function":const I=[];for(let t=0;t<n.children.length;t+=1)I.push(this.visit(n.children[t],s));return this.runtime.callFunction(n.name,I);case"ExpressionReference":const m=n.children[0];return m.jmespathType=i.TOK_EXPREF,m;default:throw new Error(`Unknown node type: ${n.type}`)}}computeSliceParams(t,e){let[r,n,i]=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid slice, step cannot be 0");throw t.name="RuntimeError",t}const s=i<0;return r=null===r?s?t-1:0:this.capSliceRange(t,r,i),n=null===n?s?-1:t:this.capSliceRange(t,n,i),[r,n,i]}capSliceRange(t,e,r){let n=e;return n<0?(n+=t,n<0&&(n=r<0?-1:0)):n>=t&&(n=r<0?t-1:t),n}},E=T.TYPE_ANY,R=T.TYPE_ARRAY,p=T.TYPE_ARRAY_NUMBER,O=T.TYPE_ARRAY_STRING,f=T.TYPE_BOOLEAN,A=T.TYPE_EXPREF,d=T.TYPE_NULL,N=T.TYPE_NUMBER,K=T.TYPE_OBJECT,y=T.TYPE_STRING;function P(t){return a.parse(t)}function Y(t){return c.tokenize(t)}const g=(t,e,r)=>{l.runtime.registerFunction(t,e,r)};function v(t,e){const r=a.parse(e);return l.search(r,t)}const L=l,I={compile:P,registerFunction:g,search:v,tokenize:Y,TreeInterpreter:L,TYPE_ANY:E,TYPE_ARRAY_NUMBER:p,TYPE_ARRAY_STRING:O,TYPE_ARRAY:R,TYPE_BOOLEAN:f,TYPE_EXPREF:A,TYPE_NULL:d,TYPE_NUMBER:N,TYPE_OBJECT:K,TYPE_STRING:y};export default I;export{E as TYPE_ANY,R as TYPE_ARRAY,p as TYPE_ARRAY_NUMBER,O as TYPE_ARRAY_STRING,f as TYPE_BOOLEAN,A as TYPE_EXPREF,d as TYPE_NULL,N as TYPE_NUMBER,K as TYPE_OBJECT,y as TYPE_STRING,L as TreeInterpreter,P as compile,I as jmespath,g as registerFunction,v as search,Y as tokenize}; | ||
//# sourceMappingURL=jmespath.esm.min.js.map |
@@ -1,16 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jmespath={})}(this,(function(t){"use strict"; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function e(){for(var t=0,e=0,r=arguments.length;e<r;e++)t+=arguments[e].length;var n=Array(t),i=0;for(e=0;e<r;e++)for(var s=arguments[e],o=0,a=s.length;o<a;o++,i++)n[i]=s[o];return n}var r,n,i=function(t){return null!==t&&"[object Object]"===Object.prototype.toString.call(t)},s=function(t,e){if(t===e)return!0;if(typeof t!=typeof e)return!1;if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(var r=0;r<t.length;r+=1)if(!s(t[r],e[r]))return!1;return!0}if(i(t)&&i(e)){var n=Object.entries(t),o=new Set(Object.keys(e));if(n.length!==o.size)return!1;for(var a=0,u=n;a<u.length;a++){var c=u[a],T=c[0],h=c[1];if(!s(h,e[T]))return!1;o.delete(T)}return 0===o.size}return!1},o=function(t){if(""===t||!1===t||null==t)return!0;if(Array.isArray(t)&&0===t.length)return!0;if(i(t)){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}return!1},a=function(t){return t>="0"&&t<="9"||"-"===t};!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(n||(n={}));var u,c,T=((r={"(":n.TOK_LPAREN,")":n.TOK_RPAREN,"*":n.TOK_STAR,",":n.TOK_COMMA,".":n.TOK_DOT,":":n.TOK_COLON,"@":n.TOK_CURRENT}).$=n.TOK_ROOT,r["]"]=n.TOK_RBRACKET,r["{"]=n.TOK_LBRACE,r["}"]=n.TOK_RBRACE,r),h={"!":!0,"<":!0,"=":!0,">":!0},_={"\t":!0,"\n":!0,"\r":!0," ":!0},E=new(function(){function t(){this._current=0}return t.prototype.tokenize=function(t){var e,r,i,s,o=[];for(this._current=0;this._current<t.length;)if((s=t[this._current])>="a"&&s<="z"||s>="A"&&s<="Z"||"_"===s)e=this._current,r=this.consumeUnquotedIdentifier(t),o.push({start:e,type:n.TOK_UNQUOTEDIDENTIFIER,value:r});else if(void 0!==T[t[this._current]])o.push({start:this._current,type:T[t[this._current]],value:t[this._current]}),this._current+=1;else if(a(t[this._current]))i=this.consumeNumber(t),o.push(i);else if("["===t[this._current])i=this.consumeLBracket(t),o.push(i);else if('"'===t[this._current])e=this._current,r=this.consumeQuotedIdentifier(t),o.push({start:e,type:n.TOK_QUOTEDIDENTIFIER,value:r});else if("'"===t[this._current])e=this._current,r=this.consumeRawStringLiteral(t),o.push({start:e,type:n.TOK_LITERAL,value:r});else if("`"===t[this._current]){e=this._current;var u=this.consumeLiteral(t);o.push({start:e,type:n.TOK_LITERAL,value:u})}else if(void 0!==h[t[this._current]])(i=this.consumeOperator(t))&&o.push(i);else if(void 0!==_[t[this._current]])this._current+=1;else if("&"===t[this._current])e=this._current,this._current+=1,"&"===t[this._current]?(this._current+=1,o.push({start:e,type:n.TOK_AND,value:"&&"})):o.push({start:e,type:n.TOK_EXPREF,value:"&"});else{if("|"!==t[this._current]){var c=new Error("Unknown character: "+t[this._current]);throw c.name="LexerError",c}e=this._current,this._current+=1,"|"===t[this._current]?(this._current+=1,o.push({start:e,type:n.TOK_OR,value:"||"})):o.push({start:e,type:n.TOK_PIPE,value:"|"})}return o},t.prototype.consumeUnquotedIdentifier=function(t){var e,r=this._current;for(this._current+=1;this._current<t.length&&((e=t[this._current])>="a"&&e<="z"||e>="A"&&e<="Z"||e>="0"&&e<="9"||"_"===e);)this._current+=1;return t.slice(r,this._current)},t.prototype.consumeQuotedIdentifier=function(t){var e=this._current;this._current+=1;for(var r=t.length;'"'!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&'"'!==t[n+1]?n+=1:n+=2,this._current=n}return this._current+=1,JSON.parse(t.slice(e,this._current))},t.prototype.consumeRawStringLiteral=function(t){var e=this._current;this._current+=1;for(var r=t.length;"'"!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&"'"!==t[n+1]?n+=1:n+=2,this._current=n}return this._current+=1,t.slice(e+1,this._current-1).replace("\\'","'")},t.prototype.consumeNumber=function(t){var e=this._current;this._current+=1;for(var r=t.length;a(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:n.TOK_NUMBER}},t.prototype.consumeLBracket=function(t){var e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:n.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:n.TOK_FLATTEN,value:"[]"}):{start:e,type:n.TOK_LBRACKET,value:"["}},t.prototype.consumeOperator=function(t){var e=this._current,r=t[e];return this._current+=1,"!"===r?"="===t[this._current]?(this._current+=1,{start:e,type:n.TOK_NE,value:"!="}):{start:e,type:n.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:n.TOK_LTE,value:"<="}):{start:e,type:n.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:n.TOK_GTE,value:">="}):{start:e,type:n.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:n.TOK_EQ,value:"=="}):void 0},t.prototype.consumeLiteral=function(t){this._current+=1;for(var e=this._current,r=t.length;"`"!==t[this._current]&&this._current<r;){var n=this._current;"\\"!==t[n]||"\\"!==t[n+1]&&"`"!==t[n+1]?n+=1:n+=2,this._current=n}var i=t.slice(e,this._current).trimLeft();i=i.replace("\\`","`");var s=this.looksLikeJSON(i)?JSON.parse(i):JSON.parse('"'+i+'"');return this._current+=1,s},t.prototype.looksLikeJSON=function(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0]))try{return JSON.parse(t),!0}catch(t){return!1}return!1},t}()),l=((u={})[n.TOK_EOF]=0,u[n.TOK_UNQUOTEDIDENTIFIER]=0,u[n.TOK_QUOTEDIDENTIFIER]=0,u[n.TOK_RBRACKET]=0,u[n.TOK_RPAREN]=0,u[n.TOK_COMMA]=0,u[n.TOK_RBRACE]=0,u[n.TOK_NUMBER]=0,u[n.TOK_CURRENT]=0,u[n.TOK_EXPREF]=0,u[n.TOK_ROOT]=0,u[n.TOK_PIPE]=1,u[n.TOK_OR]=2,u[n.TOK_AND]=3,u[n.TOK_EQ]=5,u[n.TOK_GT]=5,u[n.TOK_LT]=5,u[n.TOK_GTE]=5,u[n.TOK_LTE]=5,u[n.TOK_NE]=5,u[n.TOK_FLATTEN]=9,u[n.TOK_STAR]=20,u[n.TOK_FILTER]=21,u[n.TOK_DOT]=40,u[n.TOK_NOT]=45,u[n.TOK_LBRACE]=50,u[n.TOK_LBRACKET]=55,u[n.TOK_LPAREN]=60,u),p=new(function(){function t(){this.index=0,this.tokens=[]}return t.prototype.parse=function(t){this.loadTokens(t),this.index=0;var e=this.expression(0);if(this.lookahead(0)!==n.TOK_EOF){var r=this.lookaheadToken(0);this.errorToken(r,"Unexpected token type: "+r.type+", value: "+r.value)}return e},t.prototype.loadTokens=function(t){this.tokens=e(E.tokenize(t),[{type:n.TOK_EOF,value:"",start:t.length}])},t.prototype.expression=function(t){var e=this.lookaheadToken(0);this.advance();for(var r=this.nud(e),n=this.lookahead(0);t<l[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r},t.prototype.lookahead=function(t){return this.tokens[this.index+t].type},t.prototype.lookaheadToken=function(t){return this.tokens[this.index+t]},t.prototype.advance=function(){this.index+=1},t.prototype.nud=function(t){var e,r;switch(t.type){case n.TOK_LITERAL:return{type:"Literal",value:t.value};case n.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case n.TOK_QUOTEDIDENTIFIER:var i={type:"Field",name:t.value};if(this.lookahead(0)===n.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return i;case n.TOK_NOT:return{type:"NotExpression",children:[e=this.expression(l.Not)]};case n.TOK_STAR:return{type:"ValueProjection",children:[{type:"Identity"},e=this.lookahead(0)===n.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(l.Star)]};case n.TOK_FILTER:return this.led(t.type,{type:"Identity"});case n.TOK_LBRACE:return this.parseMultiselectHash();case n.TOK_FLATTEN:return{type:"Projection",children:[{type:n.TOK_FLATTEN,children:[{type:"Identity"}]},e=this.parseProjectionRHS(l.Flatten)]};case n.TOK_LBRACKET:return this.lookahead(0)===n.TOK_NUMBER||this.lookahead(0)===n.TOK_COLON?(e=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},e)):this.lookahead(0)===n.TOK_STAR&&this.lookahead(1)===n.TOK_RBRACKET?(this.advance(),this.advance(),{children:[{type:"Identity"},e=this.parseProjectionRHS(l.Star)],type:"Projection"}):this.parseMultiselectList();case n.TOK_CURRENT:return{type:n.TOK_CURRENT};case n.TOK_ROOT:return{type:n.TOK_ROOT};case n.TOK_EXPREF:return{type:"ExpressionReference",children:[r=this.expression(l.Expref)]};case n.TOK_LPAREN:for(var s=[];this.lookahead(0)!==n.TOK_RPAREN;)this.lookahead(0)===n.TOK_CURRENT?(r={type:n.TOK_CURRENT},this.advance()):r=this.expression(0),s.push(r);return this.match(n.TOK_RPAREN),s[0];default:this.errorToken(t)}},t.prototype.led=function(t,e){var r;switch(t){case n.TOK_DOT:var i=l.Dot;return this.lookahead(0)!==n.TOK_STAR?{type:"Subexpression",children:[e,r=this.parseDotRHS(i)]}:(this.advance(),{type:"ValueProjection",children:[e,r=this.parseProjectionRHS(i)]});case n.TOK_PIPE:return r=this.expression(l.Pipe),{type:n.TOK_PIPE,children:[e,r]};case n.TOK_OR:return{type:"OrExpression",children:[e,r=this.expression(l.Or)]};case n.TOK_AND:return{type:"AndExpression",children:[e,r=this.expression(l.And)]};case n.TOK_LPAREN:for(var s=e.name,o=[],a=void 0;this.lookahead(0)!==n.TOK_RPAREN;)this.lookahead(0)===n.TOK_CURRENT?(a={type:n.TOK_CURRENT},this.advance()):a=this.expression(0),this.lookahead(0)===n.TOK_COMMA&&this.match(n.TOK_COMMA),o.push(a);return this.match(n.TOK_RPAREN),{name:s,type:"Function",children:o};case n.TOK_FILTER:var u=this.expression(0);return this.match(n.TOK_RBRACKET),{type:"FilterProjection",children:[e,r=this.lookahead(0)===n.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(l.Filter),u]};case n.TOK_FLATTEN:return{type:"Projection",children:[{type:n.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(l.Flatten)]};case n.TOK_EQ:case n.TOK_NE:case n.TOK_GT:case n.TOK_GTE:case n.TOK_LT:case n.TOK_LTE:return this.parseComparator(e,t);case n.TOK_LBRACKET:var c=this.lookaheadToken(0);return c.type===n.TOK_NUMBER||c.type===n.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(n.TOK_STAR),this.match(n.TOK_RBRACKET),{type:"Projection",children:[e,r=this.parseProjectionRHS(l.Star)]});default:return this.errorToken(this.lookaheadToken(0))}},t.prototype.match=function(t){if(this.lookahead(0)!==t){var e=this.lookaheadToken(0);this.errorToken(e,"Expected "+t+", got: "+e.type)}else this.advance()},t.prototype.errorToken=function(t,e){void 0===e&&(e="");var r=new Error(e||"Invalid token ("+t.type+'): "'+t.value+'"');throw r.name="ParserError",r},t.prototype.parseIndexExpression=function(){if(this.lookahead(0)===n.TOK_COLON||this.lookahead(1)===n.TOK_COLON)return this.parseSliceExpression();var t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(n.TOK_RBRACKET),t},t.prototype.projectIfSlice=function(t,e){var r={type:"IndexExpression",children:[t,e]};return"Slice"===e.type?{children:[r,this.parseProjectionRHS(l.Star)],type:"Projection"}:r},t.prototype.parseSliceExpression=function(){for(var t=[null,null,null],e=0,r=this.lookahead(0);r!==n.TOK_RBRACKET&&e<3;){if(r===n.TOK_COLON)e+=1,this.advance();else if(r===n.TOK_NUMBER)t[e]=this.lookaheadToken(0).value,this.advance();else{var i=this.lookaheadToken(0);this.errorToken(i,"Syntax error, unexpected token: "+i.value+"("+i.type+")")}r=this.lookahead(0)}return this.match(n.TOK_RBRACKET),{children:t,type:"Slice"}},t.prototype.parseComparator=function(t,e){return{type:"Comparator",name:e,children:[t,this.expression(l[e])]}},t.prototype.parseDotRHS=function(t){var e=this.lookahead(0);if([n.TOK_UNQUOTEDIDENTIFIER,n.TOK_QUOTEDIDENTIFIER,n.TOK_STAR].includes(e))return this.expression(t);if(e===n.TOK_LBRACKET)return this.match(n.TOK_LBRACKET),this.parseMultiselectList();if(e===n.TOK_LBRACE)return this.match(n.TOK_LBRACE),this.parseMultiselectHash();var r=this.lookaheadToken(0);this.errorToken(r,"Syntax error, unexpected token: "+r.value+"("+r.type+")")},t.prototype.parseProjectionRHS=function(t){if(l[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===n.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===n.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===n.TOK_DOT)return this.match(n.TOK_DOT),this.parseDotRHS(t);var e=this.lookaheadToken(0);this.errorToken(e,"Syntax error, unexpected token: "+e.value+"("+e.type+")")},t.prototype.parseMultiselectList=function(){for(var t=[];this.lookahead(0)!==n.TOK_RBRACKET;){var e=this.expression(0);if(t.push(e),this.lookahead(0)===n.TOK_COMMA&&(this.match(n.TOK_COMMA),this.lookahead(0)===n.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(n.TOK_RBRACKET),{type:"MultiSelectList",children:t}},t.prototype.parseMultiselectHash=function(){for(var t,e,r,i=[],s=[n.TOK_UNQUOTEDIDENTIFIER,n.TOK_QUOTEDIDENTIFIER];;){if(t=this.lookaheadToken(0),!s.includes(t.type))throw new Error("Expecting an identifier token, got: "+t.type);if(e=t.value,this.advance(),this.match(n.TOK_COLON),r=this.expression(0),i.push({value:r,type:"KeyValuePair",name:e}),this.lookahead(0)===n.TOK_COMMA)this.match(n.TOK_COMMA);else if(this.lookahead(0)===n.TOK_RBRACE){this.match(n.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:i}},t}());!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING"}(c||(c={}));var f=function(){function t(t){var r,n=this;this.TYPE_NAME_TABLE=((r={})[c.TYPE_NUMBER]="number",r[c.TYPE_ANY]="any",r[c.TYPE_STRING]="string",r[c.TYPE_ARRAY]="array",r[c.TYPE_OBJECT]="object",r[c.TYPE_BOOLEAN]="boolean",r[c.TYPE_EXPREF]="expression",r[c.TYPE_NULL]="null",r[c.TYPE_ARRAY_NUMBER]="Array<number>",r[c.TYPE_ARRAY_STRING]="Array<string>",r),this.functionAbs=function(t){var e=t[0];return Math.abs(e)},this.functionAvg=function(t){for(var e=t[0],r=0,n=0;n<e.length;n+=1)r+=e[n];return r/e.length},this.functionCeil=function(t){var e=t[0];return Math.ceil(e)},this.functionContains=function(t){var e=t[0],r=t[1];return e.includes(r)},this.functionEndsWith=function(t){var e=t[0],r=t[1];return e.includes(r,e.length-r.length)},this.functionFloor=function(t){var e=t[0];return Math.floor(e)},this.functionJoin=function(t){var e=t[0];return t[1].join(e)},this.functionKeys=function(t){var e=t[0];return Object.keys(e)},this.functionLength=function(t){var e=t[0];return i(e)?Object.keys(e).length:e.length},this.functionMap=function(t){if(!n._interpreter)return[];for(var e=[],r=n._interpreter,i=t[0],s=t[1],o=0;o<s.length;o+=1)e.push(r.visit(i,s[o]));return e},this.functionMax=function(t){var e=t[0];if(!e.length)return null;if(n.getTypeName(e[0])===c.TYPE_NUMBER)return Math.max.apply(Math,e);for(var r=e,i=r[0],s=1;s<r.length;s+=1)i.localeCompare(r[s])<0&&(i=r[s]);return i},this.functionMaxBy=function(t){for(var e,r,i=t[1],s=t[0],o=n.createKeyFunction(i,[c.TYPE_NUMBER,c.TYPE_STRING]),a=-1/0,u=0;u<s.length;u+=1)void 0!==(r=o&&o(s[u]))&&r>a&&(a=r,e=s[u]);return e},this.functionMerge=function(t){for(var e={},r=0;r<t.length;r+=1){var n=t[r];e=Object.assign(e,n)}return e},this.functionMin=function(t){var e=t[0];if(!e.length)return null;if(n.getTypeName(e[0])===c.TYPE_NUMBER)return Math.min.apply(Math,e);for(var r=e,i=r[0],s=1;s<r.length;s+=1)r[s].localeCompare(i)<0&&(i=r[s]);return i},this.functionMinBy=function(t){for(var e,r,i=t[1],s=t[0],o=n.createKeyFunction(i,[c.TYPE_NUMBER,c.TYPE_STRING]),a=1/0,u=0;u<s.length;u+=1)void 0!==(r=o&&o(s[u]))&&r<a&&(a=r,e=s[u]);return e},this.functionNotNull=function(t){for(var e=0;e<t.length;e+=1)if(n.getTypeName(t[e])!==c.TYPE_NULL)return t[e];return null},this.functionReverse=function(t){var e=t[0];if(n.getTypeName(e)===c.TYPE_STRING){for(var r=e,i="",s=r.length-1;s>=0;s-=1)i+=r[s];return i}var o=e.slice(0);return o.reverse(),o},this.functionSort=function(t){return e(t[0]).sort()},this.functionSortBy=function(t){if(!n._interpreter)return[];var e=t[0].slice(0);if(0===e.length)return e;var r=n._interpreter,i=t[1],s=n.getTypeName(r.visit(i,e[0]));if(void 0!==s&&![c.TYPE_NUMBER,c.TYPE_STRING].includes(s))throw new Error("TypeError: unexpected type ("+n.TYPE_NAME_TABLE[s]+")");for(var o=[],a=0;a<e.length;a+=1)o.push([a,e[a]]);o.sort((function(t,e){var o=r.visit(i,t[1]),a=r.visit(i,e[1]);if(n.getTypeName(o)!==s)throw new Error("TypeError: expected ("+n.TYPE_NAME_TABLE[s]+"), received "+n.TYPE_NAME_TABLE[n.getTypeName(o)]);if(n.getTypeName(a)!==s)throw new Error("TypeError: expected ("+n.TYPE_NAME_TABLE[s]+"), received "+n.TYPE_NAME_TABLE[n.getTypeName(a)]);return o>a?1:o<a?-1:t[0]-e[0]}));for(var u=0;u<o.length;u+=1)e[u]=o[u][1];return e},this.functionStartsWith=function(t){var e=t[0],r=t[1];return e.startsWith(r)},this.functionSum=function(t){return t[0].reduce((function(t,e){return t+e}),0)},this.functionToArray=function(t){var e=t[0];return n.getTypeName(e)===c.TYPE_ARRAY?e:[e]},this.functionToNumber=function(t){var e,r=t[0],i=n.getTypeName(r);return i===c.TYPE_NUMBER?r:i!==c.TYPE_STRING||(e=+r,isNaN(e))?null:e},this.functionToString=function(t){var e=t[0];return n.getTypeName(e)===c.TYPE_STRING?e:JSON.stringify(e)},this.functionType=function(t){var e=t[0];switch(n.getTypeName(e)){case c.TYPE_NUMBER:return"number";case c.TYPE_STRING:return"string";case c.TYPE_ARRAY:return"array";case c.TYPE_OBJECT:return"object";case c.TYPE_BOOLEAN:return"boolean";case c.TYPE_EXPREF:return"expref";case c.TYPE_NULL:return"null";default:return}},this.functionValues=function(t){var e=t[0];return Object.values(e)},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[c.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[c.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[c.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[c.TYPE_STRING,c.TYPE_ARRAY]},{types:[c.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[c.TYPE_STRING]},{types:[c.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[c.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[c.TYPE_STRING]},{types:[c.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[c.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[c.TYPE_STRING,c.TYPE_ARRAY,c.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[c.TYPE_EXPREF]},{types:[c.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[c.TYPE_ARRAY_NUMBER,c.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[c.TYPE_ARRAY]},{types:[c.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[c.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[c.TYPE_ARRAY_NUMBER,c.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[c.TYPE_ARRAY]},{types:[c.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[c.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[c.TYPE_STRING,c.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[c.TYPE_ARRAY_STRING,c.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[c.TYPE_ARRAY]},{types:[c.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[c.TYPE_STRING]},{types:[c.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[c.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[c.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[c.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[c.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[c.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[c.TYPE_OBJECT]}]}},this._interpreter=t}return t.prototype.registerFunction=function(t,e,r){if(t in this.functionTable)throw new Error("Function already defined: "+t+"()");this.functionTable[t]={_func:e.bind(this),_signature:r}},t.prototype.callFunction=function(t,e){var r=this.functionTable[t];if(void 0===r)throw new Error("Unknown function: "+t+"()");return this.validateArgs(t,e,r._signature),r._func.call(this,e)},t.prototype.validateInputSignatures=function(t,e){for(var r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error("ArgumentError: "+t+"() 'variadic' argument "+(r+1)+" must occur last")},t.prototype.validateArgs=function(t,e,r){var n,i,s,o=this;this.validateInputSignatures(t,r);var a,u,c,T=r.filter((function(t){var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,h=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,_=e.length<T,E=e.length>r.length,l=_&&(!h&&T>1||h)?"at least ":"";if(h&&_||!h&&(_||E))throw s=r.length>1,new Error("ArgumentError: "+t+"() takes "+l+T+" argument"+(s?"s":"")+" but received "+e.length);for(var p=0;p<r.length;p+=1){c=!1,a=r[p].types,u=this.getTypeName(e[p]);var f=void 0;for(f=0;f<a.length;f+=1)if(void 0!==u&&this.typeMatches(u,a[f],e[p])){c=!0;break}if(!c&&void 0!==u){var R=a.map((function(t){return o.TYPE_NAME_TABLE[t]})).join(" | ");throw new Error("TypeError: "+t+"() expected argument "+(p+1)+" to be type ("+R+") but received type "+this.TYPE_NAME_TABLE[u]+" instead.")}}},t.prototype.typeMatches=function(t,e,r){if(e===c.TYPE_ANY)return!0;if(e!==c.TYPE_ARRAY_STRING&&e!==c.TYPE_ARRAY_NUMBER&&e!==c.TYPE_ARRAY)return t===e;if(e===c.TYPE_ARRAY)return t===c.TYPE_ARRAY;if(t===c.TYPE_ARRAY){var n=void 0;e===c.TYPE_ARRAY_NUMBER?n=c.TYPE_NUMBER:e===c.TYPE_ARRAY_STRING&&(n=c.TYPE_STRING);for(var i=0;i<r.length;i+=1){var s=this.getTypeName(r[i]);if(void 0!==s&&void 0!==n&&!this.typeMatches(s,n,r[i]))return!1}return!0}return!1},t.prototype.getTypeName=function(t){switch(Object.prototype.toString.call(t)){case"[object String]":return c.TYPE_STRING;case"[object Number]":return c.TYPE_NUMBER;case"[object Array]":return c.TYPE_ARRAY;case"[object Boolean]":return c.TYPE_BOOLEAN;case"[object Null]":return c.TYPE_NULL;case"[object Object]":return t.jmespathType===n.TOK_EXPREF?c.TYPE_EXPREF:c.TYPE_OBJECT;default:return}},t.prototype.createKeyFunction=function(t,e){var r=this;if(this._interpreter){var n=this._interpreter;return function(i){var s=n.visit(t,i);if(!e.includes(r.getTypeName(s))){var o="TypeError: expected one of ("+e.map((function(t){return r.TYPE_NAME_TABLE[t]})).join(" | ")+"), received "+r.TYPE_NAME_TABLE[r.getTypeName(s)];throw new Error(o)}return s}}},t}(),R=new(function(){function t(){this._rootValue=null,this.runtime=new f(this)}return t.prototype.search=function(t,e){return this._rootValue=e,this.visit(t,e)},t.prototype.visit=function(t,r){var a,u,c,T,h,_,E,l,p,f;switch(t.type){case"Field":return null===r?null:i(r)?void 0===(_=r[t.name])?null:_:null;case"Subexpression":for(c=this.visit(t.children[0],r),p=1;p<t.children.length;p+=1)if(null===(c=this.visit(t.children[1],c)))return null;return c;case"IndexExpression":return E=this.visit(t.children[0],r),this.visit(t.children[1],E);case"Index":if(!Array.isArray(r))return null;var R=t.value;return R<0&&(R=r.length+R),void 0===(c=r[R])&&(c=null),c;case"Slice":if(!Array.isArray(r))return null;var O=e(t.children),y=this.computeSliceParams(r.length,O),v=y[0],d=y[1],A=y[2];if(c=[],A>0)for(p=v;p<d;p+=A)c.push(r[p]);else for(p=v;p>d;p+=A)c.push(r[p]);return c;case"Projection":if(f=this.visit(t.children[0],r),!Array.isArray(f))return null;for(l=[],p=0;p<f.length;p+=1)null!==(u=this.visit(t.children[1],f[p]))&&l.push(u);return l;case"ValueProjection":if(f=this.visit(t.children[0],r),!i(f))return null;l=[];var N=Object.values(f);for(p=0;p<N.length;p+=1)null!==(u=this.visit(t.children[1],N[p]))&&l.push(u);return l;case"FilterProjection":if(f=this.visit(t.children[0],r),!Array.isArray(f))return null;var P=[],K=[];for(p=0;p<f.length;p+=1)a=this.visit(t.children[2],f[p]),o(a)||P.push(f[p]);for(var Y=0;Y<P.length;Y+=1)null!==(u=this.visit(t.children[1],P[Y]))&&K.push(u);return K;case"Comparator":switch(T=this.visit(t.children[0],r),h=this.visit(t.children[1],r),t.name){case n.TOK_EQ:c=s(T,h);break;case n.TOK_NE:c=!s(T,h);break;case n.TOK_GT:c=T>h;break;case n.TOK_GTE:c=T>=h;break;case n.TOK_LT:c=T<h;break;case n.TOK_LTE:c=T<=h;break;default:throw new Error("Unknown comparator: "+t.name)}return c;case n.TOK_FLATTEN:var g=this.visit(t.children[0],r);if(!Array.isArray(g))return null;var L=[];for(p=0;p<g.length;p+=1)u=g[p],Array.isArray(u)?L=e(L,u):L.push(u);return L;case"Identity":return r;case"MultiSelectList":if(null===r)return null;for(l=[],p=0;p<t.children.length;p+=1)l.push(this.visit(t.children[p],r));return l;case"MultiSelectHash":if(null===r)return null;l={};var m=void 0;for(p=0;p<t.children.length;p+=1)l[(m=t.children[p]).name]=this.visit(m.value,r);return l;case"OrExpression":return a=this.visit(t.children[0],r),o(a)&&(a=this.visit(t.children[1],r)),a;case"AndExpression":return T=this.visit(t.children[0],r),o(T)?T:this.visit(t.children[1],r);case"NotExpression":return T=this.visit(t.children[0],r),o(T);case"Literal":return t.value;case n.TOK_PIPE:return E=this.visit(t.children[0],r),this.visit(t.children[1],E);case n.TOK_CURRENT:return r;case n.TOK_ROOT:return this._rootValue;case"Function":var I=[];for(Y=0;Y<t.children.length;Y+=1)I.push(this.visit(t.children[Y],r));return this.runtime.callFunction(t.name,I);case"ExpressionReference":var k=t.children[0];return k.jmespathType=n.TOK_EXPREF,k;default:throw new Error("Unknown node type: "+t.type)}},t.prototype.computeSliceParams=function(t,e){var r=e[0],n=e[1],i=e[2];if(null===i)i=1;else if(0===i){var s=new Error("Invalid slice, step cannot be 0");throw s.name="RuntimeError",s}var o=i<0;return[r=null===r?o?t-1:0:this.capSliceRange(t,r,i),n=null===n?o?-1:t:this.capSliceRange(t,n,i),i]},t.prototype.capSliceRange=function(t,e,r){var n=e;return n<0?(n+=t)<0&&(n=r<0?-1:0):n>=t&&(n=r<0?t-1:t),n},t}()),O=c.TYPE_ANY,y=c.TYPE_ARRAY,v=c.TYPE_ARRAY_NUMBER,d=c.TYPE_ARRAY_STRING,A=c.TYPE_BOOLEAN,N=c.TYPE_EXPREF,P=c.TYPE_NULL,K=c.TYPE_NUMBER,Y=c.TYPE_OBJECT,g=c.TYPE_STRING;function L(t){return p.parse(t)}function m(t){return E.tokenize(t)}var I=function(t,e,r){R.runtime.registerFunction(t,e,r)};function k(t,e){var r=p.parse(e);return R.search(r,t)}var B=R,S={compile:L,registerFunction:I,search:k,tokenize:m,TreeInterpreter:B,TYPE_ANY:O,TYPE_ARRAY_NUMBER:v,TYPE_ARRAY_STRING:d,TYPE_ARRAY:y,TYPE_BOOLEAN:A,TYPE_EXPREF:N,TYPE_NULL:P,TYPE_NUMBER:K,TYPE_OBJECT:Y,TYPE_STRING:g};t.TYPE_ANY=O,t.TYPE_ARRAY=y,t.TYPE_ARRAY_NUMBER=v,t.TYPE_ARRAY_STRING=d,t.TYPE_BOOLEAN=A,t.TYPE_EXPREF=N,t.TYPE_NULL=P,t.TYPE_NUMBER=K,t.TYPE_OBJECT=Y,t.TYPE_STRING=g,t.TreeInterpreter=B,t.compile=L,t.default=S,t.jmespath=S,t.registerFunction=I,t.search=k,t.tokenize=m,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jmespath={})}(this,(function(t){"use strict";const e=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),r=(t,n)=>{if(t===n)return!0;if(typeof t!=typeof n)return!1;if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(let e=0;e<t.length;e+=1)if(!r(t[e],n[e]))return!1;return!0}if(e(t)&&e(n)){const e=Object.entries(t),i=new Set(Object.keys(n));if(e.length!==i.size)return!1;for(const[t,s]of e){if(!r(s,n[t]))return!1;i.delete(t)}return 0===i.size}return!1},n=t=>{if(""===t||!1===t||null==t)return!0;if(Array.isArray(t)&&0===t.length)return!0;if(e(t)){for(const e in t)if(t.hasOwnProperty(e))return!1;return!0}return!1},i=t=>t>="0"&&t<="9"||"-"===t;var s;!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(s||(s={}));const o={"(":s.TOK_LPAREN,")":s.TOK_RPAREN,"*":s.TOK_STAR,",":s.TOK_COMMA,".":s.TOK_DOT,":":s.TOK_COLON,"@":s.TOK_CURRENT,$:s.TOK_ROOT,"]":s.TOK_RBRACKET,"{":s.TOK_LBRACE,"}":s.TOK_RBRACE},c={"!":!0,"<":!0,"=":!0,">":!0},h={"\t":!0,"\n":!0,"\r":!0," ":!0};const u=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,n,u;for(this._current=0;this._current<t.length;)if((a=t[this._current])>="a"&&a<="z"||a>="A"&&a<="Z"||"_"===a)r=this._current,n=this.consumeUnquotedIdentifier(t),e.push({start:r,type:s.TOK_UNQUOTEDIDENTIFIER,value:n});else if(void 0!==o[t[this._current]])e.push({start:this._current,type:o[t[this._current]],value:t[this._current]}),this._current+=1;else if(i(t[this._current]))u=this.consumeNumber(t),e.push(u);else if("["===t[this._current])u=this.consumeLBracket(t),e.push(u);else if('"'===t[this._current])r=this._current,n=this.consumeQuotedIdentifier(t),e.push({start:r,type:s.TOK_QUOTEDIDENTIFIER,value:n});else if("'"===t[this._current])r=this._current,n=this.consumeRawStringLiteral(t),e.push({start:r,type:s.TOK_LITERAL,value:n});else if("`"===t[this._current]){r=this._current;const n=this.consumeLiteral(t);e.push({start:r,type:s.TOK_LITERAL,value:n})}else if(void 0!==c[t[this._current]])u=this.consumeOperator(t),u&&e.push(u);else if(void 0!==h[t[this._current]])this._current+=1;else if("&"===t[this._current])r=this._current,this._current+=1,"&"===t[this._current]?(this._current+=1,e.push({start:r,type:s.TOK_AND,value:"&&"})):e.push({start:r,type:s.TOK_EXPREF,value:"&"});else{if("|"!==t[this._current]){const e=new Error(`Unknown character: ${t[this._current]}`);throw e.name="LexerError",e}r=this._current,this._current+=1,"|"===t[this._current]?(this._current+=1,e.push({start:r,type:s.TOK_OR,value:"||"})):e.push({start:r,type:s.TOK_PIPE,value:"|"})}var a;return e}consumeUnquotedIdentifier(t){const e=this._current;for(this._current+=1;this._current<t.length&&((r=t[this._current])>="a"&&r<="z"||r>="A"&&r<="Z"||r>="0"&&r<="9"||"_"===r);)this._current+=1;var r;return t.slice(e,this._current)}consumeQuotedIdentifier(t){const e=this._current;this._current+=1;const r=t.length;for(;'"'!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&'"'!==t[e+1]?e+=1:e+=2,this._current=e}return this._current+=1,JSON.parse(t.slice(e,this._current))}consumeRawStringLiteral(t){const e=this._current;this._current+=1;const r=t.length;for(;"'"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"'"!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;return t.slice(e+1,this._current-1).replace("\\'","'")}consumeNumber(t){const e=this._current;this._current+=1;const r=t.length;for(;i(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:s.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:s.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:s.TOK_FLATTEN,value:"[]"}):{start:e,type:s.TOK_LBRACKET,value:"["}}consumeOperator(t){const e=this._current,r=t[e];return this._current+=1,"!"===r?"="===t[this._current]?(this._current+=1,{start:e,type:s.TOK_NE,value:"!="}):{start:e,type:s.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:s.TOK_LTE,value:"<="}):{start:e,type:s.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:s.TOK_GTE,value:">="}):{start:e,type:s.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:s.TOK_EQ,value:"=="}):void 0}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimLeft();n=n.replace("\\`","`");const i=this.looksLikeJSON(n)?JSON.parse(n):JSON.parse(`"${n}"`);return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0]))try{return JSON.parse(t),!0}catch(t){return!1}return!1}},a={[s.TOK_EOF]:0,[s.TOK_UNQUOTEDIDENTIFIER]:0,[s.TOK_QUOTEDIDENTIFIER]:0,[s.TOK_RBRACKET]:0,[s.TOK_RPAREN]:0,[s.TOK_COMMA]:0,[s.TOK_RBRACE]:0,[s.TOK_NUMBER]:0,[s.TOK_CURRENT]:0,[s.TOK_EXPREF]:0,[s.TOK_ROOT]:0,[s.TOK_PIPE]:1,[s.TOK_OR]:2,[s.TOK_AND]:3,[s.TOK_EQ]:5,[s.TOK_GT]:5,[s.TOK_LT]:5,[s.TOK_GTE]:5,[s.TOK_LTE]:5,[s.TOK_NE]:5,[s.TOK_FLATTEN]:9,[s.TOK_STAR]:20,[s.TOK_FILTER]:21,[s.TOK_DOT]:40,[s.TOK_NOT]:45,[s.TOK_LBRACE]:50,[s.TOK_LBRACKET]:55,[s.TOK_LPAREN]:60};const T=new class{constructor(){this.index=0,this.tokens=[]}parse(t){this.loadTokens(t),this.index=0;const e=this.expression(0);if(this.lookahead(0)!==s.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Unexpected token type: ${t.type}, value: ${t.value}`)}return e}loadTokens(t){this.tokens=[...u.tokenize(t),{type:s.TOK_EOF,value:"",start:t.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<a[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r}lookahead(t){return this.tokens[this.index+t].type}lookaheadToken(t){return this.tokens[this.index+t]}advance(){this.index+=1}nud(t){let e,r,n;switch(t.type){case s.TOK_LITERAL:return{type:"Literal",value:t.value};case s.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case s.TOK_QUOTEDIDENTIFIER:const i={type:"Field",name:t.value};if(this.lookahead(0)===s.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return i;case s.TOK_NOT:return r=this.expression(a.Not),{type:"NotExpression",children:[r]};case s.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===s.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(a.Star),{type:"ValueProjection",children:[e,r]};case s.TOK_FILTER:return this.led(t.type,{type:"Identity"});case s.TOK_LBRACE:return this.parseMultiselectHash();case s.TOK_FLATTEN:return e={type:s.TOK_FLATTEN,children:[{type:"Identity"}]},r=this.parseProjectionRHS(a.Flatten),{type:"Projection",children:[e,r]};case s.TOK_LBRACKET:return this.lookahead(0)===s.TOK_NUMBER||this.lookahead(0)===s.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},r)):this.lookahead(0)===s.TOK_STAR&&this.lookahead(1)===s.TOK_RBRACKET?(this.advance(),this.advance(),r=this.parseProjectionRHS(a.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case s.TOK_CURRENT:return{type:s.TOK_CURRENT};case s.TOK_ROOT:return{type:s.TOK_ROOT};case s.TOK_EXPREF:return n=this.expression(a.Expref),{type:"ExpressionReference",children:[n]};case s.TOK_LPAREN:const o=[];for(;this.lookahead(0)!==s.TOK_RPAREN;)this.lookahead(0)===s.TOK_CURRENT?(n={type:s.TOK_CURRENT},this.advance()):n=this.expression(0),o.push(n);return this.match(s.TOK_RPAREN),o[0];default:this.errorToken(t)}}led(t,e){let r;switch(t){case s.TOK_DOT:const n=a.Dot;return this.lookahead(0)!==s.TOK_STAR?(r=this.parseDotRHS(n),{type:"Subexpression",children:[e,r]}):(this.advance(),r=this.parseProjectionRHS(n),{type:"ValueProjection",children:[e,r]});case s.TOK_PIPE:return r=this.expression(a.Pipe),{type:s.TOK_PIPE,children:[e,r]};case s.TOK_OR:return r=this.expression(a.Or),{type:"OrExpression",children:[e,r]};case s.TOK_AND:return r=this.expression(a.And),{type:"AndExpression",children:[e,r]};case s.TOK_LPAREN:const i=e.name,o=[];let c;for(;this.lookahead(0)!==s.TOK_RPAREN;)this.lookahead(0)===s.TOK_CURRENT?(c={type:s.TOK_CURRENT},this.advance()):c=this.expression(0),this.lookahead(0)===s.TOK_COMMA&&this.match(s.TOK_COMMA),o.push(c);this.match(s.TOK_RPAREN);return{name:i,type:"Function",children:o};case s.TOK_FILTER:const h=this.expression(0);return this.match(s.TOK_RBRACKET),r=this.lookahead(0)===s.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(a.Filter),{type:"FilterProjection",children:[e,r,h]};case s.TOK_FLATTEN:return{type:"Projection",children:[{type:s.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(a.Flatten)]};case s.TOK_EQ:case s.TOK_NE:case s.TOK_GT:case s.TOK_GTE:case s.TOK_LT:case s.TOK_LTE:return this.parseComparator(e,t);case s.TOK_LBRACKET:const u=this.lookaheadToken(0);return u.type===s.TOK_NUMBER||u.type===s.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(s.TOK_STAR),this.match(s.TOK_RBRACKET),r=this.parseProjectionRHS(a.Star),{type:"Projection",children:[e,r]});default:return this.errorToken(this.lookaheadToken(0))}}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===s.TOK_COLON||this.lookahead(1)===s.TOK_COLON)return this.parseSliceExpression();const t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(s.TOK_RBRACKET),t}projectIfSlice(t,e){const r={type:"IndexExpression",children:[t,e]};return"Slice"===e.type?{children:[r,this.parseProjectionRHS(a.Star)],type:"Projection"}:r}parseSliceExpression(){const t=[null,null,null];let e=0,r=this.lookahead(0);for(;r!==s.TOK_RBRACKET&&e<3;){if(r===s.TOK_COLON)e+=1,this.advance();else if(r===s.TOK_NUMBER)t[e]=this.lookaheadToken(0).value,this.advance();else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}r=this.lookahead(0)}return this.match(s.TOK_RBRACKET),{children:t,type:"Slice"}}parseComparator(t,e){return{type:"Comparator",name:e,children:[t,this.expression(a[e])]}}parseDotRHS(t){const e=this.lookahead(0);if([s.TOK_UNQUOTEDIDENTIFIER,s.TOK_QUOTEDIDENTIFIER,s.TOK_STAR].includes(e))return this.expression(t);if(e===s.TOK_LBRACKET)return this.match(s.TOK_LBRACKET),this.parseMultiselectList();if(e===s.TOK_LBRACE)return this.match(s.TOK_LBRACE),this.parseMultiselectHash();const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseProjectionRHS(t){if(a[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===s.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===s.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===s.TOK_DOT)return this.match(s.TOK_DOT),this.parseDotRHS(t);const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error, unexpected token: ${e.value}(${e.type})`)}parseMultiselectList(){const t=[];for(;this.lookahead(0)!==s.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===s.TOK_COMMA&&(this.match(s.TOK_COMMA),this.lookahead(0)===s.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(s.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[s.TOK_UNQUOTEDIDENTIFIER,s.TOK_QUOTEDIDENTIFIER];let r,n,i;for(;;){if(r=this.lookaheadToken(0),!e.includes(r.type))throw new Error(`Expecting an identifier token, got: ${r.type}`);if(n=r.value,this.advance(),this.match(s.TOK_COLON),i=this.expression(0),t.push({value:i,type:"KeyValuePair",name:n}),this.lookahead(0)===s.TOK_COMMA)this.match(s.TOK_COMMA);else if(this.lookahead(0)===s.TOK_RBRACE){this.match(s.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}};var _;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING"}(_||(_={}));class l{constructor(t){this.TYPE_NAME_TABLE={[_.TYPE_NUMBER]:"number",[_.TYPE_ANY]:"any",[_.TYPE_STRING]:"string",[_.TYPE_ARRAY]:"array",[_.TYPE_OBJECT]:"object",[_.TYPE_BOOLEAN]:"boolean",[_.TYPE_EXPREF]:"expression",[_.TYPE_NULL]:"null",[_.TYPE_ARRAY_NUMBER]:"Array<number>",[_.TYPE_ARRAY_STRING]:"Array<string>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{let e=0;for(let r=0;r<t.length;r+=1)e+=t[r];return e/t.length},this.functionCeil=([t])=>Math.ceil(t),this.functionContains=t=>{const[e,r]=t;return e.includes(r)},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFloor=([t])=>Math.floor(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([t])=>e(t)?Object.keys(t).length:t.length,this.functionMap=t=>{if(!this._interpreter)return[];const e=[],r=this._interpreter,n=t[0],i=t[1];for(let t=0;t<i.length;t+=1)e.push(r.visit(n,i[t]));return e},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===_.TYPE_NUMBER)return Math.max(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)r.localeCompare(e[t])<0&&(r=e[t]);return r},this.functionMaxBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[_.TYPE_NUMBER,_.TYPE_STRING]);let i,s,o=-1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s>o&&(o=s,i=r[t]);return i},this.functionMerge=t=>{let e={};for(let r=0;r<t.length;r+=1){const n=t[r];e=Object.assign(e,n)}return e},this.functionMin=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===_.TYPE_NUMBER)return Math.min(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)e[t].localeCompare(r)<0&&(r=e[t]);return r},this.functionMinBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[_.TYPE_NUMBER,_.TYPE_STRING]);let i,s,o=1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s<o&&(o=s,i=r[t]);return i},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==_.TYPE_NULL)return t[e];return null},this.functionReverse=([t])=>{if(this.getTypeName(t)===_.TYPE_STRING){const e=t;let r="";for(let t=e.length-1;t>=0;t-=1)r+=e[t];return r}const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>[...t].sort(),this.functionSortBy=t=>{if(!this._interpreter)return[];const e=t[0].slice(0);if(0===e.length)return e;const r=this._interpreter,n=t[1],i=this.getTypeName(r.visit(n,e[0]));if(void 0!==i&&![_.TYPE_NUMBER,_.TYPE_STRING].includes(i))throw new Error(`TypeError: unexpected type (${this.TYPE_NAME_TABLE[i]})`);const s=[];for(let t=0;t<e.length;t+=1)s.push([t,e[t]]);s.sort(((t,e)=>{const s=r.visit(n,t[1]),o=r.visit(n,e[1]);if(this.getTypeName(s)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(s)]}`);if(this.getTypeName(o)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(o)]}`);return s>o?1:s<o?-1:t[0]-e[0]}));for(let t=0;t<s.length;t+=1)e[t]=s[t][1];return e},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===_.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===_.TYPE_NUMBER?t:e!==_.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===_.TYPE_STRING?t:JSON.stringify(t),this.functionType=([t])=>{switch(this.getTypeName(t)){case _.TYPE_NUMBER:return"number";case _.TYPE_STRING:return"string";case _.TYPE_ARRAY:return"array";case _.TYPE_OBJECT:return"object";case _.TYPE_BOOLEAN:return"boolean";case _.TYPE_EXPREF:return"expref";case _.TYPE_NULL:return"null";default:return}},this.functionValues=([t])=>Object.values(t),this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[_.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[_.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[_.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[_.TYPE_STRING,_.TYPE_ARRAY]},{types:[_.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[_.TYPE_STRING]},{types:[_.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[_.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[_.TYPE_STRING]},{types:[_.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[_.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[_.TYPE_STRING,_.TYPE_ARRAY,_.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[_.TYPE_EXPREF]},{types:[_.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[_.TYPE_ARRAY_NUMBER,_.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[_.TYPE_ARRAY]},{types:[_.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[_.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[_.TYPE_ARRAY_NUMBER,_.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[_.TYPE_ARRAY]},{types:[_.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[_.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[_.TYPE_STRING,_.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[_.TYPE_ARRAY_STRING,_.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[_.TYPE_ARRAY]},{types:[_.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[_.TYPE_STRING]},{types:[_.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[_.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[_.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[_.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[_.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[_.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[_.TYPE_OBJECT]}]}},this._interpreter=t}registerFunction(t,e,r){if(t in this.functionTable)throw new Error(`Function already defined: ${t}()`);this.functionTable[t]={_func:e.bind(this),_signature:r}}callFunction(t,e){const r=this.functionTable[t];if(void 0===r)throw new Error(`Unknown function: ${t}()`);return this.validateArgs(t,e,r._signature),r._func.call(this,e)}validateInputSignatures(t,e){for(let r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error(`ArgumentError: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,c=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,h=e.length<o,u=e.length>r.length,a=h&&(!c&&o>1||c)?"at least ":"";if(c&&h||!c&&(h||u))throw s=r.length>1,new Error(`ArgumentError: ${t}() takes ${a}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`TypeError: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===_.TYPE_ANY)return!0;if(e!==_.TYPE_ARRAY_STRING&&e!==_.TYPE_ARRAY_NUMBER&&e!==_.TYPE_ARRAY)return t===e;if(e===_.TYPE_ARRAY)return t===_.TYPE_ARRAY;if(t===_.TYPE_ARRAY){let t;e===_.TYPE_ARRAY_NUMBER?t=_.TYPE_NUMBER:e===_.TYPE_ARRAY_STRING&&(t=_.TYPE_STRING);for(let e=0;e<r.length;e+=1){const n=this.getTypeName(r[e]);if(void 0!==n&&void 0!==t&&!this.typeMatches(n,t,r[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return _.TYPE_STRING;case"[object Number]":return _.TYPE_NUMBER;case"[object Array]":return _.TYPE_ARRAY;case"[object Boolean]":return _.TYPE_BOOLEAN;case"[object Null]":return _.TYPE_NULL;case"[object Object]":return t.jmespathType===s.TOK_EXPREF?_.TYPE_EXPREF:_.TYPE_OBJECT;default:return}}createKeyFunction(t,e){if(!this._interpreter)return;const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`TypeError: expected one of (${e.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(i)]}`;throw new Error(t)}return i}}}const E=new class{constructor(){this._rootValue=null,this.runtime=new l(this)}search(t,e){return this._rootValue=e,this.visit(t,e)}visit(t,i){let o,c,h,u,a,T,_,l,E,R,p;switch(t.type){case"Field":return null===i?null:e(i)?(T=i[t.name],void 0===T?null:T):null;case"Subexpression":for(h=this.visit(t.children[0],i),R=1;R<t.children.length;R+=1)if(h=this.visit(t.children[1],h),null===h)return null;return h;case"IndexExpression":return _=this.visit(t.children[0],i),l=this.visit(t.children[1],_),l;case"Index":if(!Array.isArray(i))return null;let O=t.value;return O<0&&(O=i.length+O),h=i[O],void 0===h&&(h=null),h;case"Slice":if(!Array.isArray(i))return null;const f=[...t.children],A=this.computeSliceParams(i.length,f),[d,N,P]=A;if(h=[],P>0)for(R=d;R<N;R+=P)h.push(i[R]);else for(R=d;R>N;R+=P)h.push(i[R]);return h;case"Projection":if(p=this.visit(t.children[0],i),!Array.isArray(p))return null;for(E=[],R=0;R<p.length;R+=1)c=this.visit(t.children[1],p[R]),null!==c&&E.push(c);return E;case"ValueProjection":if(p=this.visit(t.children[0],i),!e(p))return null;E=[];const y=Object.values(p);for(R=0;R<y.length;R+=1)c=this.visit(t.children[1],y[R]),null!==c&&E.push(c);return E;case"FilterProjection":if(p=this.visit(t.children[0],i),!Array.isArray(p))return null;const K=[],Y=[];for(R=0;R<p.length;R+=1)o=this.visit(t.children[2],p[R]),n(o)||K.push(p[R]);for(let e=0;e<K.length;e+=1)c=this.visit(t.children[1],K[e]),null!==c&&Y.push(c);return Y;case"Comparator":switch(u=this.visit(t.children[0],i),a=this.visit(t.children[1],i),t.name){case s.TOK_EQ:h=r(u,a);break;case s.TOK_NE:h=!r(u,a);break;case s.TOK_GT:h=u>a;break;case s.TOK_GTE:h=u>=a;break;case s.TOK_LT:h=u<a;break;case s.TOK_LTE:h=u<=a;break;default:throw new Error(`Unknown comparator: ${t.name}`)}return h;case s.TOK_FLATTEN:const g=this.visit(t.children[0],i);if(!Array.isArray(g))return null;let v=[];for(R=0;R<g.length;R+=1)c=g[R],Array.isArray(c)?v=[...v,...c]:v.push(c);return v;case"Identity":return i;case"MultiSelectList":if(null===i)return null;for(E=[],R=0;R<t.children.length;R+=1)E.push(this.visit(t.children[R],i));return E;case"MultiSelectHash":if(null===i)return null;let L;for(E={},R=0;R<t.children.length;R+=1)L=t.children[R],E[L.name]=this.visit(L.value,i);return E;case"OrExpression":return o=this.visit(t.children[0],i),n(o)&&(o=this.visit(t.children[1],i)),o;case"AndExpression":return u=this.visit(t.children[0],i),n(u)?u:this.visit(t.children[1],i);case"NotExpression":return u=this.visit(t.children[0],i),n(u);case"Literal":return t.value;case s.TOK_PIPE:return _=this.visit(t.children[0],i),this.visit(t.children[1],_);case s.TOK_CURRENT:return i;case s.TOK_ROOT:return this._rootValue;case"Function":const m=[];for(let e=0;e<t.children.length;e+=1)m.push(this.visit(t.children[e],i));return this.runtime.callFunction(t.name,m);case"ExpressionReference":const I=t.children[0];return I.jmespathType=s.TOK_EXPREF,I;default:throw new Error(`Unknown node type: ${t.type}`)}}computeSliceParams(t,e){let[r,n,i]=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid slice, step cannot be 0");throw t.name="RuntimeError",t}const s=i<0;return r=null===r?s?t-1:0:this.capSliceRange(t,r,i),n=null===n?s?-1:t:this.capSliceRange(t,n,i),[r,n,i]}capSliceRange(t,e,r){let n=e;return n<0?(n+=t,n<0&&(n=r<0?-1:0)):n>=t&&(n=r<0?t-1:t),n}},R=_.TYPE_ANY,p=_.TYPE_ARRAY,O=_.TYPE_ARRAY_NUMBER,f=_.TYPE_ARRAY_STRING,A=_.TYPE_BOOLEAN,d=_.TYPE_EXPREF,N=_.TYPE_NULL,P=_.TYPE_NUMBER,y=_.TYPE_OBJECT,K=_.TYPE_STRING;function Y(t){return T.parse(t)}function g(t){return u.tokenize(t)}const v=(t,e,r)=>{E.runtime.registerFunction(t,e,r)};function L(t,e){const r=T.parse(e);return E.search(r,t)}const m=E,I={compile:Y,registerFunction:v,search:L,tokenize:g,TreeInterpreter:m,TYPE_ANY:R,TYPE_ARRAY_NUMBER:O,TYPE_ARRAY_STRING:f,TYPE_ARRAY:p,TYPE_BOOLEAN:A,TYPE_EXPREF:d,TYPE_NULL:N,TYPE_NUMBER:P,TYPE_OBJECT:y,TYPE_STRING:K};t.TYPE_ANY=R,t.TYPE_ARRAY=p,t.TYPE_ARRAY_NUMBER=O,t.TYPE_ARRAY_STRING=f,t.TYPE_BOOLEAN=A,t.TYPE_EXPREF=d,t.TYPE_NULL=N,t.TYPE_NUMBER=P,t.TYPE_OBJECT=y,t.TYPE_STRING=K,t.TreeInterpreter=m,t.compile=Y,t.default=I,t.jmespath=I,t.registerFunction=v,t.search=L,t.tokenize=g,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=jmespath.umd.min.js.map |
@@ -7,6 +7,6 @@ "use strict"; | ||
exports.jmespath = exports.TreeInterpreter = exports.search = exports.registerFunction = exports.tokenize = exports.compile = exports.TYPE_STRING = exports.TYPE_OBJECT = exports.TYPE_NUMBER = exports.TYPE_NULL = exports.TYPE_EXPREF = exports.TYPE_BOOLEAN = exports.TYPE_ARRAY_STRING = exports.TYPE_ARRAY_NUMBER = exports.TYPE_ARRAY = exports.TYPE_ANY = void 0; | ||
var Parser_1 = __importDefault(require("./Parser")); | ||
var Lexer_1 = __importDefault(require("./Lexer")); | ||
var TreeInterpreter_1 = __importDefault(require("./TreeInterpreter")); | ||
var Runtime_1 = require("./Runtime"); | ||
const Parser_1 = __importDefault(require("./Parser")); | ||
const Lexer_1 = __importDefault(require("./Lexer")); | ||
const TreeInterpreter_1 = __importDefault(require("./TreeInterpreter")); | ||
const Runtime_1 = require("./Runtime"); | ||
exports.TYPE_ANY = Runtime_1.InputArgument.TYPE_ANY; | ||
@@ -23,3 +23,3 @@ exports.TYPE_ARRAY = Runtime_1.InputArgument.TYPE_ARRAY; | ||
function compile(expression) { | ||
var nodeTree = Parser_1.default.parse(expression); | ||
const nodeTree = Parser_1.default.parse(expression); | ||
return nodeTree; | ||
@@ -32,3 +32,3 @@ } | ||
exports.tokenize = tokenize; | ||
var registerFunction = function (functionName, customFunction, signature) { | ||
const registerFunction = (functionName, customFunction, signature) => { | ||
TreeInterpreter_1.default.runtime.registerFunction(functionName, customFunction, signature); | ||
@@ -38,3 +38,3 @@ }; | ||
function search(data, expression) { | ||
var nodeTree = Parser_1.default.parse(expression); | ||
const nodeTree = Parser_1.default.parse(expression); | ||
return TreeInterpreter_1.default.search(nodeTree, data); | ||
@@ -45,6 +45,6 @@ } | ||
exports.jmespath = { | ||
compile: compile, | ||
compile, | ||
registerFunction: exports.registerFunction, | ||
search: search, | ||
tokenize: tokenize, | ||
search, | ||
tokenize, | ||
TreeInterpreter: exports.TreeInterpreter, | ||
@@ -51,0 +51,0 @@ TYPE_ANY: exports.TYPE_ANY, |
"use strict"; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Lexer = exports.basicTokens = exports.Token = void 0; | ||
var index_1 = require("./utils/index"); | ||
const index_1 = require("./utils/index"); | ||
var Token; | ||
@@ -39,17 +38,16 @@ (function (Token) { | ||
})(Token = exports.Token || (exports.Token = {})); | ||
exports.basicTokens = (_a = { | ||
'(': Token.TOK_LPAREN, | ||
')': Token.TOK_RPAREN, | ||
'*': Token.TOK_STAR, | ||
',': Token.TOK_COMMA, | ||
'.': Token.TOK_DOT, | ||
':': Token.TOK_COLON, | ||
'@': Token.TOK_CURRENT | ||
}, | ||
_a['$'] = Token.TOK_ROOT, | ||
_a[']'] = Token.TOK_RBRACKET, | ||
_a['{'] = Token.TOK_LBRACE, | ||
_a['}'] = Token.TOK_RBRACE, | ||
_a); | ||
var operatorStartToken = { | ||
exports.basicTokens = { | ||
'(': Token.TOK_LPAREN, | ||
')': Token.TOK_RPAREN, | ||
'*': Token.TOK_STAR, | ||
',': Token.TOK_COMMA, | ||
'.': Token.TOK_DOT, | ||
':': Token.TOK_COLON, | ||
'@': Token.TOK_CURRENT, | ||
['$']: Token.TOK_ROOT, | ||
']': Token.TOK_RBRACKET, | ||
'{': Token.TOK_LBRACE, | ||
'}': Token.TOK_RBRACE, | ||
}; | ||
const operatorStartToken = { | ||
'!': true, | ||
@@ -60,3 +58,3 @@ '<': true, | ||
}; | ||
var skipChars = { | ||
const skipChars = { | ||
'\t': true, | ||
@@ -67,12 +65,12 @@ '\n': true, | ||
}; | ||
var StreamLexer = /** @class */ (function () { | ||
function StreamLexer() { | ||
class StreamLexer { | ||
constructor() { | ||
this._current = 0; | ||
} | ||
StreamLexer.prototype.tokenize = function (stream) { | ||
var tokens = []; | ||
tokenize(stream) { | ||
const tokens = []; | ||
this._current = 0; | ||
var start; | ||
var identifier; | ||
var token; | ||
let start; | ||
let identifier; | ||
let token; | ||
while (this._current < stream.length) { | ||
@@ -83,3 +81,3 @@ if (index_1.isAlpha(stream[this._current])) { | ||
tokens.push({ | ||
start: start, | ||
start, | ||
type: Token.TOK_UNQUOTEDIDENTIFIER, | ||
@@ -109,3 +107,3 @@ value: identifier, | ||
tokens.push({ | ||
start: start, | ||
start, | ||
type: Token.TOK_QUOTEDIDENTIFIER, | ||
@@ -115,7 +113,7 @@ value: identifier, | ||
} | ||
else if (stream[this._current] === "'") { | ||
else if (stream[this._current] === `'`) { | ||
start = this._current; | ||
identifier = this.consumeRawStringLiteral(stream); | ||
tokens.push({ | ||
start: start, | ||
start, | ||
type: Token.TOK_LITERAL, | ||
@@ -127,5 +125,5 @@ value: identifier, | ||
start = this._current; | ||
var literal = this.consumeLiteral(stream); | ||
const literal = this.consumeLiteral(stream); | ||
tokens.push({ | ||
start: start, | ||
start, | ||
type: Token.TOK_LITERAL, | ||
@@ -147,6 +145,6 @@ value: literal, | ||
this._current += 1; | ||
tokens.push({ start: start, type: Token.TOK_AND, value: '&&' }); | ||
tokens.push({ start, type: Token.TOK_AND, value: '&&' }); | ||
} | ||
else { | ||
tokens.push({ start: start, type: Token.TOK_EXPREF, value: '&' }); | ||
tokens.push({ start, type: Token.TOK_EXPREF, value: '&' }); | ||
} | ||
@@ -159,10 +157,10 @@ } | ||
this._current += 1; | ||
tokens.push({ start: start, type: Token.TOK_OR, value: '||' }); | ||
tokens.push({ start, type: Token.TOK_OR, value: '||' }); | ||
} | ||
else { | ||
tokens.push({ start: start, type: Token.TOK_PIPE, value: '|' }); | ||
tokens.push({ start, type: Token.TOK_PIPE, value: '|' }); | ||
} | ||
} | ||
else { | ||
var error = new Error("Unknown character: " + stream[this._current]); | ||
const error = new Error(`Unknown character: ${stream[this._current]}`); | ||
error.name = 'LexerError'; | ||
@@ -173,5 +171,5 @@ throw error; | ||
return tokens; | ||
}; | ||
StreamLexer.prototype.consumeUnquotedIdentifier = function (stream) { | ||
var start = this._current; | ||
} | ||
consumeUnquotedIdentifier(stream) { | ||
const start = this._current; | ||
this._current += 1; | ||
@@ -182,9 +180,9 @@ while (this._current < stream.length && index_1.isAlphaNum(stream[this._current])) { | ||
return stream.slice(start, this._current); | ||
}; | ||
StreamLexer.prototype.consumeQuotedIdentifier = function (stream) { | ||
var start = this._current; | ||
} | ||
consumeQuotedIdentifier(stream) { | ||
const start = this._current; | ||
this._current += 1; | ||
var maxLength = stream.length; | ||
const maxLength = stream.length; | ||
while (stream[this._current] !== '"' && this._current < maxLength) { | ||
var current = this._current; | ||
let current = this._current; | ||
if (stream[current] === '\\' && (stream[current + 1] === '\\' || stream[current + 1] === '"')) { | ||
@@ -200,10 +198,10 @@ current += 2; | ||
return JSON.parse(stream.slice(start, this._current)); | ||
}; | ||
StreamLexer.prototype.consumeRawStringLiteral = function (stream) { | ||
var start = this._current; | ||
} | ||
consumeRawStringLiteral(stream) { | ||
const start = this._current; | ||
this._current += 1; | ||
var maxLength = stream.length; | ||
while (stream[this._current] !== "'" && this._current < maxLength) { | ||
var current = this._current; | ||
if (stream[current] === '\\' && (stream[current + 1] === '\\' || stream[current + 1] === "'")) { | ||
const maxLength = stream.length; | ||
while (stream[this._current] !== `'` && this._current < maxLength) { | ||
let current = this._current; | ||
if (stream[current] === '\\' && (stream[current + 1] === '\\' || stream[current + 1] === `'`)) { | ||
current += 2; | ||
@@ -217,31 +215,31 @@ } | ||
this._current += 1; | ||
var literal = stream.slice(start + 1, this._current - 1); | ||
return literal.replace("\\'", "'"); | ||
}; | ||
StreamLexer.prototype.consumeNumber = function (stream) { | ||
var start = this._current; | ||
const literal = stream.slice(start + 1, this._current - 1); | ||
return literal.replace(`\\'`, `'`); | ||
} | ||
consumeNumber(stream) { | ||
const start = this._current; | ||
this._current += 1; | ||
var maxLength = stream.length; | ||
const maxLength = stream.length; | ||
while (index_1.isNum(stream[this._current]) && this._current < maxLength) { | ||
this._current += 1; | ||
} | ||
var value = parseInt(stream.slice(start, this._current), 10); | ||
return { start: start, value: value, type: Token.TOK_NUMBER }; | ||
}; | ||
StreamLexer.prototype.consumeLBracket = function (stream) { | ||
var start = this._current; | ||
const value = parseInt(stream.slice(start, this._current), 10); | ||
return { start, value, type: Token.TOK_NUMBER }; | ||
} | ||
consumeLBracket(stream) { | ||
const start = this._current; | ||
this._current += 1; | ||
if (stream[this._current] === '?') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_FILTER, value: '[?' }; | ||
return { start, type: Token.TOK_FILTER, value: '[?' }; | ||
} | ||
if (stream[this._current] === ']') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_FLATTEN, value: '[]' }; | ||
return { start, type: Token.TOK_FLATTEN, value: '[]' }; | ||
} | ||
return { start: start, type: Token.TOK_LBRACKET, value: '[' }; | ||
}; | ||
StreamLexer.prototype.consumeOperator = function (stream) { | ||
var start = this._current; | ||
var startingChar = stream[start]; | ||
return { start, type: Token.TOK_LBRACKET, value: '[' }; | ||
} | ||
consumeOperator(stream) { | ||
const start = this._current; | ||
const startingChar = stream[start]; | ||
this._current += 1; | ||
@@ -251,5 +249,5 @@ if (startingChar === '!') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_NE, value: '!=' }; | ||
return { start, type: Token.TOK_NE, value: '!=' }; | ||
} | ||
return { start: start, type: Token.TOK_NOT, value: '!' }; | ||
return { start, type: Token.TOK_NOT, value: '!' }; | ||
} | ||
@@ -259,5 +257,5 @@ if (startingChar === '<') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_LTE, value: '<=' }; | ||
return { start, type: Token.TOK_LTE, value: '<=' }; | ||
} | ||
return { start: start, type: Token.TOK_LT, value: '<' }; | ||
return { start, type: Token.TOK_LT, value: '<' }; | ||
} | ||
@@ -267,17 +265,17 @@ if (startingChar === '>') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_GTE, value: '>=' }; | ||
return { start, type: Token.TOK_GTE, value: '>=' }; | ||
} | ||
return { start: start, type: Token.TOK_GT, value: '>' }; | ||
return { start, type: Token.TOK_GT, value: '>' }; | ||
} | ||
if (startingChar === '=' && stream[this._current] === '=') { | ||
this._current += 1; | ||
return { start: start, type: Token.TOK_EQ, value: '==' }; | ||
return { start, type: Token.TOK_EQ, value: '==' }; | ||
} | ||
}; | ||
StreamLexer.prototype.consumeLiteral = function (stream) { | ||
} | ||
consumeLiteral(stream) { | ||
this._current += 1; | ||
var start = this._current; | ||
var maxLength = stream.length; | ||
const start = this._current; | ||
const maxLength = stream.length; | ||
while (stream[this._current] !== '`' && this._current < maxLength) { | ||
var current = this._current; | ||
let current = this._current; | ||
if (stream[current] === '\\' && (stream[current + 1] === '\\' || stream[current + 1] === '`')) { | ||
@@ -291,14 +289,14 @@ current += 2; | ||
} | ||
var literalString = stream.slice(start, this._current).trimLeft(); | ||
let literalString = stream.slice(start, this._current).trimLeft(); | ||
literalString = literalString.replace('\\`', '`'); | ||
var literal = this.looksLikeJSON(literalString) | ||
const literal = this.looksLikeJSON(literalString) | ||
? JSON.parse(literalString) | ||
: JSON.parse("\"" + literalString + "\""); | ||
: JSON.parse(`"${literalString}"`); | ||
this._current += 1; | ||
return literal; | ||
}; | ||
StreamLexer.prototype.looksLikeJSON = function (literalString) { | ||
var startingChars = '[{"'; | ||
var jsonLiterals = ['true', 'false', 'null']; | ||
var numberLooking = '-0123456789'; | ||
} | ||
looksLikeJSON(literalString) { | ||
const startingChars = '[{"'; | ||
const jsonLiterals = ['true', 'false', 'null']; | ||
const numberLooking = '-0123456789'; | ||
if (literalString === '') { | ||
@@ -323,7 +321,6 @@ return false; | ||
return false; | ||
}; | ||
return StreamLexer; | ||
}()); | ||
} | ||
} | ||
exports.Lexer = new StreamLexer(); | ||
exports.default = exports.Lexer; | ||
//# sourceMappingURL=Lexer.js.map |
@@ -21,66 +21,58 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Parser = void 0; | ||
var Lexer_1 = __importStar(require("./Lexer")); | ||
var bindingPower = (_a = {}, | ||
_a[Lexer_1.Token.TOK_EOF] = 0, | ||
_a[Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER] = 0, | ||
_a[Lexer_1.Token.TOK_QUOTEDIDENTIFIER] = 0, | ||
_a[Lexer_1.Token.TOK_RBRACKET] = 0, | ||
_a[Lexer_1.Token.TOK_RPAREN] = 0, | ||
_a[Lexer_1.Token.TOK_COMMA] = 0, | ||
_a[Lexer_1.Token.TOK_RBRACE] = 0, | ||
_a[Lexer_1.Token.TOK_NUMBER] = 0, | ||
_a[Lexer_1.Token.TOK_CURRENT] = 0, | ||
_a[Lexer_1.Token.TOK_EXPREF] = 0, | ||
_a[Lexer_1.Token.TOK_ROOT] = 0, | ||
_a[Lexer_1.Token.TOK_PIPE] = 1, | ||
_a[Lexer_1.Token.TOK_OR] = 2, | ||
_a[Lexer_1.Token.TOK_AND] = 3, | ||
_a[Lexer_1.Token.TOK_EQ] = 5, | ||
_a[Lexer_1.Token.TOK_GT] = 5, | ||
_a[Lexer_1.Token.TOK_LT] = 5, | ||
_a[Lexer_1.Token.TOK_GTE] = 5, | ||
_a[Lexer_1.Token.TOK_LTE] = 5, | ||
_a[Lexer_1.Token.TOK_NE] = 5, | ||
_a[Lexer_1.Token.TOK_FLATTEN] = 9, | ||
_a[Lexer_1.Token.TOK_STAR] = 20, | ||
_a[Lexer_1.Token.TOK_FILTER] = 21, | ||
_a[Lexer_1.Token.TOK_DOT] = 40, | ||
_a[Lexer_1.Token.TOK_NOT] = 45, | ||
_a[Lexer_1.Token.TOK_LBRACE] = 50, | ||
_a[Lexer_1.Token.TOK_LBRACKET] = 55, | ||
_a[Lexer_1.Token.TOK_LPAREN] = 60, | ||
_a); | ||
var TokenParser = /** @class */ (function () { | ||
function TokenParser() { | ||
const Lexer_1 = __importStar(require("./Lexer")); | ||
const bindingPower = { | ||
[Lexer_1.Token.TOK_EOF]: 0, | ||
[Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER]: 0, | ||
[Lexer_1.Token.TOK_QUOTEDIDENTIFIER]: 0, | ||
[Lexer_1.Token.TOK_RBRACKET]: 0, | ||
[Lexer_1.Token.TOK_RPAREN]: 0, | ||
[Lexer_1.Token.TOK_COMMA]: 0, | ||
[Lexer_1.Token.TOK_RBRACE]: 0, | ||
[Lexer_1.Token.TOK_NUMBER]: 0, | ||
[Lexer_1.Token.TOK_CURRENT]: 0, | ||
[Lexer_1.Token.TOK_EXPREF]: 0, | ||
[Lexer_1.Token.TOK_ROOT]: 0, | ||
[Lexer_1.Token.TOK_PIPE]: 1, | ||
[Lexer_1.Token.TOK_OR]: 2, | ||
[Lexer_1.Token.TOK_AND]: 3, | ||
[Lexer_1.Token.TOK_EQ]: 5, | ||
[Lexer_1.Token.TOK_GT]: 5, | ||
[Lexer_1.Token.TOK_LT]: 5, | ||
[Lexer_1.Token.TOK_GTE]: 5, | ||
[Lexer_1.Token.TOK_LTE]: 5, | ||
[Lexer_1.Token.TOK_NE]: 5, | ||
[Lexer_1.Token.TOK_FLATTEN]: 9, | ||
[Lexer_1.Token.TOK_STAR]: 20, | ||
[Lexer_1.Token.TOK_FILTER]: 21, | ||
[Lexer_1.Token.TOK_DOT]: 40, | ||
[Lexer_1.Token.TOK_NOT]: 45, | ||
[Lexer_1.Token.TOK_LBRACE]: 50, | ||
[Lexer_1.Token.TOK_LBRACKET]: 55, | ||
[Lexer_1.Token.TOK_LPAREN]: 60, | ||
}; | ||
class TokenParser { | ||
constructor() { | ||
this.index = 0; | ||
this.tokens = []; | ||
} | ||
TokenParser.prototype.parse = function (expression) { | ||
parse(expression) { | ||
this.loadTokens(expression); | ||
this.index = 0; | ||
var ast = this.expression(0); | ||
const ast = this.expression(0); | ||
if (this.lookahead(0) !== Lexer_1.Token.TOK_EOF) { | ||
var token = this.lookaheadToken(0); | ||
this.errorToken(token, "Unexpected token type: " + token.type + ", value: " + token.value); | ||
const token = this.lookaheadToken(0); | ||
this.errorToken(token, `Unexpected token type: ${token.type}, value: ${token.value}`); | ||
} | ||
return ast; | ||
}; | ||
TokenParser.prototype.loadTokens = function (expression) { | ||
this.tokens = __spreadArrays(Lexer_1.default.tokenize(expression), [{ type: Lexer_1.Token.TOK_EOF, value: '', start: expression.length }]); | ||
}; | ||
TokenParser.prototype.expression = function (rbp) { | ||
var leftToken = this.lookaheadToken(0); | ||
} | ||
loadTokens(expression) { | ||
this.tokens = [...Lexer_1.default.tokenize(expression), { type: Lexer_1.Token.TOK_EOF, value: '', start: expression.length }]; | ||
} | ||
expression(rbp) { | ||
const leftToken = this.lookaheadToken(0); | ||
this.advance(); | ||
var left = this.nud(leftToken); | ||
var currentTokenType = this.lookahead(0); | ||
let left = this.nud(leftToken); | ||
let currentTokenType = this.lookahead(0); | ||
while (rbp < bindingPower[currentTokenType]) { | ||
@@ -92,16 +84,16 @@ this.advance(); | ||
return left; | ||
}; | ||
TokenParser.prototype.lookahead = function (offset) { | ||
} | ||
lookahead(offset) { | ||
return this.tokens[this.index + offset].type; | ||
}; | ||
TokenParser.prototype.lookaheadToken = function (offset) { | ||
} | ||
lookaheadToken(offset) { | ||
return this.tokens[this.index + offset]; | ||
}; | ||
TokenParser.prototype.advance = function () { | ||
} | ||
advance() { | ||
this.index += 1; | ||
}; | ||
TokenParser.prototype.nud = function (token) { | ||
var left; | ||
var right; | ||
var expression; | ||
} | ||
nud(token) { | ||
let left; | ||
let right; | ||
let expression; | ||
switch (token.type) { | ||
@@ -113,3 +105,3 @@ case Lexer_1.Token.TOK_LITERAL: | ||
case Lexer_1.Token.TOK_QUOTEDIDENTIFIER: | ||
var node = { type: 'Field', name: token.value }; | ||
const node = { type: 'Field', name: token.value }; | ||
if (this.lookahead(0) === Lexer_1.Token.TOK_LPAREN) { | ||
@@ -161,3 +153,3 @@ throw new Error('Quoted identifier not allowed for function names.'); | ||
case Lexer_1.Token.TOK_LPAREN: | ||
var args = []; | ||
const args = []; | ||
while (this.lookahead(0) !== Lexer_1.Token.TOK_RPAREN) { | ||
@@ -178,8 +170,8 @@ if (this.lookahead(0) === Lexer_1.Token.TOK_CURRENT) { | ||
} | ||
}; | ||
TokenParser.prototype.led = function (tokenName, left) { | ||
var right; | ||
} | ||
led(tokenName, left) { | ||
let right; | ||
switch (tokenName) { | ||
case Lexer_1.Token.TOK_DOT: | ||
var rbp = bindingPower.Dot; | ||
const rbp = bindingPower.Dot; | ||
if (this.lookahead(0) !== Lexer_1.Token.TOK_STAR) { | ||
@@ -202,5 +194,5 @@ right = this.parseDotRHS(rbp); | ||
case Lexer_1.Token.TOK_LPAREN: | ||
var name_1 = left.name; | ||
var args = []; | ||
var expression = void 0; | ||
const name = left.name; | ||
const args = []; | ||
let expression; | ||
while (this.lookahead(0) !== Lexer_1.Token.TOK_RPAREN) { | ||
@@ -220,6 +212,6 @@ if (this.lookahead(0) === Lexer_1.Token.TOK_CURRENT) { | ||
this.match(Lexer_1.Token.TOK_RPAREN); | ||
var node = { name: name_1, type: 'Function', children: args }; | ||
const node = { name, type: 'Function', children: args }; | ||
return node; | ||
case Lexer_1.Token.TOK_FILTER: | ||
var condition = this.expression(0); | ||
const condition = this.expression(0); | ||
this.match(Lexer_1.Token.TOK_RBRACKET); | ||
@@ -231,4 +223,4 @@ right = | ||
case Lexer_1.Token.TOK_FLATTEN: | ||
var leftNode = { type: Lexer_1.Token.TOK_FLATTEN, children: [left] }; | ||
var rightNode = this.parseProjectionRHS(bindingPower.Flatten); | ||
const leftNode = { type: Lexer_1.Token.TOK_FLATTEN, children: [left] }; | ||
const rightNode = this.parseProjectionRHS(bindingPower.Flatten); | ||
return { type: 'Projection', children: [leftNode, rightNode] }; | ||
@@ -243,3 +235,3 @@ case Lexer_1.Token.TOK_EQ: | ||
case Lexer_1.Token.TOK_LBRACKET: | ||
var token = this.lookaheadToken(0); | ||
const token = this.lookaheadToken(0); | ||
if (token.type === Lexer_1.Token.TOK_NUMBER || token.type === Lexer_1.Token.TOK_COLON) { | ||
@@ -256,4 +248,4 @@ right = this.parseIndexExpression(); | ||
} | ||
}; | ||
TokenParser.prototype.match = function (tokenType) { | ||
} | ||
match(tokenType) { | ||
if (this.lookahead(0) === tokenType) { | ||
@@ -264,17 +256,16 @@ this.advance(); | ||
else { | ||
var token = this.lookaheadToken(0); | ||
this.errorToken(token, "Expected " + tokenType + ", got: " + token.type); | ||
const token = this.lookaheadToken(0); | ||
this.errorToken(token, `Expected ${tokenType}, got: ${token.type}`); | ||
} | ||
}; | ||
TokenParser.prototype.errorToken = function (token, message) { | ||
if (message === void 0) { message = ''; } | ||
var error = new Error(message || "Invalid token (" + token.type + "): \"" + token.value + "\""); | ||
} | ||
errorToken(token, message = '') { | ||
const error = new Error(message || `Invalid token (${token.type}): "${token.value}"`); | ||
error.name = 'ParserError'; | ||
throw error; | ||
}; | ||
TokenParser.prototype.parseIndexExpression = function () { | ||
} | ||
parseIndexExpression() { | ||
if (this.lookahead(0) === Lexer_1.Token.TOK_COLON || this.lookahead(1) === Lexer_1.Token.TOK_COLON) { | ||
return this.parseSliceExpression(); | ||
} | ||
var node = { | ||
const node = { | ||
type: 'Index', | ||
@@ -286,5 +277,5 @@ value: this.lookaheadToken(0).value, | ||
return node; | ||
}; | ||
TokenParser.prototype.projectIfSlice = function (left, right) { | ||
var indexExpr = { type: 'IndexExpression', children: [left, right] }; | ||
} | ||
projectIfSlice(left, right) { | ||
const indexExpr = { type: 'IndexExpression', children: [left, right] }; | ||
if (right.type === 'Slice') { | ||
@@ -297,7 +288,7 @@ return { | ||
return indexExpr; | ||
}; | ||
TokenParser.prototype.parseSliceExpression = function () { | ||
var parts = [null, null, null]; | ||
var index = 0; | ||
var currentTokenType = this.lookahead(0); | ||
} | ||
parseSliceExpression() { | ||
const parts = [null, null, null]; | ||
let index = 0; | ||
let currentTokenType = this.lookahead(0); | ||
while (currentTokenType !== Lexer_1.Token.TOK_RBRACKET && index < 3) { | ||
@@ -313,4 +304,4 @@ if (currentTokenType === Lexer_1.Token.TOK_COLON) { | ||
else { | ||
var token = this.lookaheadToken(0); | ||
this.errorToken(token, "Syntax error, unexpected token: " + token.value + "(" + token.type + ")"); | ||
const token = this.lookaheadToken(0); | ||
this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`); | ||
} | ||
@@ -324,10 +315,10 @@ currentTokenType = this.lookahead(0); | ||
}; | ||
}; | ||
TokenParser.prototype.parseComparator = function (left, comparator) { | ||
var right = this.expression(bindingPower[comparator]); | ||
} | ||
parseComparator(left, comparator) { | ||
const right = this.expression(bindingPower[comparator]); | ||
return { type: 'Comparator', name: comparator, children: [left, right] }; | ||
}; | ||
TokenParser.prototype.parseDotRHS = function (rbp) { | ||
var lookahead = this.lookahead(0); | ||
var exprTokens = [Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER, Lexer_1.Token.TOK_QUOTEDIDENTIFIER, Lexer_1.Token.TOK_STAR]; | ||
} | ||
parseDotRHS(rbp) { | ||
const lookahead = this.lookahead(0); | ||
const exprTokens = [Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER, Lexer_1.Token.TOK_QUOTEDIDENTIFIER, Lexer_1.Token.TOK_STAR]; | ||
if (exprTokens.includes(lookahead)) { | ||
@@ -344,6 +335,6 @@ return this.expression(rbp); | ||
} | ||
var token = this.lookaheadToken(0); | ||
this.errorToken(token, "Syntax error, unexpected token: " + token.value + "(" + token.type + ")"); | ||
}; | ||
TokenParser.prototype.parseProjectionRHS = function (rbp) { | ||
const token = this.lookaheadToken(0); | ||
this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`); | ||
} | ||
parseProjectionRHS(rbp) { | ||
if (bindingPower[this.lookahead(0)] < 10) { | ||
@@ -362,9 +353,9 @@ return { type: 'Identity' }; | ||
} | ||
var token = this.lookaheadToken(0); | ||
this.errorToken(token, "Syntax error, unexpected token: " + token.value + "(" + token.type + ")"); | ||
}; | ||
TokenParser.prototype.parseMultiselectList = function () { | ||
var expressions = []; | ||
const token = this.lookaheadToken(0); | ||
this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`); | ||
} | ||
parseMultiselectList() { | ||
const expressions = []; | ||
while (this.lookahead(0) !== Lexer_1.Token.TOK_RBRACKET) { | ||
var expression = this.expression(0); | ||
const expression = this.expression(0); | ||
expressions.push(expression); | ||
@@ -380,9 +371,9 @@ if (this.lookahead(0) === Lexer_1.Token.TOK_COMMA) { | ||
return { type: 'MultiSelectList', children: expressions }; | ||
}; | ||
TokenParser.prototype.parseMultiselectHash = function () { | ||
var pairs = []; | ||
var identifierTypes = [Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER, Lexer_1.Token.TOK_QUOTEDIDENTIFIER]; | ||
var keyToken; | ||
var keyName; | ||
var value; | ||
} | ||
parseMultiselectHash() { | ||
const pairs = []; | ||
const identifierTypes = [Lexer_1.Token.TOK_UNQUOTEDIDENTIFIER, Lexer_1.Token.TOK_QUOTEDIDENTIFIER]; | ||
let keyToken; | ||
let keyName; | ||
let value; | ||
// tslint:disable-next-line: prettier | ||
@@ -392,3 +383,3 @@ for (;;) { | ||
if (!identifierTypes.includes(keyToken.type)) { | ||
throw new Error("Expecting an identifier token, got: " + keyToken.type); | ||
throw new Error(`Expecting an identifier token, got: ${keyToken.type}`); | ||
} | ||
@@ -399,3 +390,3 @@ keyName = keyToken.value; | ||
value = this.expression(0); | ||
pairs.push({ value: value, type: 'KeyValuePair', name: keyName }); | ||
pairs.push({ value, type: 'KeyValuePair', name: keyName }); | ||
if (this.lookahead(0) === Lexer_1.Token.TOK_COMMA) { | ||
@@ -410,7 +401,6 @@ this.match(Lexer_1.Token.TOK_COMMA); | ||
return { type: 'MultiSelectHash', children: pairs }; | ||
}; | ||
return TokenParser; | ||
}()); | ||
} | ||
} | ||
exports.Parser = new TokenParser(); | ||
exports.default = exports.Parser; | ||
//# sourceMappingURL=Parser.js.map |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Runtime = exports.InputArgument = void 0; | ||
var Lexer_1 = require("./Lexer"); | ||
var utils_1 = require("./utils"); | ||
const Lexer_1 = require("./Lexer"); | ||
const utils_1 = require("./utils"); | ||
var InputArgument; | ||
@@ -26,26 +19,22 @@ (function (InputArgument) { | ||
})(InputArgument = exports.InputArgument || (exports.InputArgument = {})); | ||
var Runtime = /** @class */ (function () { | ||
function Runtime(interpreter) { | ||
var _a; | ||
var _this = this; | ||
this.TYPE_NAME_TABLE = (_a = {}, | ||
_a[InputArgument.TYPE_NUMBER] = 'number', | ||
_a[InputArgument.TYPE_ANY] = 'any', | ||
_a[InputArgument.TYPE_STRING] = 'string', | ||
_a[InputArgument.TYPE_ARRAY] = 'array', | ||
_a[InputArgument.TYPE_OBJECT] = 'object', | ||
_a[InputArgument.TYPE_BOOLEAN] = 'boolean', | ||
_a[InputArgument.TYPE_EXPREF] = 'expression', | ||
_a[InputArgument.TYPE_NULL] = 'null', | ||
_a[InputArgument.TYPE_ARRAY_NUMBER] = 'Array<number>', | ||
_a[InputArgument.TYPE_ARRAY_STRING] = 'Array<string>', | ||
_a); | ||
this.functionAbs = function (_a) { | ||
var inputValue = _a[0]; | ||
class Runtime { | ||
constructor(interpreter) { | ||
this.TYPE_NAME_TABLE = { | ||
[InputArgument.TYPE_NUMBER]: 'number', | ||
[InputArgument.TYPE_ANY]: 'any', | ||
[InputArgument.TYPE_STRING]: 'string', | ||
[InputArgument.TYPE_ARRAY]: 'array', | ||
[InputArgument.TYPE_OBJECT]: 'object', | ||
[InputArgument.TYPE_BOOLEAN]: 'boolean', | ||
[InputArgument.TYPE_EXPREF]: 'expression', | ||
[InputArgument.TYPE_NULL]: 'null', | ||
[InputArgument.TYPE_ARRAY_NUMBER]: 'Array<number>', | ||
[InputArgument.TYPE_ARRAY_STRING]: 'Array<string>', | ||
}; | ||
this.functionAbs = ([inputValue]) => { | ||
return Math.abs(inputValue); | ||
}; | ||
this.functionAvg = function (_a) { | ||
var inputArray = _a[0]; | ||
var sum = 0; | ||
for (var i = 0; i < inputArray.length; i += 1) { | ||
this.functionAvg = ([inputArray]) => { | ||
let sum = 0; | ||
for (let i = 0; i < inputArray.length; i += 1) { | ||
sum += inputArray[i]; | ||
@@ -55,28 +44,24 @@ } | ||
}; | ||
this.functionCeil = function (_a) { | ||
var inputValue = _a[0]; | ||
this.functionCeil = ([inputValue]) => { | ||
return Math.ceil(inputValue); | ||
}; | ||
this.functionContains = function (resolvedArgs) { | ||
var searchable = resolvedArgs[0], searchValue = resolvedArgs[1]; | ||
this.functionContains = resolvedArgs => { | ||
const [searchable, searchValue] = resolvedArgs; | ||
return searchable.includes(searchValue); | ||
}; | ||
this.functionEndsWith = function (resolvedArgs) { | ||
var searchStr = resolvedArgs[0], suffix = resolvedArgs[1]; | ||
this.functionEndsWith = resolvedArgs => { | ||
const [searchStr, suffix] = resolvedArgs; | ||
return searchStr.includes(suffix, searchStr.length - suffix.length); | ||
}; | ||
this.functionFloor = function (_a) { | ||
var inputValue = _a[0]; | ||
this.functionFloor = ([inputValue]) => { | ||
return Math.floor(inputValue); | ||
}; | ||
this.functionJoin = function (resolvedArgs) { | ||
var joinChar = resolvedArgs[0], listJoin = resolvedArgs[1]; | ||
this.functionJoin = resolvedArgs => { | ||
const [joinChar, listJoin] = resolvedArgs; | ||
return listJoin.join(joinChar); | ||
}; | ||
this.functionKeys = function (_a) { | ||
var inputObject = _a[0]; | ||
this.functionKeys = ([inputObject]) => { | ||
return Object.keys(inputObject); | ||
}; | ||
this.functionLength = function (_a) { | ||
var inputValue = _a[0]; | ||
this.functionLength = ([inputValue]) => { | ||
if (!utils_1.isObject(inputValue)) { | ||
@@ -87,11 +72,11 @@ return inputValue.length; | ||
}; | ||
this.functionMap = function (resolvedArgs) { | ||
if (!_this._interpreter) { | ||
this.functionMap = (resolvedArgs) => { | ||
if (!this._interpreter) { | ||
return []; | ||
} | ||
var mapped = []; | ||
var interpreter = _this._interpreter; | ||
var exprefNode = resolvedArgs[0]; | ||
var elements = resolvedArgs[1]; | ||
for (var i = 0; i < elements.length; i += 1) { | ||
const mapped = []; | ||
const interpreter = this._interpreter; | ||
const exprefNode = resolvedArgs[0]; | ||
const elements = resolvedArgs[1]; | ||
for (let i = 0; i < elements.length; i += 1) { | ||
mapped.push(interpreter.visit(exprefNode, elements[i])); | ||
@@ -101,14 +86,13 @@ } | ||
}; | ||
this.functionMax = function (_a) { | ||
var inputValue = _a[0]; | ||
this.functionMax = ([inputValue]) => { | ||
if (!inputValue.length) { | ||
return null; | ||
} | ||
var typeName = _this.getTypeName(inputValue[0]); | ||
const typeName = this.getTypeName(inputValue[0]); | ||
if (typeName === InputArgument.TYPE_NUMBER) { | ||
return Math.max.apply(Math, inputValue); | ||
return Math.max(...inputValue); | ||
} | ||
var elements = inputValue; | ||
var maxElement = elements[0]; | ||
for (var i = 1; i < elements.length; i += 1) { | ||
const elements = inputValue; | ||
let maxElement = elements[0]; | ||
for (let i = 1; i < elements.length; i += 1) { | ||
if (maxElement.localeCompare(elements[i]) < 0) { | ||
@@ -120,10 +104,10 @@ maxElement = elements[i]; | ||
}; | ||
this.functionMaxBy = function (resolvedArgs) { | ||
var exprefNode = resolvedArgs[1]; | ||
var resolvedArray = resolvedArgs[0]; | ||
var keyFunction = _this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]); | ||
var maxNumber = -Infinity; | ||
var maxRecord; | ||
var current; | ||
for (var i = 0; i < resolvedArray.length; i += 1) { | ||
this.functionMaxBy = (resolvedArgs) => { | ||
const exprefNode = resolvedArgs[1]; | ||
const resolvedArray = resolvedArgs[0]; | ||
const keyFunction = this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]); | ||
let maxNumber = -Infinity; | ||
let maxRecord; | ||
let current; | ||
for (let i = 0; i < resolvedArray.length; i += 1) { | ||
current = keyFunction && keyFunction(resolvedArray[i]); | ||
@@ -137,6 +121,6 @@ if (current !== undefined && current > maxNumber) { | ||
}; | ||
this.functionMerge = function (resolvedArgs) { | ||
var merged = {}; | ||
for (var i = 0; i < resolvedArgs.length; i += 1) { | ||
var current = resolvedArgs[i]; | ||
this.functionMerge = resolvedArgs => { | ||
let merged = {}; | ||
for (let i = 0; i < resolvedArgs.length; i += 1) { | ||
const current = resolvedArgs[i]; | ||
merged = Object.assign(merged, current); | ||
@@ -149,14 +133,13 @@ // for (const key in current) { | ||
}; | ||
this.functionMin = function (_a) { | ||
var inputValue = _a[0]; | ||
this.functionMin = ([inputValue]) => { | ||
if (!inputValue.length) { | ||
return null; | ||
} | ||
var typeName = _this.getTypeName(inputValue[0]); | ||
const typeName = this.getTypeName(inputValue[0]); | ||
if (typeName === InputArgument.TYPE_NUMBER) { | ||
return Math.min.apply(Math, inputValue); | ||
return Math.min(...inputValue); | ||
} | ||
var elements = inputValue; | ||
var minElement = elements[0]; | ||
for (var i = 1; i < elements.length; i += 1) { | ||
const elements = inputValue; | ||
let minElement = elements[0]; | ||
for (let i = 1; i < elements.length; i += 1) { | ||
if (elements[i].localeCompare(minElement) < 0) { | ||
@@ -168,10 +151,10 @@ minElement = elements[i]; | ||
}; | ||
this.functionMinBy = function (resolvedArgs) { | ||
var exprefNode = resolvedArgs[1]; | ||
var resolvedArray = resolvedArgs[0]; | ||
var keyFunction = _this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]); | ||
var minNumber = Infinity; | ||
var minRecord; | ||
var current; | ||
for (var i = 0; i < resolvedArray.length; i += 1) { | ||
this.functionMinBy = (resolvedArgs) => { | ||
const exprefNode = resolvedArgs[1]; | ||
const resolvedArray = resolvedArgs[0]; | ||
const keyFunction = this.createKeyFunction(exprefNode, [InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING]); | ||
let minNumber = Infinity; | ||
let minRecord; | ||
let current; | ||
for (let i = 0; i < resolvedArray.length; i += 1) { | ||
current = keyFunction && keyFunction(resolvedArray[i]); | ||
@@ -185,5 +168,5 @@ if (current !== undefined && current < minNumber) { | ||
}; | ||
this.functionNotNull = function (resolvedArgs) { | ||
for (var i = 0; i < resolvedArgs.length; i += 1) { | ||
if (_this.getTypeName(resolvedArgs[i]) !== InputArgument.TYPE_NULL) { | ||
this.functionNotNull = (resolvedArgs) => { | ||
for (let i = 0; i < resolvedArgs.length; i += 1) { | ||
if (this.getTypeName(resolvedArgs[i]) !== InputArgument.TYPE_NULL) { | ||
return resolvedArgs[i]; | ||
@@ -194,9 +177,8 @@ } | ||
}; | ||
this.functionReverse = function (_a) { | ||
var inputValue = _a[0]; | ||
var typeName = _this.getTypeName(inputValue); | ||
this.functionReverse = ([inputValue]) => { | ||
const typeName = this.getTypeName(inputValue); | ||
if (typeName === InputArgument.TYPE_STRING) { | ||
var originalStr = inputValue; | ||
var reversedStr = ''; | ||
for (var i = originalStr.length - 1; i >= 0; i -= 1) { | ||
const originalStr = inputValue; | ||
let reversedStr = ''; | ||
for (let i = originalStr.length - 1; i >= 0; i -= 1) { | ||
reversedStr += originalStr[i]; | ||
@@ -206,36 +188,35 @@ } | ||
} | ||
var reversedArray = inputValue.slice(0); | ||
const reversedArray = inputValue.slice(0); | ||
reversedArray.reverse(); | ||
return reversedArray; | ||
}; | ||
this.functionSort = function (_a) { | ||
var inputValue = _a[0]; | ||
return __spreadArrays(inputValue).sort(); | ||
this.functionSort = ([inputValue]) => { | ||
return [...inputValue].sort(); | ||
}; | ||
this.functionSortBy = function (resolvedArgs) { | ||
if (!_this._interpreter) { | ||
this.functionSortBy = (resolvedArgs) => { | ||
if (!this._interpreter) { | ||
return []; | ||
} | ||
var sortedArray = resolvedArgs[0].slice(0); | ||
const sortedArray = resolvedArgs[0].slice(0); | ||
if (sortedArray.length === 0) { | ||
return sortedArray; | ||
} | ||
var interpreter = _this._interpreter; | ||
var exprefNode = resolvedArgs[1]; | ||
var requiredType = _this.getTypeName(interpreter.visit(exprefNode, sortedArray[0])); | ||
const interpreter = this._interpreter; | ||
const exprefNode = resolvedArgs[1]; | ||
const requiredType = this.getTypeName(interpreter.visit(exprefNode, sortedArray[0])); | ||
if (requiredType !== undefined && ![InputArgument.TYPE_NUMBER, InputArgument.TYPE_STRING].includes(requiredType)) { | ||
throw new Error("TypeError: unexpected type (" + _this.TYPE_NAME_TABLE[requiredType] + ")"); | ||
throw new Error(`TypeError: unexpected type (${this.TYPE_NAME_TABLE[requiredType]})`); | ||
} | ||
var decorated = []; | ||
for (var i = 0; i < sortedArray.length; i += 1) { | ||
const decorated = []; | ||
for (let i = 0; i < sortedArray.length; i += 1) { | ||
decorated.push([i, sortedArray[i]]); | ||
} | ||
decorated.sort(function (a, b) { | ||
var exprA = interpreter.visit(exprefNode, a[1]); | ||
var exprB = interpreter.visit(exprefNode, b[1]); | ||
if (_this.getTypeName(exprA) !== requiredType) { | ||
throw new Error("TypeError: expected (" + _this.TYPE_NAME_TABLE[requiredType] + "), received " + _this.TYPE_NAME_TABLE[_this.getTypeName(exprA)]); | ||
decorated.sort((a, b) => { | ||
const exprA = interpreter.visit(exprefNode, a[1]); | ||
const exprB = interpreter.visit(exprefNode, b[1]); | ||
if (this.getTypeName(exprA) !== requiredType) { | ||
throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[requiredType]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(exprA)]}`); | ||
} | ||
else if (_this.getTypeName(exprB) !== requiredType) { | ||
throw new Error("TypeError: expected (" + _this.TYPE_NAME_TABLE[requiredType] + "), received " + _this.TYPE_NAME_TABLE[_this.getTypeName(exprB)]); | ||
else if (this.getTypeName(exprB) !== requiredType) { | ||
throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[requiredType]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(exprB)]}`); | ||
} | ||
@@ -247,3 +228,3 @@ if (exprA > exprB) { | ||
}); | ||
for (var j = 0; j < decorated.length; j += 1) { | ||
for (let j = 0; j < decorated.length; j += 1) { | ||
sortedArray[j] = decorated[j][1]; | ||
@@ -253,13 +234,10 @@ } | ||
}; | ||
this.functionStartsWith = function (_a) { | ||
var searchable = _a[0], searchStr = _a[1]; | ||
this.functionStartsWith = ([searchable, searchStr]) => { | ||
return searchable.startsWith(searchStr); | ||
}; | ||
this.functionSum = function (_a) { | ||
var inputValue = _a[0]; | ||
return inputValue.reduce(function (x, y) { return x + y; }, 0); | ||
this.functionSum = ([inputValue]) => { | ||
return inputValue.reduce((x, y) => x + y, 0); | ||
}; | ||
this.functionToArray = function (_a) { | ||
var inputValue = _a[0]; | ||
if (_this.getTypeName(inputValue) === InputArgument.TYPE_ARRAY) { | ||
this.functionToArray = ([inputValue]) => { | ||
if (this.getTypeName(inputValue) === InputArgument.TYPE_ARRAY) { | ||
return inputValue; | ||
@@ -269,6 +247,5 @@ } | ||
}; | ||
this.functionToNumber = function (_a) { | ||
var inputValue = _a[0]; | ||
var typeName = _this.getTypeName(inputValue); | ||
var convertedValue; | ||
this.functionToNumber = ([inputValue]) => { | ||
const typeName = this.getTypeName(inputValue); | ||
let convertedValue; | ||
if (typeName === InputArgument.TYPE_NUMBER) { | ||
@@ -285,5 +262,4 @@ return inputValue; | ||
}; | ||
this.functionToString = function (_a) { | ||
var inputValue = _a[0]; | ||
if (_this.getTypeName(inputValue) === InputArgument.TYPE_STRING) { | ||
this.functionToString = ([inputValue]) => { | ||
if (this.getTypeName(inputValue) === InputArgument.TYPE_STRING) { | ||
return inputValue; | ||
@@ -293,5 +269,4 @@ } | ||
}; | ||
this.functionType = function (_a) { | ||
var inputValue = _a[0]; | ||
switch (_this.getTypeName(inputValue)) { | ||
this.functionType = ([inputValue]) => { | ||
switch (this.getTypeName(inputValue)) { | ||
case InputArgument.TYPE_NUMBER: | ||
@@ -315,4 +290,3 @@ return 'number'; | ||
}; | ||
this.functionValues = function (_a) { | ||
var inputObject = _a[0]; | ||
this.functionValues = ([inputObject]) => { | ||
return Object.values(inputObject); | ||
@@ -558,5 +532,5 @@ }; | ||
} | ||
Runtime.prototype.registerFunction = function (name, customFunction, signature) { | ||
registerFunction(name, customFunction, signature) { | ||
if (name in this.functionTable) { | ||
throw new Error("Function already defined: " + name + "()"); | ||
throw new Error(`Function already defined: ${name}()`); | ||
} | ||
@@ -567,40 +541,39 @@ this.functionTable[name] = { | ||
}; | ||
}; | ||
Runtime.prototype.callFunction = function (name, resolvedArgs) { | ||
var functionEntry = this.functionTable[name]; | ||
} | ||
callFunction(name, resolvedArgs) { | ||
const functionEntry = this.functionTable[name]; | ||
if (functionEntry === undefined) { | ||
throw new Error("Unknown function: " + name + "()"); | ||
throw new Error(`Unknown function: ${name}()`); | ||
} | ||
this.validateArgs(name, resolvedArgs, functionEntry._signature); | ||
return functionEntry._func.call(this, resolvedArgs); | ||
}; | ||
Runtime.prototype.validateInputSignatures = function (name, signature) { | ||
for (var i = 0; i < signature.length; i += 1) { | ||
} | ||
validateInputSignatures(name, signature) { | ||
for (let i = 0; i < signature.length; i += 1) { | ||
if ('variadic' in signature[i] && i !== signature.length - 1) { | ||
throw new Error("ArgumentError: " + name + "() 'variadic' argument " + (i + 1) + " must occur last"); | ||
throw new Error(`ArgumentError: ${name}() 'variadic' argument ${i + 1} must occur last`); | ||
} | ||
} | ||
}; | ||
Runtime.prototype.validateArgs = function (name, args, signature) { | ||
var _this = this; | ||
} | ||
validateArgs(name, args, signature) { | ||
var _a, _b; | ||
var pluralized; | ||
let pluralized; | ||
this.validateInputSignatures(name, signature); | ||
var numberOfRequiredArgs = signature.filter(function (argSignature) { var _a; return (_a = !argSignature.optional) !== null && _a !== void 0 ? _a : false; }).length; | ||
var lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false; | ||
var tooFewArgs = args.length < numberOfRequiredArgs; | ||
var tooManyArgs = args.length > signature.length; | ||
var tooFewModifier = tooFewArgs && ((!lastArgIsVariadic && numberOfRequiredArgs > 1) || lastArgIsVariadic) ? 'at least ' : ''; | ||
const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return (_a = !argSignature.optional) !== null && _a !== void 0 ? _a : false; }).length; | ||
const lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false; | ||
const tooFewArgs = args.length < numberOfRequiredArgs; | ||
const tooManyArgs = args.length > signature.length; | ||
const tooFewModifier = tooFewArgs && ((!lastArgIsVariadic && numberOfRequiredArgs > 1) || lastArgIsVariadic) ? 'at least ' : ''; | ||
if ((lastArgIsVariadic && tooFewArgs) || (!lastArgIsVariadic && (tooFewArgs || tooManyArgs))) { | ||
pluralized = signature.length > 1; | ||
throw new Error("ArgumentError: " + name + "() takes " + tooFewModifier + numberOfRequiredArgs + " argument" + ((pluralized && 's') || '') + " but received " + args.length); | ||
throw new Error(`ArgumentError: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
} | ||
var currentSpec; | ||
var actualType; | ||
var typeMatched; | ||
for (var i = 0; i < signature.length; i += 1) { | ||
let currentSpec; | ||
let actualType; | ||
let typeMatched; | ||
for (let i = 0; i < signature.length; i += 1) { | ||
typeMatched = false; | ||
currentSpec = signature[i].types; | ||
actualType = this.getTypeName(args[i]); | ||
var j = void 0; | ||
let j; | ||
for (j = 0; j < currentSpec.length; j += 1) { | ||
@@ -613,12 +586,12 @@ if (actualType !== undefined && this.typeMatches(actualType, currentSpec[j], args[i])) { | ||
if (!typeMatched && actualType !== undefined) { | ||
var expected = currentSpec | ||
.map(function (typeIdentifier) { | ||
return _this.TYPE_NAME_TABLE[typeIdentifier]; | ||
const expected = currentSpec | ||
.map((typeIdentifier) => { | ||
return this.TYPE_NAME_TABLE[typeIdentifier]; | ||
}) | ||
.join(' | '); | ||
throw new Error("TypeError: " + name + "() expected argument " + (i + 1) + " to be type (" + expected + ") but received type " + this.TYPE_NAME_TABLE[actualType] + " instead."); | ||
throw new Error(`TypeError: ${name}() expected argument ${i + 1} to be type (${expected}) but received type ${this.TYPE_NAME_TABLE[actualType]} instead.`); | ||
} | ||
} | ||
}; | ||
Runtime.prototype.typeMatches = function (actual, expected, argValue) { | ||
} | ||
typeMatches(actual, expected, argValue) { | ||
if (expected === InputArgument.TYPE_ANY) { | ||
@@ -634,3 +607,3 @@ return true; | ||
if (actual === InputArgument.TYPE_ARRAY) { | ||
var subtype = void 0; | ||
let subtype; | ||
if (expected === InputArgument.TYPE_ARRAY_NUMBER) { | ||
@@ -642,4 +615,4 @@ subtype = InputArgument.TYPE_NUMBER; | ||
} | ||
for (var i = 0; i < argValue.length; i += 1) { | ||
var typeName = this.getTypeName(argValue[i]); | ||
for (let i = 0; i < argValue.length; i += 1) { | ||
const typeName = this.getTypeName(argValue[i]); | ||
if (typeName !== undefined && subtype !== undefined && !this.typeMatches(typeName, subtype, argValue[i])) { | ||
@@ -656,4 +629,4 @@ return false; | ||
return false; | ||
}; | ||
Runtime.prototype.getTypeName = function (obj) { | ||
} | ||
getTypeName(obj) { | ||
switch (Object.prototype.toString.call(obj)) { | ||
@@ -678,15 +651,14 @@ case '[object String]': | ||
} | ||
}; | ||
Runtime.prototype.createKeyFunction = function (exprefNode, allowedTypes) { | ||
var _this = this; | ||
} | ||
createKeyFunction(exprefNode, allowedTypes) { | ||
if (!this._interpreter) { | ||
return; | ||
} | ||
var interpreter = this._interpreter; | ||
var keyFunc = function (x) { | ||
var current = interpreter.visit(exprefNode, x); | ||
if (!allowedTypes.includes(_this.getTypeName(current))) { | ||
var msg = "TypeError: expected one of (" + allowedTypes | ||
.map(function (t) { return _this.TYPE_NAME_TABLE[t]; }) | ||
.join(' | ') + "), received " + _this.TYPE_NAME_TABLE[_this.getTypeName(current)]; | ||
const interpreter = this._interpreter; | ||
const keyFunc = (x) => { | ||
const current = interpreter.visit(exprefNode, x); | ||
if (!allowedTypes.includes(this.getTypeName(current))) { | ||
const msg = `TypeError: expected one of (${allowedTypes | ||
.map(t => this.TYPE_NAME_TABLE[t]) | ||
.join(' | ')}), received ${this.TYPE_NAME_TABLE[this.getTypeName(current)]}`; | ||
throw new Error(msg); | ||
@@ -697,6 +669,5 @@ } | ||
return keyFunc; | ||
}; | ||
return Runtime; | ||
}()); | ||
} | ||
} | ||
exports.Runtime = Runtime; | ||
//# sourceMappingURL=Runtime.js.map |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TreeInterpreterInstance = exports.TreeInterpreter = void 0; | ||
var utils_1 = require("./utils"); | ||
var Lexer_1 = require("./Lexer"); | ||
var Runtime_1 = require("./Runtime"); | ||
var TreeInterpreter = /** @class */ (function () { | ||
function TreeInterpreter() { | ||
const utils_1 = require("./utils"); | ||
const Lexer_1 = require("./Lexer"); | ||
const Runtime_1 = require("./Runtime"); | ||
class TreeInterpreter { | ||
constructor() { | ||
this._rootValue = null; | ||
this.runtime = new Runtime_1.Runtime(this); | ||
} | ||
TreeInterpreter.prototype.search = function (node, value) { | ||
search(node, value) { | ||
this._rootValue = value; | ||
return this.visit(node, value); | ||
}; | ||
TreeInterpreter.prototype.visit = function (node, value) { | ||
var matched; | ||
var current; | ||
var result; | ||
var first; | ||
var second; | ||
var field; | ||
var left; | ||
var right; | ||
var collected; | ||
var i; | ||
var base; | ||
} | ||
visit(node, value) { | ||
let matched; | ||
let current; | ||
let result; | ||
let first; | ||
let second; | ||
let field; | ||
let left; | ||
let right; | ||
let collected; | ||
let i; | ||
let base; | ||
switch (node.type) { | ||
@@ -65,3 +58,3 @@ case 'Field': | ||
} | ||
var index = node.value; | ||
let index = node.value; | ||
if (index < 0) { | ||
@@ -79,8 +72,8 @@ index = value.length + index; | ||
} | ||
var sliceParams = __spreadArrays(node.children); | ||
var computed = this.computeSliceParams(value.length, sliceParams); | ||
var start = computed[0], stop_1 = computed[1], step = computed[2]; | ||
const sliceParams = [...node.children]; | ||
const computed = this.computeSliceParams(value.length, sliceParams); | ||
const [start, stop, step] = computed; | ||
result = []; | ||
if (step > 0) { | ||
for (i = start; i < stop_1; i += step) { | ||
for (i = start; i < stop; i += step) { | ||
result.push(value[i]); | ||
@@ -90,3 +83,3 @@ } | ||
else { | ||
for (i = start; i > stop_1; i += step) { | ||
for (i = start; i > stop; i += step) { | ||
result.push(value[i]); | ||
@@ -115,3 +108,3 @@ } | ||
collected = []; | ||
var values = Object.values(base); | ||
const values = Object.values(base); | ||
for (i = 0; i < values.length; i += 1) { | ||
@@ -129,4 +122,4 @@ current = this.visit(node.children[1], values[i]); | ||
} | ||
var filtered = []; | ||
var finalResults = []; | ||
const filtered = []; | ||
const finalResults = []; | ||
for (i = 0; i < base.length; i += 1) { | ||
@@ -138,3 +131,3 @@ matched = this.visit(node.children[2], base[i]); | ||
} | ||
for (var j = 0; j < filtered.length; j += 1) { | ||
for (let j = 0; j < filtered.length; j += 1) { | ||
current = this.visit(node.children[1], filtered[j]); | ||
@@ -169,15 +162,15 @@ if (current !== null) { | ||
default: | ||
throw new Error("Unknown comparator: " + node.name); | ||
throw new Error(`Unknown comparator: ${node.name}`); | ||
} | ||
return result; | ||
case Lexer_1.Token.TOK_FLATTEN: | ||
var original = this.visit(node.children[0], value); | ||
const original = this.visit(node.children[0], value); | ||
if (!Array.isArray(original)) { | ||
return null; | ||
} | ||
var merged = []; | ||
let merged = []; | ||
for (i = 0; i < original.length; i += 1) { | ||
current = original[i]; | ||
if (Array.isArray(current)) { | ||
merged = __spreadArrays(merged, current); | ||
merged = [...merged, ...current]; | ||
} | ||
@@ -205,3 +198,3 @@ else { | ||
collected = {}; | ||
var child = void 0; | ||
let child; | ||
for (i = 0; i < node.children.length; i += 1) { | ||
@@ -237,4 +230,4 @@ child = node.children[i]; | ||
case 'Function': | ||
var resolvedArgs = []; | ||
for (var j = 0; j < node.children.length; j += 1) { | ||
const resolvedArgs = []; | ||
for (let j = 0; j < node.children.length; j += 1) { | ||
resolvedArgs.push(this.visit(node.children[j], value)); | ||
@@ -244,11 +237,11 @@ } | ||
case 'ExpressionReference': | ||
var refNode = node.children[0]; | ||
const refNode = node.children[0]; | ||
refNode.jmespathType = Lexer_1.Token.TOK_EXPREF; | ||
return refNode; | ||
default: | ||
throw new Error("Unknown node type: " + node.type); | ||
throw new Error(`Unknown node type: ${node.type}`); | ||
} | ||
}; | ||
TreeInterpreter.prototype.computeSliceParams = function (arrayLength, sliceParams) { | ||
var start = sliceParams[0], stop = sliceParams[1], step = sliceParams[2]; | ||
} | ||
computeSliceParams(arrayLength, sliceParams) { | ||
let [start, stop, step] = sliceParams; | ||
if (step === null) { | ||
@@ -258,13 +251,13 @@ step = 1; | ||
else if (step === 0) { | ||
var error = new Error('Invalid slice, step cannot be 0'); | ||
const error = new Error('Invalid slice, step cannot be 0'); | ||
error.name = 'RuntimeError'; | ||
throw error; | ||
} | ||
var stepValueNegative = step < 0 ? true : false; | ||
const stepValueNegative = step < 0 ? true : false; | ||
start = start === null ? (stepValueNegative ? arrayLength - 1 : 0) : this.capSliceRange(arrayLength, start, step); | ||
stop = stop === null ? (stepValueNegative ? -1 : arrayLength) : this.capSliceRange(arrayLength, stop, step); | ||
return [start, stop, step]; | ||
}; | ||
TreeInterpreter.prototype.capSliceRange = function (arrayLength, actualValue, step) { | ||
var nextActualValue = actualValue; | ||
} | ||
capSliceRange(arrayLength, actualValue, step) { | ||
let nextActualValue = actualValue; | ||
if (nextActualValue < 0) { | ||
@@ -280,5 +273,4 @@ nextActualValue += arrayLength; | ||
return nextActualValue; | ||
}; | ||
return TreeInterpreter; | ||
}()); | ||
} | ||
} | ||
exports.TreeInterpreter = TreeInterpreter; | ||
@@ -285,0 +277,0 @@ exports.TreeInterpreterInstance = new TreeInterpreter(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isAlphaNum = exports.isNum = exports.isAlpha = exports.isFalse = exports.strictDeepEqual = exports.isObject = void 0; | ||
var isObject = function (obj) { | ||
const isObject = (obj) => { | ||
return obj !== null && Object.prototype.toString.call(obj) === '[object Object]'; | ||
}; | ||
exports.isObject = isObject; | ||
var strictDeepEqual = function (first, second) { | ||
const strictDeepEqual = (first, second) => { | ||
if (first === second) { | ||
@@ -19,3 +19,3 @@ return true; | ||
} | ||
for (var i = 0; i < first.length; i += 1) { | ||
for (let i = 0; i < first.length; i += 1) { | ||
if (!exports.strictDeepEqual(first[i], second[i])) { | ||
@@ -28,9 +28,8 @@ return false; | ||
if (exports.isObject(first) && exports.isObject(second)) { | ||
var firstEntries = Object.entries(first); | ||
var secondKeys = new Set(Object.keys(second)); | ||
const firstEntries = Object.entries(first); | ||
const secondKeys = new Set(Object.keys(second)); | ||
if (firstEntries.length !== secondKeys.size) { | ||
return false; | ||
} | ||
for (var _i = 0, firstEntries_1 = firstEntries; _i < firstEntries_1.length; _i++) { | ||
var _a = firstEntries_1[_i], key = _a[0], value = _a[1]; | ||
for (const [key, value] of firstEntries) { | ||
if (!exports.strictDeepEqual(value, second[key])) { | ||
@@ -46,3 +45,3 @@ return false; | ||
exports.strictDeepEqual = strictDeepEqual; | ||
var isFalse = function (obj) { | ||
const isFalse = (obj) => { | ||
if (obj === '' || obj === false || obj === null || obj === undefined) { | ||
@@ -55,3 +54,3 @@ return true; | ||
if (exports.isObject(obj)) { | ||
for (var key in obj) { | ||
for (const key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
@@ -66,3 +65,3 @@ return false; | ||
exports.isFalse = isFalse; | ||
var isAlpha = function (ch) { | ||
const isAlpha = (ch) => { | ||
// tslint:disable-next-line: strict-comparisons | ||
@@ -72,3 +71,3 @@ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '_'; | ||
exports.isAlpha = isAlpha; | ||
var isNum = function (ch) { | ||
const isNum = (ch) => { | ||
// tslint:disable-next-line: strict-comparisons | ||
@@ -78,3 +77,3 @@ return (ch >= '0' && ch <= '9') || ch === '-'; | ||
exports.isNum = isNum; | ||
var isAlphaNum = function (ch) { | ||
const isAlphaNum = (ch) => { | ||
// tslint:disable-next-line: strict-comparisons | ||
@@ -81,0 +80,0 @@ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch === '_'; |
{ | ||
"name": "@metrichor/jmespath", | ||
"description": "Typescript implementation of the JMESPath spec (100% compliant)", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"author": { | ||
@@ -54,30 +54,30 @@ "name": "Oxford Nanopore Technologies", | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^17.1.0", | ||
"@rollup/plugin-commonjs": "^19.0.0", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^11.1.1", | ||
"@rollup/plugin-typescript": "^8.1.1", | ||
"@typescript-eslint/eslint-plugin": "^4.15.0", | ||
"@typescript-eslint/parser": "^4.15.0", | ||
"@rollup/plugin-node-resolve": "^13.0.0", | ||
"@rollup/plugin-typescript": "^8.2.1", | ||
"@typescript-eslint/eslint-plugin": "^4.24.0", | ||
"@typescript-eslint/parser": "^4.24.0", | ||
"benchmark": "^2.1.4", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.19.0", | ||
"eslint-config-prettier": "^7.2.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"husky": "^5.0.9", | ||
"eslint": "^7.27.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"husky": "^6.0.0", | ||
"jest": "^26.6.3", | ||
"jest-cli": "^26.6.3", | ||
"jest-config": "^26.6.3", | ||
"lint-staged": "^10.5.4", | ||
"prettier": "^2.2.1", | ||
"lint-staged": "^11.0.0", | ||
"prettier": "^2.3.0", | ||
"prettier-eslint": "^12.0.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.38.5", | ||
"rollup": "^2.49.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"shelljs": "^0.8.4", | ||
"ts-jest": "^26.5.1", | ||
"ts-jest": "^26.5.6", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.4" | ||
"typescript": "^4.2.4" | ||
}, | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
407162
5531