Skip to content

Commit 12c5c98

Browse files
committed
2.1.0: CommentArray
1 parent 80f355b commit 12c5c98

File tree

4 files changed

+133
-2
lines changed

4 files changed

+133
-2
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Imagine that if the user settings are saved in `${library}.json`, and the user
3232

3333
So, **if you want to parse a JSON string with comments, modify it, then save it back**, `comment-json` is your must choice!
3434

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+
3541
## Install
3642

3743
```sh
@@ -338,6 +344,80 @@ stringify(obj, null, 2)
338344
// }
339345
```
340346
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+
const parsed = 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:
388+
389+
```js
390+
parsed.foo = ['quux'].concat(parsed.foo)
391+
stringify(parsed, null, 2)
392+
// {
393+
// "foo": [
394+
// "quux",
395+
// "qux",
396+
// "bar",
397+
// "baz"
398+
// ]
399+
// }
400+
401+
// Whoooops!! 😩 Comments are gone
402+
```
403+
404+
Instead, we should:
405+
406+
```js
407+
parsed.foo = new CommentArray('quux').concat(parsed.foo)
408+
stringify(parsed, null, 2)
409+
// {
410+
// "foo": [
411+
// "quux",
412+
// "qux",
413+
// // bar
414+
// "bar",
415+
// // baz
416+
// "baz"
417+
// ]
418+
// }
419+
```
420+
341421
## License
342422
343423
[MIT](LICENSE)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "comment-json",
3-
"version": "2.0.9",
3+
"version": "2.1.0",
44
"description": "Parse and stringify JSON with comments. It will retain comments even after saved!",
55
"main": "src/index.js",
66
"scripts": {

src/array.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,30 @@ class CommentArray extends Array {
251251

252252
return ret
253253
}
254+
255+
concat (...items) {
256+
let {length} = this
257+
const ret = super.concat(...items)
258+
259+
if (!items.length) {
260+
return ret
261+
}
262+
263+
items.forEach(item => {
264+
const prev = length
265+
length += isArray(item)
266+
? item.length
267+
: 1
268+
269+
if (!(item instanceof CommentArray)) {
270+
return
271+
}
272+
273+
move_comments(ret, item, 0, item.length, prev)
274+
})
275+
276+
return ret
277+
}
254278
}
255279

256280
module.exports = {

test/array.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const test = require('ava')
22
const {
33
isFunction, isObject, isString, isArray
44
} = require('core-util-is')
5-
const {parse, stringify, assign} = require('../src')
5+
const {
6+
parse, stringify, assign, CommentArray
7+
} = require('../src')
68

79
const st = o => stringify(o, null, 2)
810

@@ -217,3 +219,28 @@ test('assign', t => {
217219
t.throws(() => assign({}, parsed, false), /keys/)
218220
t.throws(() => assign(), /convert/)
219221
})
222+
223+
test('concat', t => {
224+
const parsed = parse(`[
225+
// bar
226+
"bar",
227+
// baz,
228+
"baz"
229+
]`)
230+
231+
const concated = new CommentArray('quux').concat(
232+
'qux',
233+
parsed
234+
)
235+
236+
t.is(stringify(concated, null, 2), `[
237+
"quux",
238+
"qux",
239+
// bar
240+
"bar",
241+
// baz,
242+
"baz"
243+
]`)
244+
245+
t.is(stringify(new CommentArray().concat()), '[]')
246+
})

0 commit comments

Comments
 (0)