Releases: kaelzhang/node-comment-json
4.2.2
4.1.0
MAJOR 4.0.0, 2020-09-01
- MAJOR: fixes #21, removes
after-comma:${prop}
andafter
symbol properties, and introduceafter:${prop}
- MAJOR: #20 symbol properties of comments are now not enumerable, so that you could use
Object.keys
to get the keys of the parsed object as the normal ones
4.0.0
brings breaking changes, but however, logically but not programmatically.
So for most scenarios, you need NOT to change your code, and an upgrade is recommended.
Upgrade Guide
Use assign
instead of object spreading
const parsed = parse(`{
// comment
"foo": "bar"
}`)
In 3.x
, it is ok to do as following
stringify({...parsed}, null, 2)
// {
// // comment
// "foo": "bar"
// }
But with [email protected]
, the code above will print
{
"foo": "bar"
}
Because since 4.0.0
, symbol properties of comments are non-enumerable, so object spreading will not work for this case, and you should use assign
function to copy those properties.
const {assign} = require('comment-json')
stringify(assign({}, parsed), null, 2)
Changes of symbol properties
TL;NR
{
"foo": "bar" /* after value */, // after foo
"bar": "baz" // after bar
}
In 3.x
- The comment
after foo
is marked asSymbol.for('after-comma:foo')
- The comment
after bar
is marked asSymbol.for('after-value:bar')
But in 4.x
- The comment
after foo
is marked asSymbol.for('after:foo')
- The comment
after bar
is marked asSymbol.for('after:bar')
And in both 3.x
and 4.x
after value
is marked as Symbol.for('after-value:foo')
3.0.0
In 2.x
, inline comments after comma(,
) are not belong to the current prop, see #17
const {parse, stringify, assign} = require('comment-json')
// Just insert a new property `baz` into the parsed object and return a new object
// TL;NR
const insert_baz = (origin, value) => {
const obj = {}
assign(obj, origin, ['foo'])
obj.baz = value
assign(obj, origin, ['bar'])
return obj
}
// 2.x
const parsed = parse(`{
"foo": "foo", // foo
"bar": "bar"
}`)
const obj = insert_baz(parsed, 'baz')
console.log(stringify(obj, null, 2))
// {
// "foo": "foo",
// "baz": "baz", // foo
// "bar": "bar"
// }
Because, in 2.x
, inline comment ' foo'
is a before:bar
comment token of property bar
.
And, in the new MAJOR version 3.0.0
, we introduce a new comment token type after-comma:${prop}
to fix this issue.
If the code above is executed with comment-json 3.0.0
, the result will be
{
"foo": "foo", // foo
"baz": "baz",
"bar": "bar"
}
3.0.0
brings a breaking change, but however, logically but not programmatically.
So for most scenarios, you need not to change your code.
2.2.0
2.1.0
2.0.9
2.0.9
This major version fixes several issues and provides new features:
- supports comments everywhere, yes, EVERYWHERE in a JSON file, eventually 😆
- fixes comments in arrays, #7 #9
Breaking changes vs 1.x
The new version uses Symbol
s to store comments so that they will never conflict with normal JSON property fields, so the structure of the return value of method parse()
changes.
For details, see the document