jsonc-morph 0.1.0 → 0.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
@@ -3,39 +3,56 @@
3
3
  [![JSR](https://jsr.io/badges/@david/jsonc-morph)](https://jsr.io/@david/jsonc-morph)
4
4
  [![npm version](https://badge.fury.io/js/jsonc-morph.svg)](https://badge.fury.io/js/jsonc-morph)
5
5
 
6
+ Programmatically edit JSONC in JavaScript.
7
+
8
+ This is especially useful for making programmatic changes to JSON config files.
9
+ It's not recommended for very large files as this is using
10
+ [jsonc-parser](https://github.com/dprint/jsonc-parser/) via Wasm under the hood.
11
+
12
+ ## Install
13
+
14
+ Deno:
15
+
6
16
  ```
7
- # deno
8
- > deno add @jsr:@david/jsonc-morph
9
- # npm
10
- > npm install jsonc-morph
17
+ deno add @jsr:@david/jsonc-morph
11
18
  ```
12
19
 
13
- Programmatically edit JSONC files.
20
+ Or with npm:
14
21
 
15
- This is especially useful for user maintained config files that you need to make
16
- programmatic changes to.
22
+ ```
23
+ npm install jsonc-morph
24
+ ```
17
25
 
18
26
  ## Example
19
27
 
20
28
  ```ts
21
- import { CstRootNode } from "@david/jsonc-morph";
29
+ import { parse } from "@david/jsonc-morph";
30
+
31
+ const root = parse(`{
32
+ // 1
33
+ "data" /* 2 */: 123 // 3
34
+ } // 4`);
35
+
36
+ // get the root object
37
+ const rootObj = root.asObjectOrThrow();
22
38
 
23
- const root = CstRootNode.parse(`{
24
- // comment
25
- "data": 123
26
- }`);
27
- let rootObj = root.objectValueOrSet();
39
+ // set its "data" property to have a new value
28
40
  rootObj.getOrThrow("data").setValue({
29
41
  "nested": true,
30
42
  });
43
+
44
+ // append a new key
31
45
  rootObj.append("new_key", [456, 789, false]);
32
- const output = root.toString();
33
- // {
34
- // // comment
35
- // "data": {
36
- // "nested": true
37
- // },
38
- // "new_key": [456, 789, false]
39
- // }
40
- console.log(output);
46
+
47
+ // inspect the output
48
+ assertEquals(
49
+ root.toString(),
50
+ `{
51
+ // 1
52
+ "data" /* 2 */: {
53
+ "nested": true
54
+ }, // 3
55
+ "new_key": [456, 789, false]
56
+ } // 4`,
57
+ );
41
58
  ```