@swaggerexpert/jsonpath 1.0.2 → 1.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
@@ -30,6 +30,9 @@ The development of this library contributed to the identification and formal sub
30
30
  - [Installation](#installation)
31
31
  - [Usage](#usage)
32
32
  - [Parsing](#parsing)
33
+ - [Concrete Syntax Tree (CST)](#concrete-syntax-tree-cst)
34
+ - [Interpreting Parse result as XML](#interpreting-parse-result-as-xml)
35
+ - [Parse statistics](#parse-statistics)
33
36
  - [Errors](#errors)
34
37
  - [Grammar](#grammar)
35
38
  - [More about JSONPath](#more-about-jsonpath)
@@ -256,7 +259,27 @@ const parseResult = parse('$.store.book[0].title', { ast: new JSONPathQueryCST()
256
259
  }
257
260
  ```
258
261
 
259
- ###### Interpreting AST as XML
262
+ ###### Concrete Syntax Tree (CST)
263
+
264
+ [Concrete Syntax Tree](https://en.wikipedia.org/wiki/Parse_tree) (Parse tree) is available on parse result via `computed` field.
265
+ Instance of `JSONPathQueryCST` needs to be assigned to `ast` option in `parse` function (default behavior).
266
+ CST is suitable to be consumed by other tools like IDEs, editors, etc...
267
+
268
+ ```js
269
+ import { parse } from '@swaggerexpert/jsonpath';
270
+
271
+ const { computed: CST } = parse('$.store.book[0].title');
272
+ ```
273
+
274
+ or
275
+
276
+ ```js
277
+ import { parse, JSONPathQueryCST } from '@swaggerexpert/jsonpath';
278
+
279
+ const { computed: CST } = parse('$.store.book[0].title', { ast: new JSONPathQueryCST() });
280
+ ```
281
+
282
+ ###### Interpreting Parse result as XML
260
283
 
261
284
  ```js
262
285
  import { parse } from '@swaggerexpert/jsonpath';
@@ -265,6 +288,20 @@ const parseResult = parse('$.store.book[0].title');
265
288
  const xml = parseResult.ast.toXml();
266
289
  ```
267
290
 
291
+ ###### Parse statistics
292
+
293
+ `parse` function returns additional statistical information about the parsing process.
294
+ Collection of the statistics can be enabled by setting `stats` option to `true`.
295
+
296
+ ```js
297
+ import { parse } from '@swaggerexpert/jsonpath';
298
+
299
+ const { stats } = parse('$.store.book[0].title', { stats: true });
300
+
301
+ stats.displayStats(); // returns operator stats
302
+ stats.displayHits(); // returns rules grouped by hit count
303
+ ```
304
+
268
305
  #### Errors
269
306
 
270
307
  `@swaggerexpert/jsonpath` provides a structured error class hierarchy,
@@ -453,8 +490,8 @@ LCALPHA = %x61-7A ; "a".."z"
453
490
  function-expr = function-name left-paren S [function-argument ; MODIFICATION: surrogate text rule used
454
491
  *(S comma S function-argument)] S right-paren ; MODIFICATION: surrogate text rule used
455
492
  function-argument = logical-expr / ; MODIFICATION: https://www.rfc-editor.org/errata/eid8343
456
- function-expr /
457
493
  filter-query / ; (includes singular-query)
494
+ function-expr /
458
495
  literal
459
496
 
460
497
 
@@ -11,6 +11,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
11
11
  const grammar = new _grammar.default();
12
12
  const parse = (jsonPath, {
13
13
  ast = new _JSONPathQueryCST.default(),
14
+ stats = false,
14
15
  evaluator = _translate.default
15
16
  } = {}) => {
16
17
  if (typeof jsonPath !== 'string') {
@@ -19,6 +20,7 @@ const parse = (jsonPath, {
19
20
  try {
20
21
  const parser = new _apgLite.Parser();
21
22
  parser.ast = ast;
23
+ if (stats) parser.stats = new _apgLite.Stats();
22
24
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
23
25
  const computed = evaluator(ast, {
24
26
  result
@@ -26,6 +28,7 @@ const parse = (jsonPath, {
26
28
  return {
27
29
  result,
28
30
  ast,
31
+ stats: parser.stats,
29
32
  computed
30
33
  };
31
34
  } catch (error) {
@@ -1,4 +1,4 @@
1
- import { Parser } from 'apg-lite';
1
+ import { Parser, Stats } from 'apg-lite';
2
2
  import Grammar from "../grammar.mjs";
3
3
  import translateEvaluator from "./evaluators/translate.mjs";
4
4
  import JSONPathQueryCST from "./ast/JSONPathQueryCST.mjs";
@@ -6,6 +6,7 @@ import JSONPathParseError from "../errors/JSONPathParseError.mjs";
6
6
  const grammar = new Grammar();
7
7
  const parse = (jsonPath, {
8
8
  ast = new JSONPathQueryCST(),
9
+ stats = false,
9
10
  evaluator = translateEvaluator
10
11
  } = {}) => {
11
12
  if (typeof jsonPath !== 'string') {
@@ -14,6 +15,7 @@ const parse = (jsonPath, {
14
15
  try {
15
16
  const parser = new Parser();
16
17
  parser.ast = ast;
18
+ if (stats) parser.stats = new Stats();
17
19
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
18
20
  const computed = evaluator(ast, {
19
21
  result
@@ -21,6 +23,7 @@ const parse = (jsonPath, {
21
23
  return {
22
24
  result,
23
25
  ast,
26
+ stats: parser.stats,
24
27
  computed
25
28
  };
26
29
  } catch (error) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.0.2",
6
+ "version": "1.1.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
@@ -5,6 +5,7 @@ export function parse(jsonpath: string, options?: ParseOptions): ParseResult;
5
5
 
6
6
  export interface ParseOptions {
7
7
  readonly ast?: AST;
8
+ readonly stats?: boolean;
8
9
  }
9
10
 
10
11
  export interface AST {
@@ -18,14 +19,20 @@ export interface ParseResult {
18
19
  };
19
20
  readonly ast: AST;
20
21
  readonly computed: Record<string, CSTNode>;
22
+ readonly stats?: Stats;
21
23
  }
22
24
 
23
25
  export interface CSTNode {
24
- type: string,
25
- text: string,
26
- start: number,
27
- length: number,
28
- children: CSTNode[],
26
+ readonly type: string,
27
+ readonly text: string,
28
+ readonly start: number,
29
+ readonly length: number,
30
+ readonly children: CSTNode[],
31
+ }
32
+
33
+ export interface Stats {
34
+ displayStats(): string;
35
+ displayHits(): string;
29
36
  }
30
37
 
31
38
  /**