UNPKG

@metrichor/jmespath

Version:

Typescript implementation of the JMESPath spec (100% compliant)

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