Skip to content
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: add newline option #198

Merged
merged 1 commit into from
Apr 12, 2023
Merged
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 @@ -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,
})
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()
})