@jmespath-community/jmespath 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,45 +1,3 @@
1
- var Token;
2
- (function (Token) {
3
- Token["TOK_EOF"] = "EOF";
4
- Token["TOK_VARIABLE"] = "Variable";
5
- Token["TOK_ASSIGN"] = "Assign";
6
- Token["TOK_UNQUOTEDIDENTIFIER"] = "UnquotedIdentifier";
7
- Token["TOK_QUOTEDIDENTIFIER"] = "QuotedIdentifier";
8
- Token["TOK_RBRACKET"] = "Rbracket";
9
- Token["TOK_RPAREN"] = "Rparen";
10
- Token["TOK_COMMA"] = "Comma";
11
- Token["TOK_COLON"] = "Colon";
12
- Token["TOK_RBRACE"] = "Rbrace";
13
- Token["TOK_NUMBER"] = "Number";
14
- Token["TOK_CURRENT"] = "Current";
15
- Token["TOK_ROOT"] = "Root";
16
- Token["TOK_EXPREF"] = "Expref";
17
- Token["TOK_PIPE"] = "Pipe";
18
- Token["TOK_OR"] = "Or";
19
- Token["TOK_AND"] = "And";
20
- Token["TOK_EQ"] = "EQ";
21
- Token["TOK_GT"] = "GT";
22
- Token["TOK_LT"] = "LT";
23
- Token["TOK_GTE"] = "GTE";
24
- Token["TOK_LTE"] = "LTE";
25
- Token["TOK_NE"] = "NE";
26
- Token["TOK_PLUS"] = "Plus";
27
- Token["TOK_MINUS"] = "Minus";
28
- Token["TOK_MULTIPLY"] = "Multiply";
29
- Token["TOK_DIVIDE"] = "Divide";
30
- Token["TOK_MODULO"] = "Modulo";
31
- Token["TOK_DIV"] = "Div";
32
- Token["TOK_FLATTEN"] = "Flatten";
33
- Token["TOK_STAR"] = "Star";
34
- Token["TOK_FILTER"] = "Filter";
35
- Token["TOK_DOT"] = "Dot";
36
- Token["TOK_NOT"] = "Not";
37
- Token["TOK_LBRACE"] = "Lbrace";
38
- Token["TOK_LBRACKET"] = "Lbracket";
39
- Token["TOK_LPAREN"] = "Lparen";
40
- Token["TOK_LITERAL"] = "Literal";
41
- })(Token || (Token = {}));
42
-
43
1
  const isObject = (obj) => {
44
2
  return obj !== null && Object.prototype.toString.call(obj) === '[object Object]';
45
3
  };
@@ -166,6 +124,125 @@ const mod = (left, right) => {
166
124
  return result;
167
125
  };
168
126
 
127
+ const findFirst = (subject, sub, start, end) => {
128
+ if (!subject || !sub) {
129
+ return null;
130
+ }
131
+ start = Math.max(ensureInteger((start = start || 0)), 0);
132
+ end = Math.min(ensureInteger((end = end || subject.length)), subject.length);
133
+ const offset = subject.slice(start, end).indexOf(sub);
134
+ return offset === -1 ? null : offset + start;
135
+ };
136
+ const findLast = (subject, sub, start, end) => {
137
+ if (!subject || !sub) {
138
+ return null;
139
+ }
140
+ start = Math.max(ensureInteger((start = start || 0)), 0);
141
+ end = Math.min(ensureInteger((end = end || subject.length)), subject.length);
142
+ const offset = subject.slice(start, end).lastIndexOf(sub);
143
+ const result = offset === -1 ? null : offset + start;
144
+ return result;
145
+ };
146
+ const lower = (subject) => subject.toLowerCase();
147
+ const ensurePadFuncParams = (name, width, padding) => {
148
+ padding = padding || ' ';
149
+ if (padding.length > 1) {
150
+ throw new Error(`invalid value, ${name} expects its 'pad' parameter to be a valid string with a single codepoint`);
151
+ }
152
+ ensurePositiveInteger(width);
153
+ return padding;
154
+ };
155
+ const padLeft = (subject, width, padding) => {
156
+ padding = ensurePadFuncParams('pad_left', width, padding);
157
+ return (subject && subject.padStart(width, padding)) || '';
158
+ };
159
+ const padRight = (subject, width, padding) => {
160
+ padding = ensurePadFuncParams('pad_right', width, padding);
161
+ return (subject && subject.padEnd(width, padding)) || '';
162
+ };
163
+ const replace = (subject, string, by, count) => {
164
+ if (count === 0) {
165
+ return subject;
166
+ }
167
+ if (!count) {
168
+ // emulating es2021: String.prototype.replaceAll()
169
+ return subject.split(string).join(by);
170
+ }
171
+ ensurePositiveInteger(count);
172
+ [...Array(count).keys()].map(() => (subject = subject.replace(string, by)));
173
+ return subject;
174
+ };
175
+ const split = (subject, search, count) => {
176
+ if (subject.length == 0 && search.length === 0) {
177
+ return [];
178
+ }
179
+ if (count === null || count === undefined) {
180
+ return subject.split(search);
181
+ }
182
+ ensurePositiveInteger(count);
183
+ if (count === 0) {
184
+ return [subject];
185
+ }
186
+ const split = subject.split(search);
187
+ return [...split.slice(0, count), split.slice(count).join(search)];
188
+ };
189
+ const trim = (subject, chars) => {
190
+ return trimLeft(trimRight(subject, chars), chars);
191
+ };
192
+ const trimLeft = (subject, chars) => {
193
+ return trimImpl(subject, list => new RegExp(`^[${list}]*(.*?)`), chars);
194
+ };
195
+ const trimRight = (subject, chars) => {
196
+ return trimImpl(subject, list => new RegExp(`(.*?)[${list}]*\$`), chars);
197
+ };
198
+ const trimImpl = (subject, regExper, chars) => {
199
+ const pattern = chars ? chars.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') : '\\s\u0085';
200
+ return subject.replace(regExper(pattern), '$1');
201
+ };
202
+ const upper = (subject) => subject.toUpperCase();
203
+
204
+ var Token;
205
+ (function (Token) {
206
+ Token["TOK_EOF"] = "EOF";
207
+ Token["TOK_VARIABLE"] = "Variable";
208
+ Token["TOK_ASSIGN"] = "Assign";
209
+ Token["TOK_UNQUOTEDIDENTIFIER"] = "UnquotedIdentifier";
210
+ Token["TOK_QUOTEDIDENTIFIER"] = "QuotedIdentifier";
211
+ Token["TOK_RBRACKET"] = "Rbracket";
212
+ Token["TOK_RPAREN"] = "Rparen";
213
+ Token["TOK_COMMA"] = "Comma";
214
+ Token["TOK_COLON"] = "Colon";
215
+ Token["TOK_RBRACE"] = "Rbrace";
216
+ Token["TOK_NUMBER"] = "Number";
217
+ Token["TOK_CURRENT"] = "Current";
218
+ Token["TOK_ROOT"] = "Root";
219
+ Token["TOK_EXPREF"] = "Expref";
220
+ Token["TOK_PIPE"] = "Pipe";
221
+ Token["TOK_OR"] = "Or";
222
+ Token["TOK_AND"] = "And";
223
+ Token["TOK_EQ"] = "EQ";
224
+ Token["TOK_GT"] = "GT";
225
+ Token["TOK_LT"] = "LT";
226
+ Token["TOK_GTE"] = "GTE";
227
+ Token["TOK_LTE"] = "LTE";
228
+ Token["TOK_NE"] = "NE";
229
+ Token["TOK_PLUS"] = "Plus";
230
+ Token["TOK_MINUS"] = "Minus";
231
+ Token["TOK_MULTIPLY"] = "Multiply";
232
+ Token["TOK_DIVIDE"] = "Divide";
233
+ Token["TOK_MODULO"] = "Modulo";
234
+ Token["TOK_DIV"] = "Div";
235
+ Token["TOK_FLATTEN"] = "Flatten";
236
+ Token["TOK_STAR"] = "Star";
237
+ Token["TOK_FILTER"] = "Filter";
238
+ Token["TOK_DOT"] = "Dot";
239
+ Token["TOK_NOT"] = "Not";
240
+ Token["TOK_LBRACE"] = "Lbrace";
241
+ Token["TOK_LBRACKET"] = "Lbracket";
242
+ Token["TOK_LPAREN"] = "Lparen";
243
+ Token["TOK_LITERAL"] = "Literal";
244
+ })(Token || (Token = {}));
245
+
169
246
  const basicTokens = {
170
247
  '(': Token.TOK_LPAREN,
171
248
  ')': Token.TOK_RPAREN,
@@ -360,7 +437,7 @@ class StreamLexer {
360
437
  }
361
438
  this._current += 1;
362
439
  const literal = stream.slice(start + 1, this._current - 1);
363
- return literal.replace(`\\'`, `'`).replace(`\\\\`, `\\`);
440
+ return replace(replace(literal, `\\\\`, `\\`), `\\'`, `'`);
364
441
  }
365
442
  consumeNumber(stream) {
366
443
  const start = this._current;
@@ -634,18 +711,10 @@ class TokenParser {
634
711
  return { type: 'ExpressionReference', child };
635
712
  }
636
713
  case Token.TOK_LPAREN: {
714
+ console.log('nud::TOK_LPAREN');
637
715
  const args = [];
638
- while (this.lookahead(0) !== Token.TOK_RPAREN) {
639
- let expression;
640
- if (this.lookahead(0) === Token.TOK_CURRENT) {
641
- expression = { type: Token.TOK_CURRENT };
642
- this.advance();
643
- }
644
- else {
645
- expression = this.expression(0);
646
- }
647
- args.push(expression);
648
- }
716
+ let expression = this.expression(0);
717
+ args.push(expression);
649
718
  this.match(Token.TOK_RPAREN);
650
719
  return args[0];
651
720
  }
@@ -965,83 +1034,6 @@ class Text {
965
1034
  }
966
1035
  }
