You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80Lines changed: 80 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,12 @@ Imagine that if the user settings are saved in `${library}.json`, and the user
32
32
33
33
So, **if you want to parse a JSON string with comments, modify it, then save it back**, `comment-json` is your must choice!
34
34
35
+
## How?
36
+
37
+
`comment-json` parse JSON strings with comments and save comment tokens into [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) properties.
38
+
39
+
For JSON array with comments, `comment-json` extends the vanilla `Array` object into [`CommentArray`](#commentarray) whose instances could handle comments changes even after a comment array is modified.
40
+
35
41
## Install
36
42
37
43
```sh
@@ -338,6 +344,80 @@ stringify(obj, null, 2)
338
344
// }
339
345
```
340
346
347
+
## `CommentArray`
348
+
349
+
> Advanced Section
350
+
351
+
All arrays of the parsed object are `CommentArray`s.
352
+
353
+
The constructor of `CommentArray` could be accessed by:
354
+
355
+
```js
356
+
const {CommentArray} =require('comment-json')
357
+
```
358
+
359
+
If we modify a comment array, its comment symbol properties could be handled automatically.
360
+
361
+
```js
362
+
constparsed=parse(`{
363
+
"foo": [
364
+
// bar
365
+
"bar",
366
+
// baz,
367
+
"baz"
368
+
]
369
+
}`)
370
+
371
+
parsed.foo.unshift('qux')
372
+
373
+
stringify(parsed, null, 2)
374
+
// {
375
+
// "foo": [
376
+
// "qux",
377
+
// // bar
378
+
// "bar",
379
+
// // baz
380
+
// "baz"
381
+
// ]
382
+
// }
383
+
```
384
+
385
+
Oh yeah! 😆
386
+
387
+
But pay attention, if you reassign the property of a comment array with a normal array, all comments will be gone:
0 commit comments