Skip to content

Some INI parsers demand EOL between section header and keys #63

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ The `options` object may contain the following:
`=` character. By default, whitespace is omitted, to be friendly to
some persnickety old parsers that don't tolerate it well. But some
find that it's more human-readable and pretty with the whitespace.
* `newline` Boolean to specify whether to put an additional newline
after a section header. Some INI file parsers (for example the TOSHIBA
FlashAir one) need this to parse the file successfully. By default,
the additional newline is omitted.

For backwards compatibility reasons, if a `string` options is passed
in, then it is assumed to be the `section` value.
Expand Down
9 changes: 6 additions & 3 deletions ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ function encode (obj, opt) {
if (typeof opt === 'string') {
opt = {
section: opt,
whitespace: false
whitespace: false,
newline: false
}
} else {
opt = opt || {}
opt.whitespace = opt.whitespace === true
opt.newline = opt.newline === true
}

var separator = opt.whitespace ? ' = ' : '='
Expand All @@ -36,15 +38,16 @@ function encode (obj, opt) {
})

if (opt.section && out.length) {
out = '[' + safe(opt.section) + ']' + eol + out
out = '[' + safe(opt.section) + ']' + (opt.newline ? eol + eol : eol) + out
}

children.forEach(function (k, _, __) {
var nk = dotSplit(k).join('\\.')
var section = (opt.section ? opt.section + '.' : '') + nk
var child = encode(obj[k], {
section: section,
whitespace: opt.whitespace
whitespace: opt.whitespace,
newline: opt.newline
})
if (out.length && child.length) {
out += eol
Expand Down
13 changes: 13 additions & 0 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ var i = require("../")
+ '[log.level]\n'
+ 'label = debug\n'
+ 'value = 10\n'
, expectH = '[log]\n\n'
+ 'type=file\n\n'
+ '[log.level]\n\n'
+ 'label=debug\n'
+ 'value=10\n'

test("decode from file", function (t) {
var d = i.decode(data)
Expand Down Expand Up @@ -105,3 +110,11 @@ test("encode with whitespace", function (t) {
t.equal(e, expectG)
t.end()
})

test("encode with newline", function (t) {
var obj = {log: { type:'file', level: {label:'debug', value:10} } }
e = i.encode(obj, {newline: true})

t.equal(e, expectH)
t.end()
})