Skip to content

Commit eb761e4

Browse files
committed
4.0.4 (breaking, but ok): assign: special copying rules according to argument keys, related to #26
1 parent 22e096c commit eb761e4

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ console.log(stringify(result, null, 2)) // is the same as `code`
413413
This method is used to copy the enumerable own properties and their corresponding comment symbol properties to the target object.
414414
415415
```js
416-
const parsed = parse(`{
416+
const parsed = parse(`// before all
417+
{
417418
// This is a comment
418419
"foo": "bar"
419420
}`)
@@ -423,13 +424,53 @@ const obj = assign({
423424
}, parsed)
424425

425426
stringify(obj, null, 2)
427+
// // before all
426428
// {
427429
// "bar": "baz",
428430
// // This is a comment
429431
// "foo": "bar"
430432
// }
431433
```
432434
435+
### Special cases about `keys`
436+
437+
But if argument `keys` is specified and is not empty, then comment ` before all`, which belongs to no properties, will **NOT** be copied.
438+
439+
```js
440+
const obj = assign({
441+
bar: 'baz'
442+
}, parsed, ['foo'])
443+
444+
stringify(obj, null, 2)
445+
// {
446+
// "bar": "baz",
447+
// // This is a comment
448+
// "foo": "bar"
449+
// }
450+
```
451+
452+
Specifying the argument `keys` as an empty array indicates that it will only copy non-property symbols of comments
453+
454+
```js
455+
const obj = assign({
456+
bar: 'baz'
457+
}, parsed, [])
458+
459+
stringify(obj, null, 2)
460+
// // before all
461+
// {
462+
// "bar": "baz",
463+
// }
464+
```
465+
466+
Non-property symbols includes:
467+
468+
```js
469+
Symbol.for('before-all')
470+
Symbol.for('before')
471+
Symbol.for('after-all')
472+
```
473+
433474
## `CommentArray`
434475
435476
> Advanced Section

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": "4.0.3",
3+
"version": "4.0.4",
44
"description": "Parse and stringify JSON with comments. It will retain comments even after saved!",
55
"main": "src/index.js",
66
"scripts": {

src/common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ module.exports = {
131131

132132
if (keys === UNDEFINED) {
133133
keys = Object.keys(source)
134-
// We only assign non-property comments if there `keys` are not specified
134+
// We assign non-property comments
135+
// if argument `keys` is not specified
135136
assign_non_prop_comments(target, source)
136137
} else if (!isArray(keys)) {
137138
throw new TypeError('keys must be array or undefined')
139+
} else if (keys.length === 0) {
140+
// Or argument `keys` is an empty array
141+
assign_non_prop_comments(target, source)
138142
}
139143

140144
return assign(target, source, keys)

test/others.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ test('#26: non-property comments', t => {
114114
'should assign non-property comments if no keys'
115115
)
116116

117+
t.is(
118+
stringify(
119+
assign(
120+
{
121+
a: 1
122+
},
123+
parsed,
124+
[]
125+
),
126+
null, 2
127+
),
128+
`// before all
129+
{
130+
"a": 1
131+
}
132+
// after all`,
133+
'should assign non-property comments if keys is an empty array'
134+
)
135+
117136
t.is(
118137
stringify(
119138
assign(

0 commit comments

Comments
 (0)