json-p3 0.1.1 → 0.2.1

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
@@ -19,101 +19,6 @@ JSONPath, JSON Patch and JSON Pointer for JavaScript.
19
19
 
20
20
  ---
21
21
 
22
- **Table of Contents**
23
-
24
- - [Install](#install)
25
- - [JSONPath](#jsonpath)
26
- - [JSON Pointer](#json-pointer)
27
- - [JSON Patch](#json-patch)
28
-
29
- ## Install
30
-
31
- ### Node.js
32
-
33
- Use npm:
34
-
35
- ```console
36
- npm install --save json-p3
37
- ```
38
-
39
- Or yarn:
40
-
41
- ```console
42
- yarn add json-p3
43
- ```
44
-
45
- Or pnpm:
46
-
47
- ```console
48
- pnpm add json-p3
49
- ```
50
-
51
- And use ES module imports:
52
-
53
- ```javascript
54
- import { query } from "json-p3";
55
-
56
- const data = {
57
- players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
58
- visitors: [{ name: "Brian" }, { name: "Roy" }],
59
- };
60
-
61
- const nodes = query("$..name", data);
62
- console.log(nodes.values());
63
- // [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
64
- ```
65
-
66
- Or CommonJS modules:
67
-
68
- ```javascript
69
- const { query } = require("json-p3");
70
-
71
- const data = {
72
- players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
73
- visitors: [{ name: "Brian" }, { name: "Roy" }],
74
- };
75
-
76
- const nodes = query("$..name", data);
77
- console.log(nodes.values());
78
- // [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
79
- ```
80
-
81
- ### Browser
82
-
83
- Download and include JSON P3 in a script tag:
84
-
85
- ```html
86
- <script src="path/to/json-p3.iife.min.js"></script>
87
- <script>
88
- const data = {
89
- players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
90
- visitors: [{ name: "Brian" }, { name: "Roy" }],
91
- };
92
- const nodes = json_p3.query("$..name");
93
- console.log(nodes.values());
94
- // [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
95
- </script>
96
- ```
97
-
98
- Or use a CDN:
99
-
100
- ```html
101
- <script src="https://cdn.jsdelivr.net/npm/json-p3@0.1.0/dist/json-p3.iife.min.js"></script>
102
- <script>
103
- const data = {
104
- players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
105
- visitors: [{ name: "Brian" }, { name: "Roy" }],
106
- };
107
- const nodes = json_p3.query("$..name");
108
- console.log(nodes.values());
109
- // [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
110
- </script>
111
- ```
112
-
113
- ## JSONPath
114
-
115
- Retrieve values from JSON-like data using JSONPath query expressions. We strictly follow standards described in the [IETF JSONPath draft](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-20).
116
-
117
22
  ```javascript
118
23
  import { jsonpath } from "json-p3";
119
24
 
@@ -130,205 +35,33 @@ const nodes = jsonpath.query("$.users[?@.score < 100].name", data);
130
35
  console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
131
36
  ```
132
37
 
133
- The result of `jsonpath.query()` is an instance of `JSONPathNodeList`. That is a list of `JSONPathNode` objects, one node for each value in the target JSON document matching the query. Each node has a:
134
-
135
- - `value` - The value found in the target JSON document. This could be an array, object or primitive value.
136
- - `location` - An array of property names and array indices that were required to reach the node's value in the target JSON document.
137
- - `path` - The normalized JSONPath to this node in the target JSON document.
138
-
139
- Use `JSONPathNodeList.paths()` to retrieve all node paths.
140
-
141
- ```javascript
142
- // .. continued from above
143
- console.log(nodes.paths());
144
- ```
145
-
146
- **Output:**
147
-
148
- ```plain
149
- [
150
- "$['users']['1']['name']",
151
- "$['users']['2']['name']",
152
- "$['users']['3']['name']"
153
- ]
154
- ```
155
-
156
- And `JSONPathNodeList.locations()` to get an array of node locations.
157
-
158
- ```javascript
159
- // .. continued from above
160
- console.log(nodes.locations());
161
- ```
38
+ ## Links
162
39
 
163
- **Output:**
40
+ - Docs: https://jg-rp.github.io/json-p3/
41
+ - Install: https://jg-rp.github.io/json-p3/#install
42
+ - JSONPath syntax: https://jg-rp.github.io/json-p3/guides/jsonpath-syntax
43
+ - API reference: https://jg-rp.github.io/json-p3/api
44
+ - Change log: https://github.com/jg-rp/json-p3/blob/main/CHANGELOG.md
45
+ - NPM: https://www.npmjs.com/package/json-p3
46
+ - Issue tracker: https://github.com/jg-rp/json-p3/issues
164
47
 
165
- ```plain
166
- [
167
- [ 'users', 1, 'name' ],
168
- [ 'users', 2, 'name' ],
169
- [ 'users', 3, 'name' ]
170
- ]
171
- ```
48
+ ## Bundles
172
49
 
173
- `JSONPathNodeList` objects are iterable too.
50
+ JSON P3 is written in TypeScript, compiled to JavaScript using [Babel](https://babeljs.io/), and bundled using [Rollup](https://rollupjs.org/introduction/). The following, included bundles target `defaults, maintained node version`, as defined by [Browserslist](https://browsersl.ist/#q=defaults%2C+maintained+node+versions).
174
51
 
175
- ```javascript
176
- // .. continued from above
177
- for (const node of nodes) {
178
- console.log(`${node.value} @ ${node.path}`);
179
- }
180
- ```
52
+ JSON P3 has zero runtime dependencies.
181
53
 
182
- **Output:**
54
+ | Bundle | Description |
55
+ | --------------------- | -------------------------------------------------------------------------- |
56
+ | `json-p3.cjs.js` | A CommonJS formatted bundle. |
57
+ | `json-p3.esm.js` | An ECMAScript module formatted bundle. |
58
+ | `json-p3-iife.js` | A bundle formatted as an Immediately Invoked Function Expression. |
59
+ | `json-p3-iife.min.js` | A minified bundle formatted as an Immediately Invoked Function Expression. |
183
60
 
184
- ```plain
185
- John @ $['users'][1]['name']
186
- Sally @ $['users'][2]['name']
187
- Jane @ $['users'][3]['name']
188
- ```
61
+ ## Contributing
189
62
 
190
- You can also compile a JSONPath query for repeated use against different data.
63
+ Please see [Contributing to JSON P3](https://github.com/jg-rp/json-p3/blob/main/CONTRIBUTING.md)
191
64
 
192
- ```javascript
193
- import { jsonpath } from "json-p3";
65
+ ## License
194
66
 
195
- const data = {
196
- users: [
197
- { name: "Sue", score: 100 },
198
- { name: "John", score: 86 },
199
- { name: "Sally", score: 84 },
200
- { name: "Jane", score: 55 },
201
- ],
202
- };
203
-
204
- const path = jsonpath.compile("$.users[?@.score < 100].name");
205
- const nodes = path.query(data);
206
- console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
207
- ```
208
-
209
- ## JSON Pointer
210
-
211
- Identify a single value in JSON-like data, as per RFC 6901. Use `jsonpointer.resolve()` to retrieve the value.
212
-
213
- ```javascript
214
- import { jsonpointer } from "json-p3";
215
-
216
- const data = {
217
- users: [
218
- { name: "Sue", score: 100 },
219
- { name: "John", score: 86 },
220
- { name: "Sally", score: 84 },
221
- { name: "Jane", score: 55 },
222
- ],
223
- };
224
-
225
- const rv = jsonpointer.resolve("/users/1", data);
226
- console.log(rv); // { name: 'John', score: 86 }
227
- ```
228
-
229
- If the pointer can't be resolved against the argument JSON value, one of `JSONPointerIndexError`, `JSONPointerKeyError` or `JSONPointerTypeError` is thrown. All three exceptions inherit from `JSONPointerResolutionError`.
230
-
231
- ```javascript
232
- // .. continued from above
233
- const rv = jsonpointer.resolve("/users/1/age", data);
234
- // JSONPointerKeyError: no such property ("/users/1/age")
235
- ```
236
-
237
- A fallback value can be given as a third argument, which will be returned in the event of a `JSONPointerResolutionError`.
238
-
239
- ```javascript
240
- // .. continued from above
241
- const rv = jsonpointer.resolve("/users/1/age", data, -1);
242
- console.log(rv); // -1
243
- ```
244
-
245
- You can also create an instance of `JSONPointer` then resolve it against different data.
246
-
247
- ```javascript
248
- import { JSONPointer } from "json-p3";
249
-
250
- const someData = {
251
- users: [
252
- { name: "Sue", score: 100 },
253
- { name: "John", score: 86 },
254
- { name: "Sally", score: 84 },
255
- ],
256
- };
257
-
258
- const otherData = {
259
- users: [{ name: "Brian" }, { name: "Roy" }],
260
- };
261
-
262
- const pointer = new JSONPointer("/users/1");
263
- console.log(pointer.resolve(someData)); // { name: 'John', score: 86 }
264
- console.log(pointer.resolve(otherData)); // { name: 'Roy' }
265
- ```
266
-
267
- ## JSON Patch
268
-
269
- Apply a JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)) to some data. A JSON Patch defines update operation to perform on a JSON document. **Data is modified in place.**.
270
-
271
- ```javascript
272
- import { jsonpatch } from "json-p3";
273
-
274
- const ops = [
275
- { op: "add", path: "/some/foo", value: { foo: {} } },
276
- { op: "add", path: "/some/foo", value: { bar: [] } },
277
- { op: "copy", from: "/some/other", path: "/some/foo/else" },
278
- { op: "add", path: "/some/foo/bar/-", value: 1 },
279
- ];
280
-
281
- const data = { some: { other: "thing" } };
282
- jsonpatch.apply(ops, data);
283
- console.log(data);
284
- // { some: { other: 'thing', foo: { bar: [Array], else: 'thing' } } }
285
- ```
286
-
287
- Use the `JSONPatch` class to create a patch for repeated application.
288
-
289
- ```javascript
290
- import { JSONPatch } from "json-p3";
291
-
292
- const patch = new JSONPatch([
293
- { op: "add", path: "/some/foo", value: { foo: {} } },
294
- { op: "add", path: "/some/foo", value: { bar: [] } },
295
- { op: "copy", from: "/some/other", path: "/some/foo/else" },
296
- { op: "add", path: "/some/foo/bar/-", value: 1 },
297
- ]);
298
-
299
- const data = { some: { other: "thing" } };
300
- patch.apply(data);
301
- console.log(data);
302
- // { some: { other: 'thing', foo: { bar: [Array], else: 'thing' } } }
303
- ```
304
-
305
- `JSONPatch` also offers a builder API for constructing JSON patch documents. We use strings as JSON Pointers in this example, but existing `JSONPointer` objects are OK too.
306
-
307
- ```javascript
308
- import { JSONPatch } from "json-p3";
309
-
310
- const data = { some: { other: "thing" } };
311
-
312
- const patch = new JSONPatch()
313
- .add("/some/foo", { foo: [] })
314
- .add("/some/foo", { bar: [] })
315
- .copy("/some/other", "/some/foo/else")
316
- .add("/some/foo/bar/-", "/some/foo/else");
317
-
318
- patch.apply(data);
319
- console.log(JSON.stringify(data, undefined, " "));
320
- ```
321
-
322
- **Output:**
323
-
324
- ```json
325
- {
326
- "some": {
327
- "other": "thing",
328
- "foo": {
329
- "bar": ["/some/foo/else"],
330
- "else": "thing"
331
- }
332
- }
333
- }
334
- ```
67
+ `json-p3` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export declare const version = "__VERSION__";
2
2
  export * as jsonpath from "./path";
3
- export { FunctionExpressionType, JSONPath, JSONPathEnvironment, JSONPathError, JSONPathIndexError, JSONPathLexerError, JSONPathNode, JSONPathNodeList, JSONPathSyntaxError, JSONPathTypeError, Token, TokenKind, Nothing, query, compile, } from "./path";
3
+ export { DEFAULT_ENVIRONMENT, FunctionExpressionType, JSONPath, JSONPathEnvironment, JSONPathError, JSONPathIndexError, JSONPathLexerError, JSONPathNode, JSONPathNodeList, JSONPathSyntaxError, JSONPathTypeError, JSONPathRecursionLimitError, Token, TokenKind, Nothing, query, compile, } from "./path";
4
4
  export type { JSONPathEnvironmentOptions, FilterFunction } from "./path";
5
5
  export * as jsonpointer from "./pointer";
6
- export { JSONPointer, resolve, UNDEFINED } from "./pointer";
6
+ export { JSONPointer, RelativeJSONPointer, resolve, UNDEFINED, } from "./pointer";
7
7
  export * as jsonpatch from "./patch";
8
8
  export { JSONPatch, JSONPatchError, JSONPatchTestFailure, apply, } from "./patch";
9
9
  export type { OpObject } from "./patch";