Skip to content

Commit

Permalink
feat: add newline option
Browse files Browse the repository at this point in the history
Some INI parsers demand EOL between section header and keys
  • Loading branch information
Francois-Xavier Kowalski authored and wraithgar committed Apr 11, 2023
1 parent ea4e019 commit c090a65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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
6 changes: 4 additions & 2 deletions lib/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const encode = (obj, opt) => {
} else {
opt = opt || Object.create(null)
opt.whitespace = opt.whitespace === true
opt.newline = opt.newline === true
}

const separator = opt.whitespace ? ' = ' : '='
Expand All @@ -34,16 +35,17 @@ const encode = (obj, opt) => {
}

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

for (const k of children) {
const nk = dotSplit(k).join('\\.')
const section = (opt.section ? opt.section + '.' : '') + nk
const { whitespace } = opt
const { whitespace, newline } = opt
const child = encode(obj[k], {
section,
whitespace,
newline

Check failure on line 48 in lib/ini.js

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma
})
if (out.length && child.length) {
out += eol
Expand Down
12 changes: 12 additions & 0 deletions tap-snapshots/test/foo.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ noHashComment=this\\# this is not a comment
`

exports[`test/foo.js TAP encode with newline > must match snapshot 1`] = `
[log]
type=file
[log.level]
label=debug
value=10
`

exports[`test/foo.js TAP encode with option > must match snapshot 1`] = `
[prefix.log]
type=file
Expand Down
8 changes: 8 additions & 0 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ test('encode with whitespace', function (t) {
t.matchSnapshot(e)
t.end()
})

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

t.matchSnapshot(e)
t.end()
})

0 comments on commit c090a65

Please sign in to comment.