@jmespath-community/jmespath 1.1.4 → 1.2.0

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.
Files changed (61) hide show
  1. package/README.md +107 -72
  2. package/dist/jmespath.esm.js +121 -85
  3. package/dist/jmespath.esm.min.js +1 -1
  4. package/dist/jmespath.esm.min.js.map +1 -1
  5. package/dist/jmespath.umd.js +121 -85
  6. package/dist/jmespath.umd.min.js +1 -1
  7. package/dist/jmespath.umd.min.js.map +1 -1
  8. package/dist/lib/bin/jp.js +127 -0
  9. package/dist/lib/bin/jp.js.map +1 -0
  10. package/dist/lib/package.json +70 -0
  11. package/dist/lib/{AST.type.js.map → src/AST.type.js.map} +1 -1
  12. package/dist/lib/{JSON.type.js.map → src/JSON.type.js.map} +1 -1
  13. package/dist/lib/{Lexer.js → src/Lexer.js} +7 -2
  14. package/dist/lib/src/Lexer.js.map +1 -0
  15. package/dist/lib/{Lexer.type.js → src/Lexer.type.js} +1 -0
  16. package/dist/lib/src/Lexer.type.js.map +1 -0
  17. package/dist/lib/{Parser.js → src/Parser.js} +36 -13
  18. package/dist/lib/src/Parser.js.map +1 -0
  19. package/dist/lib/{Parser.type.js.map → src/Parser.type.js.map} +1 -1
  20. package/dist/lib/{Runtime.js → src/Runtime.js} +1 -1
  21. package/dist/lib/src/Runtime.js.map +1 -0
  22. package/dist/lib/{Scope.js → src/Scope.js} +5 -5
  23. package/dist/lib/src/Scope.js.map +1 -0
  24. package/dist/lib/{TreeInterpreter.js → src/TreeInterpreter.js} +17 -10
  25. package/dist/lib/src/TreeInterpreter.js.map +1 -0
  26. package/dist/lib/{index.js → src/index.js} +2 -4
  27. package/dist/lib/src/index.js.map +1 -0
  28. package/dist/lib/{utils → src/utils}/index.js +12 -11
  29. package/dist/lib/src/utils/index.js.map +1 -0
  30. package/dist/lib/src/utils/strings.js.map +1 -0
  31. package/dist/lib/{utils → src/utils}/text.js +1 -0
  32. package/dist/lib/src/utils/text.js.map +1 -0
  33. package/dist/types/bin/jp.d.ts +2 -0
  34. package/dist/types/{AST.type.d.ts → src/AST.type.d.ts} +7 -1
  35. package/dist/types/{Lexer.type.d.ts → src/Lexer.type.d.ts} +2 -1
  36. package/dist/types/{Runtime.d.ts → src/Runtime.d.ts} +1 -1
  37. package/dist/types/{Scope.d.ts → src/Scope.d.ts} +1 -0
  38. package/dist/types/{index.d.ts → src/index.d.ts} +4 -3
  39. package/package.json +4 -1
  40. package/dist/lib/Lexer.js.map +0 -1
  41. package/dist/lib/Lexer.type.js.map +0 -1
  42. package/dist/lib/Parser.js.map +0 -1
  43. package/dist/lib/Runtime.js.map +0 -1
  44. package/dist/lib/Scope.js.map +0 -1
  45. package/dist/lib/TreeInterpreter.js.map +0 -1
  46. package/dist/lib/index.js.map +0 -1
  47. package/dist/lib/utils/index.js.map +0 -1
  48. package/dist/lib/utils/strings.js.map +0 -1
  49. package/dist/lib/utils/text.js.map +0 -1
  50. /package/dist/lib/{AST.type.js → src/AST.type.js} +0 -0
  51. /package/dist/lib/{JSON.type.js → src/JSON.type.js} +0 -0
  52. /package/dist/lib/{Parser.type.js → src/Parser.type.js} +0 -0
  53. /package/dist/lib/{utils → src/utils}/strings.js +0 -0
  54. /package/dist/types/{JSON.type.d.ts → src/JSON.type.d.ts} +0 -0
  55. /package/dist/types/{Lexer.d.ts → src/Lexer.d.ts} +0 -0
  56. /package/dist/types/{Parser.d.ts → src/Parser.d.ts} +0 -0
  57. /package/dist/types/{Parser.type.d.ts → src/Parser.type.d.ts} +0 -0
  58. /package/dist/types/{TreeInterpreter.d.ts → src/TreeInterpreter.d.ts} +0 -0
  59. /package/dist/types/{utils → src/utils}/index.d.ts +0 -0
  60. /package/dist/types/{utils → src/utils}/strings.d.ts +0 -0
  61. /package/dist/types/{utils → src/utils}/text.d.ts +0 -0
@@ -4,6 +4,49 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jmespath = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
+ var Token;
8
+ (function (Token) {
9
+ Token["TOK_EOF"] = "EOF";
10
+ Token["TOK_VARIABLE"] = "Variable";
11
+ Token["TOK_ASSIGN"] = "Assign";
12
+ Token["TOK_UNQUOTEDIDENTIFIER"] = "UnquotedIdentifier";
13
+ Token["TOK_QUOTEDIDENTIFIER"] = "QuotedIdentifier";
14
+ Token["TOK_RBRACKET"] = "Rbracket";
15
+ Token["TOK_RPAREN"] = "Rparen";
16
+ Token["TOK_COMMA"] = "Comma";
17
+ Token["TOK_COLON"] = "Colon";
18
+ Token["TOK_RBRACE"] = "Rbrace";
19
+ Token["TOK_NUMBER"] = "Number";
20
+ Token["TOK_CURRENT"] = "Current";
21
+ Token["TOK_ROOT"] = "Root";
22
+ Token["TOK_EXPREF"] = "Expref";
23
+ Token["TOK_PIPE"] = "Pipe";
24
+ Token["TOK_OR"] = "Or";
25
+ Token["TOK_AND"] = "And";
26
+ Token["TOK_EQ"] = "EQ";
27
+ Token["TOK_GT"] = "GT";
28
+ Token["TOK_LT"] = "LT";
29
+ Token["TOK_GTE"] = "GTE";
30
+ Token["TOK_LTE"] = "LTE";
31
+ Token["TOK_NE"] = "NE";
32
+ Token["TOK_PLUS"] = "Plus";
33
+ Token["TOK_MINUS"] = "Minus";
34
+ Token["TOK_MULTIPLY"] = "Multiply";
35
+ Token["TOK_DIVIDE"] = "Divide";
36
+ Token["TOK_MODULO"] = "Modulo";
37
+ Token["TOK_DIV"] = "Div";
38
+ Token["TOK_FLATTEN"] = "Flatten";
39
+ Token["TOK_STAR"] = "Star";
40
+ Token["TOK_FILTER"] = "Filter";
41
+ Token["TOK_DOT"] = "Dot";
42
+ Token["TOK_NOT"] = "Not";
43
+ Token["TOK_LBRACE"] = "Lbrace";
44
+ Token["TOK_LBRACKET"] = "Lbracket";
45
+ Token["TOK_LPAREN"] = "Lparen";
46
+ Token["TOK_LITERAL"] = "Literal";
47
+ Token["TOK_QUESTION"] = "Question";
48
+ })(Token || (Token = {}));
49
+
7
50
  const isObject = (obj) => {
8
51
  return obj !== null && Object.prototype.toString.call(obj) === '[object Object]';
9
52
  };
