@metrichor/jmespath
Version:
Typescript implementation of the JMESPath spec (100% compliant)
3 lines (2 loc) • 25.6 kB
JavaScript
const t=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),e=(r,n)=>{if(r===n)return!0;if(typeof r!=typeof n)return!1;if(Array.isArray(r)&&Array.isArray(n)){if(r.length!==n.length)return!1;for(let t=0;t<r.length;t+=1)if(!e(r[t],n[t]))return!1;return!0}if(t(r)&&t(n)){const t=Object.entries(r),i=new Set(Object.keys(n));if(t.length!==i.size)return!1;for(const[r,s]of t){if(!e(s,n[r]))return!1;i.delete(r)}return 0===i.size}return!1},r=e=>{if(""===e||!1===e||null==e)return!0;if(Array.isArray(e)&&0===e.length)return!0;if(t(e)){for(const t in e)if(e.hasOwnProperty(t))return!1;return!0}return!1},n=t=>t>="0"&&t<="9"||"-"===t;var i;!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(i||(i={}));const s={"(":i.TOK_LPAREN,")":i.TOK_RPAREN,"*":i.TOK_STAR,",":i.TOK_COMMA,".":i.TOK_DOT,":":i.TOK_COLON,"@":i.TOK_CURRENT,$:i.TOK_ROOT,"]":i.TOK_RBRACKET,"{":i.TOK_LBRACE,"}":i.TOK_RBRACE},o={"!":!0,"<":!0,"=":!0,">":!0},h={"\t":!0,"\n":!0,"\r":!0," ":!0};const c=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,c,u;for(this._current=0;this._current<t.length;)if((a=t[this._current])>="a"&&a<="z"||a>="A"&&a<="Z"||"_"===a)r=this._current,c=this.consumeUnquotedIdentifier(t),e.push({start:r,type:i.TOK_UNQUOTEDIDENTIFIER,value:c});else if(void 0!==s[t[this._current]])e.push({start:this._current,type:s[t[this._current]],value:t[this._current]}),this._current+=1;else if(n(t[this._current]))u=this.consumeNumber(t),e.push(u);else if("["===t[this._current])u=this.consumeLBracket(t),e.push(u);else if('"'===t[this._current])r=this._current,c=this.consumeQuotedIdentifier(t),e.push({start:r,type:i.TOK_QUOTEDIDENTIFIER,value:c});else if("'"===t[this._current])r=this._current,c=this.consumeRawStringLiteral(t),e.push({start:r,type:i.TOK_LITERAL,value:c});else if("`"===t[this._current]){r=this._current;const n=this.consumeLiteral(t);e.push({start:r,type:i.TOK_LITERAL,value:n})}else if(void 0!==o[t[this._current]])u=this.consumeOperator(t),u&&e.push(u);else if(void 0!==h[t[this._current]])this._current+=1;else if("&"===t[this._current])r=this._current,this._current+=1,"&"===t[this._current]?(this._current+=1,e.push({start:r,type:i.TOK_AND,value:"&&"})):e.push({start:r,type:i.TOK_EXPREF,value:"&"});else{if("|"!==t[this._current]){const e=new Error(`Unknown character: ${t[this._current]}`);throw e.name="LexerError",e}r=this._current,this._current+=1,"|"===t[this._current]?(this._current+=1,e.push({start:r,type:i.TOK_OR,value:"||"})):e.push({start:r,type:i.TOK_PIPE,value:"|"})}var a;return e}consumeUnquotedIdentifier(t){const e=this._current;for(this._current+=1;this._current<t.length&&((r=t[this._current])>="a"&&r<="z"||r>="A"&&r<="Z"||r>="0"&&r<="9"||"_"===r);)this._current+=1;var r;return t.slice(e,this._current)}consumeQuotedIdentifier(t){const e=this._current;this._current+=1;const r=t.length;for(;'"'!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&'"'!==t[e+1]?e+=1:e+=2,this._current=e}return this._current+=1,JSON.parse(t.slice(e,this._current))}consumeRawStringLiteral(t){const e=this._current;this._current+=1;const r=t.length;for(;"'"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"'"!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;return t.slice(e+1,this._current-1).replace("\\'","'")}consumeNumber(t){const e=this._current;this._current+=1;const r=t.length;for(;n(t[this._current])&&this._current<r;)this._current+=1;return{start:e,value:parseInt(t.slice(e,this._current),10),type:i.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:i.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:i.TOK_FLATTEN,value:"[]"}):{start:e,type:i.TOK_LBRACKET,value:"["}}consumeOperator(t){const e=this._current,r=t[e];return this._current+=1,"!"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_NE,value:"!="}):{start:e,type:i.TOK_NOT,value:"!"}:"<"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_LTE,value:"<="}):{start:e,type:i.TOK_LT,value:"<"}:">"===r?"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_GTE,value:">="}):{start:e,type:i.TOK_GT,value:">"}:"="===r&&"="===t[this._current]?(this._current+=1,{start:e,type:i.TOK_EQ,value:"=="}):void 0}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimLeft();n=n.replace("\\`","`");const i=this.looksLikeJSON(n)?JSON.parse(n):JSON.parse(`"${n}"`);return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0]))try{return JSON.parse(t),!0}catch(t){return!1}return!1}},u={[i.TOK_EOF]:0,[i.TOK_UNQUOTEDIDENTIFIER]:0,[i.TOK_QUOTEDIDENTIFIER]:0,[i.TOK_RBRACKET]:0,[i.TOK_RPAREN]:0,[i.TOK_COMMA]:0,[i.TOK_RBRACE]:0,[i.TOK_NUMBER]:0,[i.TOK_CURRENT]:0,[i.TOK_EXPREF]:0,[i.TOK_ROOT]:0,[i.TOK_PIPE]:1,[i.TOK_OR]:2,[i.TOK_AND]:3,[i.TOK_EQ]:5,[i.TOK_GT]:5,[i.TOK_LT]:5,[i.TOK_GTE]:5,[i.TOK_LTE]:5,[i.TOK_NE]:5,[i.TOK_FLATTEN]:9,[i.TOK_STAR]:20,[i.TOK_FILTER]:21,[i.TOK_DOT]:40,[i.TOK_NOT]:45,[i.TOK_LBRACE]:50,[i.TOK_LBRACKET]:55,[i.TOK_LPAREN]:60};const a=new class{constructor(){this.index=0,this.tokens=[]}parse(t){this.loadTokens(t),this.index=0;const e=this.expression(0);if(this.lookahead(0)!==i.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Unexpected token type: ${t.type}, value: ${t.value}`)}return e}loadTokens(t){this.tokens=[...c.tokenize(t),{type:i.TOK_EOF,value:"",start:t.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<u[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r}lookahead(t){return this.tokens[this.index+t].type}lookaheadToken(t){return this.tokens[this.index+t]}advance(){this.index+=1}nud(t){let e,r,n;switch(t.type){case i.TOK_LITERAL:return{type:"Literal",value:t.value};case i.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:t.value};case i.TOK_QUOTEDIDENTIFIER:const s={type:"Field",name:t.value};if(this.lookahead(0)===i.TOK_LPAREN)throw new Error("Quoted identifier not allowed for function names.");return s;case i.TOK_NOT:return r=this.expression(u.Not),{type:"NotExpression",children:[r]};case i.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===i.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(u.Star),{type:"ValueProjection",children:[e,r]};case i.TOK_FILTER:return this.led(t.type,{type:"Identity"});case i.TOK_LBRACE:return this.parseMultiselectHash();case i.TOK_FLATTEN:return e={type:i.TOK_FLATTEN,children:[{type:"Identity"}]},r=this.parseProjectionRHS(u.Flatten),{type:"Projection",children:[e,r]};case i.TOK_LBRACKET:return this.lookahead(0)===i.TOK_NUMBER||this.lookahead(0)===i.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice({type:"Identity"},r)):this.lookahead(0)===i.TOK_STAR&&this.lookahead(1)===i.TOK_RBRACKET?(this.advance(),this.advance(),r=this.parseProjectionRHS(u.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case i.TOK_CURRENT:return{type:i.TOK_CURRENT};case i.TOK_ROOT:return{type:i.TOK_ROOT};case i.TOK_EXPREF:return n=this.expression(u.Expref),{type:"ExpressionReference",children:[n]};case i.TOK_LPAREN:const o=[];for(;this.lookahead(0)!==i.TOK_RPAREN;)this.lookahead(0)===i.TOK_CURRENT?(n={type:i.TOK_CURRENT},this.advance()):n=this.expression(0),o.push(n);return this.match(i.TOK_RPAREN),o[0];default:this.errorToken(t)}}led(t,e){let r;switch(t){case i.TOK_DOT:const n=u.Dot;return this.lookahead(0)!==i.TOK_STAR?(r=this.parseDotRHS(n),{type:"Subexpression",children:[e,r]}):(this.advance(),r=this.parseProjectionRHS(n),{type:"ValueProjection",children:[e,r]});case i.TOK_PIPE:return r=this.expression(u.Pipe),{type:i.TOK_PIPE,children:[e,r]};case i.TOK_OR:return r=this.expression(u.Or),{type:"OrExpression",children:[e,r]};case i.TOK_AND:return r=this.expression(u.And),{type:"AndExpression",children:[e,r]};case i.TOK_LPAREN:const s=e.name,o=[];let h;for(;this.lookahead(0)!==i.TOK_RPAREN;)this.lookahead(0)===i.TOK_CURRENT?(h={type:i.TOK_CURRENT},this.advance()):h=this.expression(0),this.lookahead(0)===i.TOK_COMMA&&this.match(i.TOK_COMMA),o.push(h);this.match(i.TOK_RPAREN);return{name:s,type:"Function",children:o};case i.TOK_FILTER:const c=this.expression(0);return this.match(i.TOK_RBRACKET),r=this.lookahead(0)===i.TOK_FLATTEN&&{type:"Identity"}||this.parseProjectionRHS(u.Filter),{type:"FilterProjection",children:[e,r,c]};case i.TOK_FLATTEN:return{type:"Projection",children:[{type:i.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(u.Flatten)]};case i.TOK_EQ:case i.TOK_NE:case i.TOK_GT:case i.TOK_GTE:case i.TOK_LT:case i.TOK_LTE:return this.parseComparator(e,t);case i.TOK_LBRACKET:const a=this.lookaheadToken(0);return a.type===i.TOK_NUMBER||a.type===i.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(i.TOK_STAR),this.match(i.TOK_RBRACKET),r=this.parseProjectionRHS(u.Star),{type:"Projection",children:[e,r]});default:return this.errorToken(this.lookaheadToken(0))}}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===i.TOK_COLON||this.lookahead(1)===i.TOK_COLON)return this.parseSliceExpression();const t={type:"Index",value:this.lookaheadToken(0).value};return this.advance(),this.match(i.TOK_RBRACKET),t}projectIfSlice(t,e){const r={type:"IndexExpression",children:[t,e]};return"Slice"===e.type?{children:[r,this.parseProjectionRHS(u.Star)],type:"Projection"}:r}parseSliceExpression(){const t=[null,null,null];let e=0,r=this.lookahead(0);for(;r!==i.TOK_RBRACKET&&e<3;){if(r===i.TOK_COLON)e+=1,this.advance();else if(r===i.TOK_NUMBER)t[e]=this.lookaheadToken(0).value,this.advance();else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}r=this.lookahead(0)}return this.match(i.TOK_RBRACKET),{children:t,type:"Slice"}}parseComparator(t,e){return{type:"Comparator",name:e,children:[t,this.expression(u[e])]}}parseDotRHS(t){const e=this.lookahead(0);if([i.TOK_UNQUOTEDIDENTIFIER,i.TOK_QUOTEDIDENTIFIER,i.TOK_STAR].includes(e))return this.expression(t);if(e===i.TOK_LBRACKET)return this.match(i.TOK_LBRACKET),this.parseMultiselectList();if(e===i.TOK_LBRACE)return this.match(i.TOK_LBRACE),this.parseMultiselectHash();const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseProjectionRHS(t){if(u[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===i.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===i.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===i.TOK_DOT)return this.match(i.TOK_DOT),this.parseDotRHS(t);const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error, unexpected token: ${e.value}(${e.type})`)}parseMultiselectList(){const t=[];for(;this.lookahead(0)!==i.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===i.TOK_COMMA&&(this.match(i.TOK_COMMA),this.lookahead(0)===i.TOK_RBRACKET))throw new Error("Unexpected token Rbracket")}return this.match(i.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[i.TOK_UNQUOTEDIDENTIFIER,i.TOK_QUOTEDIDENTIFIER];let r,n,s;for(;;){if(r=this.lookaheadToken(0),!e.includes(r.type))throw new Error(`Expecting an identifier token, got: ${r.type}`);if(n=r.value,this.advance(),this.match(i.TOK_COLON),s=this.expression(0),t.push({value:s,type:"KeyValuePair",name:n}),this.lookahead(0)===i.TOK_COMMA)this.match(i.TOK_COMMA);else if(this.lookahead(0)===i.TOK_RBRACE){this.match(i.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}};var T;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING"}(T||(T={}));class _{constructor(e){this.TYPE_NAME_TABLE={[T.TYPE_NUMBER]:"number",[T.TYPE_ANY]:"any",[T.TYPE_STRING]:"string",[T.TYPE_ARRAY]:"array",[T.TYPE_OBJECT]:"object",[T.TYPE_BOOLEAN]:"boolean",[T.TYPE_EXPREF]:"expression",[T.TYPE_NULL]:"null",[T.TYPE_ARRAY_NUMBER]:"Array<number>",[T.TYPE_ARRAY_STRING]:"Array<string>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{let e=0;for(let r=0;r<t.length;r+=1)e+=t[r];return e/t.length},this.functionCeil=([t])=>Math.ceil(t),this.functionContains=t=>{const[e,r]=t;return e.includes(r)},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFloor=([t])=>Math.floor(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([e])=>t(e)?Object.keys(e).length:e.length,this.functionMap=t=>{if(!this._interpreter)return[];const e=[],r=this._interpreter,n=t[0],i=t[1];for(let t=0;t<i.length;t+=1)e.push(r.visit(n,i[t]));return e},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===T.TYPE_NUMBER)return Math.max(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)r.localeCompare(e[t])<0&&(r=e[t]);return r},this.functionMaxBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[T.TYPE_NUMBER,T.TYPE_STRING]);let i,s,o=-1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s>o&&(o=s,i=r[t]);return i},this.functionMerge=t=>{let e={};for(let r=0;r<t.length;r+=1){const n=t[r];e=Object.assign(e,n)}return e},this.functionMin=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===T.TYPE_NUMBER)return Math.min(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)e[t].localeCompare(r)<0&&(r=e[t]);return r},this.functionMinBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[T.TYPE_NUMBER,T.TYPE_STRING]);let i,s,o=1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s<o&&(o=s,i=r[t]);return i},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==T.TYPE_NULL)return t[e];return null},this.functionReverse=([t])=>{if(this.getTypeName(t)===T.TYPE_STRING){const e=t;let r="";for(let t=e.length-1;t>=0;t-=1)r+=e[t];return r}const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>[...t].sort(),this.functionSortBy=t=>{if(!this._interpreter)return[];const e=t[0].slice(0);if(0===e.length)return e;const r=this._interpreter,n=t[1],i=this.getTypeName(r.visit(n,e[0]));if(void 0!==i&&![T.TYPE_NUMBER,T.TYPE_STRING].includes(i))throw new Error(`TypeError: unexpected type (${this.TYPE_NAME_TABLE[i]})`);const s=[];for(let t=0;t<e.length;t+=1)s.push([t,e[t]]);s.sort(((t,e)=>{const s=r.visit(n,t[1]),o=r.visit(n,e[1]);if(this.getTypeName(s)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(s)]}`);if(this.getTypeName(o)!==i)throw new Error(`TypeError: expected (${this.TYPE_NAME_TABLE[i]}), received ${this.TYPE_NAME_TABLE[this.getTypeName(o)]}`);return s>o?1:s<o?-1:t[0]-e[0]}));for(let t=0;t<s.length;t+=1)e[t]=s[t][1];return e},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===T.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===T.TYPE_NUMBER?t:e!==T.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===T.TYPE_STRING?t:JSON.stringify(t),this.functionType=([t])=>{switch(this.getTypeName(t)){case T.TYPE_NUMBER:return"number";case T.TYPE_STRING:return"string";case T.TYPE_ARRAY:return"array";case T.TYPE_OBJECT:return"object";case T.TYPE_BOOLEAN:return"boolean";case T.TYPE_EXPREF:return"expref";case T.TYPE_NULL:return"null";default:return}},this.functionValues=([t])=>Object.values(t),this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[T.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[T.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[T.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY]},{types:[T.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_STRING]}]},floor:{_func:this.functionFloor,_signature:[{types:[T.TYPE_NUMBER]}]},join:{_func:this.functionJoin,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[T.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY,T.TYPE_OBJECT]}]},map:{_func:this.functionMap,_signature:[{types:[T.TYPE_EXPREF]},{types:[T.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[T.TYPE_ARRAY_NUMBER,T.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[T.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[T.TYPE_ARRAY_NUMBER,T.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[T.TYPE_ANY],variadic:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[T.TYPE_STRING,T.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[T.TYPE_ARRAY_STRING,T.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[T.TYPE_ARRAY]},{types:[T.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[T.TYPE_STRING]},{types:[T.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[T.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[T.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[T.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[T.TYPE_ANY]}]},type:{_func:this.functionType,_signature:[{types:[T.TYPE_ANY]}]},values:{_func:this.functionValues,_signature:[{types:[T.TYPE_OBJECT]}]}},this._interpreter=e}registerFunction(t,e,r){if(t in this.functionTable)throw new Error(`Function already defined: ${t}()`);this.functionTable[t]={_func:e.bind(this),_signature:r}}callFunction(t,e){const r=this.functionTable[t];if(void 0===r)throw new Error(`Unknown function: ${t}()`);return this.validateArgs(t,e,r._signature),r._func.call(this,e)}validateInputSignatures(t,e){for(let r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error(`ArgumentError: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,h=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,c=e.length<o,u=e.length>r.length,a=c&&(!h&&o>1||h)?"at least ":"";if(h&&c||!h&&(c||u))throw s=r.length>1,new Error(`ArgumentError: ${t}() takes ${a}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`TypeError: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===T.TYPE_ANY)return!0;if(e!==T.TYPE_ARRAY_STRING&&e!==T.TYPE_ARRAY_NUMBER&&e!==T.TYPE_ARRAY)return t===e;if(e===T.TYPE_ARRAY)return t===T.TYPE_ARRAY;if(t===T.TYPE_ARRAY){let t;e===T.TYPE_ARRAY_NUMBER?t=T.TYPE_NUMBER:e===T.TYPE_ARRAY_STRING&&(t=T.TYPE_STRING);for(let e=0;e<r.length;e+=1){const n=this.getTypeName(r[e]);if(void 0!==n&&void 0!==t&&!this.typeMatches(n,t,r[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return T.TYPE_STRING;case"[object Number]":return T.TYPE_NUMBER;case"[object Array]":return T.TYPE_ARRAY;case"[object Boolean]":return T.TYPE_BOOLEAN;case"[object Null]":return T.TYPE_NULL;case"[object Object]":return t.jmespathType===i.TOK_EXPREF?T.TYPE_EXPREF:T.TYPE_OBJECT;default:return}}createKeyFunction(t,e){if(!this._interpreter)return;const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`TypeError: expected one of (${e.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(i)]}`;throw new Error(t)}return i}}}const l=new class{constructor(){this._rootValue=null,this.runtime=new _(this)}search(t,e){return this._rootValue=e,this.visit(t,e)}visit(n,s){let o,h,c,u,a,T,_,l,E,R,p;switch(n.type){case"Field":return null===s?null:t(s)?(T=s[n.name],void 0===T?null:T):null;case"Subexpression":for(c=this.visit(n.children[0],s),R=1;R<n.children.length;R+=1)if(c=this.visit(n.children[1],c),null===c)return null;return c;case"IndexExpression":return _=this.visit(n.children[0],s),l=this.visit(n.children[1],_),l;case"Index":if(!Array.isArray(s))return null;let O=n.value;return O<0&&(O=s.length+O),c=s[O],void 0===c&&(c=null),c;case"Slice":if(!Array.isArray(s))return null;const f=[...n.children],A=this.computeSliceParams(s.length,f),[d,N,K]=A;if(c=[],K>0)for(R=d;R<N;R+=K)c.push(s[R]);else for(R=d;R>N;R+=K)c.push(s[R]);return c;case"Projection":if(p=this.visit(n.children[0],s),!Array.isArray(p))return null;for(E=[],R=0;R<p.length;R+=1)h=this.visit(n.children[1],p[R]),null!==h&&E.push(h);return E;case"ValueProjection":if(p=this.visit(n.children[0],s),!t(p))return null;E=[];const y=Object.values(p);for(R=0;R<y.length;R+=1)h=this.visit(n.children[1],y[R]),null!==h&&E.push(h);return E;case"FilterProjection":if(p=this.visit(n.children[0],s),!Array.isArray(p))return null;const P=[],Y=[];for(R=0;R<p.length;R+=1)o=this.visit(n.children[2],p[R]),r(o)||P.push(p[R]);for(let t=0;t<P.length;t+=1)h=this.visit(n.children[1],P[t]),null!==h&&Y.push(h);return Y;case"Comparator":switch(u=this.visit(n.children[0],s),a=this.visit(n.children[1],s),n.name){case i.TOK_EQ:c=e(u,a);break;case i.TOK_NE:c=!e(u,a);break;case i.TOK_GT:c=u>a;break;case i.TOK_GTE:c=u>=a;break;case i.TOK_LT:c=u<a;break;case i.TOK_LTE:c=u<=a;break;default:throw new Error(`Unknown comparator: ${n.name}`)}return c;case i.TOK_FLATTEN:const g=this.visit(n.children[0],s);if(!Array.isArray(g))return null;let v=[];for(R=0;R<g.length;R+=1)h=g[R],Array.isArray(h)?v=[...v,...h]:v.push(h);return v;case"Identity":return s;case"MultiSelectList":if(null===s)return null;for(E=[],R=0;R<n.children.length;R+=1)E.push(this.visit(n.children[R],s));return E;case"MultiSelectHash":if(null===s)return null;let L;for(E={},R=0;R<n.children.length;R+=1)L=n.children[R],E[L.name]=this.visit(L.value,s);return E;case"OrExpression":return o=this.visit(n.children[0],s),r(o)&&(o=this.visit(n.children[1],s)),o;case"AndExpression":return u=this.visit(n.children[0],s),r(u)?u:this.visit(n.children[1],s);case"NotExpression":return u=this.visit(n.children[0],s),r(u);case"Literal":return n.value;case i.TOK_PIPE:return _=this.visit(n.children[0],s),this.visit(n.children[1],_);case i.TOK_CURRENT:return s;case i.TOK_ROOT:return this._rootValue;case"Function":const I=[];for(let t=0;t<n.children.length;t+=1)I.push(this.visit(n.children[t],s));return this.runtime.callFunction(n.name,I);case"ExpressionReference":const m=n.children[0];return m.jmespathType=i.TOK_EXPREF,m;default:throw new Error(`Unknown node type: ${n.type}`)}}computeSliceParams(t,e){let[r,n,i]=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid slice, step cannot be 0");throw t.name="RuntimeError",t}const s=i<0;return r=null===r?s?t-1:0:this.capSliceRange(t,r,i),n=null===n?s?-1:t:this.capSliceRange(t,n,i),[r,n,i]}capSliceRange(t,e,r){let n=e;return n<0?(n+=t,n<0&&(n=r<0?-1:0)):n>=t&&(n=r<0?t-1:t),n}},E=T.TYPE_ANY,R=T.TYPE_ARRAY,p=T.TYPE_ARRAY_NUMBER,O=T.TYPE_ARRAY_STRING,f=T.TYPE_BOOLEAN,A=T.TYPE_EXPREF,d=T.TYPE_NULL,N=T.TYPE_NUMBER,K=T.TYPE_OBJECT,y=T.TYPE_STRING;function P(t){return a.parse(t)}function Y(t){return c.tokenize(t)}const g=(t,e,r)=>{l.runtime.registerFunction(t,e,r)};function v(t,e){const r=a.parse(e);return l.search(r,t)}const L=l,I={compile:P,registerFunction:g,search:v,tokenize:Y,TreeInterpreter:L,TYPE_ANY:E,TYPE_ARRAY_NUMBER:p,TYPE_ARRAY_STRING:O,TYPE_ARRAY:R,TYPE_BOOLEAN:f,TYPE_EXPREF:A,TYPE_NULL:d,TYPE_NUMBER:N,TYPE_OBJECT:K,TYPE_STRING:y};export default I;export{E as TYPE_ANY,R as TYPE_ARRAY,p as TYPE_ARRAY_NUMBER,O as TYPE_ARRAY_STRING,f as TYPE_BOOLEAN,A as TYPE_EXPREF,d as TYPE_NULL,N as TYPE_NUMBER,K as TYPE_OBJECT,y as TYPE_STRING,L as TreeInterpreter,P as compile,I as jmespath,g as registerFunction,v as search,Y as tokenize};
//# sourceMappingURL=jmespath.esm.min.js.map