967
1036
 
968
- const findFirst = (subject, sub, start, end) => {
969
- if (!subject || !sub) {
970
- return null;
971
- }
972
- start = Math.max(ensureInteger((start = start || 0)), 0);
973
- end = Math.min(ensureInteger((end = end || subject.length)), subject.length);
974
- const offset = subject.slice(start, end).indexOf(sub);
975
- return offset === -1 ? null : offset + start;
976
- };
977
- const findLast = (subject, sub, start, end) => {
978
- if (!subject || !sub) {
979
- return null;
980
- }
981
- start = Math.max(ensureInteger((start = start || 0)), 0);
982
- end = Math.min(ensureInteger((end = end || subject.length)), subject.length);
983
- const offset = subject.slice(start, end).lastIndexOf(sub);
984
- const result = offset === -1 ? null : offset + start;
985
- return result;
986
- };
987
- const lower = (subject) => subject.toLowerCase();
988
- const ensurePadFuncParams = (name, width, padding) => {
989
- padding = padding || ' ';
990
- if (padding.length > 1) {
991
- throw new Error(`invalid value, ${name} expects its 'pad' parameter to be a valid string with a single codepoint`);
992
- }
993
- ensurePositiveInteger(width);
994
- return padding;
995
- };
996
- const padLeft = (subject, width, padding) => {
997
- padding = ensurePadFuncParams('pad_left', width, padding);
998
- return (subject && subject.padStart(width, padding)) || '';
999
- };
1000
- const padRight = (subject, width, padding) => {
1001
- padding = ensurePadFuncParams('pad_right', width, padding);
1002
- return (subject && subject.padEnd(width, padding)) || '';
1003
- };
1004
- const replace = (subject, string, by, count) => {
1005
- if (count === 0) {
1006
- return subject;
1007
- }
1008
- if (!count) {
1009
- // emulating es2021: String.prototype.replaceAll()
1010
- return subject.split(string).join(by);
1011
- }
1012
- ensurePositiveInteger(count);
1013
- [...Array(count).keys()].map(() => (subject = subject.replace(string, by)));
1014
- return subject;
1015
- };
1016
- const split = (subject, search, count) => {
1017
- if (subject.length == 0 && search.length === 0) {
1018
- return [];
1019
- }
1020
- if (count === null || count === undefined) {
1021
- return subject.split(search);
1022
- }
1023
- ensurePositiveInteger(count);
1024
- if (count === 0) {
1025
- return [subject];
1026
- }
1027
- const split = subject.split(search);
1028
- return [...split.slice(0, count), split.slice(count).join(search)];
1029
- };
1030
- const trim = (subject, chars) => {
1031
- return trimLeft(trimRight(subject, chars), chars);
1032
- };
1033
- const trimLeft = (subject, chars) => {
1034
- return trimImpl(subject, list => new RegExp(`^[${list}]*(.*?)`), chars);
1035
- };
1036
- const trimRight = (subject, chars) => {
1037
- return trimImpl(subject, list => new RegExp(`(.*?)[${list}]*\$`), chars);
1038
- };
1039
- const trimImpl = (subject, regExper, chars) => {
1040
- const pattern = chars ? chars.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') : '\\s\u0085';
1041
- return subject.replace(regExper(pattern), '$1');
1042
- };
1043
- const upper = (subject) => subject.toUpperCase();
1044
-
1045
1037
  var InputArgument;
1046
1038
  (function (InputArgument) {
1047
1039
  InputArgument[InputArgument["TYPE_NUMBER"] = 0] = "TYPE_NUMBER";
@@ -1819,18 +1811,19 @@ class Runtime {
1819
1811
  },
1820
1812
  };
1821
1813
  this._interpreter = interpreter;
1814
+ this._functionTable = this.functionTable;
1822
1815
  }