@@ -42,31 +85,32 @@
42
85
  return false;
43
86
  };
44
87
  const isFalse = (obj) => {
88
+ // falsey values
89
+ if (obj === null || obj === undefined || obj === false) {
90
+ return true;
91
+ }
92
+ if (typeof obj === 'string') {
93
+ return obj === '';
94
+ }
45
95
  if (typeof obj === 'object') {
46
- if (obj === null) {
47
- return true;
48
- }
49
96
  if (Array.isArray(obj)) {
50
97
  return obj.length === 0;
51
98
  }
52
- // eslint-disable-next-line @typescript-eslint/naming-convention
53
- for (const _key in obj) {
54
- return false;
99
+ if (obj === null) {
100
+ return true;
55
101
  }
56
- return true;
102
+ // check if object is empty
103
+ return Object.keys(obj).length === 0;
57
104
  }
58
- return !(typeof obj === 'number' || obj);
105
+ return false;
59
106
  };
60
107
  const isAlpha = (ch) => {
61
- // tslint:disable-next-line: strict-comparisons
62
108
  return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '_';
63
109
  };
64
110
  const isNum = (ch) => {
65
- // tslint:disable-next-line: strict-comparisons
66
111
  return (ch >= '0' && ch <= '9') || ch === '-';
67
112
  };
68
113
  const isAlphaNum = (ch) => {
69
- // tslint:disable-next-line: strict-comparisons
70
114
  return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch === '_';
71
115
  };
72
116
  const ensureInteger = (value) => {
@@ -207,48 +251,6 @@
207
251
  };
208
252
  const upper = (subject) => subject.toUpperCase();
209
253
 
210
- var Token;
211
- (function (Token) {
212
- Token["TOK_EOF"] = "EOF";
213
- Token["TOK_VARIABLE"] = "Variable";
214
- Token["TOK_ASSIGN"] = "Assign";
215
- Token["TOK_UNQUOTEDIDENTIFIER"] = "UnquotedIdentifier";
216
- Token["TOK_QUOTEDIDENTIFIER"] = "QuotedIdentifier";
217
- Token["TOK_RBRACKET"] = "Rbracket";
218
- Token["TOK_RPAREN"] = "Rparen";
219
- Token["TOK_COMMA"] = "Comma";
220
- Token["TOK_COLON"] = "Colon";
221
- Token["TOK_RBRACE"] = "Rbrace";
222
- Token["TOK_NUMBER"] = "Number";
223
- Token["TOK_CURRENT"] = "Current";
224
- Token["TOK_ROOT"] = "Root";
225
- Token["TOK_EXPREF"] = "Expref";
226
- Token["TOK_PIPE"] = "Pipe";
227
- Token["TOK_OR"] = "Or";
228
- Token["TOK_AND"] = "And";
229
- Token["TOK_EQ"] = "EQ";
230
- Token["TOK_GT"] = "GT";
231
- Token["TOK_LT"] = "LT";
232
- Token["TOK_GTE"] = "GTE";
233
- Token["TOK_LTE"] = "LTE";
234
- Token["TOK_NE"] = "NE";
235
- Token["TOK_PLUS"] = "Plus";
236
- Token["TOK_MINUS"] = "Minus";
237
- Token["TOK_MULTIPLY"] = "Multiply";
238
- Token["TOK_DIVIDE"] = "Divide";
239
- Token["TOK_MODULO"] = "Modulo";
240
- Token["TOK_DIV"] = "Div";
241
- Token["TOK_FLATTEN"] = "Flatten";
242
- Token["TOK_STAR"] = "Star";
243
- Token["TOK_FILTER"] = "Filter";
244
- Token["TOK_DOT"] = "Dot";
245
- Token["TOK_NOT"] = "Not";
246
- Token["TOK_LBRACE"] = "Lbrace";
247
- Token["TOK_LBRACKET"] = "Lbracket";
248
- Token["TOK_LPAREN"] = "Lparen";
249
- Token["TOK_LITERAL"] = "Literal";
250
- })(Token || (Token = {}));
251
-
252
254
  const basicTokens = {
253
255
  '(': Token.TOK_LPAREN,
254
256
  ')': Token.TOK_RPAREN,
@@ -262,6 +264,7 @@
262
264
  '}': Token.TOK_RBRACE,
263
265
  '+': Token.TOK_PLUS,
264
266
  '%': Token.TOK_MODULO,
267
+ '?': Token.TOK_QUESTION,
265
268
  '\u2212': Token.TOK_MINUS,
266
269
  '\u00d7': Token.TOK_MULTIPLY,
267
270
  '\u00f7': Token.TOK_DIVIDE,
@@ -473,7 +476,11 @@
473
476
  this._current += 1;
474
477
  if (this._current < stream.length && stream[this._current] === peek) {
475
478
  this._current += 1;
476
- return { start: start, type: orElse, value: stream.slice(start, this._current) };
479
+ return {
480
+ start: start,
481
+ type: orElse,
482
+ value: stream.slice(start, this._current),
483
+ };
477
484
  }
478
485
  return { start: start, type: token, value: stream[start] };
479
486
  }
@@ -579,8 +586,9 @@
579
586
  [Token.TOK_ROOT]: 0,
580
587
  [Token.TOK_ASSIGN]: 1,
581
588
  [Token.TOK_PIPE]: 1,
582
- [Token.TOK_OR]: 2,
583
- [Token.TOK_AND]: 3,
589
+ [Token.TOK_QUESTION]: 2,
590
+ [Token.TOK_OR]: 3,
591
+ [Token.TOK_AND]: 4,
584
592
  [Token.TOK_EQ]: 5,
585
593
  [Token.TOK_GT]: 5,
586
594
  [Token.TOK_LT]: 5,
@@ -671,23 +679,33 @@
671
679
  }
672
680
  case Token.TOK_MINUS: {
673
681
  const child = this.expression(bindingPower.Minus);
674
- return { type: 'Unary', operator: token.type, operand: child };
682
+ return {
683
+ type: 'Unary',
684
+ operator: token.type,
685
+ operand: child,
686
+ };
675
687
  }
676
688
  case Token.TOK_PLUS: {
677
689
  const child = this.expression(bindingPower.Plus);
678
- return { type: 'Unary', operator: token.type, operand: child };
690
+ return {
691
+ type: 'Unary',
692
+ operator: token.type,
693
+ operand: child,
694
+ };
679
695
  }
680
696
  case Token.TOK_STAR: {
681
697
  const left = { type: 'Identity' };
682
- const right = this.lookahead(0) === Token.TOK_RBRACKET ? left : this.parseProjectionRHS(bindingPower.Star);
683
- return { type: 'ValueProjection', left, right };
698
+ return { type: 'ValueProjection', left, right: this.parseProjectionRHS(bindingPower.Star) };
684
699
  }
685
700
  case Token.TOK_FILTER:
686
701
  return this.led(token.type, { type: 'Identity' });
687
702
  case Token.TOK_LBRACE:
688
703
  return this.parseMultiselectHash();
689
704
  case Token.TOK_FLATTEN: {
690
- const left = { type: 'Flatten', child: { type: 'Identity' } };
705
+ const left = {
706
+ type: 'Flatten',
707
+ child: { type: 'Identity' },
708
+ };
691
709
  const right = this.parseProjectionRHS(bindingPower.Flatten);
692
710
  return { type: 'Projection', left, right };
693
711
  }
@@ -717,12 +735,9 @@
717
735
  return { type: 'ExpressionReference', child };
718
736
  }
