@loancrate/json-selector 2.0.0 → 2.1.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # LoanCrate JSON Selectors
2
2
 
3
- LoanCrate JSON Selectors are based on a subset of [JMESPath](https://jmespath.org/specification.html)
3
+ LoanCrate JSON Selectors are based on a subset of [JMESPath](https://jmespath.org)
4
4
  with a shorthand/extension for selecting an object from an array based on ID.
5
5
 
6
6
  Currently, the subset includes everything except [functions](https://jmespath.org/specification.html#functions-expressions),
@@ -58,6 +58,50 @@ accessor.delete();
58
58
  console.log(obj.foo.bar[0].value); // undefined
59
59
  ```
60
60
 
61
+ ## Operator Precedence
62
+
63
+ As mentioned above, JSON Selectors are based on
64
+ [JMESPath](https://jmespath.org). Although JMESPath claims to have an "ABNF
65
+ grammar with a complete specification", the
66
+ [specification](https://jmespath.org/specification.html) is not complete
67
+ regarding operator precedence, since it only mentions the relative precedence of
68
+ 5 tokens (`|`, `||`, `&&`, `!`, and `]`). To discover the precedence of other
69
+ operators, we must turn to the [JMESPath source
70
+ code](https://github.com/jmespath/jmespath.js/blob/master/jmespath.js). It is
71
+ implemented as a [Top-Down Operator Precedence (TDOP)
72
+ parser](https://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing),
73
+ which is based on principles like "token binding power", "null denotation"
74
+ (**nud**), and "left denotation" (**led**). Given knowledge of these principles
75
+ and the [binding power
76
+ table](https://github.com/jmespath/jmespath.js/blob/master/jmespath.js#L474-L501)
77
+ from the source, we can reverse-engineer the operator precedence of JMESPath.
78
+
79
+ Essentially, the expression grammar is structured as a left-hand side (LHS)
80
+ expression followed by zero or more right-hand side (RHS) expressions (which are
81
+ often projections on the result of the LHS). RHS expressions are consumed by the
82
+ parser and projected onto the LHS as long as they have the same or higher
83
+ binding power as the LHS. RHS expressions with lower binding power are projected
84
+ onto the result of the overall expression to the left, as opposed to the nearest
85
+ subexpression. For example, since dot (40) has a higher binding power than left
86
+ bracket (55), `a.b.c['id'].d.e` is parsed and evaluated like
87
+ `((a.b.c)['id']).d.e`. Binding power and precedence can be summarized as
88
+ follows, in increasing order:
89
+
90
+ - pipe: `|`
91
+ - or: `||`
92
+ - and: `&&`
93
+ - compare: `<=`, `>=`, `<`, `>`, `==`, `!=`
94
+ - not: `!`
95
+ - flatten projection: `[]`
96
+ - filter projection: `[?`
97
+ - star/slice projection: `[*`, `[<number?>:`
98
+ - index/ID access: `[<number>`, `['`
99
+ - member access: `.`
100
+
101
+ However, as a [special
102
+ case](https://github.com/jmespath/jmespath.js/blob/master/jmespath.js#L803-L805),
103
+ member access can directly follow (act as RHS) for any projection.
104
+
61
105
  ## License
62
106
 
63
107
  This library is available under the [ISC license](LICENSE).
package/dist/ast.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { JsonValue } from "type-fest";
2
- export declare type JsonSelectorNodeType = JsonSelector["type"];
2
+ export type JsonSelectorNodeType = JsonSelector["type"];
3
3
  export interface JsonSelectorCurrent {
4
4
  type: "current";
5
5
  }
@@ -51,7 +51,7 @@ export interface JsonSelectorNot {
51
51
  type: "not";
52
52
  expression: JsonSelector;
53
53
  }
54
- export declare type JsonSelectorCompareOperator = "<" | "<=" | "==" | ">=" | ">" | "!=";
54
+ export type JsonSelectorCompareOperator = "<" | "<=" | "==" | ">=" | ">" | "!=";
55
55
  export interface JsonSelectorCompare {
56
56
  type: "compare";
57
57
  operator: JsonSelectorCompareOperator;
@@ -73,5 +73,5 @@ export interface JsonSelectorPipe {
73
73
  lhs: JsonSelector;
74
74
  rhs: JsonSelector;
75
75
  }
76
- export declare type JsonSelector = JsonSelectorCurrent | JsonSelectorLiteral | JsonSelectorIdentifier | JsonSelectorFieldAccess | JsonSelectorIndexAccess | JsonSelectorIdAccess | JsonSelectorProject | JsonSelectorFilter | JsonSelectorSlice | JsonSelectorFlatten | JsonSelectorNot | JsonSelectorCompare | JsonSelectorAnd | JsonSelectorOr | JsonSelectorPipe;
76
+ export type JsonSelector = JsonSelectorCurrent | JsonSelectorLiteral | JsonSelectorIdentifier | JsonSelectorFieldAccess | JsonSelectorIndexAccess | JsonSelectorIdAccess | JsonSelectorProject | JsonSelectorFilter | JsonSelectorSlice | JsonSelectorFlatten | JsonSelectorNot | JsonSelectorCompare | JsonSelectorAnd | JsonSelectorOr | JsonSelectorPipe;
77
77
  //# sourceMappingURL=ast.d.ts.map
package/dist/ast.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,oBAAY,oBAAoB,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;AAExD,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,SAAS,CAAC;CAClB;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,YAAY,CAAC;IACnB,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,aAAa,CAAC;IACpB,UAAU,EAAE,YAAY,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,aAAa,CAAC;IACpB,UAAU,EAAE,YAAY,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,UAAU,CAAC;IACjB,UAAU,EAAE,YAAY,CAAC;IACzB,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,SAAS,CAAC;IAChB,UAAU,EAAE,YAAY,CAAC;IACzB,UAAU,CAAC,EAAE,YAAY,CAAC;CAC3B;AAED,MA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