1823
1816
  registerFunction(name, customFunction, signature) {
1824
- if (name in this.functionTable) {
1817
+ if (name in this._functionTable) {
1825
1818
  throw new Error(`Function already defined: ${name}()`);
1826
1819
  }
1827
- this.functionTable[name] = {
1820
+ this._functionTable[name] = {
1828
1821
  _func: customFunction.bind(this),
1829
1822
  _signature: signature,
1830
1823
  };
1831
1824
  }
1832
1825
  callFunction(name, resolvedArgs) {
1833
- const functionEntry = this.functionTable[name];
1826
+ const functionEntry = this._functionTable[name];
1834
1827
  if (functionEntry === undefined) {
1835
1828
  throw new Error(`Unknown function: ${name}()`);
1836
1829
  }
@@ -1848,7 +1841,7 @@ class Runtime {
1848
1841
  var _a, _b;
1849
1842
  let pluralized;
1850
1843
  this.validateInputSignatures(name, signature);
1851
- const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return (_a = !argSignature.optional) !== null && _a !== void 0 ? _a : false; }).length;
1844
+ const numberOfRequiredArgs = signature.filter(argSignature => { var _a; return !((_a = argSignature.optional) !== null && _a !== void 0 ? _a : false); }).length;
1852
1845
  const lastArgIsVariadic = (_b = (_a = signature[signature.length - 1]) === null || _a === void 0 ? void 0 : _a.variadic) !== null && _b !== void 0 ? _b : false;
1853
1846
  const tooFewArgs = args.length < numberOfRequiredArgs;
1854
1847
  const tooManyArgs = args.length > signature.length;
@@ -1992,6 +1985,7 @@ class TreeInterpreter$1 {
1992
1985
  }
1993
1986
  withScope(scope) {
1994
1987
  const interpreter = new TreeInterpreter$1();
1988
+ interpreter.runtime._functionTable = this.runtime._functionTable;
1995
1989
  interpreter._rootValue = this._rootValue;
1996
1990
  interpreter._scope = this._scope.withScope(scope);
1997
1991
  return interpreter;
@@ -2002,7 +1996,7 @@ class TreeInterpreter$1 {
2002
1996
  return this.visit(node, value);
2003
1997
  }
2004
1998
  visit(node, value) {
2005
- var _a, _b, _c;
1999
+ var _a, _b, _c, _d;
2006
2000
  switch (node.type) {
2007
2001
  case 'Field':
2008
2002
  const identifier = node.name;
@@ -2037,14 +2031,14 @@ class TreeInterpreter$1 {
2037
2031
  return this.visit(node.right, this.visit(node.left, value));
2038
2032
  case 'Subexpression': {
2039
2033
  const result = this.visit(node.left, value);
2040
- return (result != null && this.visit(node.right, result)) || null;
2034
+ return result != null ? (_c = this.visit(node.right, result)) !== null && _c !== void 0 ? _c : null : null;
2041
2035
  }
2042
2036
  case 'Index': {
2043
2037
  if (!Array.isArray(value)) {
2044
2038
  return null;
2045
2039
  }
2046
2040
  const index = node.value < 0 ? value.length + node.value : node.value;
2047
- return (_c = value[index]) !== null && _c !== void 0 ? _c : null;
2041
+ return (_d = value[index]) !== null && _d !== void 0 ? _d : null;
2048
2042
  }
2049
2043
  case 'Slice': {
2050
2044
  if (!Array.isArray(value) && typeof value !== 'string') {
@@ -1,2 +1,2 @@
1
- var t;!function(t){t.TOK_EOF="EOF",t.TOK_VARIABLE="Variable",t.TOK_ASSIGN="Assign",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_PLUS="Plus",t.TOK_MINUS="Minus",t.TOK_MULTIPLY="Multiply",t.TOK_DIVIDE="Divide",t.TOK_MODULO="Modulo",t.TOK_DIV="Div",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"}(t||(t={}));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("object"==typeof t){if(null===t)return!0;if(Array.isArray(t))return 0===t.length;for(const e in t)return!1;return!0}return!("number"==typeof t||t)},i=t=>t>="a"&&t<="z"||t>="A"&&t<="Z"||"_"===t,s=t=>t>="0"&&t<="9"||"-"===t,o=t=>{if("number"!=typeof t||Math.floor(t)!==t)throw new Error("invalid-value: expecting an integer.");return t},a=t=>{if("number"!=typeof t||t<0||Math.floor(t)!==t)throw new Error("invalid-value: expecting a non-negative integer.");return t},u=(...t)=>{for(let e=0;e<t.length;e++){if(null===t[e]||void 0===t[e])throw new Error("not-a-number: undefined");if("number"!=typeof t[e])throw new Error("not-a-number")}},c=t=>{if(!(t=+t))throw new Error("not-a-number: divide by zero");return t},h={"(":t.TOK_LPAREN,")":t.TOK_RPAREN,"*":t.TOK_STAR,",":t.TOK_COMMA,".":t.TOK_DOT,":":t.TOK_COLON,"@":t.TOK_CURRENT,"]":t.TOK_RBRACKET,"{":t.TOK_LBRACE,"}":t.TOK_RBRACE,"+":t.TOK_PLUS,"%":t.TOK_MODULO,"−":t.TOK_MINUS,"×":t.TOK_MULTIPLY,"÷":t.TOK_DIVIDE},T={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},_={"\t":!0,"\n":!0,"\r":!0," ":!0};const l=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(e,r){const n=[];let o,a,u;for(this._current=0,this._enable_legacy_literals=(null==r?void 0:r.enable_legacy_literals)||!1;this._current<e.length;)if(i(e[this._current]))o=this._current,a=this.consumeUnquotedIdentifier(e),n.push({start:o,type:t.TOK_UNQUOTEDIDENTIFIER,value:a});else if(void 0!==h[e[this._current]])n.push({start:this._current,type:h[e[this._current]],value:e[this._current]}),this._current+=1;else if("$"===e[this._current])o=this._current,this._current+1<e.length&&i(e[this._current+1])?(this._current+=1,a=this.consumeUnquotedIdentifier(e),n.push({start:o,type:t.TOK_VARIABLE,value:a})):(n.push({start:o,type:t.TOK_ROOT,value:e[this._current]}),this._current+=1);else if("-"===e[this._current])if(this._current+1<e.length&&s(e[this._current+1])){const t=this.consumeNumber(e);t&&n.push(t)}else{const e={start:this._current,type:t.TOK_MINUS,value:"-"};n.push(e),this._current+=1}else if(s(e[this._current]))u=this.consumeNumber(e),n.push(u);else if("["===e[this._current])u=this.consumeLBracket(e),n.push(u);else if('"'===e[this._current])o=this._current,a=this.consumeQuotedIdentifier(e),n.push({start:o,type:t.TOK_QUOTEDIDENTIFIER,value:a});else if("'"===e[this._current])o=this._current,a=this.consumeRawStringLiteral(e),n.push({start:o,type:t.TOK_LITERAL,value:a});else if("`"===e[this._current]){o=this._current;const r=this.consumeLiteral(e);n.push({start:o,type:t.TOK_LITERAL,value:r})}else if(void 0!==T[e[this._current]])u=this.consumeOperator(e),u&&n.push(u);else{if(void 0===_[e[this._current]]){const t=new Error(`Syntax error: unknown character: ${e[this._current]}`);throw t.name="LexerError",t}this._current+=1}return n}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}this._current+=1;const[n,i]=this.parseJSON(t.slice(e,this._current));if(!i){const t=new Error("syntax: unexpected end of JSON input");throw t.name="LexerError",t}return n}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("\\'","'").replace("\\\\","\\")}consumeNumber(e){const r=this._current;this._current+=1;const n=e.length;for(;s(e[this._current])&&this._current<n;)this._current+=1;return{start:r,value:parseInt(e.slice(r,this._current),10),type:t.TOK_NUMBER}}consumeLBracket(e){const r=this._current;return this._current+=1,"?"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FILTER,value:"[?"}):"]"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FLATTEN,value:"[]"}):{start:r,type:t.TOK_LBRACKET,value:"["}}consumeOrElse(t,e,r,n){const i=this._current;return this._current+=1,this._current<t.length&&t[this._current]===e?(this._current+=1,{start:i,type:n,value:t.slice(i,this._current)}):{start:i,type:r,value:t[i]}}consumeOperator(e){switch(e[this._current]){case"!":return this.consumeOrElse(e,"=",t.TOK_NOT,t.TOK_NE);case"<":return this.consumeOrElse(e,"=",t.TOK_LT,t.TOK_LTE);case">":return this.consumeOrElse(e,"=",t.TOK_GT,t.TOK_GTE);case"=":return this.consumeOrElse(e,"=",t.TOK_ASSIGN,t.TOK_EQ);case"&":return this.consumeOrElse(e,"&",t.TOK_EXPREF,t.TOK_AND);case"|":return this.consumeOrElse(e,"|",t.TOK_PIPE,t.TOK_OR);case"/":return this.consumeOrElse(e,"/",t.TOK_DIVIDE,t.TOK_DIV)}}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimStart();n=n.replace("\\`","`");let i=null,s=!1;if(this.looksLikeJSON(n)&&([i,s]=this.parseJSON(n)),!s&&this._enable_legacy_literals&&([i,s]=this.parseJSON(`"${n}"`)),!s){const e=new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${t[this._current]}`);throw e.name="LexerError",e}return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0])){const[e,r]=this.parseJSON(t);return r}return!1}parseJSON(t){try{return[JSON.parse(t),!0]}catch(t){return[null,!1]}}},E={[t.TOK_EOF]:0,[t.TOK_VARIABLE]:0,[t.TOK_UNQUOTEDIDENTIFIER]:0,[t.TOK_QUOTEDIDENTIFIER]:0,[t.TOK_RBRACKET]:0,[t.TOK_RPAREN]:0,[t.TOK_COMMA]:0,[t.TOK_RBRACE]:0,[t.TOK_NUMBER]:0,[t.TOK_CURRENT]:0,[t.TOK_EXPREF]:0,[t.TOK_ROOT]:0,[t.TOK_ASSIGN]:1,[t.TOK_PIPE]:1,[t.TOK_OR]:2,[t.TOK_AND]:3,[t.TOK_EQ]:5,[t.TOK_GT]:5,[t.TOK_LT]:5,[t.TOK_GTE]:5,[t.TOK_LTE]:5,[t.TOK_NE]:5,[t.TOK_MINUS]:6,[t.TOK_PLUS]:6,[t.TOK_DIV]:7,[t.TOK_DIVIDE]:7,[t.TOK_MODULO]:7,[t.TOK_MULTIPLY]:7,[t.TOK_FLATTEN]:9,[t.TOK_STAR]:20,[t.TOK_FILTER]:21,[t.TOK_DOT]:40,[t.TOK_NOT]:45,[t.TOK_LBRACE]:50,[t.TOK_LBRACKET]:55,[t.TOK_LPAREN]:60};class p{constructor(){this.index=0,this.tokens=[]}parse(e,r){this.loadTokens(e,r||{enable_legacy_literals:!1}),this.index=0;const n=this.expression(0);if(this.lookahead(0)!==t.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error: unexpected token type: ${t.type}, value: ${t.value}`)}return n}loadTokens(e,r){this.tokens=[...l.tokenize(e,r),{type:t.TOK_EOF,value:"",start:e.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<E[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(e){switch(e.type){case t.TOK_VARIABLE:return{type:"Variable",name:e.value};case t.TOK_LITERAL:return{type:"Literal",value:e.value};case t.TOK_UNQUOTEDIDENTIFIER:return p.isKeyword(e,"let")&&this.lookahead(0)===t.TOK_VARIABLE?this.parseLetExpression():{type:"Field",name:e.value};case t.TOK_QUOTEDIDENTIFIER:if(this.lookahead(0)===t.TOK_LPAREN)throw new Error("Syntax error: quoted identifier not allowed for function names.");return{type:"Field",name:e.value};case t.TOK_NOT:return{type:"NotExpression",child:this.expression(E.Not)};case t.TOK_MINUS:{const t=this.expression(E.Minus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_PLUS:{const t=this.expression(E.Plus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_STAR:{const e={type:"Identity"};return{type:"ValueProjection",left:e,right:this.lookahead(0)===t.TOK_RBRACKET?e:this.parseProjectionRHS(E.Star)}}case t.TOK_FILTER:return this.led(e.type,{type:"Identity"});case t.TOK_LBRACE:return this.parseMultiselectHash();case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:{type:"Identity"}},right:this.parseProjectionRHS(E.Flatten)};case t.TOK_LBRACKET:if(this.lookahead(0)===t.TOK_NUMBER||this.lookahead(0)===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice({type:"Identity"},t)}if(this.lookahead(0)===t.TOK_STAR&&this.lookahead(1)===t.TOK_RBRACKET){this.advance(),this.advance();return{left:{type:"Identity"},right:this.parseProjectionRHS(E.Star),type:"Projection"}}return this.parseMultiselectList();case t.TOK_CURRENT:return{type:t.TOK_CURRENT};case t.TOK_ROOT:return{type:t.TOK_ROOT};case t.TOK_EXPREF:return{type:"ExpressionReference",child:this.expression(E.Expref)};case t.TOK_LPAREN:{const e=[];for(;this.lookahead(0)!==t.TOK_RPAREN;){let r;this.lookahead(0)===t.TOK_CURRENT?(r={type:t.TOK_CURRENT},this.advance()):r=this.expression(0),e.push(r)}return this.match(t.TOK_RPAREN),e[0]}default:this.errorToken(e)}}led(e,r){switch(e){case t.TOK_DOT:{const e=E.Dot;if(this.lookahead(0)!==t.TOK_STAR){return{type:"Subexpression",left:r,right:this.parseDotRHS(e)}}this.advance();return{type:"ValueProjection",left:r,right:this.parseProjectionRHS(e)}}case t.TOK_PIPE:return{type:"Pipe",left:r,right:this.expression(E.Pipe)};case t.TOK_OR:return{type:"OrExpression",left:r,right:this.expression(E.Or)};case t.TOK_AND:return{type:"AndExpression",left:r,right:this.expression(E.And)};case t.TOK_LPAREN:if("Field"!==r.type)throw new Error("Syntax error: expected a Field node");return{name:r.name,type:"Function",children:this.parseCommaSeparatedExpressionsUntilToken(t.TOK_RPAREN)};case t.TOK_FILTER:{const e=this.expression(0);this.match(t.TOK_RBRACKET);return{type:"FilterProjection",left:r,right:this.lookahead(0)===t.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(E.Filter),condition:e}}case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:r},right:this.parseProjectionRHS(E.Flatten)};case t.TOK_ASSIGN:{const t=r,e=this.expression(0);return{type:"Binding",variable:t.name,reference:e}}case t.TOK_EQ:case t.TOK_NE:case t.TOK_GT:case t.TOK_GTE:case t.TOK_LT:case t.TOK_LTE:return this.parseComparator(r,e);case t.TOK_PLUS:case t.TOK_MINUS:case t.TOK_MULTIPLY:case t.TOK_STAR:case t.TOK_DIVIDE:case t.TOK_MODULO:case t.TOK_DIV:return this.parseArithmetic(r,e);case t.TOK_LBRACKET:{const e=this.lookaheadToken(0);if(e.type===t.TOK_NUMBER||e.type===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice(r,t)}this.match(t.TOK_STAR),this.match(t.TOK_RBRACKET);return{type:"Projection",left:r,right:this.parseProjectionRHS(E.Star)}}default:return this.errorToken(this.lookaheadToken(0))}}static isKeyword(e,r){return e.type===t.TOK_UNQUOTEDIDENTIFIER&&e.value===r}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error: expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Syntax error: invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===t.TOK_COLON||this.lookahead(1)===t.TOK_COLON)return this.parseSliceExpression();const e=Number(this.lookaheadToken(0).value);return this.advance(),this.match(t.TOK_RBRACKET),{type:"Index",value:e}}projectIfSlice(t,e){const r={type:"IndexExpression",left:t,right:e};return"Slice"===e.type?{left:r,right:this.parseProjectionRHS(E.Star),type:"Projection"}:r}parseSliceExpression(){const e=[null,null,null];let r=0,n=this.lookaheadToken(0);for(;n.type!=t.TOK_RBRACKET&&r<3;){if(n.type===t.TOK_COLON)r++,3===r&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(n.type===t.TOK_NUMBER){const t=this.lookaheadToken(0).value;e[r]=t,this.advance()}else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}n=this.lookaheadToken(0)}this.match(t.TOK_RBRACKET);const[i,s,o]=e;return{type:"Slice",start:i,stop:s,step:o}}parseLetExpression(){const t=this.parseCommaSeparatedExpressionsUntilKeyword("in"),e=this.expression(0);return{type:"LetExpression",bindings:t.map((t=>t)),expression:e}}parseCommaSeparatedExpressionsUntilKeyword(t){return this.parseCommaSeparatedExpressionsUntil((()=>p.isKeyword(this.lookaheadToken(0),t)),(()=>{this.advance()}))}parseCommaSeparatedExpressionsUntilToken(t){return this.parseCommaSeparatedExpressionsUntil((()=>this.lookahead(0)===t),(()=>this.match(t)))}parseCommaSeparatedExpressionsUntil(e,r){const n=[];let i;for(;!e();)i=this.expression(0),this.lookahead(0)===t.TOK_COMMA&&this.match(t.TOK_COMMA),n.push(i);return r(),n}parseComparator(t,e){return{type:"Comparator",name:e,left:t,right:this.expression(E[e])}}parseArithmetic(t,e){return{type:"Arithmetic",operator:e,left:t,right:this.expression(E[e])}}parseDotRHS(e){const r=this.lookahead(0);if([t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER,t.TOK_STAR].includes(r))return this.expression(e);if(r===t.TOK_LBRACKET)return this.match(t.TOK_LBRACKET),this.parseMultiselectList();if(r===t.TOK_LBRACE)return this.match(t.TOK_LBRACE),this.parseMultiselectHash();const n=this.lookaheadToken(0);this.errorToken(n,`Syntax error, unexpected token: ${n.value}(${n.type})`)}parseProjectionRHS(e){if(E[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===t.TOK_LBRACKET)return this.expression(e);if(this.lookahead(0)===t.TOK_FILTER)return this.expression(e);if(this.lookahead(0)===t.TOK_DOT)return this.match(t.TOK_DOT),this.parseDotRHS(e);const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseMultiselectList(){const e=[];for(;this.lookahead(0)!==t.TOK_RBRACKET;){const r=this.expression(0);if(e.push(r),this.lookahead(0)===t.TOK_COMMA&&(this.match(t.TOK_COMMA),this.lookahead(0)===t.TOK_RBRACKET))throw new Error("Syntax error: unexpected token Rbracket")}return this.match(t.TOK_RBRACKET),{type:"MultiSelectList",children:e}}parseMultiselectHash(){const e=[],r=[t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER];let n,i,s;for(;;){if(n=this.lookaheadToken(0),!r.includes(n.type))throw new Error(`Syntax error: expecting an identifier token, got: ${n.type}`);if(i=n.value,this.advance(),this.match(t.TOK_COLON),s=this.expression(0),e.push({value:s,type:"KeyValuePair",name:i}),this.lookahead(0)===t.TOK_COMMA)this.match(t.TOK_COMMA);else if(this.lookahead(0)===t.TOK_RBRACE){this.match(t.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:e}}}const R=new p;class f{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return f.compare(this,new f(t))}static get comparer(){return(t,e)=>new f(t).compareTo(e)}static compare(t,e){const r=t.codePoints,n=e.codePoints;for(let t=0;t<Math.min(r.length,n.length);t++)if(r[t]!==n[t])return r[t]-n[t]>0?1:-1;return r.length-n.length>0?1:-1}reverse(){return String.fromCodePoint(...this.codePoints.reverse())}get codePoints(){return[...this._text].map((t=>t.codePointAt(0)))}}const O=(t,e,r)=>{if((r=r||" ").length>1)throw new Error(`invalid value, ${t} expects its 'pad' parameter to be a valid string with a single codepoint`);return a(e),r},y=(t,e)=>d(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),A=(t,e)=>d(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),d=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")};var P;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING",t[t.TYPE_ARRAY_OBJECT=10]="TYPE_ARRAY_OBJECT",t[t.TYPE_ARRAY_ARRAY=11]="TYPE_ARRAY_ARRAY"}(P||(P={}));class Y{constructor(t){this.TYPE_NAME_TABLE={[P.TYPE_NUMBER]:"number",[P.TYPE_ANY]:"any",[P.TYPE_STRING]:"string",[P.TYPE_ARRAY]:"array",[P.TYPE_OBJECT]:"object",[P.TYPE_BOOLEAN]:"boolean",[P.TYPE_EXPREF]:"expression",[P.TYPE_NULL]:"null",[P.TYPE_ARRAY_NUMBER]:"Array<number>",[P.TYPE_ARRAY_OBJECT]:"Array<object>",[P.TYPE_ARRAY_STRING]:"Array<string>",[P.TYPE_ARRAY_ARRAY]:"Array<Array<any>>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{if(!t||0==t.length)return null;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,e])=>{if(Array.isArray(t)){return t.includes(e)}if("string"==typeof t){if("string"==typeof e)return t.includes(e)}return null},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFindFirst=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(o(r=r||0),0),n=Math.min(o(n=n||t.length),t.length);const i=t.slice(r,n).indexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFindLast=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(o(r=r||0),0),n=Math.min(o(n=n||t.length),t.length);const i=t.slice(r,n).lastIndexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFloor=([t])=>Math.floor(t),this.functionFromItems=([t])=>(t.map((t=>{if(2!=t.length||"string"!=typeof t[0])throw new Error("invalid value, each array must contain two elements, a pair of string and value")})),Object.fromEntries(t)),this.functionGroupBy=([t,e])=>{const r=this.createKeyFunction(e,[P.TYPE_STRING]);return t.reduce(((t,e)=>{const n=r(null!=e?e:{});return(t[n]=t[n]||[]).push(e),t}),{})},this.functionItems=([t])=>Object.entries(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([t])=>"string"==typeof t?new f(t).length:Array.isArray(t)?t.length:Object.keys(t).length,this.functionLower=([t])=>(t=>t.toLowerCase())(t),this.functionMap=([t,e])=>{if(!this._interpreter)return[];const r=[],n=this._interpreter;for(let i=0;i<e.length;i+=1)r.push(n.visit(t,e[i]));return r},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===P.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,[P.TYPE_NUMBER,P.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||null},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])===P.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,[P.TYPE_NUMBER,P.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||null},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==P.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=O("pad_left",e,r),t&&t.padStart(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionPadRight=t=>((t,e,r)=>(r=O("pad_right",e,r),t&&t.padEnd(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionReplace=t=>((t,e,r,n)=>0===n?t:n?(a(n),[...Array(n).keys()].map((()=>t=t.replace(e,r))),t):t.split(e).join(r))(t[0],t[1],t[2],t.length>3?t[3]:void 0),this.functionSplit=t=>((t,e,r)=>{if(0==t.length&&0===e.length)return[];if(null==r)return t.split(e);if(a(r),0===r)return[t];const n=t.split(e);return[...n.slice(0,r),n.slice(r).join(e)]})(t[0],t[1],t.length>2?t[2]:void 0),this.functionReverse=([t])=>{if(this.getTypeName(t)===P.TYPE_STRING)return new f(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(f.comparer):[...t].sort(),this.functionSortBy=t=>{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&&![P.TYPE_NUMBER,P.TYPE_STRING].includes(i))throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[i]})`);function s(t,e){throw new Error(`Invalid type: expected (${t.TYPE_NAME_TABLE[i]}), received ${t.TYPE_NAME_TABLE[t.getTypeName(e)]}`)}return e.sort(((t,e)=>{const o=r.visit(n,t),a=r.visit(n,e);return this.getTypeName(o)!==i?s(this,o):this.getTypeName(a)!==i&&s(this,a),i===P.TYPE_STRING?f.comparer(o,a):o-a}))},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===P.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===P.TYPE_NUMBER?t:e!==P.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===P.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>y(A(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return y(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return A(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case P.TYPE_NUMBER:return"number";case P.TYPE_STRING:return"string";case P.TYPE_ARRAY:return"array";case P.TYPE_OBJECT:return"object";case P.TYPE_BOOLEAN:return"boolean";case P.TYPE_NULL:return"null";default:throw new Error("invalid-type")}},this.functionUpper=([t])=>(t=>t.toUpperCase())(t),this.functionValues=([t])=>Object.values(t),this.functionZip=t=>{const e=Math.min(...t.map((t=>t.length)));return Array(e).fill(null).map(((e,r)=>t.map((t=>t[r]))))},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[P.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[P.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[P.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY]},{types:[P.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0},{types:[P.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0},{types:[P.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[P.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[P.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[P.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[P.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY,P.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[P.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[P.TYPE_EXPREF]},{types:[P.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[P.TYPE_ARRAY_NUMBER,P.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[P.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[P.TYPE_ARRAY_NUMBER,P.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[P.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER]},{types:[P.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER]},{types:[P.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[P.TYPE_ARRAY_STRING,P.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[P.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[P.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[P.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[P.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[P.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[P.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[P.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[P.TYPE_ARRAY],variadic:!0}]}},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(`Invalid arity: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,a=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,u=e.length<o,c=e.length>r.length,h=u&&(!a&&o>1||a)?"at least ":"";if(a&&u||!a&&(u||c))throw s=r.length>1,new Error(`Invalid arity: ${t}() takes ${h}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`Invalid type: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===P.TYPE_ANY)return!0;if(e!==P.TYPE_ARRAY_STRING&&e!==P.TYPE_ARRAY_NUMBER&&e!==P.TYPE_ARRAY_OBJECT&&e!==P.TYPE_ARRAY_ARRAY&&e!==P.TYPE_ARRAY)return t===e;if(e===P.TYPE_ARRAY)return t===P.TYPE_ARRAY;if(t===P.TYPE_ARRAY){let t;e===P.TYPE_ARRAY_NUMBER?t=P.TYPE_NUMBER:e===P.TYPE_ARRAY_OBJECT?t=P.TYPE_OBJECT:e===P.TYPE_ARRAY_STRING?t=P.TYPE_STRING:e===P.TYPE_ARRAY_ARRAY&&(t=P.TYPE_ARRAY);const n=r;for(let e=0;e<n.length;e+=1){const r=this.getTypeName(n[e]);if(void 0!==r&&void 0!==t&&!this.typeMatches(r,t,n[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return P.TYPE_STRING;case"[object Number]":return P.TYPE_NUMBER;case"[object Array]":return P.TYPE_ARRAY;case"[object Boolean]":return P.TYPE_BOOLEAN;case"[object Null]":return P.TYPE_NULL;case"[object Object]":return t.expref?P.TYPE_EXPREF:P.TYPE_OBJECT;default:return}}createKeyFunction(t,e){const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`Invalid type: 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}}}class N{constructor(){this.inner=void 0,this.data={}}withScope(t){const e=new N;return e.inner=this,e.data=t,e}getValue(t){if(t in this.data){const e=this.data[t];if(null!=e)return e}return this.inner?this.inner.getValue(t):null}}class K{constructor(){this._rootValue=null,this.runtime=new Y(this),this._scope=new N}withScope(t){const e=new K;return e._rootValue=this._rootValue,e._scope=this._scope.withScope(t),e}search(t,e){return this._rootValue=e,this._scope=new N,this.visit(t,e)}visit(e,i){var s,o,a,h,T;switch(e.type){case"Field":const _=e.name;let l=null;return null===i||"object"!=typeof i||Array.isArray(i)||(l=null!==(s=i[_])&&void 0!==s?s:null),l;case"LetExpression":{const{bindings:t,expression:r}=e;let n={};return t.forEach((t=>{const e=this.visit(t,i);n=Object.assign(Object.assign({},n),e)})),this.withScope(n).visit(r,i)}case"Binding":{const{variable:t,reference:r}=e;return{[t]:this.visit(r,i)}}case"Variable":{const t=e.name,r=null!==(o=this._scope.getValue(t))&&void 0!==o?o:null;if(null==r)throw new Error(`Error referencing undefined variable ${t}`);return r}case"IndexExpression":return this.visit(e.right,this.visit(e.left,i));case"Subexpression":{const t=this.visit(e.left,i);return null!=t&&this.visit(e.right,t)||null}case"Index":{if(!Array.isArray(i))return null;const t=e.value<0?i.length+e.value:e.value;return null!==(a=i[t])&&void 0!==a?a:null}case"Slice":{if(!Array.isArray(i)&&"string"!=typeof i)return null;const{start:t,stop:r,step:n}=this.computeSliceParams(i.length,e);if("string"==typeof i){const e=[...i];return this.slice(e,t,r,n).join("")}return this.slice(i,t,r,n)}case"Projection":{const{left:t,right:r}=e;let n=!1;"IndexExpression"===t.type&&"Slice"===t.right.type&&(n=!0);const s=this.visit(t,i);if(n&&"string"==typeof s)return this.visit(r,s);if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(r,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:t,right:r}=e,n=this.visit(t,i);if(null===n||"object"!=typeof n||Array.isArray(n))return null;const s=[],o=Object.values(n);for(const t of o){const e=this.visit(r,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:t,right:r,condition:s}=e,o=this.visit(t,i);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(n(e))continue;const i=this.visit(r,t);null!==i&&a.push(i)}return a}case"Arithmetic":{const r=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.operator){case t.TOK_PLUS:return u(h=r,T=n),h+T;case t.TOK_MINUS:return((t,e)=>(u(t,e),t-e))(r,n);case t.TOK_MULTIPLY:case t.TOK_STAR:return((t,e)=>(u(t,e),t*e))(r,n);case t.TOK_DIVIDE:return((t,e)=>(u(t,e),t/c(e)))(r,n);case t.TOK_MODULO:return((t,e)=>(u(t,e),t%e))(r,n);case t.TOK_DIV:return((t,e)=>(u(t,e),Math.floor(t/c(e))))(r,n);default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Unary":{const r=this.visit(e.operand,i);switch(e.operator){case t.TOK_PLUS:return u(r),r;case t.TOK_MINUS:return u(r),-r;default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Comparator":{const t=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.name){case"EQ":return r(t,n);case"NE":return!r(t,n)}if("number"!=typeof t||"number"!=typeof n)return null;switch(e.name){case"GT":return t>n;case"GTE":return t>=n;case"LT":return t<n;case"LTE":return t<=n}}case"Flatten":{const t=this.visit(e.child,i);return Array.isArray(t)?t.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return t}case"MultiSelectHash":{const t={};for(const r of e.children)t[r.name]=this.visit(r.value,i);return t}case"OrExpression":{const t=this.visit(e.left,i);return n(t)?this.visit(e.right,i):t}case"AndExpression":{const t=this.visit(e.left,i);return n(t)?t:this.visit(e.right,i)}case"NotExpression":return n(this.visit(e.child,i));case"Literal":return e.value;case"Pipe":return this.visit(e.right,this.visit(e.left,i));case"Function":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return this.runtime.callFunction(e.name,t)}case"ExpressionReference":return Object.assign({expref:!0},e.child);case"Current":case"Identity":return i}}computeSliceParams(t,e){let{start:r,stop:n,step:i}=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid value: slice step cannot be 0");throw t.name="RuntimeError",t}return r=null===r?i<0?t-1:0:this.capSliceRange(t,r,i),n=null===n?i<0?-1:t:this.capSliceRange(t,n,i),{start:r,stop:n,step: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}slice(t,e,r,n){const i=[];if(n>0)for(let s=e;s<r;s+=n)i.push(t[s]);else for(let s=e;s>r;s+=n)i.push(t[s]);return i}}const g=new K,m=P.TYPE_ANY,I=P.TYPE_ARRAY,S=P.TYPE_ARRAY_ARRAY,v=P.TYPE_ARRAY_NUMBER,L=P.TYPE_ARRAY_OBJECT,M=P.TYPE_ARRAY_STRING,B=P.TYPE_BOOLEAN,x=P.TYPE_EXPREF,U=P.TYPE_NULL,k=P.TYPE_NUMBER,w=P.TYPE_OBJECT,C=P.TYPE_STRING;function b(t,e){return R.parse(t,e)}function F(t,e){return l.tokenize(t,e)}const G=(t,e,r)=>{g.runtime.registerFunction(t,e,r)};function D(t,e,r){const n=R.parse(e,r);return g.search(n,t)}function j(){return new N}const $=g,J={compile:b,registerFunction:G,search:D,tokenize:F,TreeInterpreter:$,TYPE_ANY:m,TYPE_ARRAY_NUMBER:v,TYPE_ARRAY_STRING:M,TYPE_ARRAY:I,TYPE_BOOLEAN:B,TYPE_EXPREF:x,TYPE_NULL:U,TYPE_NUMBER:k,TYPE_OBJECT:w,TYPE_STRING:C};export{j as Scope,m as TYPE_ANY,I as TYPE_ARRAY,S as TYPE_ARRAY_ARRAY,v as TYPE_ARRAY_NUMBER,L as TYPE_ARRAY_OBJECT,M as TYPE_ARRAY_STRING,B as TYPE_BOOLEAN,x as TYPE_EXPREF,U as TYPE_NULL,k as TYPE_NUMBER,w as TYPE_OBJECT,C as TYPE_STRING,$ as TreeInterpreter,b as compile,J as default,J as jmespath,G as registerFunction,D as search,F as tokenize};
1
+ 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=t=>{if("object"==typeof t){if(null===t)return!0;if(Array.isArray(t))return 0===t.length;for(const e in t)return!1;return!0}return!("number"==typeof t||t)},n=t=>t>="a"&&t<="z"||t>="A"&&t<="Z"||"_"===t,i=t=>t>="0"&&t<="9"||"-"===t,s=t=>{if("number"!=typeof t||Math.floor(t)!==t)throw new Error("invalid-value: expecting an integer.");return t},o=t=>{if("number"!=typeof t||t<0||Math.floor(t)!==t)throw new Error("invalid-value: expecting a non-negative integer.");return t},a=(...t)=>{for(let e=0;e<t.length;e++){if(null===t[e]||void 0===t[e])throw new Error("not-a-number: undefined");if("number"!=typeof t[e])throw new Error("not-a-number")}},u=t=>{if(!(t=+t))throw new Error("not-a-number: divide by zero");return t},c=(t,e,r)=>{if((r=r||" ").length>1)throw new Error(`invalid value, ${t} expects its 'pad' parameter to be a valid string with a single codepoint`);return o(e),r},h=(t,e,r,n)=>0===n?t:n?(o(n),[...Array(n).keys()].map((()=>t=t.replace(e,r))),t):t.split(e).join(r),T=(t,e)=>l(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),_=(t,e)=>l(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),l=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")};var E;!function(t){t.TOK_EOF="EOF",t.TOK_VARIABLE="Variable",t.TOK_ASSIGN="Assign",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_PLUS="Plus",t.TOK_MINUS="Minus",t.TOK_MULTIPLY="Multiply",t.TOK_DIVIDE="Divide",t.TOK_MODULO="Modulo",t.TOK_DIV="Div",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"}(E||(E={}));const p={"(":E.TOK_LPAREN,")":E.TOK_RPAREN,"*":E.TOK_STAR,",":E.TOK_COMMA,".":E.TOK_DOT,":":E.TOK_COLON,"@":E.TOK_CURRENT,"]":E.TOK_RBRACKET,"{":E.TOK_LBRACE,"}":E.TOK_RBRACE,"+":E.TOK_PLUS,"%":E.TOK_MODULO,"−":E.TOK_MINUS,"×":E.TOK_MULTIPLY,"÷":E.TOK_DIVIDE},R={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},f={"\t":!0,"\n":!0,"\r":!0," ":!0};const O=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(t,e){const r=[];let s,o,a;for(this._current=0,this._enable_legacy_literals=(null==e?void 0:e.enable_legacy_literals)||!1;this._current<t.length;)if(n(t[this._current]))s=this._current,o=this.consumeUnquotedIdentifier(t),r.push({start:s,type:E.TOK_UNQUOTEDIDENTIFIER,value:o});else if(void 0!==p[t[this._current]])r.push({start:this._current,type:p[t[this._current]],value:t[this._current]}),this._current+=1;else if("$"===t[this._current])s=this._current,this._current+1<t.length&&n(t[this._current+1])?(this._current+=1,o=this.consumeUnquotedIdentifier(t),r.push({start:s,type:E.TOK_VARIABLE,value:o})):(r.push({start:s,type:E.TOK_ROOT,value:t[this._current]}),this._current+=1);else if("-"===t[this._current])if(this._current+1<t.length&&i(t[this._current+1])){const e=this.consumeNumber(t);e&&r.push(e)}else{const t={start:this._current,type:E.TOK_MINUS,value:"-"};r.push(t),this._current+=1}else if(i(t[this._current]))a=this.consumeNumber(t),r.push(a);else if("["===t[this._current])a=this.consumeLBracket(t),r.push(a);else if('"'===t[this._current])s=this._current,o=this.consumeQuotedIdentifier(t),r.push({start:s,type:E.TOK_QUOTEDIDENTIFIER,value:o});else if("'"===t[this._current])s=this._current,o=this.consumeRawStringLiteral(t),r.push({start:s,type:E.TOK_LITERAL,value:o});else if("`"===t[this._current]){s=this._current;const e=this.consumeLiteral(t);r.push({start:s,type:E.TOK_LITERAL,value:e})}else if(void 0!==R[t[this._current]])a=this.consumeOperator(t),a&&r.push(a);else{if(void 0===f[t[this._current]]){const e=new Error(`Syntax error: unknown character: ${t[this._current]}`);throw e.name="LexerError",e}this._current+=1}return r}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}this._current+=1;const[n,i]=this.parseJSON(t.slice(e,this._current));if(!i){const t=new Error("syntax: unexpected end of JSON input");throw t.name="LexerError",t}return n}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;const n=t.slice(e+1,this._current-1);return h(h(n,"\\\\","\\"),"\\'","'")}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:E.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:E.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:E.TOK_FLATTEN,value:"[]"}):{start:e,type:E.TOK_LBRACKET,value:"["}}consumeOrElse(t,e,r,n){const i=this._current;return this._current+=1,this._current<t.length&&t[this._current]===e?(this._current+=1,{start:i,type:n,value:t.slice(i,this._current)}):{start:i,type:r,value:t[i]}}consumeOperator(t){switch(t[this._current]){case"!":return this.consumeOrElse(t,"=",E.TOK_NOT,E.TOK_NE);case"<":return this.consumeOrElse(t,"=",E.TOK_LT,E.TOK_LTE);case">":return this.consumeOrElse(t,"=",E.TOK_GT,E.TOK_GTE);case"=":return this.consumeOrElse(t,"=",E.TOK_ASSIGN,E.TOK_EQ);case"&":return this.consumeOrElse(t,"&",E.TOK_EXPREF,E.TOK_AND);case"|":return this.consumeOrElse(t,"|",E.TOK_PIPE,E.TOK_OR);case"/":return this.consumeOrElse(t,"/",E.TOK_DIVIDE,E.TOK_DIV)}}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimStart();n=n.replace("\\`","`");let i=null,s=!1;if(this.looksLikeJSON(n)&&([i,s]=this.parseJSON(n)),!s&&this._enable_legacy_literals&&([i,s]=this.parseJSON(`"${n}"`)),!s){const e=new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${t[this._current]}`);throw e.name="LexerError",e}return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0])){const[e,r]=this.parseJSON(t);return r}return!1}parseJSON(t){try{return[JSON.parse(t),!0]}catch(t){return[null,!1]}}},y={[E.TOK_EOF]:0,[E.TOK_VARIABLE]:0,[E.TOK_UNQUOTEDIDENTIFIER]:0,[E.TOK_QUOTEDIDENTIFIER]:0,[E.TOK_RBRACKET]:0,[E.TOK_RPAREN]:0,[E.TOK_COMMA]:0,[E.TOK_RBRACE]:0,[E.TOK_NUMBER]:0,[E.TOK_CURRENT]:0,[E.TOK_EXPREF]:0,[E.TOK_ROOT]:0,[E.TOK_ASSIGN]:1,[E.TOK_PIPE]:1,[E.TOK_OR]:2,[E.TOK_AND]:3,[E.TOK_EQ]:5,[E.TOK_GT]:5,[E.TOK_LT]:5,[E.TOK_GTE]:5,[E.TOK_LTE]:5,[E.TOK_NE]:5,[E.TOK_MINUS]:6,[E.TOK_PLUS]:6,[E.TOK_DIV]:7,[E.TOK_DIVIDE]:7,[E.TOK_MODULO]:7,[E.TOK_MULTIPLY]:7,[E.TOK_FLATTEN]:9,[E.TOK_STAR]:20,[E.TOK_FILTER]:21,[E.TOK_DOT]:40,[E.TOK_NOT]:45,[E.TOK_LBRACE]:50,[E.TOK_LBRACKET]:55,[E.TOK_LPAREN]:60};class A{constructor(){this.index=0,this.tokens=[]}parse(t,e){this.loadTokens(t,e||{enable_legacy_literals:!1}),this.index=0;const r=this.expression(0);if(this.lookahead(0)!==E.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error: unexpected token type: ${t.type}, value: ${t.value}`)}return r}loadTokens(t,e){this.tokens=[...O.tokenize(t,e),{type:E.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<y[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){switch(t.type){case E.TOK_VARIABLE:return{type:"Variable",name:t.value};case E.TOK_LITERAL:return{type:"Literal",value:t.value};case E.TOK_UNQUOTEDIDENTIFIER:return A.isKeyword(t,"let")&&this.lookahead(0)===E.TOK_VARIABLE?this.parseLetExpression():{type:"Field",name:t.value};case E.TOK_QUOTEDIDENTIFIER:if(this.lookahead(0)===E.TOK_LPAREN)throw new Error("Syntax error: quoted identifier not allowed for function names.");return{type:"Field",name:t.value};case E.TOK_NOT:return{type:"NotExpression",child:this.expression(y.Not)};case E.TOK_MINUS:{const e=this.expression(y.Minus);return{type:"Unary",operator:t.type,operand:e}}case E.TOK_PLUS:{const e=this.expression(y.Plus);return{type:"Unary",operator:t.type,operand:e}}case E.TOK_STAR:{const t={type:"Identity"};return{type:"ValueProjection",left:t,right:this.lookahead(0)===E.TOK_RBRACKET?t:this.parseProjectionRHS(y.Star)}}case E.TOK_FILTER:return this.led(t.type,{type:"Identity"});case E.TOK_LBRACE:return this.parseMultiselectHash();case E.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:{type:"Identity"}},right:this.parseProjectionRHS(y.Flatten)};case E.TOK_LBRACKET:if(this.lookahead(0)===E.TOK_NUMBER||this.lookahead(0)===E.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice({type:"Identity"},t)}if(this.lookahead(0)===E.TOK_STAR&&this.lookahead(1)===E.TOK_RBRACKET){this.advance(),this.advance();return{left:{type:"Identity"},right:this.parseProjectionRHS(y.Star),type:"Projection"}}return this.parseMultiselectList();case E.TOK_CURRENT:return{type:E.TOK_CURRENT};case E.TOK_ROOT:return{type:E.TOK_ROOT};case E.TOK_EXPREF:return{type:"ExpressionReference",child:this.expression(y.Expref)};case E.TOK_LPAREN:{console.log("nud::TOK_LPAREN");const t=[];let e=this.expression(0);return t.push(e),this.match(E.TOK_RPAREN),t[0]}default:this.errorToken(t)}}led(t,e){switch(t){case E.TOK_DOT:{const t=y.Dot;if(this.lookahead(0)!==E.TOK_STAR){return{type:"Subexpression",left:e,right:this.parseDotRHS(t)}}this.advance();return{type:"ValueProjection",left:e,right:this.parseProjectionRHS(t)}}case E.TOK_PIPE:return{type:"Pipe",left:e,right:this.expression(y.Pipe)};case E.TOK_OR:return{type:"OrExpression",left:e,right:this.expression(y.Or)};case E.TOK_AND:return{type:"AndExpression",left:e,right:this.expression(y.And)};case E.TOK_LPAREN:if("Field"!==e.type)throw new Error("Syntax error: expected a Field node");return{name:e.name,type:"Function",children:this.parseCommaSeparatedExpressionsUntilToken(E.TOK_RPAREN)};case E.TOK_FILTER:{const t=this.expression(0);this.match(E.TOK_RBRACKET);return{type:"FilterProjection",left:e,right:this.lookahead(0)===E.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(y.Filter),condition:t}}case E.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:e},right:this.parseProjectionRHS(y.Flatten)};case E.TOK_ASSIGN:{const t=e,r=this.expression(0);return{type:"Binding",variable:t.name,reference:r}}case E.TOK_EQ:case E.TOK_NE:case E.TOK_GT:case E.TOK_GTE:case E.TOK_LT:case E.TOK_LTE:return this.parseComparator(e,t);case E.TOK_PLUS:case E.TOK_MINUS:case E.TOK_MULTIPLY:case E.TOK_STAR:case E.TOK_DIVIDE:case E.TOK_MODULO:case E.TOK_DIV:return this.parseArithmetic(e,t);case E.TOK_LBRACKET:{const t=this.lookaheadToken(0);if(t.type===E.TOK_NUMBER||t.type===E.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice(e,t)}this.match(E.TOK_STAR),this.match(E.TOK_RBRACKET);return{type:"Projection",left:e,right:this.parseProjectionRHS(y.Star)}}default:return this.errorToken(this.lookaheadToken(0))}}static isKeyword(t,e){return t.type===E.TOK_UNQUOTEDIDENTIFIER&&t.value===e}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error: expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Syntax error: invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===E.TOK_COLON||this.lookahead(1)===E.TOK_COLON)return this.parseSliceExpression();const t=Number(this.lookaheadToken(0).value);return this.advance(),this.match(E.TOK_RBRACKET),{type:"Index",value:t}}projectIfSlice(t,e){const r={type:"IndexExpression",left:t,right:e};return"Slice"===e.type?{left:r,right:this.parseProjectionRHS(y.Star),type:"Projection"}:r}parseSliceExpression(){const t=[null,null,null];let e=0,r=this.lookaheadToken(0);for(;r.type!=E.TOK_RBRACKET&&e<3;){if(r.type===E.TOK_COLON)e++,3===e&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(r.type===E.TOK_NUMBER){const r=this.lookaheadToken(0).value;t[e]=r,this.advance()}else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}r=this.lookaheadToken(0)}this.match(E.TOK_RBRACKET);const[n,i,s]=t;return{type:"Slice",start:n,stop:i,step:s}}parseLetExpression(){const t=this.parseCommaSeparatedExpressionsUntilKeyword("in"),e=this.expression(0);return{type:"LetExpression",bindings:t.map((t=>t)),expression:e}}parseCommaSeparatedExpressionsUntilKeyword(t){return this.parseCommaSeparatedExpressionsUntil((()=>A.isKeyword(this.lookaheadToken(0),t)),(()=>{this.advance()}))}parseCommaSeparatedExpressionsUntilToken(t){return this.parseCommaSeparatedExpressionsUntil((()=>this.lookahead(0)===t),(()=>this.match(t)))}parseCommaSeparatedExpressionsUntil(t,e){const r=[];let n;for(;!t();)n=this.expression(0),this.lookahead(0)===E.TOK_COMMA&&this.match(E.TOK_COMMA),r.push(n);return e(),r}parseComparator(t,e){return{type:"Comparator",name:e,left:t,right:this.expression(y[e])}}parseArithmetic(t,e){return{type:"Arithmetic",operator:e,left:t,right:this.expression(y[e])}}parseDotRHS(t){const e=this.lookahead(0);if([E.TOK_UNQUOTEDIDENTIFIER,E.TOK_QUOTEDIDENTIFIER,E.TOK_STAR].includes(e))return this.expression(t);if(e===E.TOK_LBRACKET)return this.match(E.TOK_LBRACKET),this.parseMultiselectList();if(e===E.TOK_LBRACE)return this.match(E.TOK_LBRACE),this.parseMultiselectHash();const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseProjectionRHS(t){if(y[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===E.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===E.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===E.TOK_DOT)return this.match(E.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)!==E.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===E.TOK_COMMA&&(this.match(E.TOK_COMMA),this.lookahead(0)===E.TOK_RBRACKET))throw new Error("Syntax error: unexpected token Rbracket")}return this.match(E.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[E.TOK_UNQUOTEDIDENTIFIER,E.TOK_QUOTEDIDENTIFIER];let r,n,i;for(;;){if(r=this.lookaheadToken(0),!e.includes(r.type))throw new Error(`Syntax error: expecting an identifier token, got: ${r.type}`);if(n=r.value,this.advance(),this.match(E.TOK_COLON),i=this.expression(0),t.push({value:i,type:"KeyValuePair",name:n}),this.lookahead(0)===E.TOK_COMMA)this.match(E.TOK_COMMA);else if(this.lookahead(0)===E.TOK_RBRACE){this.match(E.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}}const d=new A;class P{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return P.compare(this,new P(t))}static get comparer(){return(t,e)=>new P(t).compareTo(e)}static compare(t,e){const r=t.codePoints,n=e.codePoints;for(let t=0;t<Math.min(r.length,n.length);t++)if(r[t]!==n[t])return r[t]-n[t]>0?1:-1;return r.length-n.length>0?1:-1}reverse(){return String.fromCodePoint(...this.codePoints.reverse())}get codePoints(){return[...this._text].map((t=>t.codePointAt(0)))}}var Y;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING",t[t.TYPE_ARRAY_OBJECT=10]="TYPE_ARRAY_OBJECT",t[t.TYPE_ARRAY_ARRAY=11]="TYPE_ARRAY_ARRAY"}(Y||(Y={}));class N{constructor(t){this.TYPE_NAME_TABLE={[Y.TYPE_NUMBER]:"number",[Y.TYPE_ANY]:"any",[Y.TYPE_STRING]:"string",[Y.TYPE_ARRAY]:"array",[Y.TYPE_OBJECT]:"object",[Y.TYPE_BOOLEAN]:"boolean",[Y.TYPE_EXPREF]:"expression",[Y.TYPE_NULL]:"null",[Y.TYPE_ARRAY_NUMBER]:"Array<number>",[Y.TYPE_ARRAY_OBJECT]:"Array<object>",[Y.TYPE_ARRAY_STRING]:"Array<string>",[Y.TYPE_ARRAY_ARRAY]:"Array<Array<any>>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{if(!t||0==t.length)return null;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,e])=>{if(Array.isArray(t)){return t.includes(e)}if("string"==typeof t){if("string"==typeof e)return t.includes(e)}return null},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFindFirst=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(s(r=r||0),0),n=Math.min(s(n=n||t.length),t.length);const i=t.slice(r,n).indexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFindLast=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(s(r=r||0),0),n=Math.min(s(n=n||t.length),t.length);const i=t.slice(r,n).lastIndexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFloor=([t])=>Math.floor(t),this.functionFromItems=([t])=>(t.map((t=>{if(2!=t.length||"string"!=typeof t[0])throw new Error("invalid value, each array must contain two elements, a pair of string and value")})),Object.fromEntries(t)),this.functionGroupBy=([t,e])=>{const r=this.createKeyFunction(e,[Y.TYPE_STRING]);return t.reduce(((t,e)=>{const n=r(null!=e?e:{});return(t[n]=t[n]||[]).push(e),t}),{})},this.functionItems=([t])=>Object.entries(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([t])=>"string"==typeof t?new P(t).length:Array.isArray(t)?t.length:Object.keys(t).length,this.functionLower=([t])=>(t=>t.toLowerCase())(t),this.functionMap=([t,e])=>{if(!this._interpreter)return[];const r=[],n=this._interpreter;for(let i=0;i<e.length;i+=1)r.push(n.visit(t,e[i]));return r},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===Y.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,[Y.TYPE_NUMBER,Y.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||null},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])===Y.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,[Y.TYPE_NUMBER,Y.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||null},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==Y.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=c("pad_left",e,r),t&&t.padStart(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionPadRight=t=>((t,e,r)=>(r=c("pad_right",e,r),t&&t.padEnd(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionReplace=t=>{const e=t[0],r=t[1],n=t[2];return h(e,r,n,t.length>3?t[3]:void 0)},this.functionSplit=t=>((t,e,r)=>{if(0==t.length&&0===e.length)return[];if(null==r)return t.split(e);if(o(r),0===r)return[t];const n=t.split(e);return[...n.slice(0,r),n.slice(r).join(e)]})(t[0],t[1],t.length>2?t[2]:void 0),this.functionReverse=([t])=>{if(this.getTypeName(t)===Y.TYPE_STRING)return new P(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(P.comparer):[...t].sort(),this.functionSortBy=t=>{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&&![Y.TYPE_NUMBER,Y.TYPE_STRING].includes(i))throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[i]})`);function s(t,e){throw new Error(`Invalid type: expected (${t.TYPE_NAME_TABLE[i]}), received ${t.TYPE_NAME_TABLE[t.getTypeName(e)]}`)}return e.sort(((t,e)=>{const o=r.visit(n,t),a=r.visit(n,e);return this.getTypeName(o)!==i?s(this,o):this.getTypeName(a)!==i&&s(this,a),i===Y.TYPE_STRING?P.comparer(o,a):o-a}))},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===Y.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===Y.TYPE_NUMBER?t:e!==Y.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===Y.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>T(_(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return T(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return _(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case Y.TYPE_NUMBER:return"number";case Y.TYPE_STRING:return"string";case Y.TYPE_ARRAY:return"array";case Y.TYPE_OBJECT:return"object";case Y.TYPE_BOOLEAN:return"boolean";case Y.TYPE_NULL:return"null";default:throw new Error("invalid-type")}},this.functionUpper=([t])=>(t=>t.toUpperCase())(t),this.functionValues=([t])=>Object.values(t),this.functionZip=t=>{const e=Math.min(...t.map((t=>t.length)));return Array(e).fill(null).map(((e,r)=>t.map((t=>t[r]))))},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[Y.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[Y.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[Y.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[Y.TYPE_STRING,Y.TYPE_ARRAY]},{types:[Y.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER],optional:!0},{types:[Y.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER],optional:!0},{types:[Y.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[Y.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[Y.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[Y.TYPE_ARRAY]},{types:[Y.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[Y.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[Y.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[Y.TYPE_STRING,Y.TYPE_ARRAY,Y.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[Y.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[Y.TYPE_EXPREF]},{types:[Y.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[Y.TYPE_ARRAY_NUMBER,Y.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[Y.TYPE_ARRAY]},{types:[Y.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[Y.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[Y.TYPE_ARRAY_NUMBER,Y.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[Y.TYPE_ARRAY]},{types:[Y.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[Y.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER]},{types:[Y.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER]},{types:[Y.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]},{types:[Y.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[Y.TYPE_STRING,Y.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[Y.TYPE_ARRAY_STRING,Y.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[Y.TYPE_ARRAY]},{types:[Y.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[Y.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[Y.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[Y.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[Y.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[Y.TYPE_STRING]},{types:[Y.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[Y.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[Y.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[Y.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[Y.TYPE_ARRAY],variadic:!0}]}},this._interpreter=t,this._functionTable=this.functionTable}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(`Invalid arity: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return!(null!==(e=t.optional)&&void 0!==e&&e)})).length,a=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,u=e.length<o,c=e.length>r.length,h=u&&(!a&&o>1||a)?"at least ":"";if(a&&u||!a&&(u||c))throw s=r.length>1,new Error(`Invalid arity: ${t}() takes ${h}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`Invalid type: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===Y.TYPE_ANY)return!0;if(e!==Y.TYPE_ARRAY_STRING&&e!==Y.TYPE_ARRAY_NUMBER&&e!==Y.TYPE_ARRAY_OBJECT&&e!==Y.TYPE_ARRAY_ARRAY&&e!==Y.TYPE_ARRAY)return t===e;if(e===Y.TYPE_ARRAY)return t===Y.TYPE_ARRAY;if(t===Y.TYPE_ARRAY){let t;e===Y.TYPE_ARRAY_NUMBER?t=Y.TYPE_NUMBER:e===Y.TYPE_ARRAY_OBJECT?t=Y.TYPE_OBJECT:e===Y.TYPE_ARRAY_STRING?t=Y.TYPE_STRING:e===Y.TYPE_ARRAY_ARRAY&&(t=Y.TYPE_ARRAY);const n=r;for(let e=0;e<n.length;e+=1){const r=this.getTypeName(n[e]);if(void 0!==r&&void 0!==t&&!this.typeMatches(r,t,n[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return Y.TYPE_STRING;case"[object Number]":return Y.TYPE_NUMBER;case"[object Array]":return Y.TYPE_ARRAY;case"[object Boolean]":return Y.TYPE_BOOLEAN;case"[object Null]":return Y.TYPE_NULL;case"[object Object]":return t.expref?Y.TYPE_EXPREF:Y.TYPE_OBJECT;default:return}}createKeyFunction(t,e){const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`Invalid type: 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}}}class g{constructor(){this.inner=void 0,this.data={}}withScope(t){const e=new g;return e.inner=this,e.data=t,e}getValue(t){if(t in this.data){const e=this.data[t];if(null!=e)return e}return this.inner?this.inner.getValue(t):null}}class K{constructor(){this._rootValue=null,this.runtime=new N(this),this._scope=new g}withScope(t){const e=new K;return e.runtime._functionTable=this.runtime._functionTable,e._rootValue=this._rootValue,e._scope=this._scope.withScope(t),e}search(t,e){return this._rootValue=e,this._scope=new g,this.visit(t,e)}visit(t,n){var i,s,o,c,h,T;switch(t.type){case"Field":const _=t.name;let l=null;return null===n||"object"!=typeof n||Array.isArray(n)||(l=null!==(i=n[_])&&void 0!==i?i:null),l;case"LetExpression":{const{bindings:e,expression:r}=t;let i={};return e.forEach((t=>{const e=this.visit(t,n);i=Object.assign(Object.assign({},i),e)})),this.withScope(i).visit(r,n)}case"Binding":{const{variable:e,reference:r}=t;return{[e]:this.visit(r,n)}}case"Variable":{const e=t.name,r=null!==(s=this._scope.getValue(e))&&void 0!==s?s:null;if(null==r)throw new Error(`Error referencing undefined variable ${e}`);return r}case"IndexExpression":return this.visit(t.right,this.visit(t.left,n));case"Subexpression":{const e=this.visit(t.left,n);return null!=e&&null!==(o=this.visit(t.right,e))&&void 0!==o?o:null}case"Index":{if(!Array.isArray(n))return null;const e=t.value<0?n.length+t.value:t.value;return null!==(c=n[e])&&void 0!==c?c:null}case"Slice":{if(!Array.isArray(n)&&"string"!=typeof n)return null;const{start:e,stop:r,step:i}=this.computeSliceParams(n.length,t);if("string"==typeof n){const t=[...n];return this.slice(t,e,r,i).join("")}return this.slice(n,e,r,i)}case"Projection":{const{left:e,right:r}=t;let i=!1;"IndexExpression"===e.type&&"Slice"===e.right.type&&(i=!0);const s=this.visit(e,n);if(i&&"string"==typeof s)return this.visit(r,s);if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(r,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:e,right:r}=t,i=this.visit(e,n);if(null===i||"object"!=typeof i||Array.isArray(i))return null;const s=[],o=Object.values(i);for(const t of o){const e=this.visit(r,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:e,right:i,condition:s}=t,o=this.visit(e,n);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(r(e))continue;const n=this.visit(i,t);null!==n&&a.push(n)}return a}case"Arithmetic":{const e=this.visit(t.left,n),r=this.visit(t.right,n);switch(t.operator){case E.TOK_PLUS:return a(h=e,T=r),h+T;case E.TOK_MINUS:return((t,e)=>(a(t,e),t-e))(e,r);case E.TOK_MULTIPLY:case E.TOK_STAR:return((t,e)=>(a(t,e),t*e))(e,r);case E.TOK_DIVIDE:return((t,e)=>(a(t,e),t/u(e)))(e,r);case E.TOK_MODULO:return((t,e)=>(a(t,e),t%e))(e,r);case E.TOK_DIV:return((t,e)=>(a(t,e),Math.floor(t/u(e))))(e,r);default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Unary":{const e=this.visit(t.operand,n);switch(t.operator){case E.TOK_PLUS:return a(e),e;case E.TOK_MINUS:return a(e),-e;default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Comparator":{const r=this.visit(t.left,n),i=this.visit(t.right,n);switch(t.name){case"EQ":return e(r,i);case"NE":return!e(r,i)}if("number"!=typeof r||"number"!=typeof i)return null;switch(t.name){case"GT":return r>i;case"GTE":return r>=i;case"LT":return r<i;case"LTE":return r<=i}}case"Flatten":{const e=this.visit(t.child,n);return Array.isArray(e)?e.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const e=[];for(const r of t.children)e.push(this.visit(r,n));return e}case"MultiSelectHash":{const e={};for(const r of t.children)e[r.name]=this.visit(r.value,n);return e}case"OrExpression":{const e=this.visit(t.left,n);return r(e)?this.visit(t.right,n):e}case"AndExpression":{const e=this.visit(t.left,n);return r(e)?e:this.visit(t.right,n)}case"NotExpression":return r(this.visit(t.child,n));case"Literal":return t.value;case"Pipe":return this.visit(t.right,this.visit(t.left,n));case"Function":{const e=[];for(const r of t.children)e.push(this.visit(r,n));return this.runtime.callFunction(t.name,e)}case"ExpressionReference":return Object.assign({expref:!0},t.child);case"Current":case"Identity":return n}}computeSliceParams(t,e){let{start:r,stop:n,step:i}=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid value: slice step cannot be 0");throw t.name="RuntimeError",t}return r=null===r?i<0?t-1:0:this.capSliceRange(t,r,i),n=null===n?i<0?-1:t:this.capSliceRange(t,n,i),{start:r,stop:n,step: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}slice(t,e,r,n){const i=[];if(n>0)for(let s=e;s<r;s+=n)i.push(t[s]);else for(let s=e;s>r;s+=n)i.push(t[s]);return i}}const m=new K,I=Y.TYPE_ANY,S=Y.TYPE_ARRAY,v=Y.TYPE_ARRAY_ARRAY,L=Y.TYPE_ARRAY_NUMBER,M=Y.TYPE_ARRAY_OBJECT,B=Y.TYPE_ARRAY_STRING,x=Y.TYPE_BOOLEAN,U=Y.TYPE_EXPREF,k=Y.TYPE_NULL,w=Y.TYPE_NUMBER,b=Y.TYPE_OBJECT,C=Y.TYPE_STRING;function F(t,e){return d.parse(t,e)}function G(t,e){return O.tokenize(t,e)}const D=(t,e,r)=>{m.runtime.registerFunction(t,e,r)};function j(t,e,r){const n=d.parse(e,r);return m.search(n,t)}function $(){return new g}const J=m,V={compile:F,registerFunction:D,search:j,tokenize:G,TreeInterpreter:J,TYPE_ANY:I,TYPE_ARRAY_NUMBER:L,TYPE_ARRAY_STRING:B,TYPE_ARRAY:S,TYPE_BOOLEAN:x,TYPE_EXPREF:U,TYPE_NULL:k,TYPE_NUMBER:w,TYPE_OBJECT:b,TYPE_STRING:C};export{$ as Scope,I as TYPE_ANY,S as TYPE_ARRAY,v as TYPE_ARRAY_ARRAY,L as TYPE_ARRAY_NUMBER,M as TYPE_ARRAY_OBJECT,B as TYPE_ARRAY_STRING,x as TYPE_BOOLEAN,U as TYPE_EXPREF,k as TYPE_NULL,w as TYPE_NUMBER,b as TYPE_OBJECT,C as TYPE_STRING,J as TreeInterpreter,F as compile,V as default,V as jmespath,D as registerFunction,j as search,G as tokenize};
2
2
  //# sourceMappingURL=jmespath.esm.min.js.map