@loancrate/json-selector 1.0.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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2022, Loan Crate, Inc.
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # LoanCrate JSON Selectors
2
+
3
+ LoanCrate JSON Selectors are based on a subset of [JMESPath](https://jmespath.org/specification.html)
4
+ with a shorthand/extension for selecting an object from an array based on ID.
5
+ Specifically, the subset includes the [Basic Expressions](https://jmespath.org/tutorial.html#basic-expressions)
6
+ ([identifiers](https://jmespath.org/specification.html#identifiers),
7
+ [subexpressions](https://jmespath.org/specification.html#subexpressions),
8
+ and [index expressions](https://jmespath.org/specification.html#indexexpressions)).
9
+ To allow for selection by ID, we extend index expressions to accept a
10
+ [raw string literal](https://jmespath.org/specification.html#raw-string-literals)
11
+ (as opposed to a numeric literal), which represents the value of the `id` property
12
+ of the desired object from an array of objects.
13
+ Formally, `x['y']` would be equivalent to `x[?id == 'y'] | [0]` in JMESPath.
14
+ This should be unambiguous relative to the existing grammar and semantics.
15
+
16
+ ## Installation
17
+
18
+ ```sh
19
+ npm add @loancrate/json-selector
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ts
25
+ import {
26
+ accessWithJsonSelector,
27
+ parseJsonSelector,
28
+ } from "@loancrate/json-selector";
29
+
30
+ const obj = {
31
+ foo: {
32
+ bar: [
33
+ {
34
+ id: "x",
35
+ value: 1,
36
+ },
37
+ ],
38
+ },
39
+ };
40
+ const selector = parseJsonSelector("foo.bar['x'].value");
41
+ const accessor = accessWithJsonSelector(selector, obj);
42
+ console.log(accessor.get()); // 1
43
+ console.log(obj.foo.bar[0].value); // 1
44
+ accessor.set(2);
45
+ console.log(obj.foo.bar[0].value); // 2
46
+ accessor.delete();
47
+ console.log(obj.foo.bar[0].value); // undefined
48
+ ```
49
+
50
+ ## License
51
+
52
+ This library is available under the [ISC license](LICENSE).
@@ -0,0 +1,10 @@
1
+ declare function peg$SyntaxError(message: any, expected: any, found: any, location: any): Error;
2
+ declare class peg$SyntaxError {
3
+ constructor(message: any, expected: any, found: any, location: any);
4
+ format(sources: any): string;
5
+ }
6
+ declare namespace peg$SyntaxError {
7
+ function buildMessage(expected: any, found: any): string;
8
+ }
9
+ declare function peg$parse(input: any, options: any): any;
10
+ export { peg$SyntaxError as SyntaxError, peg$parse as parse };