@metrichor/jmespath
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -1,2 +0,2 @@ | ||
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="function"==typeof String.prototype.trimLeft?t=>t.trimLeft():t=>{const e=/^\s*(.*)/.exec(t);return e&&e[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_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_RBRACKET,"{":s.TOK_LBRACE,"}":s.TOK_RBRACE},h={"!":!0,"<":!0,"=":!0,">":!0},c={"\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!==h[t[this._current]])u=this.consumeOperator(t),u&&e.push(u);else if(void 0!==c[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 i=n(t.slice(e,this._current));i=i.replace("\\`","`");const s=this.looksLikeJSON(i)?JSON.parse(i):JSON.parse(`"${i}"`);return this._current+=1,s}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_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_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 h;for(;this.lookahead(0)!==s.TOK_RPAREN;)this.lookahead(0)===s.TOK_CURRENT?(h={type:s.TOK_CURRENT},this.advance()):h=this.expression(0),this.lookahead(0)===s.TOK_COMMA&&this.match(s.TOK_COMMA),o.push(h);this.match(s.TOK_RPAREN);return{name:i,type:"Function",children:o};case s.TOK_FILTER:const c=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,c]};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,o;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),o={value:i,type:"KeyValuePair",name:n},t.push(o),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(e){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=([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])===_.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=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)}validateArgs(t,e,r){let n,i,s,o;if(r[r.length-1].variadic){if(e.length<r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes at least ${r.length} argument${n?"s":""} but received ${e.length}`)}else if(e.length!==r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes ${r.length} argument${n?"s":""} but received ${e.length}`);for(let n=0;n<r.length;n+=1){let h;for(o=!1,i=r[n].types,s=this.getTypeName(e[n]),h=0;h<i.length;h+=1)if(void 0!==s&&this.typeMatches(s,i[h],e[n])){o=!0;break}if(!o){const e=i.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[s]} 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.runtime=new l(this)}search(t,e){return this.visit(t,e)}visit(n,i){let o,h,c,u,a,T,_,l,E,R,p;switch(n.type){case"Field":return null===i?null:t(i)?(T=i[n.name],void 0===T?null:T):null;case"Subexpression":for(c=this.visit(n.children[0],i),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],i),l=this.visit(n.children[1],_),l;case"Index":if(!Array.isArray(i))return null;let O=n.value;return O<0&&(O=i.length+O),c=i[O],void 0===c&&(c=null),c;case"Slice":if(!Array.isArray(i))return null;const f=[...n.children],A=this.computeSliceParams(i.length,f),[d,N,y]=A;if(c=[],y>0)for(R=d;R<N;R+=y)c.push(i[R]);else for(R=d;R>N;R+=y)c.push(i[R]);return c;case"Projection":if(p=this.visit(n.children[0],i),!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],i),!t(p))return null;E=[];const P=Object.values(p);for(R=0;R<P.length;R+=1)h=this.visit(n.children[1],P[R]),null!==h&&E.push(h);return E;case"FilterProjection":if(p=this.visit(n.children[0],i),!Array.isArray(p))return null;const K=[],Y=[];for(R=0;R<p.length;R+=1)o=this.visit(n.children[2],p[R]),r(o)||K.push(p[R]);for(let t=0;t<K.length;t+=1)h=this.visit(n.children[1],K[t]),null!==h&&Y.push(h);return Y;case"Comparator":switch(u=this.visit(n.children[0],i),a=this.visit(n.children[1],i),n.name){case s.TOK_EQ:c=e(u,a);break;case s.TOK_NE:c=!e(u,a);break;case s.TOK_GT:c=u>a;break;case s.TOK_GTE:c=u>=a;break;case s.TOK_LT:c=u<a;break;case s.TOK_LTE:c=u<=a;break;default:throw new Error("Unknown comparator: "+n.name)}return c;case s.TOK_FLATTEN:const g=this.visit(n.children[0],i);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 i;case"MultiSelectList":if(null===i)return null;for(E=[],R=0;R<n.children.length;R+=1)E.push(this.visit(n.children[R],i));return E;case"MultiSelectHash":if(null===i)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,i);return E;case"OrExpression":return o=this.visit(n.children[0],i),r(o)&&(o=this.visit(n.children[1],i)),o;case"AndExpression":return u=this.visit(n.children[0],i),r(u)?u:this.visit(n.children[1],i);case"NotExpression":return u=this.visit(n.children[0],i),r(u);case"Literal":return n.value;case s.TOK_PIPE:return _=this.visit(n.children[0],i),this.visit(n.children[1],_);case s.TOK_CURRENT:return i;case"Function":const I=[];for(let t=0;t<n.children.length;t+=1)I.push(this.visit(n.children[t],i));return this.runtime.callFunction(n.name,I);case"ExpressionReference":const m=n.children[0];return m.jmespathType=s.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}},R=_.TYPE_ANY,p=_.TYPE_ARRAY,O=_.TYPE_ARRAY_NUMBER,f=_.TYPE_ARRAY_STRING,A=_.TYPE_BOOLEAN,d=_.TYPE_EXPREF,N=_.TYPE_NULL,y=_.TYPE_NUMBER,P=_.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 I=E,m={compile:Y,registerFunction:v,search:L,tokenize:g,TreeInterpreter:I,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:y,TYPE_OBJECT:P,TYPE_STRING:K};export default m;export{R as TYPE_ANY,p as TYPE_ARRAY,O as TYPE_ARRAY_NUMBER,f as TYPE_ARRAY_STRING,A as TYPE_BOOLEAN,d as TYPE_EXPREF,N as TYPE_NULL,y as TYPE_NUMBER,P as TYPE_OBJECT,K as TYPE_STRING,I as TreeInterpreter,Y as compile,m as jmespath,v as registerFunction,L as search,g 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="function"==typeof String.prototype.trimLeft?t=>t.trimLeft():t=>{const e=/^\s*(.*)/.exec(t);return e&&e[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_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_RBRACKET,"{":s.TOK_LBRACE,"}":s.TOK_RBRACE},h={"!":!0,"<":!0,"=":!0,">":!0},c={"\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!==h[t[this._current]])u=this.consumeOperator(t),u&&e.push(u);else if(void 0!==c[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 i=n(t.slice(e,this._current));i=i.replace("\\`","`");const s=this.looksLikeJSON(i)?JSON.parse(i):JSON.parse(`"${i}"`);return this._current+=1,s}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_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_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 h;for(;this.lookahead(0)!==s.TOK_RPAREN;)this.lookahead(0)===s.TOK_CURRENT?(h={type:s.TOK_CURRENT},this.advance()):h=this.expression(0),this.lookahead(0)===s.TOK_COMMA&&this.match(s.TOK_COMMA),o.push(h);this.match(s.TOK_RPAREN);return{name:i,type:"Function",children:o};case s.TOK_FILTER:const c=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,c]};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,o;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),o={value:i,type:"KeyValuePair",name:n},t.push(o),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(e){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=([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])===_.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=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)}validateArgs(t,e,r){let n,i,s,o;if(r[r.length-1].variadic){if(e.length<r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes at least ${r.length} argument${n?"s":""} but received ${e.length}`)}else if(e.length!==r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes ${r.length} argument${n?"s":""} but received ${e.length}`);for(let n=0;n<r.length;n+=1){let h;for(o=!1,i=r[n].types,s=this.getTypeName(e[n]),h=0;h<i.length;h+=1)if(void 0!==s&&this.typeMatches(s,i[h],e[n])){o=!0;break}if(!o){const e=i.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[s]} 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.runtime=new l(this)}search(t,e){return this.visit(t,e)}visit(n,i){let o,h,c,u,a,T,_,l,E,R,p;switch(n.type){case"Field":return null===i?null:t(i)?(T=i[n.name],void 0===T?null:T):null;case"Subexpression":for(c=this.visit(n.children[0],i),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],i),l=this.visit(n.children[1],_),l;case"Index":if(!Array.isArray(i))return null;let O=n.value;return O<0&&(O=i.length+O),c=i[O],void 0===c&&(c=null),c;case"Slice":if(!Array.isArray(i))return null;const f=[...n.children],A=this.computeSliceParams(i.length,f),[d,N,y]=A;if(c=[],y>0)for(R=d;R<N;R+=y)c.push(i[R]);else for(R=d;R>N;R+=y)c.push(i[R]);return c;case"Projection":if(p=this.visit(n.children[0],i),!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],i),!t(p))return null;E=[];const P=Object.values(p);for(R=0;R<P.length;R+=1)h=this.visit(n.children[1],P[R]),null!==h&&E.push(h);return E;case"FilterProjection":if(p=this.visit(n.children[0],i),!Array.isArray(p))return null;const K=[],Y=[];for(R=0;R<p.length;R+=1)o=this.visit(n.children[2],p[R]),r(o)||K.push(p[R]);for(let t=0;t<K.length;t+=1)h=this.visit(n.children[1],K[t]),null!==h&&Y.push(h);return Y;case"Comparator":switch(u=this.visit(n.children[0],i),a=this.visit(n.children[1],i),n.name){case s.TOK_EQ:c=e(u,a);break;case s.TOK_NE:c=!e(u,a);break;case s.TOK_GT:c=u>a;break;case s.TOK_GTE:c=u>=a;break;case s.TOK_LT:c=u<a;break;case s.TOK_LTE:c=u<=a;break;default:throw new Error("Unknown comparator: "+n.name)}return c;case s.TOK_FLATTEN:const g=this.visit(n.children[0],i);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 i;case"MultiSelectList":if(null===i)return null;for(E=[],R=0;R<n.children.length;R+=1)E.push(this.visit(n.children[R],i));return E;case"MultiSelectHash":if(null===i)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,i);return E;case"OrExpression":return o=this.visit(n.children[0],i),r(o)&&(o=this.visit(n.children[1],i)),o;case"AndExpression":return u=this.visit(n.children[0],i),r(u)?u:this.visit(n.children[1],i);case"NotExpression":return u=this.visit(n.children[0],i),r(u);case"Literal":return n.value;case s.TOK_PIPE:return _=this.visit(n.children[0],i),this.visit(n.children[1],_);case s.TOK_CURRENT:return i;case"Function":const I=[];for(let t=0;t<n.children.length;t+=1)I.push(this.visit(n.children[t],i));return this.runtime.callFunction(n.name,I);case"ExpressionReference":const m=n.children[0];return m.jmespathType=s.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}},R=_.TYPE_ANY,p=_.TYPE_ARRAY,O=_.TYPE_ARRAY_NUMBER,f=_.TYPE_ARRAY_STRING,A=_.TYPE_BOOLEAN,d=_.TYPE_EXPREF,N=_.TYPE_NULL,y=_.TYPE_NUMBER,P=_.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 I=E,m={compile:Y,registerFunction:v,search:L,tokenize:g,TreeInterpreter:I,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:y,TYPE_OBJECT:P,TYPE_STRING:K};export default m;export{R as TYPE_ANY,p as TYPE_ARRAY,O as TYPE_ARRAY_NUMBER,f as TYPE_ARRAY_STRING,A as TYPE_BOOLEAN,d as TYPE_EXPREF,N as TYPE_NULL,y as TYPE_NUMBER,P as TYPE_OBJECT,K as TYPE_STRING,I as TreeInterpreter,Y as compile,m as jmespath,v as registerFunction,L as search,g as tokenize}; | ||
//# sourceMappingURL=jmespath.esm.min.js.map |
@@ -1,2 +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";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="function"==typeof String.prototype.trimLeft?t=>t.trimLeft():t=>{const e=/^\s*(.*)/.exec(t);return e&&e[1]},s=t=>t>="0"&&t<="9"||"-"===t;var o;!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_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"}(o||(o={}));const c={"(":o.TOK_LPAREN,")":o.TOK_RPAREN,"*":o.TOK_STAR,",":o.TOK_COMMA,".":o.TOK_DOT,":":o.TOK_COLON,"@":o.TOK_CURRENT,"]":o.TOK_RBRACKET,"{":o.TOK_LBRACE,"}":o.TOK_RBRACE},h={"!":!0,"<":!0,"=":!0,">":!0},u={"\t":!0,"\n":!0,"\r":!0," ":!0};const T=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,n,i;for(this._current=0;this._current<t.length;)if((T=t[this._current])>="a"&&T<="z"||T>="A"&&T<="Z"||"_"===T)r=this._current,n=this.consumeUnquotedIdentifier(t),e.push({start:r,type:o.TOK_UNQUOTEDIDENTIFIER,value:n});else if(void 0!==c[t[this._current]])e.push({start:this._current,type:c[t[this._current]],value:t[this._current]}),this._current+=1;else if(s(t[this._current]))i=this.consumeNumber(t),e.push(i);else if("["===t[this._current])i=this.consumeLBracket(t),e.push(i);else if('"'===t[this._current])r=this._current,n=this.consumeQuotedIdentifier(t),e.push({start:r,type:o.TOK_QUOTEDIDENTIFIER,value:n});else if("'"===t[this._current])r=this._current,n=this.consumeRawStringLiteral(t),e.push({start:r,type:o.TOK_LITERAL,value:n});else if("`"===t[this._current]){r=this._current;const n=this.consumeLiteral(t);e.push({start:r,type:o.TOK_LITERAL,value:n})}else if(void 0!==h[t[this._current]])i=this.consumeOperator(t),i&&e.push(i);else if(void 0!==u[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:o.TOK_AND,value:"&&"})):e.push({start:r,type:o.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:o.TOK_OR,value:"||"})):e.push({start:r,type:o.TOK_PIPE,value:"|"})}var T;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(;s(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:o.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:o.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:o.TOK_FLATTEN,value:"[]"}):{start:e,type:o.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:o.TOK_NE,value:"!="}):{start:e,type:o.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:o.TOK_LTE,value:"<="}):{start:e,type:o.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:o.TOK_GTE,value:">="}):{start:e,type:o.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:o.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=i(t.slice(e,this._current));n=n.replace("\\`","`");const s=this.looksLikeJSON(n)?JSON.parse(n):JSON.parse(`"${n}"`);return this._current+=1,s}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={[o.TOK_EOF]:0,[o.TOK_UNQUOTEDIDENTIFIER]:0,[o.TOK_QUOTEDIDENTIFIER]:0,[o.TOK_RBRACKET]:0,[o.TOK_RPAREN]:0,[o.TOK_COMMA]:0,[o.TOK_RBRACE]:0,[o.TOK_NUMBER]:0,[o.TOK_CURRENT]:0,[o.TOK_EXPREF]:0,[o.TOK_PIPE]:1,[o.TOK_OR]:2,[o.TOK_AND]:3,[o.TOK_EQ]:5,[o.TOK_GT]:5,[o.TOK_LT]:5,[o.TOK_GTE]:5,[o.TOK_LTE]:5,[o.TOK_NE]:5,[o.TOK_FLATTEN]:9,[o.TOK_STAR]:20,[o.TOK_FILTER]:21,[o.TOK_DOT]:40,[o.TOK_NOT]:45,[o.TOK_LBRACE]:50,[o.TOK_LBRACKET]:55,[o.TOK_LPAREN]:60};const _=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)!==o.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Unexpected token type: ${t.type}, value: ${t.value}`)}return e}loadTokens(t){this.tokens=[...T.tokenize(t),{type:o.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 o.TOK_LITERAL:return{type:"Literal",value:t.value};case o.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case o.TOK_QUOTEDIDENTIFIER:const i={type:"Field",name:t.value};if(this.lookahead(0)===o.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return i;case o.TOK_NOT:return r=this.expression(a.Not),{type:"NotExpression",children:[r]};case o.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===o.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(a.Star),{type:"ValueProjection",children:[e,r]};case o.TOK_FILTER:return this.led(t.type,{type:"Identity"});case o.TOK_LBRACE:return this.parseMultiselectHash();case o.TOK_FLATTEN:return e={type:o.TOK_FLATTEN,children:[{type:"Identity"}]},r=this.parseProjectionRHS(a.Flatten),{type:"Projection",children:[e,r]};case o.TOK_LBRACKET:return this.lookahead(0)===o.TOK_NUMBER||this.lookahead(0)===o.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},r)):this.lookahead(0)===o.TOK_STAR&&this.lookahead(1)===o.TOK_RBRACKET?(this.advance(),this.advance(),r=this.parseProjectionRHS(a.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case o.TOK_CURRENT:return{type:o.TOK_CURRENT};case o.TOK_EXPREF:return n=this.expression(a.Expref),{type:"ExpressionReference",children:[n]};case o.TOK_LPAREN:const s=[];for(;this.lookahead(0)!==o.TOK_RPAREN;)this.lookahead(0)===o.TOK_CURRENT?(n={type:o.TOK_CURRENT},this.advance()):n=this.expression(0),s.push(n);return this.match(o.TOK_RPAREN),s[0];default:this.errorToken(t)}}led(t,e){let r;switch(t){case o.TOK_DOT:const n=a.Dot;return this.lookahead(0)!==o.TOK_STAR?(r=this.parseDotRHS(n),{type:"Subexpression",children:[e,r]}):(this.advance(),r=this.parseProjectionRHS(n),{type:"ValueProjection",children:[e,r]});case o.TOK_PIPE:return r=this.expression(a.Pipe),{type:o.TOK_PIPE,children:[e,r]};case o.TOK_OR:return r=this.expression(a.Or),{type:"OrExpression",children:[e,r]};case o.TOK_AND:return r=this.expression(a.And),{type:"AndExpression",children:[e,r]};case o.TOK_LPAREN:const i=e.name,s=[];let c;for(;this.lookahead(0)!==o.TOK_RPAREN;)this.lookahead(0)===o.TOK_CURRENT?(c={type:o.TOK_CURRENT},this.advance()):c=this.expression(0),this.lookahead(0)===o.TOK_COMMA&&this.match(o.TOK_COMMA),s.push(c);this.match(o.TOK_RPAREN);return{name:i,type:"Function",children:s};case o.TOK_FILTER:const h=this.expression(0);return this.match(o.TOK_RBRACKET),r=this.lookahead(0)===o.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(a.Filter),{type:"FilterProjection",children:[e,r,h]};case o.TOK_FLATTEN:return{type:"Projection",children:[{type:o.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(a.Flatten)]};case o.TOK_EQ:case o.TOK_NE:case o.TOK_GT:case o.TOK_GTE:case o.TOK_LT:case o.TOK_LTE:return this.parseComparator(e,t);case o.TOK_LBRACKET:const u=this.lookaheadToken(0);return u.type===o.TOK_NUMBER||u.type===o.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(o.TOK_STAR),this.match(o.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)===o.TOK_COLON||this.lookahead(1)===o.TOK_COLON)return this.parseSliceExpression();const t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(o.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!==o.TOK_RBRACKET&&e<3;){if(r===o.TOK_COLON)e+=1,this.advance();else if(r===o.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(o.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([o.TOK_UNQUOTEDIDENTIFIER,o.TOK_QUOTEDIDENTIFIER,o.TOK_STAR].includes(e))return this.expression(t);if(e===o.TOK_LBRACKET)return this.match(o.TOK_LBRACKET),this.parseMultiselectList();if(e===o.TOK_LBRACE)return this.match(o.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)===o.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===o.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===o.TOK_DOT)return this.match(o.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)!==o.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===o.TOK_COMMA&&(this.match(o.TOK_COMMA),this.lookahead(0)===o.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(o.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[o.TOK_UNQUOTEDIDENTIFIER,o.TOK_QUOTEDIDENTIFIER];let r,n,i,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(o.TOK_COLON),i=this.expression(0),s={value:i,type:"KeyValuePair",name:n},t.push(s),this.lookahead(0)===o.TOK_COMMA)this.match(o.TOK_COMMA);else if(this.lookahead(0)===o.TOK_RBRACE){this.match(o.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}};var l;!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"}(l||(l={}));class E{constructor(t){this.TYPE_NAME_TABLE={[l.TYPE_NUMBER]:"number",[l.TYPE_ANY]:"any",[l.TYPE_STRING]:"string",[l.TYPE_ARRAY]:"array",[l.TYPE_OBJECT]:"object",[l.TYPE_BOOLEAN]:"boolean",[l.TYPE_EXPREF]:"expression",[l.TYPE_NULL]:"null",[l.TYPE_ARRAY_NUMBER]:"Array<number>",[l.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])===l.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,[l.TYPE_NUMBER,l.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])===l.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,[l.TYPE_NUMBER,l.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])!==l.TYPE_NULL)return t[e];return null},this.functionReverse=([t])=>{if(this.getTypeName(t)===l.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&&![l.TYPE_NUMBER,l.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)===l.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===l.TYPE_NUMBER?t:e!==l.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===l.TYPE_STRING?t:JSON.stringify(t),this.functionType=([t])=>{switch(this.getTypeName(t)){case l.TYPE_NUMBER:return"number";case l.TYPE_STRING:return"string";case l.TYPE_ARRAY:return"array";case l.TYPE_OBJECT:return"object";case l.TYPE_BOOLEAN:return"boolean";case l.TYPE_EXPREF:return"expref";case l.TYPE_NULL:return"null";default:return}},this.functionValues=([t])=>Object.values(t),this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[l.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[l.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[l.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY]},{types:[l.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[l.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[l.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY,l.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[l.TYPE_EXPREF]},{types:[l.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[l.TYPE_ARRAY_NUMBER,l.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[l.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[l.TYPE_ARRAY_NUMBER,l.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[l.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[l.TYPE_ARRAY_STRING,l.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[l.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[l.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[l.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[l.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[l.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[l.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)}validateArgs(t,e,r){let n,i,s,o;if(r[r.length-1].variadic){if(e.length<r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes at least ${r.length} argument${n?"s":""} but received ${e.length}`)}else if(e.length!==r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes ${r.length} argument${n?"s":""} but received ${e.length}`);for(let n=0;n<r.length;n+=1){let c;for(o=!1,i=r[n].types,s=this.getTypeName(e[n]),c=0;c<i.length;c+=1)if(void 0!==s&&this.typeMatches(s,i[c],e[n])){o=!0;break}if(!o){const e=i.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[s]} instead.`)}}}typeMatches(t,e,r){if(e===l.TYPE_ANY)return!0;if(e!==l.TYPE_ARRAY_STRING&&e!==l.TYPE_ARRAY_NUMBER&&e!==l.TYPE_ARRAY)return t===e;if(e===l.TYPE_ARRAY)return t===l.TYPE_ARRAY;if(t===l.TYPE_ARRAY){let t;e===l.TYPE_ARRAY_NUMBER?t=l.TYPE_NUMBER:e===l.TYPE_ARRAY_STRING&&(t=l.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 l.TYPE_STRING;case"[object Number]":return l.TYPE_NUMBER;case"[object Array]":return l.TYPE_ARRAY;case"[object Boolean]":return l.TYPE_BOOLEAN;case"[object Null]":return l.TYPE_NULL;case"[object Object]":return t.jmespathType===o.TOK_EXPREF?l.TYPE_EXPREF:l.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 R=new class{constructor(){this.runtime=new E(this)}search(t,e){return this.visit(t,e)}visit(t,i){let s,c,h,u,T,a,_,l,E,R,p;switch(t.type){case"Field":return null===i?null:e(i)?(a=i[t.name],void 0===a?null:a):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)s=this.visit(t.children[2],p[R]),n(s)||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),T=this.visit(t.children[1],i),t.name){case o.TOK_EQ:h=r(u,T);break;case o.TOK_NE:h=!r(u,T);break;case o.TOK_GT:h=u>T;break;case o.TOK_GTE:h=u>=T;break;case o.TOK_LT:h=u<T;break;case o.TOK_LTE:h=u<=T;break;default:throw new Error("Unknown comparator: "+t.name)}return h;case o.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 s=this.visit(t.children[0],i),n(s)&&(s=this.visit(t.children[1],i)),s;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 o.TOK_PIPE:return _=this.visit(t.children[0],i),this.visit(t.children[1],_);case o.TOK_CURRENT:return i;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=o.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}},p=l.TYPE_ANY,O=l.TYPE_ARRAY,f=l.TYPE_ARRAY_NUMBER,A=l.TYPE_ARRAY_STRING,d=l.TYPE_BOOLEAN,N=l.TYPE_EXPREF,P=l.TYPE_NULL,y=l.TYPE_NUMBER,K=l.TYPE_OBJECT,Y=l.TYPE_STRING;function g(t){return _.parse(t)}function v(t){return T.tokenize(t)}const L=(t,e,r)=>{R.runtime.registerFunction(t,e,r)};function m(t,e){const r=_.parse(e);return R.search(r,t)}const I=R,k={compile:g,registerFunction:L,search:m,tokenize:v,TreeInterpreter:I,TYPE_ANY:p,TYPE_ARRAY_NUMBER:f,TYPE_ARRAY_STRING:A,TYPE_ARRAY:O,TYPE_BOOLEAN:d,TYPE_EXPREF:N,TYPE_NULL:P,TYPE_NUMBER:y,TYPE_OBJECT:K,TYPE_STRING:Y};t.TYPE_ANY=p,t.TYPE_ARRAY=O,t.TYPE_ARRAY_NUMBER=f,t.TYPE_ARRAY_STRING=A,t.TYPE_BOOLEAN=d,t.TYPE_EXPREF=N,t.TYPE_NULL=P,t.TYPE_NUMBER=y,t.TYPE_OBJECT=K,t.TYPE_STRING=Y,t.TreeInterpreter=I,t.compile=g,t.default=k,t.jmespath=k,t.registerFunction=L,t.search=m,t.tokenize=v,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="function"==typeof String.prototype.trimLeft?t=>t.trimLeft():t=>{const e=/^\s*(.*)/.exec(t);return e&&e[1]},s=t=>t>="0"&&t<="9"||"-"===t;var o;!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_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"}(o||(o={}));const c={"(":o.TOK_LPAREN,")":o.TOK_RPAREN,"*":o.TOK_STAR,",":o.TOK_COMMA,".":o.TOK_DOT,":":o.TOK_COLON,"@":o.TOK_CURRENT,"]":o.TOK_RBRACKET,"{":o.TOK_LBRACE,"}":o.TOK_RBRACE},h={"!":!0,"<":!0,"=":!0,">":!0},u={"\t":!0,"\n":!0,"\r":!0," ":!0};const T=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,n,i;for(this._current=0;this._current<t.length;)if((T=t[this._current])>="a"&&T<="z"||T>="A"&&T<="Z"||"_"===T)r=this._current,n=this.consumeUnquotedIdentifier(t),e.push({start:r,type:o.TOK_UNQUOTEDIDENTIFIER,value:n});else if(void 0!==c[t[this._current]])e.push({start:this._current,type:c[t[this._current]],value:t[this._current]}),this._current+=1;else if(s(t[this._current]))i=this.consumeNumber(t),e.push(i);else if("["===t[this._current])i=this.consumeLBracket(t),e.push(i);else if('"'===t[this._current])r=this._current,n=this.consumeQuotedIdentifier(t),e.push({start:r,type:o.TOK_QUOTEDIDENTIFIER,value:n});else if("'"===t[this._current])r=this._current,n=this.consumeRawStringLiteral(t),e.push({start:r,type:o.TOK_LITERAL,value:n});else if("`"===t[this._current]){r=this._current;const n=this.consumeLiteral(t);e.push({start:r,type:o.TOK_LITERAL,value:n})}else if(void 0!==h[t[this._current]])i=this.consumeOperator(t),i&&e.push(i);else if(void 0!==u[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:o.TOK_AND,value:"&&"})):e.push({start:r,type:o.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:o.TOK_OR,value:"||"})):e.push({start:r,type:o.TOK_PIPE,value:"|"})}var T;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(;s(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:o.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:o.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:o.TOK_FLATTEN,value:"[]"}):{start:e,type:o.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:o.TOK_NE,value:"!="}):{start:e,type:o.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:o.TOK_LTE,value:"<="}):{start:e,type:o.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:o.TOK_GTE,value:">="}):{start:e,type:o.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:o.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=i(t.slice(e,this._current));n=n.replace("\\`","`");const s=this.looksLikeJSON(n)?JSON.parse(n):JSON.parse(`"${n}"`);return this._current+=1,s}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={[o.TOK_EOF]:0,[o.TOK_UNQUOTEDIDENTIFIER]:0,[o.TOK_QUOTEDIDENTIFIER]:0,[o.TOK_RBRACKET]:0,[o.TOK_RPAREN]:0,[o.TOK_COMMA]:0,[o.TOK_RBRACE]:0,[o.TOK_NUMBER]:0,[o.TOK_CURRENT]:0,[o.TOK_EXPREF]:0,[o.TOK_PIPE]:1,[o.TOK_OR]:2,[o.TOK_AND]:3,[o.TOK_EQ]:5,[o.TOK_GT]:5,[o.TOK_LT]:5,[o.TOK_GTE]:5,[o.TOK_LTE]:5,[o.TOK_NE]:5,[o.TOK_FLATTEN]:9,[o.TOK_STAR]:20,[o.TOK_FILTER]:21,[o.TOK_DOT]:40,[o.TOK_NOT]:45,[o.TOK_LBRACE]:50,[o.TOK_LBRACKET]:55,[o.TOK_LPAREN]:60};const _=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)!==o.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Unexpected token type: ${t.type}, value: ${t.value}`)}return e}loadTokens(t){this.tokens=[...T.tokenize(t),{type:o.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 o.TOK_LITERAL:return{type:"Literal",value:t.value};case o.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case o.TOK_QUOTEDIDENTIFIER:const i={type:"Field",name:t.value};if(this.lookahead(0)===o.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return i;case o.TOK_NOT:return r=this.expression(a.Not),{type:"NotExpression",children:[r]};case o.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===o.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(a.Star),{type:"ValueProjection",children:[e,r]};case o.TOK_FILTER:return this.led(t.type,{type:"Identity"});case o.TOK_LBRACE:return this.parseMultiselectHash();case o.TOK_FLATTEN:return e={type:o.TOK_FLATTEN,children:[{type:"Identity"}]},r=this.parseProjectionRHS(a.Flatten),{type:"Projection",children:[e,r]};case o.TOK_LBRACKET:return this.lookahead(0)===o.TOK_NUMBER||this.lookahead(0)===o.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},r)):this.lookahead(0)===o.TOK_STAR&&this.lookahead(1)===o.TOK_RBRACKET?(this.advance(),this.advance(),r=this.parseProjectionRHS(a.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case o.TOK_CURRENT:return{type:o.TOK_CURRENT};case o.TOK_EXPREF:return n=this.expression(a.Expref),{type:"ExpressionReference",children:[n]};case o.TOK_LPAREN:const s=[];for(;this.lookahead(0)!==o.TOK_RPAREN;)this.lookahead(0)===o.TOK_CURRENT?(n={type:o.TOK_CURRENT},this.advance()):n=this.expression(0),s.push(n);return this.match(o.TOK_RPAREN),s[0];default:this.errorToken(t)}}led(t,e){let r;switch(t){case o.TOK_DOT:const n=a.Dot;return this.lookahead(0)!==o.TOK_STAR?(r=this.parseDotRHS(n),{type:"Subexpression",children:[e,r]}):(this.advance(),r=this.parseProjectionRHS(n),{type:"ValueProjection",children:[e,r]});case o.TOK_PIPE:return r=this.expression(a.Pipe),{type:o.TOK_PIPE,children:[e,r]};case o.TOK_OR:return r=this.expression(a.Or),{type:"OrExpression",children:[e,r]};case o.TOK_AND:return r=this.expression(a.And),{type:"AndExpression",children:[e,r]};case o.TOK_LPAREN:const i=e.name,s=[];let c;for(;this.lookahead(0)!==o.TOK_RPAREN;)this.lookahead(0)===o.TOK_CURRENT?(c={type:o.TOK_CURRENT},this.advance()):c=this.expression(0),this.lookahead(0)===o.TOK_COMMA&&this.match(o.TOK_COMMA),s.push(c);this.match(o.TOK_RPAREN);return{name:i,type:"Function",children:s};case o.TOK_FILTER:const h=this.expression(0);return this.match(o.TOK_RBRACKET),r=this.lookahead(0)===o.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(a.Filter),{type:"FilterProjection",children:[e,r,h]};case o.TOK_FLATTEN:return{type:"Projection",children:[{type:o.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(a.Flatten)]};case o.TOK_EQ:case o.TOK_NE:case o.TOK_GT:case o.TOK_GTE:case o.TOK_LT:case o.TOK_LTE:return this.parseComparator(e,t);case o.TOK_LBRACKET:const u=this.lookaheadToken(0);return u.type===o.TOK_NUMBER||u.type===o.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(o.TOK_STAR),this.match(o.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)===o.TOK_COLON||this.lookahead(1)===o.TOK_COLON)return this.parseSliceExpression();const t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(o.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!==o.TOK_RBRACKET&&e<3;){if(r===o.TOK_COLON)e+=1,this.advance();else if(r===o.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(o.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([o.TOK_UNQUOTEDIDENTIFIER,o.TOK_QUOTEDIDENTIFIER,o.TOK_STAR].includes(e))return this.expression(t);if(e===o.TOK_LBRACKET)return this.match(o.TOK_LBRACKET),this.parseMultiselectList();if(e===o.TOK_LBRACE)return this.match(o.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)===o.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===o.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===o.TOK_DOT)return this.match(o.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)!==o.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===o.TOK_COMMA&&(this.match(o.TOK_COMMA),this.lookahead(0)===o.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(o.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[o.TOK_UNQUOTEDIDENTIFIER,o.TOK_QUOTEDIDENTIFIER];let r,n,i,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(o.TOK_COLON),i=this.expression(0),s={value:i,type:"KeyValuePair",name:n},t.push(s),this.lookahead(0)===o.TOK_COMMA)this.match(o.TOK_COMMA);else if(this.lookahead(0)===o.TOK_RBRACE){this.match(o.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}};var l;!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"}(l||(l={}));class E{constructor(t){this.TYPE_NAME_TABLE={[l.TYPE_NUMBER]:"number",[l.TYPE_ANY]:"any",[l.TYPE_STRING]:"string",[l.TYPE_ARRAY]:"array",[l.TYPE_OBJECT]:"object",[l.TYPE_BOOLEAN]:"boolean",[l.TYPE_EXPREF]:"expression",[l.TYPE_NULL]:"null",[l.TYPE_ARRAY_NUMBER]:"Array<number>",[l.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])===l.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,[l.TYPE_NUMBER,l.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])===l.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,[l.TYPE_NUMBER,l.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])!==l.TYPE_NULL)return t[e];return null},this.functionReverse=([t])=>{if(this.getTypeName(t)===l.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&&![l.TYPE_NUMBER,l.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)===l.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===l.TYPE_NUMBER?t:e!==l.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===l.TYPE_STRING?t:JSON.stringify(t),this.functionType=([t])=>{switch(this.getTypeName(t)){case l.TYPE_NUMBER:return"number";case l.TYPE_STRING:return"string";case l.TYPE_ARRAY:return"array";case l.TYPE_OBJECT:return"object";case l.TYPE_BOOLEAN:return"boolean";case l.TYPE_EXPREF:return"expref";case l.TYPE_NULL:return"null";default:return}},this.functionValues=([t])=>Object.values(t),this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[l.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[l.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[l.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY]},{types:[l.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[l.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[l.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY,l.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[l.TYPE_EXPREF]},{types:[l.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[l.TYPE_ARRAY_NUMBER,l.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[l.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[l.TYPE_ARRAY_NUMBER,l.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[l.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[l.TYPE_STRING,l.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[l.TYPE_ARRAY_STRING,l.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[l.TYPE_ARRAY]},{types:[l.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[l.TYPE_STRING]},{types:[l.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[l.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[l.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[l.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[l.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[l.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[l.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)}validateArgs(t,e,r){let n,i,s,o;if(r[r.length-1].variadic){if(e.length<r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes at least ${r.length} argument${n?"s":""} but received ${e.length}`)}else if(e.length!==r.length)throw n=r.length>1,new Error(`ArgumentError: ${t}() takes ${r.length} argument${n?"s":""} but received ${e.length}`);for(let n=0;n<r.length;n+=1){let c;for(o=!1,i=r[n].types,s=this.getTypeName(e[n]),c=0;c<i.length;c+=1)if(void 0!==s&&this.typeMatches(s,i[c],e[n])){o=!0;break}if(!o){const e=i.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[s]} instead.`)}}}typeMatches(t,e,r){if(e===l.TYPE_ANY)return!0;if(e!==l.TYPE_ARRAY_STRING&&e!==l.TYPE_ARRAY_NUMBER&&e!==l.TYPE_ARRAY)return t===e;if(e===l.TYPE_ARRAY)return t===l.TYPE_ARRAY;if(t===l.TYPE_ARRAY){let t;e===l.TYPE_ARRAY_NUMBER?t=l.TYPE_NUMBER:e===l.TYPE_ARRAY_STRING&&(t=l.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 l.TYPE_STRING;case"[object Number]":return l.TYPE_NUMBER;case"[object Array]":return l.TYPE_ARRAY;case"[object Boolean]":return l.TYPE_BOOLEAN;case"[object Null]":return l.TYPE_NULL;case"[object Object]":return t.jmespathType===o.TOK_EXPREF?l.TYPE_EXPREF:l.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 R=new class{constructor(){this.runtime=new E(this)}search(t,e){return this.visit(t,e)}visit(t,i){let s,c,h,u,T,a,_,l,E,R,p;switch(t.type){case"Field":return null===i?null:e(i)?(a=i[t.name],void 0===a?null:a):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)s=this.visit(t.children[2],p[R]),n(s)||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),T=this.visit(t.children[1],i),t.name){case o.TOK_EQ:h=r(u,T);break;case o.TOK_NE:h=!r(u,T);break;case o.TOK_GT:h=u>T;break;case o.TOK_GTE:h=u>=T;break;case o.TOK_LT:h=u<T;break;case o.TOK_LTE:h=u<=T;break;default:throw new Error("Unknown comparator: "+t.name)}return h;case o.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 s=this.visit(t.children[0],i),n(s)&&(s=this.visit(t.children[1],i)),s;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 o.TOK_PIPE:return _=this.visit(t.children[0],i),this.visit(t.children[1],_);case o.TOK_CURRENT:return i;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=o.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}},p=l.TYPE_ANY,O=l.TYPE_ARRAY,f=l.TYPE_ARRAY_NUMBER,A=l.TYPE_ARRAY_STRING,d=l.TYPE_BOOLEAN,N=l.TYPE_EXPREF,P=l.TYPE_NULL,y=l.TYPE_NUMBER,K=l.TYPE_OBJECT,Y=l.TYPE_STRING;function g(t){return _.parse(t)}function v(t){return T.tokenize(t)}const L=(t,e,r)=>{R.runtime.registerFunction(t,e,r)};function m(t,e){const r=_.parse(e);return R.search(r,t)}const I=R,k={compile:g,registerFunction:L,search:m,tokenize:v,TreeInterpreter:I,TYPE_ANY:p,TYPE_ARRAY_NUMBER:f,TYPE_ARRAY_STRING:A,TYPE_ARRAY:O,TYPE_BOOLEAN:d,TYPE_EXPREF:N,TYPE_NULL:P,TYPE_NUMBER:y,TYPE_OBJECT:K,TYPE_STRING:Y};t.TYPE_ANY=p,t.TYPE_ARRAY=O,t.TYPE_ARRAY_NUMBER=f,t.TYPE_ARRAY_STRING=A,t.TYPE_BOOLEAN=d,t.TYPE_EXPREF=N,t.TYPE_NULL=P,t.TYPE_NUMBER=y,t.TYPE_OBJECT=K,t.TYPE_STRING=Y,t.TreeInterpreter=I,t.compile=g,t.default=k,t.jmespath=k,t.registerFunction=L,t.search=m,t.tokenize=v,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=jmespath.umd.min.js.map |
{ | ||
"name": "@metrichor/jmespath", | ||
"description": "Typescript implementation of the JMESPath spec (100% compliant)", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"author": { | ||
@@ -62,40 +62,40 @@ "name": "Oxford Nanopore Technologies", | ||
"devDependencies": { | ||
"@commitlint/cli": "^9.1.2", | ||
"@commitlint/config-conventional": "^9.1.1", | ||
"@rollup/plugin-commonjs": "^14.0.0", | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"@rollup/plugin-commonjs": "^15.1.0", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^8.4.0", | ||
"@rollup/plugin-typescript": "^5.0.2", | ||
"@types/jest": "^26.0.7", | ||
"@types/node": "^14.0.27", | ||
"@typescript-eslint/eslint-plugin": "^3.7.1", | ||
"@typescript-eslint/parser": "^3.7.1", | ||
"@rollup/plugin-node-resolve": "^9.0.0", | ||
"@rollup/plugin-typescript": "^6.0.0", | ||
"@types/jest": "^26.0.14", | ||
"@types/node": "^14.11.8", | ||
"@typescript-eslint/eslint-plugin": "^4.4.0", | ||
"@typescript-eslint/parser": "^4.4.0", | ||
"benchmark": "^2.1.4", | ||
"colors": "^1.4.0", | ||
"commitizen": "^4.1.2", | ||
"commitizen": "^4.2.1", | ||
"coveralls": "^3.1.0", | ||
"cz-conventional-changelog": "^3.2.0", | ||
"eslint": "^7.5.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"eslint": "^7.10.0", | ||
"eslint-config-prettier": "^6.12.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"husky": "^4.2.5", | ||
"jest": "^26.1.0", | ||
"jest-cli": "^26.1.0", | ||
"jest-config": "^26.1.0", | ||
"lint-staged": "^10.2.11", | ||
"prettier": "^2.0.5", | ||
"husky": "^4.3.0", | ||
"jest": "^26.5.2", | ||
"jest-cli": "^26.5.2", | ||
"jest-config": "^26.5.2", | ||
"lint-staged": "^10.4.0", | ||
"prettier": "^2.1.2", | ||
"prettier-eslint": "^11.0.0", | ||
"prompt": "^1.0.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.23.0", | ||
"rollup-plugin-sourcemaps": "^0.6.2", | ||
"rollup-plugin-terser": "^6.1.0", | ||
"semantic-release": "^17.1.1", | ||
"rollup": "^2.29.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"semantic-release": "^17.1.2", | ||
"shelljs": "^0.8.4", | ||
"ts-jest": "^26.1.4", | ||
"ts-node": "^8.10.2", | ||
"typedoc": "^0.17.8", | ||
"typescript": "^3.9.7" | ||
"ts-jest": "^26.4.1", | ||
"ts-node": "^9.0.0", | ||
"typedoc": "^0.19.2", | ||
"typescript": "^4.0.3" | ||
}, | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
508702