-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: stringify with comments #246
Conversation
stringify(data, { comments: true }) save comments to dictionary object upon parsing, add the option to save when stringifying data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! I gave it a quick review on mobile. Let me know what you think of the suggestions.
* Whether to save the comments. | ||
*/ | ||
|
||
comments: false , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments: false , | |
comments: false, |
@@ -130,6 +145,10 @@ const decode = (str, opt = {}) => { | |||
continue | |||
} | |||
p = out[section] = out[section] || Object.create(null) | |||
if (lineCommentArray.length > 0) { | |||
commentsDictionary[section] = lineCommentArray.join('\n') + '\n' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should use platform eol also.
@@ -165,6 +184,10 @@ const decode = (str, opt = {}) => { | |||
p[key].push(value) | |||
} else { | |||
p[key] = value | |||
if (lineCommentArray.length > 0) { | |||
commentsDictionary[key] = lineCommentArray.join('\n') + '\n' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one too
@@ -1,4 +1,5 @@ | |||
const { hasOwnProperty } = Object.prototype | |||
const commentsDictionary = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will work to have as a top level dictionary shared across invocations.
If i'm reading it right, if two documents are parsed and they have keys that collide, the comments could end up in the wrong document when stringified in a different order.
// section |key = value | ||
const re = /^\[([^\]]*)\]\s*$|^([^=]+)(=(.*))?$/i | ||
const lines = str.split(/[\r\n]+/g) | ||
const duplicates = {} | ||
|
||
for (const line of lines) { | ||
if (!line || line.match(/^\s*[;#]/) || line.match(/^\s*$/)) { | ||
if (line && line.match(commentsRegEx)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if block does not need to be nested within its parent. I think it would be better to have a block that checks for empty lines and another that checks for comments.
const s = i.stringify(d, { comments: true }) | ||
t.matchSnapshot(s) | ||
t.end() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this test case needs to be its own file. It can be tested within the current foo test file.
Closing in favor of #247 |
support option to save comments when stringifying
#32
#83