@swaggerexpert/jsonpath 2.4.0 → 2.5.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
@@ -154,6 +154,7 @@ interface CSTNode {
154
154
 
155
155
  Same as CST, but optimizes the tree for more optimized representation. By default, it collapses
156
156
  fragmented `single-quoted`, `double-quoted` or `normal-single-quoted` nodes into a single node.
157
+ `text`, `segments` and `singular-query-segments` nodes, when empty, are removed from the tree.
157
158
 
158
159
  ```js
159
160
  import { parse, CSTOptimizedTranslator } from '@swaggerexpert/jsonpath';
@@ -5,28 +5,31 @@ exports.default = void 0;
5
5
  var _apgLite = require("../../apg-lite.cjs");
6
6
  var _JSONPathParseError = _interopRequireDefault(require("../../errors/JSONPathParseError.cjs"));
7
7
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
- const cst = ruleName => {
8
+ const cst = nodeType => {
9
9
  return (state, chars, phraseIndex, phraseLength, data) => {
10
+ var _data$options, _data$options2;
10
11
  if (!(typeof data === 'object' && data !== null && !Array.isArray(data))) {
11
12
  throw new _JSONPathParseError.default("parser's user data must be an object");
12
13
  }
13
14
 
14
15
  // drop the empty nodes
15
- if (phraseLength === 0) return;
16
+ if ((_data$options = data.options) != null && _data$options.optimize && phraseLength === 0 && (_data$options2 = data.options) != null && (_data$options2 = _data$options2.droppableTypes) != null && _data$options2.includes(nodeType)) {
17
+ return;
18
+ }
16
19
  if (state === _apgLite.identifiers.SEM_PRE) {
17
20
  const node = {
18
- type: ruleName,
21
+ type: nodeType,
19
22
  text: _apgLite.utilities.charsToString(chars, phraseIndex, phraseLength),
20
23
  start: phraseIndex,
21
24
  length: phraseLength,
22
25
  children: []
23
26
  };
24
27
  if (data.stack.length > 0) {
25
- var _data$options, _data$options2;
28
+ var _data$options3, _data$options4;
26
29
  const parent = data.stack[data.stack.length - 1];
27
30
  const prevSibling = parent.children[parent.children.length - 1];
28
31
  const isTextNodeWithinTextNode = parent.type === 'text' && node.type === 'text';
29
- const shouldCollapse = ((_data$options = data.options) == null ? void 0 : _data$options.optimize) && ((_data$options2 = data.options) == null || (_data$options2 = _data$options2.collapsibleTypes) == null ? void 0 : _data$options2.includes(node.type)) && (prevSibling == null ? void 0 : prevSibling.type) === node.type;
32
+ const shouldCollapse = ((_data$options3 = data.options) == null ? void 0 : _data$options3.optimize) && ((_data$options4 = data.options) == null || (_data$options4 = _data$options4.collapsibleTypes) == null ? void 0 : _data$options4.includes(node.type)) && (prevSibling == null ? void 0 : prevSibling.type) === node.type;
30
33
  if (shouldCollapse) {
31
34
  prevSibling.text += node.text;
32
35
  prevSibling.length += node.length;
@@ -18,7 +18,6 @@ const parse = (jsonPath, {
18
18
  throw new TypeError('JSONPath must be a string');
19
19
  }
20
20
  try {
21
- var _parser$ast;
22
21
  const parser = new _apgLite.Parser();
23
22
  if (translator) parser.ast = translator;
24
23
  if (stats) parser.stats = new _apgLite.Stats();
@@ -27,7 +26,7 @@ const parse = (jsonPath, {
27
26
  const result = parser.parse(grammar, startRule, jsonPath);
28
27
  return {
29
28
  result,
30
- tree: (_parser$ast = parser.ast) == null ? void 0 : _parser$ast.getTree(),
29
+ tree: result.success && translator ? parser.ast.getTree() : undefined,
31
30
  stats: parser.stats,
32
31
  trace: parser.trace
33
32
  };
@@ -6,6 +6,7 @@ var _CSTTranslator = _interopRequireDefault(require("./CSTTranslator.cjs"));
6
6
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
7
  class CSTOptimizedTranslator extends _CSTTranslator.default {
8
8
  collapsibleTypes = ['single-quoted', 'double-quoted', 'normal-single-quoted'];
9
+ droppableTypes = ['text', 'segments', 'singular-query-segments'];
9
10
  constructor({
10
11
  collapsibleTypes
11
12
  } = {}) {
@@ -17,7 +18,8 @@ class CSTOptimizedTranslator extends _CSTTranslator.default {
17
18
  getTree() {
18
19
  const options = {
19
20
  optimize: true,
20
- collapsibleTypes: this.collapsibleTypes
21
+ collapsibleTypes: this.collapsibleTypes,
22
+ droppableTypes: this.droppableTypes
21
23
  };
22
24
  const data = {
23
25
  stack: [],
@@ -1,27 +1,30 @@
1
1
  import { utilities, identifiers } from 'apg-lite';
2
2
  import JSONPathParseError from "../../errors/JSONPathParseError.mjs";
3
- const cst = ruleName => {
3
+ const cst = nodeType => {
4
4
  return (state, chars, phraseIndex, phraseLength, data) => {
5
+ var _data$options, _data$options2;
5
6
  if (!(typeof data === 'object' && data !== null && !Array.isArray(data))) {
6
7
  throw new JSONPathParseError("parser's user data must be an object");
7
8
  }
8
9
 
9
10
  // drop the empty nodes
10
- if (phraseLength === 0) return;
11
+ if ((_data$options = data.options) !== null && _data$options !== void 0 && _data$options.optimize && phraseLength === 0 && (_data$options2 = data.options) !== null && _data$options2 !== void 0 && (_data$options2 = _data$options2.droppableTypes) !== null && _data$options2 !== void 0 && _data$options2.includes(nodeType)) {
12
+ return;
13
+ }
11
14
  if (state === identifiers.SEM_PRE) {
12
15
  const node = {
13
- type: ruleName,
16
+ type: nodeType,
14
17
  text: utilities.charsToString(chars, phraseIndex, phraseLength),
15
18
  start: phraseIndex,
16
19
  length: phraseLength,
17
20
  children: []
18
21
  };
19
22
  if (data.stack.length > 0) {
20
- var _data$options, _data$options2;
23
+ var _data$options3, _data$options4;
21
24
  const parent = data.stack[data.stack.length - 1];
22
25
  const prevSibling = parent.children[parent.children.length - 1];
23
26
  const isTextNodeWithinTextNode = parent.type === 'text' && node.type === 'text';
24
- const shouldCollapse = ((_data$options = data.options) === null || _data$options === void 0 ? void 0 : _data$options.optimize) && ((_data$options2 = data.options) === null || _data$options2 === void 0 || (_data$options2 = _data$options2.collapsibleTypes) === null || _data$options2 === void 0 ? void 0 : _data$options2.includes(node.type)) && (prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.type) === node.type;
27
+ const shouldCollapse = ((_data$options3 = data.options) === null || _data$options3 === void 0 ? void 0 : _data$options3.optimize) && ((_data$options4 = data.options) === null || _data$options4 === void 0 || (_data$options4 = _data$options4.collapsibleTypes) === null || _data$options4 === void 0 ? void 0 : _data$options4.includes(node.type)) && (prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.type) === node.type;
25
28
  if (shouldCollapse) {
26
29
  prevSibling.text += node.text;
27
30
  prevSibling.length += node.length;
@@ -13,7 +13,6 @@ const parse = (jsonPath, {
13
13
  throw new TypeError('JSONPath must be a string');
14
14
  }
15
15
  try {
16
- var _parser$ast;
17
16
  const parser = new Parser();
18
17
  if (translator) parser.ast = translator;
19
18
  if (stats) parser.stats = new Stats();
@@ -22,7 +21,7 @@ const parse = (jsonPath, {
22
21
  const result = parser.parse(grammar, startRule, jsonPath);
23
22
  return {
24
23
  result,
25
- tree: (_parser$ast = parser.ast) === null || _parser$ast === void 0 ? void 0 : _parser$ast.getTree(),
24
+ tree: result.success && translator ? parser.ast.getTree() : undefined,
26
25
  stats: parser.stats,
27
26
  trace: parser.trace
28
27
  };
@@ -1,6 +1,7 @@
1
1
  import CSTTranslator from "./CSTTranslator.mjs";
2
2
  class CSTOptimizedTranslator extends CSTTranslator {
3
3
  collapsibleTypes = ['single-quoted', 'double-quoted', 'normal-single-quoted'];
4
+ droppableTypes = ['text', 'segments', 'singular-query-segments'];
4
5
  constructor({
5
6
  collapsibleTypes
6
7
  } = {}) {
@@ -12,7 +13,8 @@ class CSTOptimizedTranslator extends CSTTranslator {
12
13
  getTree() {
13
14
  const options = {
14
15
  optimize: true,
15
- collapsibleTypes: this.collapsibleTypes
16
+ collapsibleTypes: this.collapsibleTypes,
17
+ droppableTypes: this.droppableTypes
16
18
  };
17
19
  const data = {
18
20
  stack: [],
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "2.4.0",
6
+ "version": "2.5.0",
7
7
  "description": "RCF 9535 implementation of JSONPath",
8
8
  "main": "./cjs/index.cjs",
9
9
  "types": "./types/index.d.ts",