719
737
  case Token.TOK_LPAREN: {
720
- console.log('nud::TOK_LPAREN');
721
- const args = [];
722
- let expression = this.expression(0);
723
- args.push(expression);
738
+ const expression = this.expression(0);
724
739
  this.match(Token.TOK_RPAREN);
725
- return args[0];
740
+ return expression;
726
741
  }
727
742
  default:
728
743
  this.errorToken(token);
@@ -730,6 +745,17 @@
730
745
  }
731
746
  led(tokenName, left) {
732
747
  switch (tokenName) {
748
+ case Token.TOK_QUESTION: {
749
+ const trueExpr = this.expression(0);
750
+ this.match(Token.TOK_COLON);
751
+ const falseExpr = this.expression(0);
752
+ return {
753
+ type: 'Ternary',
754
+ condition: left,
755
+ trueExpr,
756
+ falseExpr,
757
+ };
758
+ }
733
759
  case Token.TOK_DOT: {
734
760
  const rbp = bindingPower.Dot;
735
761
  if (this.lookahead(0) !== Token.TOK_STAR) {
@@ -839,7 +865,11 @@
839
865
  return { type: 'Index', value };
840
866
  }
841
867
  projectIfSlice(left, right) {
842
- const indexExpr = { type: 'IndexExpression', left, right };
868
+ const indexExpr = {
869
+ type: 'IndexExpression',
870
+ left,
871
+ right,
872
+ };
843
873
  if (right.type === 'Slice') {
844
874
  return {
845
875
  left: indexExpr,
@@ -1035,6 +1065,7 @@
1035
1065
  }
1036
1066
  get codePoints() {
1037
1067
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1068
+ // biome-ignore lint: lint/style/noNonNullAssertion
1038
1069
  const array = [...this._text].map(s => s.codePointAt(0));
1039
1070
  return array;
1040
1071
  }
@@ -1963,6 +1994,9 @@
1963
1994
  this.inner = undefined;
1964
1995
  this.data = {};
1965
1996
  }
1997
+ get currentScopeData() {
1998
+ return this.data;
1999
+ }
1966
2000
  withScope(data) {
1967
2001
  const outer = new ScopeChain();
1968
2002
  outer.inner = this;
@@ -1970,11 +2004,8 @@
1970
2004
  return outer;
1971
2005
  }
1972
2006
  getValue(identifier) {
1973
- if (identifier in this.data) {
1974
- const result = this.data[identifier];
1975
- if (result !== null && result !== undefined) {
1976
- return result;
1977
- }
2007
+ if (Object.prototype.hasOwnProperty.call(this.data, identifier)) {
2008
+ return this.data[identifier];
1978
2009
  }
1979
2010
  if (this.inner) {
1980
2011
  return this.inner.getValue(identifier);
@@ -2002,15 +2033,22 @@
2002
2033
  return this.visit(node, value);
2003
2034
  }
2004
2035
  visit(node, value) {
2005
- var _a, _b, _c, _d;
2036
+ var _a, _b, _c;
2006
2037
  switch (node.type) {
2038
+ case 'Ternary': {
2039
+ const condition = this.visit(node.condition, value);
2040
+ if (!isFalse(condition)) {
2041
+ return this.visit(node.trueExpr, value);
2042
+ }
2043
+ return this.visit(node.falseExpr, value);
2044
+ }
2007
2045
  case 'Field':
2008
2046
  const identifier = node.name;
2009
- let result = null;
2010
- if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
2011
- result = (_a = value[identifier]) !== null && _a !== void 0 ? _a : null;
2047
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
2048
+ return null;
2012
2049
  }
2013
- return result;
2050
+ // return the value of the field
2051
+ return (_a = value[identifier]) !== null && _a !== void 0 ? _a : null;
2014
2052
  case 'LetExpression': {
2015
2053
  const { bindings, expression } = node;
2016
2054
  let scope = {};
@@ -2027,24 +2065,24 @@
2027
2065
  }
2028
2066
  case 'Variable': {
2029
2067
  const variable = node.name;
2030
- const result = (_b = this._scope.getValue(variable)) !== null && _b !== void 0 ? _b : null;
2031
- if (result === null || result === undefined) {
2068
+ if (!this._scope.getValue(variable) &&
2069
+ !Object.prototype.hasOwnProperty.call(this._scope.currentScopeData, variable)) {
2032
2070
  throw new Error(`Error referencing undefined variable ${variable}`);
2033
2071
  }
2034
- return result;
2072
+ return this._scope.getValue(variable);
2035
2073
  }
2036
2074
  case 'IndexExpression':
2037
2075
  return this.visit(node.right, this.visit(node.left, value));
2038
2076
  case 'Subexpression': {
2039
2077
  const result = this.visit(node.left, value);
2040
- return result != null ? (_c = this.visit(node.right, result)) !== null && _c !== void 0 ? _c : null : null;
2078
+ return result != null ? ((_b = this.visit(node.right, result)) !== null && _b !== void 0 ? _b : null) : null;
2041
2079
  }
2042
2080
  case 'Index': {
2043
2081
  if (!Array.isArray(value)) {
2044
2082
  return null;
2045
2083
  }
2046
2084
  const index = node.value < 0 ? value.length + node.value : node.value;
2047
- return (_d = value[index]) !== null && _d !== void 0 ? _d : null;
2085
+ return (_c = value[index]) !== null && _c !== void 0 ? _c : null;
2048
2086
  }
2049
2087
  case 'Slice': {
2050
2088
  if (!Array.isArray(value) && typeof value !== 'string') {
@@ -2305,9 +2343,7 @@
2305
2343
  function tokenize(expression, options) {
2306
2344
  return Lexer.tokenize(expression, options);
2307
2345
  }
2308
- const registerFunction = (functionName,
2309
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2310
- customFunction, signature) => {
2346
+ const registerFunction = (functionName, customFunction, signature) => {
2311
2347
  TreeInterpreterInstance.runtime.registerFunction(functionName, customFunction, signature);
2312
2348
  };
2313
2349
  function search(data, expression, options) {
@@ -1,2 +1,2 @@
1
- !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("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,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},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,e)=>E(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),l=(t,e)=>E(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),E=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")};var p;!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"}(p||(p={}));const R={"(":p.TOK_LPAREN,")":p.TOK_RPAREN,"*":p.TOK_STAR,",":p.TOK_COMMA,".":p.TOK_DOT,":":p.TOK_COLON,"@":p.TOK_CURRENT,"]":p.TOK_RBRACKET,"{":p.TOK_LBRACE,"}":p.TOK_RBRACE,"+":p.TOK_PLUS,"%":p.TOK_MODULO,"−":p.TOK_MINUS,"×":p.TOK_MULTIPLY,"÷":p.TOK_DIVIDE},f={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},O={"\t":!0,"\n":!0,"\r":!0," ":!0};const A=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(t,e){const r=[];let n,o,a;for(this._current=0,this._enable_legacy_literals=(null==e?void 0:e.enable_legacy_literals)||!1;this._current<t.length;)if(i(t[this._current]))n=this._current,o=this.consumeUnquotedIdentifier(t),r.push({start:n,type:p.TOK_UNQUOTEDIDENTIFIER,value:o});else if(void 0!==R[t[this._current]])r.push({start:this._current,type:R[t[this._current]],value:t[this._current]}),this._current+=1;else if("$"===t[this._current])n=this._current,this._current+1<t.length&&i(t[this._current+1])?(this._current+=1,o=this.consumeUnquotedIdentifier(t),r.push({start:n,type:p.TOK_VARIABLE,value:o})):(r.push({start:n,type:p.TOK_ROOT,value:t[this._current]}),this._current+=1);else if("-"===t[this._current])if(this._current+1<t.length&&s(t[this._current+1])){const e=this.consumeNumber(t);e&&r.push(e)}else{const t={start:this._current,type:p.TOK_MINUS,value:"-"};r.push(t),this._current+=1}else if(s(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])n=this._current,o=this.consumeQuotedIdentifier(t),r.push({start:n,type:p.TOK_QUOTEDIDENTIFIER,value:o});else if("'"===t[this._current])n=this._current,o=this.consumeRawStringLiteral(t),r.push({start:n,type:p.TOK_LITERAL,value:o});else if("`"===t[this._current]){n=this._current;const e=this.consumeLiteral(t);r.push({start:n,type:p.TOK_LITERAL,value:e})}else if(void 0!==f[t[this._current]])a=this.consumeOperator(t),a&&r.push(a);else{if(void 0===O[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 T(T(n,"\\\\","\\"),"\\'","'")}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:p.TOK_NUMBER}}consumeLBracket(t){const e=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:e,type:p.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:e,type:p.TOK_FLATTEN,value:"[]"}):{start:e,type:p.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,"=",p.TOK_NOT,p.TOK_NE);case"<":return this.consumeOrElse(t,"=",p.TOK_LT,p.TOK_LTE);case">":return this.consumeOrElse(t,"=",p.TOK_GT,p.TOK_GTE);case"=":return this.consumeOrElse(t,"=",p.TOK_ASSIGN,p.TOK_EQ);case"&":return this.consumeOrElse(t,"&",p.TOK_EXPREF,p.TOK_AND);case"|":return this.consumeOrElse(t,"|",p.TOK_PIPE,p.TOK_OR);case"/":return this.consumeOrElse(t,"/",p.TOK_DIVIDE,p.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={[p.TOK_EOF]:0,[p.TOK_VARIABLE]:0,[p.TOK_UNQUOTEDIDENTIFIER]:0,[p.TOK_QUOTEDIDENTIFIER]:0,[p.TOK_RBRACKET]:0,[p.TOK_RPAREN]:0,[p.TOK_COMMA]:0,[p.TOK_RBRACE]:0,[p.TOK_NUMBER]:0,[p.TOK_CURRENT]:0,[p.TOK_EXPREF]:0,[p.TOK_ROOT]:0,[p.TOK_ASSIGN]:1,[p.TOK_PIPE]:1,[p.TOK_OR]:2,[p.TOK_AND]:3,[p.TOK_EQ]:5,[p.TOK_GT]:5,[p.TOK_LT]:5,[p.TOK_GTE]:5,[p.TOK_LTE]:5,[p.TOK_NE]:5,[p.TOK_MINUS]:6,[p.TOK_PLUS]:6,[p.TOK_DIV]:7,[p.TOK_DIVIDE]:7,[p.TOK_MODULO]:7,[p.TOK_MULTIPLY]:7,[p.TOK_FLATTEN]:9,[p.TOK_STAR]:20,[p.TOK_FILTER]:21,[p.TOK_DOT]:40,[p.TOK_NOT]:45,[p.TOK_LBRACE]:50,[p.TOK_LBRACKET]:55,[p.TOK_LPAREN]:60};class d{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)!==p.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=[...A.tokenize(t,e),{type:p.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 p.TOK_VARIABLE:return{type:"Variable",name:t.value};case p.TOK_LITERAL:return{type:"Literal",value:t.value};case p.TOK_UNQUOTEDIDENTIFIER:return d.isKeyword(t,"let")&&this.lookahead(0)===p.TOK_VARIABLE?this.parseLetExpression():{type:"Field",name:t.value};case p.TOK_QUOTEDIDENTIFIER:if(this.lookahead(0)===p.TOK_LPAREN)throw new Error("Syntax error: quoted identifier not allowed for function names.");return{type:"Field",name:t.value};case p.TOK_NOT:return{type:"NotExpression",child:this.expression(y.Not)};case p.TOK_MINUS:{const e=this.expression(y.Minus);return{type:"Unary",operator:t.type,operand:e}}case p.TOK_PLUS:{const e=this.expression(y.Plus);return{type:"Unary",operator:t.type,operand:e}}case p.TOK_STAR:{const t={type:"Identity"};return{type:"ValueProjection",left:t,right:this.lookahead(0)===p.TOK_RBRACKET?t:this.parseProjectionRHS(y.Star)}}case p.TOK_FILTER:return this.led(t.type,{type:"Identity"});case p.TOK_LBRACE:return this.parseMultiselectHash();case p.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:{type:"Identity"}},right:this.parseProjectionRHS(y.Flatten)};case p.TOK_LBRACKET:if(this.lookahead(0)===p.TOK_NUMBER||this.lookahead(0)===p.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice({type:"Identity"},t)}if(this.lookahead(0)===p.TOK_STAR&&this.lookahead(1)===p.TOK_RBRACKET){this.advance(),this.advance();return{left:{type:"Identity"},right:this.parseProjectionRHS(y.Star),type:"Projection"}}return this.parseMultiselectList();case p.TOK_CURRENT:return{type:p.TOK_CURRENT};case p.TOK_ROOT:return{type:p.TOK_ROOT};case p.TOK_EXPREF:return{type:"ExpressionReference",child:this.expression(y.Expref)};case p.TOK_LPAREN:{console.log("nud::TOK_LPAREN");const t=[];let e=this.expression(0);return t.push(e),this.match(p.TOK_RPAREN),t[0]}default:this.errorToken(t)}}led(t,e){switch(t){case p.TOK_DOT:{const t=y.Dot;if(this.lookahead(0)!==p.TOK_STAR){return{type:"Subexpression",left:e,right:this.parseDotRHS(t)}}this.advance();return{type:"ValueProjection",left:e,right:this.parseProjectionRHS(t)}}case p.TOK_PIPE:return{type:"Pipe",left:e,right:this.expression(y.Pipe)};case p.TOK_OR:return{type:"OrExpression",left:e,right:this.expression(y.Or)};case p.TOK_AND:return{type:"AndExpression",left:e,right:this.expression(y.And)};case p.TOK_LPAREN:if("Field"!==e.type)throw new Error("Syntax error: expected a Field node");return{name:e.name,type:"Function",children:this.parseCommaSeparatedExpressionsUntilToken(p.TOK_RPAREN)};case p.TOK_FILTER:{const t=this.expression(0);this.match(p.TOK_RBRACKET);return{type:"FilterProjection",left:e,right:this.lookahead(0)===p.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(y.Filter),condition:t}}case p.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:e},right:this.parseProjectionRHS(y.Flatten)};case p.TOK_ASSIGN:{const t=e,r=this.expression(0);return{type:"Binding",variable:t.name,reference:r}}case p.TOK_EQ:case p.TOK_NE:case p.TOK_GT:case p.TOK_GTE:case p.TOK_LT:case p.TOK_LTE:return this.parseComparator(e,t);case p.TOK_PLUS:case p.TOK_MINUS:case p.TOK_MULTIPLY:case p.TOK_STAR:case p.TOK_DIVIDE:case p.TOK_MODULO:case p.TOK_DIV:return this.parseArithmetic(e,t);case p.TOK_LBRACKET:{const t=this.lookaheadToken(0);if(t.type===p.TOK_NUMBER||t.type===p.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice(e,t)}this.match(p.TOK_STAR),this.match(p.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===p.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)===p.TOK_COLON||this.lookahead(1)===p.TOK_COLON)return this.parseSliceExpression();const t=Number(this.lookaheadToken(0).value);return this.advance(),this.match(p.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!=p.TOK_RBRACKET&&e<3;){if(r.type===p.TOK_COLON)e++,3===e&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(r.type===p.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(p.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((()=>d.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)===p.TOK_COMMA&&this.match(p.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([p.TOK_UNQUOTEDIDENTIFIER,p.TOK_QUOTEDIDENTIFIER,p.TOK_STAR].includes(e))return this.expression(t);if(e===p.TOK_LBRACKET)return this.match(p.TOK_LBRACKET),this.parseMultiselectList();if(e===p.TOK_LBRACE)return this.match(p.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)===p.TOK_LBRACKET)return this.expression(t);if(this.lookahead(0)===p.TOK_FILTER)return this.expression(t);if(this.lookahead(0)===p.TOK_DOT)return this.match(p.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)!==p.TOK_RBRACKET;){const e=this.expression(0);if(t.push(e),this.lookahead(0)===p.TOK_COMMA&&(this.match(p.TOK_COMMA),this.lookahead(0)===p.TOK_RBRACKET))throw new Error("Syntax error: unexpected token Rbracket")}return this.match(p.TOK_RBRACKET),{type:"MultiSelectList",children:t}}parseMultiselectHash(){const t=[],e=[p.TOK_UNQUOTEDIDENTIFIER,p.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(p.TOK_COLON),i=this.expression(0),t.push({value:i,type:"KeyValuePair",name:n}),this.lookahead(0)===p.TOK_COMMA)this.match(p.TOK_COMMA);else if(this.lookahead(0)===p.TOK_RBRACE){this.match(p.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:t}}}const P=new d;class Y{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return Y.compare(this,new Y(t))}static get comparer(){return(t,e)=>new Y(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 N;!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"}(N||(N={}));class g{constructor(t){this.TYPE_NAME_TABLE={[N.TYPE_NUMBER]:"number",[N.TYPE_ANY]:"any",[N.TYPE_STRING]:"string",[N.TYPE_ARRAY]:"array",[N.TYPE_OBJECT]:"object",[N.TYPE_BOOLEAN]:"boolean",[N.TYPE_EXPREF]:"expression",[N.TYPE_NULL]:"null",[N.TYPE_ARRAY_NUMBER]:"Array<number>",[N.TYPE_ARRAY_OBJECT]:"Array<object>",[N.TYPE_ARRAY_STRING]:"Array<string>",[N.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,[N.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 Y(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])===N.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,[N.TYPE_NUMBER,N.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])===N.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,[N.TYPE_NUMBER,N.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])!==N.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=h("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=h("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 T(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(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)===N.TYPE_STRING)return new Y(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(Y.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&&![N.TYPE_NUMBER,N.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===N.TYPE_STRING?Y.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)===N.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===N.TYPE_NUMBER?t:e!==N.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===N.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>_(l(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return _(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return l(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case N.TYPE_NUMBER:return"number";case N.TYPE_STRING:return"string";case N.TYPE_ARRAY:return"array";case N.TYPE_OBJECT:return"object";case N.TYPE_BOOLEAN:return"boolean";case N.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:[N.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[N.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[N.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY]},{types:[N.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0},{types:[N.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0},{types:[N.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[N.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[N.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[N.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[N.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY,N.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[N.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[N.TYPE_EXPREF]},{types:[N.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[N.TYPE_ARRAY_NUMBER,N.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[N.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[N.TYPE_ARRAY_NUMBER,N.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[N.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER]},{types:[N.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER]},{types:[N.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[N.TYPE_ARRAY_STRING,N.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[N.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[N.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[N.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[N.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[N.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[N.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[N.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[N.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===N.TYPE_ANY)return!0;if(e!==N.TYPE_ARRAY_STRING&&e!==N.TYPE_ARRAY_NUMBER&&e!==N.TYPE_ARRAY_OBJECT&&e!==N.TYPE_ARRAY_ARRAY&&e!==N.TYPE_ARRAY)return t===e;if(e===N.TYPE_ARRAY)return t===N.TYPE_ARRAY;if(t===N.TYPE_ARRAY){let t;e===N.TYPE_ARRAY_NUMBER?t=N.TYPE_NUMBER:e===N.TYPE_ARRAY_OBJECT?t=N.TYPE_OBJECT:e===N.TYPE_ARRAY_STRING?t=N.TYPE_STRING:e===N.TYPE_ARRAY_ARRAY&&(t=N.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 N.TYPE_STRING;case"[object Number]":return N.TYPE_NUMBER;case"[object Array]":return N.TYPE_ARRAY;case"[object Boolean]":return N.TYPE_BOOLEAN;case"[object Null]":return N.TYPE_NULL;case"[object Object]":return t.expref?N.TYPE_EXPREF:N.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 K{constructor(){this.inner=void 0,this.data={}}withScope(t){const e=new K;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 m{constructor(){this._rootValue=null,this.runtime=new g(this),this._scope=new K}withScope(t){const e=new m;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 K,this.visit(t,e)}visit(t,e){var i,s,o,a,h,T;switch(t.type){case"Field":const _=t.name;let l=null;return null===e||"object"!=typeof e||Array.isArray(e)||(l=null!==(i=e[_])&&void 0!==i?i:null),l;case"LetExpression":{const{bindings:r,expression:n}=t;let i={};return r.forEach((t=>{const r=this.visit(t,e);i=Object.assign(Object.assign({},i),r)})),this.withScope(i).visit(n,e)}case"Binding":{const{variable:r,reference:n}=t;return{[r]:this.visit(n,e)}}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,e));case"Subexpression":{const r=this.visit(t.left,e);return null!=r&&null!==(o=this.visit(t.right,r))&&void 0!==o?o:null}case"Index":{if(!Array.isArray(e))return null;const r=t.value<0?e.length+t.value:t.value;return null!==(a=e[r])&&void 0!==a?a:null}case"Slice":{if(!Array.isArray(e)&&"string"!=typeof e)return null;const{start:r,stop:n,step:i}=this.computeSliceParams(e.length,t);if("string"==typeof e){const t=[...e];return this.slice(t,r,n,i).join("")}return this.slice(e,r,n,i)}case"Projection":{const{left:r,right:n}=t;let i=!1;"IndexExpression"===r.type&&"Slice"===r.right.type&&(i=!0);const s=this.visit(r,e);if(i&&"string"==typeof s)return this.visit(n,s);if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(n,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:r,right:n}=t,i=this.visit(r,e);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(n,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:r,right:i,condition:s}=t,o=this.visit(r,e);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(n(e))continue;const r=this.visit(i,t);null!==r&&a.push(r)}return a}case"Arithmetic":{const r=this.visit(t.left,e),n=this.visit(t.right,e);switch(t.operator){case p.TOK_PLUS:return u(h=r,T=n),h+T;case p.TOK_MINUS:return((t,e)=>(u(t,e),t-e))(r,n);case p.TOK_MULTIPLY:case p.TOK_STAR:return((t,e)=>(u(t,e),t*e))(r,n);case p.TOK_DIVIDE:return((t,e)=>(u(t,e),t/c(e)))(r,n);case p.TOK_MODULO:return((t,e)=>(u(t,e),t%e))(r,n);case p.TOK_DIV:return((t,e)=>(u(t,e),Math.floor(t/c(e))))(r,n);default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Unary":{const r=this.visit(t.operand,e);switch(t.operator){case p.TOK_PLUS:return u(r),r;case p.TOK_MINUS:return u(r),-r;default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Comparator":{const n=this.visit(t.left,e),i=this.visit(t.right,e);switch(t.name){case"EQ":return r(n,i);case"NE":return!r(n,i)}if("number"!=typeof n||"number"!=typeof i)return null;switch(t.name){case"GT":return n>i;case"GTE":return n>=i;case"LT":return n<i;case"LTE":return n<=i}}case"Flatten":{const r=this.visit(t.child,e);return Array.isArray(r)?r.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const r=[];for(const n of t.children)r.push(this.visit(n,e));return r}case"MultiSelectHash":{const r={};for(const n of t.children)r[n.name]=this.visit(n.value,e);return r}case"OrExpression":{const r=this.visit(t.left,e);return n(r)?this.visit(t.right,e):r}case"AndExpression":{const r=this.visit(t.left,e);return n(r)?r:this.visit(t.right,e)}case"NotExpression":return n(this.visit(t.child,e));case"Literal":return t.value;case"Pipe":return this.visit(t.right,this.visit(t.left,e));case"Function":{const r=[];for(const n of t.children)r.push(this.visit(n,e));return this.runtime.callFunction(t.name,r)}case"ExpressionReference":return Object.assign({expref:!0},t.child);case"Current":case"Identity":return e}}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 I=new m,S=N.TYPE_ANY,v=N.TYPE_ARRAY,L=N.TYPE_ARRAY_ARRAY,M=N.TYPE_ARRAY_NUMBER,B=N.TYPE_ARRAY_OBJECT,x=N.TYPE_ARRAY_STRING,U=N.TYPE_BOOLEAN,k=N.TYPE_EXPREF,w=N.TYPE_NULL,b=N.TYPE_NUMBER,C=N.TYPE_OBJECT,F=N.TYPE_STRING;function G(t,e){return P.parse(t,e)}function D(t,e){return A.tokenize(t,e)}const j=(t,e,r)=>{I.runtime.registerFunction(t,e,r)};function $(t,e,r){const n=P.parse(e,r);return I.search(n,t)}const J=I,V={compile:G,registerFunction:j,search:$,tokenize:D,TreeInterpreter:J,TYPE_ANY:S,TYPE_ARRAY_NUMBER:M,TYPE_ARRAY_STRING:x,TYPE_ARRAY:v,TYPE_BOOLEAN:U,TYPE_EXPREF:k,TYPE_NULL:w,TYPE_NUMBER:b,TYPE_OBJECT:C,TYPE_STRING:F};t.Scope=function(){return new K},t.TYPE_ANY=S,t.TYPE_ARRAY=v,t.TYPE_ARRAY_ARRAY=L,t.TYPE_ARRAY_NUMBER=M,t.TYPE_ARRAY_OBJECT=B,t.TYPE_ARRAY_STRING=x,t.TYPE_BOOLEAN=U,t.TYPE_EXPREF=k,t.TYPE_NULL=w,t.TYPE_NUMBER=b,t.TYPE_OBJECT=C,t.TYPE_STRING=F,t.TreeInterpreter=J,t.compile=G,t.default=V,t.jmespath=V,t.registerFunction=j,t.search=$,t.tokenize=D,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !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";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",t.TOK_QUESTION="Question"}(e||(e={}));const r=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),n=(t,e)=>{if(t===e)return!0;if(typeof t!=typeof e)return!1;if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r+=1)if(!n(t[r],e[r]))return!1;return!0}if(r(t)&&r(e)){const r=Object.entries(t),i=new Set(Object.keys(e));if(r.length!==i.size)return!1;for(const[t,s]of r){if(!n(s,e[t]))return!1;i.delete(t)}return 0===i.size}return!1},i=t=>null==t||!1===t||("string"==typeof t?""===t:"object"==typeof t&&(Array.isArray(t)?0===t.length:null===t||0===Object.keys(t).length)),s=t=>t>="a"&&t<="z"||t>="A"&&t<="Z"||"_"===t,o=t=>t>="0"&&t<="9"||"-"===t,a=t=>{if("number"!=typeof t||Math.floor(t)!==t)throw new Error("invalid-value: expecting an integer.");return t},c=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")}},h=t=>{if(!(t=+t))throw new Error("not-a-number: divide by zero");return t},T=(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 c(e),r},_=(t,e,r,n)=>0===n?t:n?(c(n),[...Array(n).keys()].map((()=>t=t.replace(e,r))),t):t.split(e).join(r),l=(t,e)=>p(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),E=(t,e)=>p(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),p=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")},R={"(":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_QUESTION,"−":e.TOK_MINUS,"×":e.TOK_MULTIPLY,"÷":e.TOK_DIVIDE},f={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},O={"\t":!0,"\n":!0,"\r":!0," ":!0};const y=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(t,r){const n=[];let i,a,c;for(this._current=0,this._enable_legacy_literals=(null==r?void 0:r.enable_legacy_literals)||!1;this._current<t.length;)if(s(t[this._current]))i=this._current,a=this.consumeUnquotedIdentifier(t),n.push({start:i,type:e.TOK_UNQUOTEDIDENTIFIER,value:a});else if(void 0!==R[t[this._current]])n.push({start:this._current,type:R[t[this._current]],value:t[this._current]}),this._current+=1;else if("$"===t[this._current])i=this._current,this._current+1<t.length&&s(t[this._current+1])?(this._current+=1,a=this.consumeUnquotedIdentifier(t),n.push({start:i,type:e.TOK_VARIABLE,value:a})):(n.push({start:i,type:e.TOK_ROOT,value:t[this._current]}),this._current+=1);else if("-"===t[this._current])if(this._current+1<t.length&&o(t[this._current+1])){const e=this.consumeNumber(t);e&&n.push(e)}else{const t={start:this._current,type:e.TOK_MINUS,value:"-"};n.push(t),this._current+=1}else if(o(t[this._current]))c=this.consumeNumber(t),n.push(c);else if("["===t[this._current])c=this.consumeLBracket(t),n.push(c);else if('"'===t[this._current])i=this._current,a=this.consumeQuotedIdentifier(t),n.push({start:i,type:e.TOK_QUOTEDIDENTIFIER,value:a});else if("'"===t[this._current])i=this._current,a=this.consumeRawStringLiteral(t),n.push({start:i,type:e.TOK_LITERAL,value:a});else if("`"===t[this._current]){i=this._current;const r=this.consumeLiteral(t);n.push({start:i,type:e.TOK_LITERAL,value:r})}else if(void 0!==f[t[this._current]])c=this.consumeOperator(t),c&&n.push(c);else{if(void 0===O[t[this._current]]){const e=new Error(`Syntax error: unknown character: ${t[this._current]}`);throw e.name="LexerError",e}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;const n=t.slice(e+1,this._current-1);return _(_(n,"\\\\","\\"),"\\'","'")}consumeNumber(t){const r=this._current;this._current+=1;const n=t.length;for(;o(t[this._current])&&this._current<n;)this._current+=1;return{start:r,value:parseInt(t.slice(r,this._current),10),type:e.TOK_NUMBER}}consumeLBracket(t){const r=this._current;return this._current+=1,"?"===t[this._current]?(this._current+=1,{start:r,type:e.TOK_FILTER,value:"[?"}):"]"===t[this._current]?(this._current+=1,{start:r,type:e.TOK_FLATTEN,value:"[]"}):{start:r,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]}}},A={[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_QUESTION]:2,[e.TOK_OR]:3,[e.TOK_AND]:4,[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 d{constructor(){this.index=0,this.tokens=[]}parse(t,r){this.loadTokens(t,r||{enable_legacy_literals:!1}),this.index=0;const n=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 n}loadTokens(t,r){this.tokens=[...y.tokenize(t,r),{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<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){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 d.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(A.Not)};case e.TOK_MINUS:{const e=this.expression(A.Minus);return{type:"Unary",operator:t.type,operand:e}}case e.TOK_PLUS:{const e=this.expression(A.Plus);return{type:"Unary",operator:t.type,operand:e}}case e.TOK_STAR:return{type:"ValueProjection",left:{type:"Identity"},right:this.parseProjectionRHS(A.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(A.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(A.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(A.Expref)};case e.TOK_LPAREN:{const t=this.expression(0);return this.match(e.TOK_RPAREN),t}default:this.errorToken(t)}}led(t,r){switch(t){case e.TOK_QUESTION:{const t=this.expression(0);this.match(e.TOK_COLON);return{type:"Ternary",condition:r,trueExpr:t,falseExpr:this.expression(0)}}case e.TOK_DOT:{const t=A.Dot;if(this.lookahead(0)!==e.TOK_STAR){return{type:"Subexpression",left:r,right:this.parseDotRHS(t)}}this.advance();return{type:"ValueProjection",left:r,right:this.parseProjectionRHS(t)}}case e.TOK_PIPE:return{type:"Pipe",left:r,right:this.expression(A.Pipe)};case e.TOK_OR:return{type:"OrExpression",left:r,right:this.expression(A.Or)};case e.TOK_AND:return{type:"AndExpression",left:r,right:this.expression(A.And)};case e.TOK_LPAREN:if("Field"!==r.type)throw new Error("Syntax error: expected a Field node");return{name:r.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:r,right:this.lookahead(0)===e.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(A.Filter),condition:t}}case e.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:r},right:this.parseProjectionRHS(A.Flatten)};case e.TOK_ASSIGN:{const t=r,e=this.expression(0);return{type:"Binding",variable:t.name,reference:e}}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(r,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(r,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(r,t)}this.match(e.TOK_STAR),this.match(e.TOK_RBRACKET);return{type:"Projection",left:r,right:this.parseProjectionRHS(A.Star)}}default:return this.errorToken(this.lookaheadToken(0))}}static isKeyword(t,r){return t.type===e.TOK_UNQUOTEDIDENTIFIER&&t.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)===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(A.Star),type:"Projection"}:r}parseSliceExpression(){const t=[null,null,null];let r=0,n=this.lookaheadToken(0);for(;n.type!=e.TOK_RBRACKET&&r<3;){if(n.type===e.TOK_COLON)r++,3===r&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(n.type===e.TOK_NUMBER){const e=this.lookaheadToken(0).value;t[r]=e,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(e.TOK_RBRACKET);const[i,s,o]=t;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((()=>d.isKeyword(this.lookaheadToken(0),t)),(()=>{this.advance()}))}parseCommaSeparatedExpressionsUntilToken(t){return this.parseCommaSeparatedExpressionsUntil((()=>this.lookahead(0)===t),(()=>this.match(t)))}parseCommaSeparatedExpressionsUntil(t,r){const n=[];let i;for(;!t();)i=this.expression(0),this.lookahead(0)===e.TOK_COMMA&&this.match(e.TOK_COMMA),n.push(i);return r(),n}parseComparator(t,e){return{type:"Comparator",name:e,left:t,right:this.expression(A[e])}}parseArithmetic(t,e){return{type:"Arithmetic",operator:e,left:t,right:this.expression(A[e])}}parseDotRHS(t){const r=this.lookahead(0);if([e.TOK_UNQUOTEDIDENTIFIER,e.TOK_QUOTEDIDENTIFIER,e.TOK_STAR].includes(r))return this.expression(t);if(r===e.TOK_LBRACKET)return this.match(e.TOK_LBRACKET),this.parseMultiselectList();if(r===e.TOK_LBRACE)return this.match(e.TOK_LBRACE),this.parseMultiselectHash();const n=this.lookaheadToken(0);this.errorToken(n,`Syntax error, unexpected token: ${n.value}(${n.type})`)}parseProjectionRHS(t){if(A[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 r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseMultiselectList(){const t=[];for(;this.lookahead(0)!==e.TOK_RBRACKET;){const r=this.expression(0);if(t.push(r),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=[],r=[e.TOK_UNQUOTEDIDENTIFIER,e.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(e.TOK_COLON),s=this.expression(0),t.push({value:s,type:"KeyValuePair",name:i}),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 P=new d;class Y{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return Y.compare(this,new Y(t))}static get comparer(){return(t,e)=>new Y(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 N;!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"}(N||(N={}));class g{constructor(t){this.TYPE_NAME_TABLE={[N.TYPE_NUMBER]:"number",[N.TYPE_ANY]:"any",[N.TYPE_STRING]:"string",[N.TYPE_ARRAY]:"array",[N.TYPE_OBJECT]:"object",[N.TYPE_BOOLEAN]:"boolean",[N.TYPE_EXPREF]:"expression",[N.TYPE_NULL]:"null",[N.TYPE_ARRAY_NUMBER]:"Array<number>",[N.TYPE_ARRAY_OBJECT]:"Array<object>",[N.TYPE_ARRAY_STRING]:"Array<string>",[N.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(a(r=r||0),0),n=Math.min(a(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(a(r=r||0),0),n=Math.min(a(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,[N.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 Y(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])===N.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,[N.TYPE_NUMBER,N.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])===N.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,[N.TYPE_NUMBER,N.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])!==N.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=T("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=T("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 _(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(c(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)===N.TYPE_STRING)return new Y(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(Y.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&&![N.TYPE_NUMBER,N.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===N.TYPE_STRING?Y.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)===N.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===N.TYPE_NUMBER?t:e!==N.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===N.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>l(E(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return l(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return E(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case N.TYPE_NUMBER:return"number";case N.TYPE_STRING:return"string";case N.TYPE_ARRAY:return"array";case N.TYPE_OBJECT:return"object";case N.TYPE_BOOLEAN:return"boolean";case N.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:[N.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[N.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[N.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY]},{types:[N.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0},{types:[N.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0},{types:[N.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[N.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[N.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[N.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[N.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY,N.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[N.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[N.TYPE_EXPREF]},{types:[N.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[N.TYPE_ARRAY_NUMBER,N.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[N.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[N.TYPE_ARRAY_NUMBER,N.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[N.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER]},{types:[N.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER]},{types:[N.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]},{types:[N.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[N.TYPE_STRING,N.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[N.TYPE_ARRAY_STRING,N.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[N.TYPE_ARRAY]},{types:[N.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[N.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[N.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[N.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[N.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[N.TYPE_STRING]},{types:[N.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[N.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[N.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[N.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[N.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,c=e.length<o,u=e.length>r.length,h=c&&(!a&&o>1||a)?"at least ":"";if(a&&c||!a&&(c||u))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===N.TYPE_ANY)return!0;if(e!==N.TYPE_ARRAY_STRING&&e!==N.TYPE_ARRAY_NUMBER&&e!==N.TYPE_ARRAY_OBJECT&&e!==N.TYPE_ARRAY_ARRAY&&e!==N.TYPE_ARRAY)return t===e;if(e===N.TYPE_ARRAY)return t===N.TYPE_ARRAY;if(t===N.TYPE_ARRAY){let t;e===N.TYPE_ARRAY_NUMBER?t=N.TYPE_NUMBER:e===N.TYPE_ARRAY_OBJECT?t=N.TYPE_OBJECT:e===N.TYPE_ARRAY_STRING?t=N.TYPE_STRING:e===N.TYPE_ARRAY_ARRAY&&(t=N.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 N.TYPE_STRING;case"[object Number]":return N.TYPE_NUMBER;case"[object Array]":return N.TYPE_ARRAY;case"[object Boolean]":return N.TYPE_BOOLEAN;case"[object Null]":return N.TYPE_NULL;case"[object Object]":return t.expref?N.TYPE_EXPREF:N.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 K{constructor(){this.inner=void 0,this.data={}}get currentScopeData(){return this.data}withScope(t){const e=new K;return e.inner=this,e.data=t,e}getValue(t){return Object.prototype.hasOwnProperty.call(this.data,t)?this.data[t]:this.inner?this.inner.getValue(t):null}}class m{constructor(){this._rootValue=null,this.runtime=new g(this),this._scope=new K}withScope(t){const e=new m;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 K,this.visit(t,e)}visit(t,r){var s,o,a,c,T;switch(t.type){case"Ternary":{const e=this.visit(t.condition,r);return i(e)?this.visit(t.falseExpr,r):this.visit(t.trueExpr,r)}case"Field":const _=t.name;return null===r||"object"!=typeof r||Array.isArray(r)?null:null!==(s=r[_])&&void 0!==s?s:null;case"LetExpression":{const{bindings:e,expression:n}=t;let i={};return e.forEach((t=>{const e=this.visit(t,r);i=Object.assign(Object.assign({},i),e)})),this.withScope(i).visit(n,r)}case"Binding":{const{variable:e,reference:n}=t;return{[e]:this.visit(n,r)}}case"Variable":{const e=t.name;if(!this._scope.getValue(e)&&!Object.prototype.hasOwnProperty.call(this._scope.currentScopeData,e))throw new Error(`Error referencing undefined variable ${e}`);return this._scope.getValue(e)}case"IndexExpression":return this.visit(t.right,this.visit(t.left,r));case"Subexpression":{const e=this.visit(t.left,r);return null!=e&&null!==(o=this.visit(t.right,e))&&void 0!==o?o:null}case"Index":{if(!Array.isArray(r))return null;const e=t.value<0?r.length+t.value:t.value;return null!==(a=r[e])&&void 0!==a?a:null}case"Slice":{if(!Array.isArray(r)&&"string"!=typeof r)return null;const{start:e,stop:n,step:i}=this.computeSliceParams(r.length,t);if("string"==typeof r){const t=[...r];return this.slice(t,e,n,i).join("")}return this.slice(r,e,n,i)}case"Projection":{const{left:e,right:n}=t;let i=!1;"IndexExpression"===e.type&&"Slice"===e.right.type&&(i=!0);const s=this.visit(e,r);if(i&&"string"==typeof s)return this.visit(n,s);if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(n,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:e,right:n}=t,i=this.visit(e,r);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(n,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:e,right:n,condition:s}=t,o=this.visit(e,r);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(i(e))continue;const r=this.visit(n,t);null!==r&&a.push(r)}return a}case"Arithmetic":{const n=this.visit(t.left,r),i=this.visit(t.right,r);switch(t.operator){case e.TOK_PLUS:return u(c=n,T=i),c+T;case e.TOK_MINUS:return((t,e)=>(u(t,e),t-e))(n,i);case e.TOK_MULTIPLY:case e.TOK_STAR:return((t,e)=>(u(t,e),t*e))(n,i);case e.TOK_DIVIDE:return((t,e)=>(u(t,e),t/h(e)))(n,i);case e.TOK_MODULO:return((t,e)=>(u(t,e),t%e))(n,i);case e.TOK_DIV:return((t,e)=>(u(t,e),Math.floor(t/h(e))))(n,i);default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Unary":{const n=this.visit(t.operand,r);switch(t.operator){case e.TOK_PLUS:return u(n),n;case e.TOK_MINUS:return u(n),-n;default:throw new Error(`Syntax error: unknown arithmetic operator: ${t.operator}`)}}case"Comparator":{const e=this.visit(t.left,r),i=this.visit(t.right,r);switch(t.name){case"EQ":return n(e,i);case"NE":return!n(e,i)}if("number"!=typeof e||"number"!=typeof i)return null;switch(t.name){case"GT":return e>i;case"GTE":return e>=i;case"LT":return e<i;case"LTE":return e<=i}}case"Flatten":{const e=this.visit(t.child,r);return Array.isArray(e)?e.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const e=[];for(const n of t.children)e.push(this.visit(n,r));return e}case"MultiSelectHash":{const e={};for(const n of t.children)e[n.name]=this.visit(n.value,r);return e}case"OrExpression":{const e=this.visit(t.left,r);return i(e)?this.visit(t.right,r):e}case"AndExpression":{const e=this.visit(t.left,r);return i(e)?e:this.visit(t.right,r)}case"NotExpression":return i(this.visit(t.child,r));case"Literal":return t.value;case"Pipe":return this.visit(t.right,this.visit(t.left,r));case"Function":{const e=[];for(const n of t.children)e.push(this.visit(n,r));return this.runtime.callFunction(t.name,e)}case"ExpressionReference":return Object.assign({expref:!0},t.child);case"Current":case"Identity":return r}}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 I=new m,S=N.TYPE_ANY,v=N.TYPE_ARRAY,L=N.TYPE_ARRAY_ARRAY,M=N.TYPE_ARRAY_NUMBER,x=N.TYPE_ARRAY_OBJECT,B=N.TYPE_ARRAY_STRING,U=N.TYPE_BOOLEAN,k=N.TYPE_EXPREF,w=N.TYPE_NULL,b=N.TYPE_NUMBER,C=N.TYPE_OBJECT,F=N.TYPE_STRING;function G(t,e){return P.parse(t,e)}function D(t,e){return y.tokenize(t,e)}const j=(t,e,r)=>{I.runtime.registerFunction(t,e,r)};function $(t,e,r){const n=P.parse(e,r);return I.search(n,t)}const J=I,V={compile:G,registerFunction:j,search:$,tokenize:D,TreeInterpreter:J,TYPE_ANY:S,TYPE_ARRAY_NUMBER:M,TYPE_ARRAY_STRING:B,TYPE_ARRAY:v,TYPE_BOOLEAN:U,TYPE_EXPREF:k,TYPE_NULL:w,TYPE_NUMBER:b,TYPE_OBJECT:C,TYPE_STRING:F};t.Scope=function(){return new K},t.TYPE_ANY=S,t.TYPE_ARRAY=v,t.TYPE_ARRAY_ARRAY=L,t.TYPE_ARRAY_NUMBER=M,t.TYPE_ARRAY_OBJECT=x,t.TYPE_ARRAY_STRING=B,t.TYPE_BOOLEAN=U,t.TYPE_EXPREF=k,t.TYPE_NULL=w,t.TYPE_NUMBER=b,t.TYPE_OBJECT=C,t.TYPE_STRING=F,t.TreeInterpreter=J,t.compile=G,t.default=V,t.jmespath=V,t.registerFunction=j,t.search=$,t.tokenize=D,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=jmespath.umd.min.js.map