@metrichor/jmespath
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -88,2 +88,3 @@ const isObject = (obj) => { | ||
Token["TOK_CURRENT"] = "Current"; | ||
Token["TOK_ROOT"] = "Root"; | ||
Token["TOK_EXPREF"] = "Expref"; | ||
@@ -117,2 +118,3 @@ Token["TOK_PIPE"] = "Pipe"; | ||
'@': Token.TOK_CURRENT, | ||
['$']: Token.TOK_ROOT, | ||
']': Token.TOK_RBRACKET, | ||
@@ -390,2 +392,3 @@ '{': Token.TOK_LBRACE, | ||
[Token.TOK_EXPREF]: 0, | ||
[Token.TOK_ROOT]: 0, | ||
[Token.TOK_PIPE]: 1, | ||
@@ -499,2 +502,4 @@ [Token.TOK_OR]: 2, | ||
return { type: Token.TOK_CURRENT }; | ||
case Token.TOK_ROOT: | ||
return { type: Token.TOK_ROOT }; | ||
case Token.TOK_EXPREF: | ||
@@ -1263,13 +1268,24 @@ expression = this.expression(bindingPower.Expref); | ||
} | ||
validateInputSignatures(name, signature) { | ||
for (let i = 0; i < signature.length; i += 1) { | ||
if ('variadic' in signature[i] && i !== signature.length - 1) { | ||
throw new Error(`ArgumentError: ${name}() 'variadic' argument ${i + 1} must occur last`); | ||
} | ||
if ('variadic' in signature[i] && 'optional' in signature[i]) { | ||
throw new Error(`ArgumentError: ${name}() 'variadic' argument ${i + 1} cannot also be 'optional'`); | ||
} | ||
} | ||
} | ||
validateArgs(name, args, signature) { | ||
var _a, _b, _c; | ||
let pluralized; | ||
if (signature[signature.length - 1].variadic) { | ||
if (args.length < signature.length) { | ||
pluralized = signature.length > 1; | ||
throw new Error(`ArgumentError: ${name}() takes at least ${signature.length} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
} | ||
} | ||
else if (args.length !== signature.length) { | ||
this.validateInputSignatures(name, signature); | ||
const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return (_a = !argSignature.optional) !== null && _a !== void 0 ? _a : false; }).length; | ||
const lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false; | ||
const tooFewArgs = args.length < numberOfRequiredArgs; | ||
const tooManyArgs = args.length > signature.length; | ||
const tooFewModifier = tooFewArgs && ((!lastArgIsVariadic && numberOfRequiredArgs > 1) || lastArgIsVariadic) ? 'at least ' : ''; | ||
if ((lastArgIsVariadic && tooFewArgs) || (!lastArgIsVariadic && (tooFewArgs || tooManyArgs))) { | ||
pluralized = signature.length > 1; | ||
throw new Error(`ArgumentError: ${name}() takes ${signature.length} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
throw new Error(`ArgumentError: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
} | ||
@@ -1279,5 +1295,7 @@ let currentSpec; | ||
let typeMatched; | ||
let isOptional; | ||
for (let i = 0; i < signature.length; i += 1) { | ||
typeMatched = false; | ||
currentSpec = signature[i].types; | ||
isOptional = (_c = signature[i].optional) !== null && _c !== void 0 ? _c : false; | ||
actualType = this.getTypeName(args[i]); | ||
@@ -1291,3 +1309,3 @@ let j; | ||
} | ||
if (!typeMatched) { | ||
if (!typeMatched && !isOptional) { | ||
const expected = currentSpec | ||
@@ -1375,5 +1393,7 @@ .map((typeIdentifier) => { | ||
constructor() { | ||
this._rootValue = null; | ||
this.runtime = new Runtime(this); | ||
} | ||
search(node, value) { | ||
this._rootValue = value; | ||
return this.visit(node, value); | ||
@@ -1584,2 +1604,4 @@ } | ||
return value; | ||
case Token.TOK_ROOT: | ||
return this._rootValue; | ||
case 'Function': | ||
@@ -1586,0 +1608,0 @@ const resolvedArgs = []; |
@@ -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_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 a=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,n,a;for(this._current=0;this._current<t.length;)if((u=t[this._current])>="a"&&u<="z"||u>="A"&&u<="Z"||"_"===u)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]))a=this.consumeNumber(t),e.push(a);else if("["===t[this._current])a=this.consumeLBracket(t),e.push(a);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]])a=this.consumeOperator(t),a&&e.push(a);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 u;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}},u={[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=[...a.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<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 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(u.Not),{type:"NotExpression",children:[r]};case s.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===s.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(u.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(u.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(u.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(u.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=u.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(u.Pipe),{type:s.TOK_PIPE,children:[e,r]};case s.TOK_OR:return r=this.expression(u.Or),{type:"OrExpression",children:[e,r]};case s.TOK_AND:return r=this.expression(u.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(u.Filter),{type:"FilterProjection",children:[e,r,h]};case s.TOK_FLATTEN:return{type:"Projection",children:[{type:s.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(u.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 a=this.lookaheadToken(0);return a.type===s.TOK_NUMBER||a.type===s.TOK_COLON?(r=this.parseIndexExpression(),this.projectIfSlice(e,r)):(this.match(s.TOK_STAR),this.match(s.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)===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(u.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(u[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(u[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)}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`);if("variadic"in e[r]&&"optional"in e[r])throw new Error(`ArgumentError: ${t}() 'variadic' argument ${r+1} cannot also be 'optional'`)}}validateArgs(t,e,r){var n,i,s;let o;this.validateInputSignatures(t,r);const c=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,a=e.length<c,u=e.length>r.length,T=a&&(!h&&c>1||h)?"at least ":"";if(h&&a||!h&&(a||u))throw o=r.length>1,new Error(`ArgumentError: ${t}() takes ${T}${c} argument${o?"s":""} but received ${e.length}`);let _,l,E,R;for(let n=0;n<r.length;n+=1){let i;for(E=!1,_=r[n].types,R=null!==(s=r[n].optional)&&void 0!==s&&s,l=this.getTypeName(e[n]),i=0;i<_.length;i+=1)if(void 0!==l&&this.typeMatches(l,_[i],e[n])){E=!0;break}if(!E&&!R){const e=_.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[l]} 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(n,i){let o,c,h,a,u,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(h=this.visit(n.children[0],i),R=1;R<n.children.length;R+=1)if(h=this.visit(n.children[1],h),null===h)return null;return h;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),h=i[O],void 0===h&&(h=null),h;case"Slice":if(!Array.isArray(i))return null;const f=[...n.children],A=this.computeSliceParams(i.length,f),[d,N,y]=A;if(h=[],y>0)for(R=d;R<N;R+=y)h.push(i[R]);else for(R=d;R>N;R+=y)h.push(i[R]);return h;case"Projection":if(p=this.visit(n.children[0],i),!Array.isArray(p))return null;for(E=[],R=0;R<p.length;R+=1)c=this.visit(n.children[1],p[R]),null!==c&&E.push(c);return E;case"ValueProjection":if(p=this.visit(n.children[0],i),!t(p))return null;E=[];const K=Object.values(p);for(R=0;R<K.length;R+=1)c=this.visit(n.children[1],K[R]),null!==c&&E.push(c);return E;case"FilterProjection":if(p=this.visit(n.children[0],i),!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)c=this.visit(n.children[1],P[t]),null!==c&&Y.push(c);return Y;case"Comparator":switch(a=this.visit(n.children[0],i),u=this.visit(n.children[1],i),n.name){case s.TOK_EQ:h=e(a,u);break;case s.TOK_NE:h=!e(a,u);break;case s.TOK_GT:h=a>u;break;case s.TOK_GTE:h=a>=u;break;case s.TOK_LT:h=a<u;break;case s.TOK_LTE:h=a<=u;break;default:throw new Error("Unknown comparator: "+n.name)}return h;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)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<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 a=this.visit(n.children[0],i),r(a)?a:this.visit(n.children[1],i);case"NotExpression":return a=this.visit(n.children[0],i),r(a);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 s.TOK_ROOT:return this._rootValue;case"Function":const m=[];for(let t=0;t<n.children.length;t+=1)m.push(this.visit(n.children[t],i));return this.runtime.callFunction(n.name,m);case"ExpressionReference":const I=n.children[0];return I.jmespathType=s.TOK_EXPREF,I;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,K=_.TYPE_OBJECT,P=_.TYPE_STRING;function Y(t){return T.parse(t)}function g(t){return a.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:y,TYPE_OBJECT:K,TYPE_STRING:P};export default I;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,K as TYPE_OBJECT,P as TYPE_STRING,m as TreeInterpreter,Y as compile,I 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_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"}(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_ROOT,"]":o.TOK_RBRACKET,"{":o.TOK_LBRACE,"}":o.TOK_RBRACE},h={"!":!0,"<":!0,"=":!0,">":!0},u={"\t":!0,"\n":!0,"\r":!0," ":!0};const a=new class{constructor(){this._current=0}tokenize(t){const e=[];let r,n,i;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: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 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(;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}},T={[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_ROOT]: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=[...a.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<T[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(T.Not),{type:"NotExpression",children:[r]};case o.TOK_STAR:return e={type:"Identity"},r=this.lookahead(0)===o.TOK_RBRACKET&&{type:"Identity"}||this.parseProjectionRHS(T.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(T.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(T.Star),{children:[{type:"Identity"},r],type:"Projection"}):this.parseMultiselectList();case o.TOK_CURRENT:return{type:o.TOK_CURRENT};case o.TOK_ROOT:return{type:o.TOK_ROOT};case o.TOK_EXPREF:return n=this.expression(T.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=T.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(T.Pipe),{type:o.TOK_PIPE,children:[e,r]};case o.TOK_OR:return r=this.expression(T.Or),{type:"OrExpression",children:[e,r]};case o.TOK_AND:return r=this.expression(T.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(T.Filter),{type:"FilterProjection",children:[e,r,h]};case o.TOK_FLATTEN:return{type:"Projection",children:[{type:o.TOK_FLATTEN,children:[e]},this.parseProjectionRHS(T.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(T.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(T.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(T[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(T[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)}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`);if("variadic"in e[r]&&"optional"in e[r])throw new Error(`ArgumentError: ${t}() 'variadic' argument ${r+1} cannot also be 'optional'`)}}validateArgs(t,e,r){var n,i,s;let o;this.validateInputSignatures(t,r);const c=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,u=e.length<c,a=e.length>r.length,T=u&&(!h&&c>1||h)?"at least ":"";if(h&&u||!h&&(u||a))throw o=r.length>1,new Error(`ArgumentError: ${t}() takes ${T}${c} argument${o?"s":""} but received ${e.length}`);let _,l,E,R;for(let n=0;n<r.length;n+=1){let i;for(E=!1,_=r[n].types,R=null!==(s=r[n].optional)&&void 0!==s&&s,l=this.getTypeName(e[n]),i=0;i<_.length;i+=1)if(void 0!==l&&this.typeMatches(l,_[i],e[n])){E=!0;break}if(!E&&!R){const e=_.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[l]} 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._rootValue=null,this.runtime=new E(this)}search(t,e){return this._rootValue=e,this.visit(t,e)}visit(t,i){let s,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)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),a=this.visit(t.children[1],i),t.name){case o.TOK_EQ:h=r(u,a);break;case o.TOK_NE:h=!r(u,a);break;case o.TOK_GT:h=u>a;break;case o.TOK_GTE:h=u>=a;break;case o.TOK_LT:h=u<a;break;case o.TOK_LTE:h=u<=a;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 o.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=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 a.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 |
@@ -17,2 +17,3 @@ "use strict"; | ||
Token["TOK_CURRENT"] = "Current"; | ||
Token["TOK_ROOT"] = "Root"; | ||
Token["TOK_EXPREF"] = "Expref"; | ||
@@ -46,2 +47,3 @@ Token["TOK_PIPE"] = "Pipe"; | ||
'@': Token.TOK_CURRENT, | ||
['$']: Token.TOK_ROOT, | ||
']': Token.TOK_RBRACKET, | ||
@@ -48,0 +50,0 @@ '{': Token.TOK_LBRACE, |
@@ -17,2 +17,3 @@ "use strict"; | ||
[Lexer_1.Token.TOK_EXPREF]: 0, | ||
[Lexer_1.Token.TOK_ROOT]: 0, | ||
[Lexer_1.Token.TOK_PIPE]: 1, | ||
@@ -126,2 +127,4 @@ [Lexer_1.Token.TOK_OR]: 2, | ||
return { type: Lexer_1.Token.TOK_CURRENT }; | ||
case Lexer_1.Token.TOK_ROOT: | ||
return { type: Lexer_1.Token.TOK_ROOT }; | ||
case Lexer_1.Token.TOK_EXPREF: | ||
@@ -128,0 +131,0 @@ expression = this.expression(bindingPower.Expref); |
@@ -528,13 +528,24 @@ "use strict"; | ||
} | ||
validateInputSignatures(name, signature) { | ||
for (let i = 0; i < signature.length; i += 1) { | ||
if ('variadic' in signature[i] && i !== signature.length - 1) { | ||
throw new Error(`ArgumentError: ${name}() 'variadic' argument ${i + 1} must occur last`); | ||
} | ||
if ('variadic' in signature[i] && 'optional' in signature[i]) { | ||
throw new Error(`ArgumentError: ${name}() 'variadic' argument ${i + 1} cannot also be 'optional'`); | ||
} | ||
} | ||
} | ||
validateArgs(name, args, signature) { | ||
var _a, _b, _c; | ||
let pluralized; | ||
if (signature[signature.length - 1].variadic) { | ||
if (args.length < signature.length) { | ||
pluralized = signature.length > 1; | ||
throw new Error(`ArgumentError: ${name}() takes at least ${signature.length} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
} | ||
} | ||
else if (args.length !== signature.length) { | ||
this.validateInputSignatures(name, signature); | ||
const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return (_a = !argSignature.optional) !== null && _a !== void 0 ? _a : false; }).length; | ||
const lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false; | ||
const tooFewArgs = args.length < numberOfRequiredArgs; | ||
const tooManyArgs = args.length > signature.length; | ||
const tooFewModifier = tooFewArgs && ((!lastArgIsVariadic && numberOfRequiredArgs > 1) || lastArgIsVariadic) ? 'at least ' : ''; | ||
if ((lastArgIsVariadic && tooFewArgs) || (!lastArgIsVariadic && (tooFewArgs || tooManyArgs))) { | ||
pluralized = signature.length > 1; | ||
throw new Error(`ArgumentError: ${name}() takes ${signature.length} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
throw new Error(`ArgumentError: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${(pluralized && 's') || ''} but received ${args.length}`); | ||
} | ||
@@ -544,5 +555,7 @@ let currentSpec; | ||
let typeMatched; | ||
let isOptional; | ||
for (let i = 0; i < signature.length; i += 1) { | ||
typeMatched = false; | ||
currentSpec = signature[i].types; | ||
isOptional = (_c = signature[i].optional) !== null && _c !== void 0 ? _c : false; | ||
actualType = this.getTypeName(args[i]); | ||
@@ -556,3 +569,3 @@ let j; | ||
} | ||
if (!typeMatched) { | ||
if (!typeMatched && !isOptional) { | ||
const expected = currentSpec | ||
@@ -559,0 +572,0 @@ .map((typeIdentifier) => { |
@@ -9,5 +9,7 @@ "use strict"; | ||
constructor() { | ||
this._rootValue = null; | ||
this.runtime = new Runtime_1.Runtime(this); | ||
} | ||
search(node, value) { | ||
this._rootValue = value; | ||
return this.visit(node, value); | ||
@@ -218,2 +220,4 @@ } | ||
return value; | ||
case Lexer_1.Token.TOK_ROOT: | ||
return this._rootValue; | ||
case 'Function': | ||
@@ -220,0 +224,0 @@ const resolvedArgs = []; |
@@ -13,2 +13,3 @@ import { JSONValue } from '.'; | ||
TOK_CURRENT = "Current", | ||
TOK_ROOT = "Root", | ||
TOK_EXPREF = "Expref", | ||
@@ -66,2 +67,3 @@ TOK_PIPE = "Pipe", | ||
'@': Token; | ||
$: Token; | ||
']': Token; | ||
@@ -68,0 +70,0 @@ '{': Token; |
@@ -19,2 +19,3 @@ import { TreeInterpreter } from './TreeInterpreter'; | ||
variadic?: boolean; | ||
optional?: boolean; | ||
} | ||
@@ -37,2 +38,3 @@ export declare type RuntimeFunction<T, U> = (resolvedArgs: T) => U; | ||
callFunction(name: string, resolvedArgs: any): unknown; | ||
private validateInputSignatures; | ||
private validateArgs; | ||
@@ -39,0 +41,0 @@ private typeMatches; |
@@ -6,2 +6,3 @@ import { ExpressionNodeTree } from './Lexer'; | ||
runtime: Runtime; | ||
private _rootValue; | ||
constructor(); | ||
@@ -8,0 +9,0 @@ search(node: ExpressionNodeTree, value: JSONValue): JSONValue; |
{ | ||
"name": "@metrichor/jmespath", | ||
"description": "Typescript implementation of the JMESPath spec (100% compliant)", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Oxford Nanopore Technologies", |
@@ -91,28 +91,2 @@ ![Node.js CI](https://github.com/nanoporetech/jmespath-ts/workflows/Node.js%20CI/badge.svg?branch=master) | ||
### `registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void` | ||
Extend the list of built in JMESpath expressions with your own functions. | ||
```javascript | ||
import {search, registerFunction, TYPE_NUMBER} from '@metrichor/jmespath' | ||
search({ foo: 60, bar: 10 }, 'divide(foo, bar)') | ||
// THROWS ERROR: Error: Unknown function: divide() | ||
registerFunction( | ||
'divide', // FUNCTION NAME | ||
(resolvedArgs) => { // CUSTOM FUNCTION | ||
const [dividend, divisor] = resolvedArgs; | ||
return dividend / divisor; | ||
}, | ||
[{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }] //SIGNATURE | ||
); | ||
search({ foo: 60,bar: 10 }, 'divide(foo, bar)'); | ||
// OUTPUTS: 6 | ||
``` | ||
### `compile(expression: string): ExpressionNodeTree` | ||
@@ -134,3 +108,62 @@ | ||
--- | ||
## EXTENSIONS TO ORIGINAL SPEC | ||
1. ### Register you own custom functions | ||
#### `registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void` | ||
Extend the list of built in JMESpath expressions with your own functions. | ||
```javascript | ||
import {search, registerFunction, TYPE_NUMBER} from '@metrichor/jmespath' | ||
search({ foo: 60, bar: 10 }, 'divide(foo, bar)') | ||
// THROWS ERROR: Error: Unknown function: divide() | ||
registerFunction( | ||
'divide', // FUNCTION NAME | ||
(resolvedArgs) => { // CUSTOM FUNCTION | ||
const [dividend, divisor] = resolvedArgs; | ||
return dividend / divisor; | ||
}, | ||
[{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }] //SIGNATURE | ||
); | ||
search({ foo: 60,bar: 10 }, 'divide(foo, bar)'); | ||
// OUTPUTS: 6 | ||
``` | ||
Optional arguments are supported by setting `{..., optional: true}` in argument signatures | ||
```javascript | ||
registerFunction( | ||
'divide', | ||
(resolvedArgs) => { | ||
const [dividend, divisor] = resolvedArgs; | ||
return dividend / divisor ?? 1; //OPTIONAL DIVISOR THAT DEFAULTS TO 1 | ||
}, | ||
[{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER], optional: true }] //SIGNATURE | ||
); | ||
search({ foo: 60, bar: 10 }, 'divide(foo)'); | ||
// OUTPUTS: 60 | ||
``` | ||
2. ### Root value access with `$` symbol | ||
```javascript | ||
search({foo: {bar: 999}, baz: [1, 2, 3]}, '$.baz[*].[@, $.foo.bar]') | ||
// OUTPUTS: | ||
// [ [ 1, 999 ], [ 2, 999 ], [ 3, 999 ] ] | ||
``` | ||
## More Resources | ||
@@ -137,0 +170,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
409827
5548
181