@jmespath-community/jmespath 1.3.0 → 1.4.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
@@ -8,29 +8,91 @@ JMESPath is a query language for JSON. It will take a JSON document
8
8
  as input and transform it into another JSON document
9
9
  given a JMESPath expression.
10
10
 
11
- ## INSTALLATION
12
-
13
- ```
11
+ ## Table of Contents
12
+
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [ESM (Node.js)](#esm-nodejs)
16
+ - [CommonJS (Node.js)](#commonjs-nodejs)
17
+ - [Browser (UMD)](#browser-umd)
18
+ - [API](#api)
19
+ - [`search(data, expression, options)`](#searchdata-d-expression-string-options-options-t)
20
+ - [`searchStream(source, expression, options)`](#searchstreamsource-expression-options-asyncgenerator)
21
+ - [`compile(expression)`](#compileexpression-string-expressionnodetree)
22
+ - [`compileQuery(expression, options)`](#compilequeryexpression-string-options-options-compiledquery)
23
+ - [`validate(expression, options)`](#validateexpression-string-options-options-validationresult)
24
+ - [Extensions](#extensions-to-original-spec)
25
+ - [More Resources](#more-resources)
26
+ - [Experimental Features](#experimental-features)
27
+
28
+ ## Installation
29
+
30
+ ```bash
14
31
  npm install @jmespath-community/jmespath
15
32
  ```
16
33
 
17
- ## USAGE
34
+ ## Usage
18
35
 
19
- ### `search(data: JSONValue, expression: string): JSONValue`
36
+ ### ESM (Node.js)
20
37
 
21
38
  ```javascript
22
39
  import { search } from "@jmespath-community/jmespath";
23
40
 
24
- search({ foo: { bar: { baz: [0, 1, 2, 3, 4] } } }, "foo.bar.baz[2]");
41
+ const data = { foo: { bar: { baz: [0, 1, 2, 3, 4] } } };
42
+ const result = search(data, "foo.bar.baz[2]");
25
43
 
26
- // OUTPUTS: 2
44
+ console.log(result);
45
+ // Outputs: 2
27
46
  ```
28
47
 
48
+ ### CommonJS (Node.js)
49
+
50
+ ```javascript
51
+ const { search } = require("@jmespath-community/jmespath");
52
+
53
+ const data = { foo: { bar: { baz: [0, 1, 2, 3, 4] } } };
54
+ const result = search(data, "foo.bar.baz[2]");
55
+
56
+ console.log(result);
57
+ // Outputs: 2
58
+ ```
59
+
60
+ ### Browser (UMD)
61
+
62
+ You can include the UMD build directly in your HTML file. The library will be available under the `jmespath` global variable.
63
+
64
+ ```html
65
+ <script src="https://unpkg.com/@jmespath-community/jmespath/dist/index.umd.min.js"></script>
66
+ <script>
67
+ const data = { foo: { bar: { baz: [0, 1, 2, 3, 4] } } };
68
+ const result = jmespath.search(data, "foo.bar.baz[2]");
69
+ console.log(result);
70
+ // Outputs: 2
71
+ </script>
72
+ ```
73
+
74
+ ## API
75
+
76
+ ### `search<T = JSONValue, D = unknown>(data: D, expression: string, options?: Options): T`
77
+
29
78
  In the example we gave the `search` function input data of
30
79
  `{foo: {bar: {baz: [0, 1, 2, 3, 4]}}}` as well as the JMESPath
31
80
  expression `foo.bar.baz[2]`, and the `search` function evaluated
32
81
  the expression against the input data to produce the result `2`.
33
82
 
83
+ Accepts custom TypeScript interfaces directly without casting:
84
+
85
+ ```typescript
86
+ interface User {
87
+ name: string;
88
+ age: number;
89
+ }
90
+
91
+ const user: User = { name: "Alice", age: 30 };
92
+ const name = search<string>(user, "name");
93
+ // name is typed as string
94
+ ```
95
+
34
96
  The JMESPath language can do _a lot_ more than select an element
35
97
  from a list. Here are a few more examples:
36
98
 
@@ -72,6 +134,61 @@ search(JSON_DOCUMENT, "foo[?age > `30`]");
72
134
  // OUTPUTS: [ { age: 35 }, { age: 40 } ]
73
135
  ```
74
136
 
