@swaggerexpert/jsonpath 2.1.0 → 2.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.
package/README.md CHANGED
@@ -37,6 +37,7 @@ The development of this library contributed to the identification and formal sub
37
37
  - [Normalized paths](#normalized-paths)
38
38
  - [Translators](#translators)
39
39
  - [CST](#cst-translator)
40
+ - [CST Optimized](#cst-optimized-translator)
40
41
  - [XML](#xml-translator)
41
42
  - [Statistics](#statistics)
42
43
  - [Tracing](#tracing)
@@ -148,6 +149,17 @@ interface CSTNode {
148
149
  }
149
150
  ```
150
151
 
152
+ ###### CST Optimized translator
153
+
154
+ Same as CST, but optimizes the tree for more optimized representation. By default, it collapses
155
+ fragmented `single-quoted` or `double-quoted` nodes into a single node.
156
+
157
+ ```js
158
+ import { parse, CSTOptimizedTranslator } from '@swaggerexpert/jsonpath';
159
+
160
+ const { tree: CST } = parse('$.store.book[0].title', { translator: new CSTOptimizedTranslator() });
161
+ ```
162
+
151
163
  ###### XML translator
152
164
 
153
165
  ```js
package/cjs/index.cjs CHANGED
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.parse = exports.XMLTranslator = exports.Grammar = exports.CSTTranslator = void 0;
4
+ exports.parse = exports.XMLTranslator = exports.Grammar = exports.CSTTranslator = exports.CSTOptimizedTranslator = void 0;
5
5
  var _grammar = _interopRequireDefault(require("./grammar.cjs"));
6
6
  exports.Grammar = _grammar.default;
7
7
  var _index = _interopRequireDefault(require("./parse/index.cjs"));
8
8
  exports.parse = _index.default;
9
9
  var _CSTTranslator = _interopRequireDefault(require("./parse/translators/CSTTranslator.cjs"));
10
10
  exports.CSTTranslator = _CSTTranslator.default;
11
+ var _CSTOptimizedTranslator = _interopRequireDefault(require("./parse/translators/CSTOptimizedTranslator.cjs"));
12
+ exports.CSTOptimizedTranslator = _CSTOptimizedTranslator.default;
11
13
  var _XMLTranslator = _interopRequireDefault(require("./parse/translators/XMLTranslator.cjs"));
12
14
  exports.XMLTranslator = _XMLTranslator.default;
13
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -22,9 +22,15 @@ const cst = ruleName => {
22
22
  children: []
23
23
  };
24
24
  if (data.stack.length > 0) {
25
+ var _data$options, _data$options2;
25
26
  const parent = data.stack[data.stack.length - 1];
26
- const isTextWithinTextNode = parent.type === 'text' && node.type === 'text';
27
- if (!isTextWithinTextNode) {
27
+ const prevSibling = parent.children[parent.children.length - 1];
28
+ 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;
30
+ if (shouldCollapse) {
31
+ prevSibling.text += node.text;
32
+ prevSibling.length += node.length;
33
+ } else if (!isTextNodeWithinTextNode) {
28
34
  parent.children.push(node);
29
35
  }
30
36
  } else {
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _CSTTranslator = _interopRequireDefault(require("./CSTTranslator.cjs"));
6
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
+ class CSTOptimizedTranslator extends _CSTTranslator.default {
8
+ collapsibleTypes = ['single-quoted', 'double-quoted'];
9
+ constructor({
10
+ collapsibleTypes
11
+ } = {}) {
12
+ super();
13
+ if (Array.isArray(collapsibleTypes)) {
14
+ this.collapsibleTypes = collapsibleTypes;
15
+ }
16
+ }
17
+ getTree() {
18
+ const options = {
19
+ optimize: true,
20
+ collapsibleTypes: this.collapsibleTypes
21
+ };
22
+ const data = {
23
+ stack: [],
24
+ root: null,
25
+ options
26
+ };
27
+ this.translate(data);
28
+ delete data.stack;
29
+ delete data.options;
30
+ return data;
31
+ }
32
+ }
33
+ var _default = exports.default = CSTOptimizedTranslator;
package/es/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as Grammar } from "./grammar.mjs";
2
2
  export { default as parse } from "./parse/index.mjs";
3
3
  export { default as CSTTranslator } from "./parse/translators/CSTTranslator.mjs";
4
+ export { default as CSTOptimizedTranslator } from "./parse/translators/CSTOptimizedTranslator.mjs";
4
5
  export { default as XMLTranslator } from "./parse/translators/XMLTranslator.mjs";
@@ -17,9 +17,15 @@ const cst = ruleName => {
17
17
  children: []
18
18
  };
19
19
  if (data.stack.length > 0) {
20
+ var _data$options, _data$options2;
20
21
  const parent = data.stack[data.stack.length - 1];
21
- const isTextWithinTextNode = parent.type === 'text' && node.type === 'text';
22
- if (!isTextWithinTextNode) {
22
+ const prevSibling = parent.children[parent.children.length - 1];
23
+ 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;
25
+ if (shouldCollapse) {
26
+ prevSibling.text += node.text;
27
+ prevSibling.length += node.length;
28
+ } else if (!isTextNodeWithinTextNode) {
23
29
  parent.children.push(node);
24
30
  }
25
31
  } else {
@@ -0,0 +1,28 @@
1
+ import CSTTranslator from "./CSTTranslator.mjs";
2
+ class CSTOptimizedTranslator extends CSTTranslator {
3
+ collapsibleTypes = ['single-quoted', 'double-quoted'];
4
+ constructor({
5
+ collapsibleTypes
6
+ } = {}) {
7
+ super();
8
+ if (Array.isArray(collapsibleTypes)) {
9
+ this.collapsibleTypes = collapsibleTypes;
10
+ }
11
+ }
12
+ getTree() {
13
+ const options = {
14
+ optimize: true,
15
+ collapsibleTypes: this.collapsibleTypes
16
+ };
17
+ const data = {
18
+ stack: [],
19
+ root: null,
20
+ options
21
+ };
22
+ this.translate(data);
23
+ delete data.stack;
24
+ delete data.options;
25
+ return data;
26
+ }
27
+ }
28
+ export default CSTOptimizedTranslator;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "2.1.0",
6
+ "version": "2.2.0",
7
7
  "description": "RCF 9535 implementation of JSONPath",
8
8
  "main": "./cjs/index.cjs",
9
9
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -16,6 +16,10 @@ export interface Translator<TTree = unknown> {
16
16
  export declare class CSTTranslator implements Translator<CSTTree> {
17
17
  getTree(): CSTTree;
18
18
  }
19
+ export declare class CSTOptimizedTranslator implements CSTTranslator {
20
+ constructor(options?: { collapsibleTypes?: string[] });
21
+ getTree(): CSTTree;
22
+ }
19
23
  export declare class XMLTranslator implements Translator<XMLTree> {
20
24
  getTree(): XMLTree;
21
25
  }