137
+ #### Automatic Type Inference
138
+
139
+ Literal path queries automatically infer property types without requiring manual type parameters or type assertions:
140
+
141
+ ```typescript
142
+ const user = {
143
+ profile: { name: "Alice", email: "alice@example.com" },
144
+ tags: ["admin", "dev"],
145
+ };
146
+
147
+ const name = search(user, "profile.name"); // inferred as string
148
+ const tag = search(user, "tags[0]"); // inferred as string
149
+ const tags = search(user, "tags[*]"); // inferred as string[]
150
+ ```
151
+
152
+ #### Runtime Security Options
153
+
154
+ Pass `options` to restrict execution limits on untrusted queries:
155
+
156
+ ```typescript
157
+ search(data, expression, {
158
+ maxDepth: 32, // expression and evaluation recursion limit
159
+ maxNodes: 128, // maximum AST node count
160
+ maxOutputSize: 1000, // maximum elements or characters in results
161
+ allowedFunctions: ['length', 'contains'], // allow-list specific built-ins
162
+ disallowedFunctions: ['custom_heavy'], // deny-list specific built-ins
163
+ timeoutMs: 100, // query execution time limit in ms
164
+ });
165
+ ```
166
+
167
+ ### `searchStream<T = JSONValue, D = unknown>(source: StreamSource<D>, expression: string | ExpressionNode, options?: StreamOptions): AsyncGenerator<T, void, unknown>`
168
+
169
+ Evaluates a JMESPath expression incrementally over an `AsyncIterable`, `ReadableStream`, or Node.js `stream.Readable`, yielding matching results without buffering large datasets in memory:
170
+
171
+ ```typescript
172
+ import jmespath from "@jmespath-community/jmespath";
173
+
174
+ async function* getEvents() {
175
+ yield { id: 1, level: "info", message: "connected" };
176
+ yield { id: 2, level: "error", message: "disk full" };
177
+ }
178
+
179
+ for await (const msg of jmespath.searchStream(getEvents(), "[?level == 'error'].message")) {
180
+ console.log(msg); // "disk full"
181
+ }
182
+ ```
183
+
184
+ Supports automatic NDJSON / JSON lines chunk decoding (`parseJson: 'auto'`), chunk flattening, precompiled ASTs, and early termination:
185
+
186
+ ```typescript
187
+ for await (const item of jmespath.searchStream(logStream, "[:10]", { limit: 10 })) {
188
+ console.log(item);
189
+ }
190
+ ```
191
+
75
192
  ### `compile(expression: string): ExpressionNodeTree`
76
193
 
77
194
  You can precompile all your expressions ready for use later on. The `compile`
@@ -83,10 +200,37 @@ import { compile, TreeInterpreter } from "@jmespath-community/jmespath";
83
200
 
84
201
  const ast = compile("foo.bar");
85
202
 
86
- TreeInterpreter.search(ast, { foo: { bar: "BAZ" } });
203
+ const result = TreeInterpreter.search(ast, { foo: { bar: "BAZ" } });
87
204
  // RETURNS: "BAZ"
88
205
  ```
89
206
 
207
+ ### `compileQuery<T = JSONValue, D = unknown>(expression: string, options?: Options): CompiledQuery<T, D>`
208
+
209
+ Precompiles an expression into a reusable, callable function:
210
+
211
+ ```typescript
212
+ import { compileQuery } from "@jmespath-community/jmespath";
213
+
214
+ const getName = compileQuery<string, { user: { name: string } }>("user.name");
215
+
216
+ const result = getName({ user: { name: "Alice" } });
217
+ // Outputs: "Alice"
218
+ ```
219
+
220
+ ### `validate(expression: string, options?: Options): ValidationResult`
221
+
222
+ Validates an expression without throwing exceptions, checking syntax and security options:
223
+
224
+ ```typescript
225
+ import { validate } from "@jmespath-community/jmespath";
226
+
227
+ const result = validate("foo.bar");
228
+ // { valid: true, ast: { type: 'Subexpression', ... } }
229
+
230
+ const invalid = validate("foo.", { maxNodes: 10 });
231
+ // { valid: false, error: 'Syntax error: ...' }
232
+ ```
233
+
90
234
  ---
91
235
 
92
236
  ## EXTENSIONS TO ORIGINAL SPEC