Skip to content

Commit b363ae6

Browse files
wraithgarFrancois-Xavier Kowalski
and
Francois-Xavier Kowalski
authored
feat: add newline option (#198)
Co-authored-by: Francois-Xavier Kowalski <[email protected]>
1 parent fa2c17e commit b363ae6

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ The `options` object may contain the following:
8282
`=` character. By default, whitespace is omitted, to be friendly to
8383
some persnickety old parsers that don't tolerate it well. But some
8484
find that it's more human-readable and pretty with the whitespace.
85+
* `newline` Boolean to specify whether to put an additional newline
86+
after a section header. Some INI file parsers (for example the TOSHIBA
87+
FlashAir one) need this to parse the file successfully. By default,
88+
the additional newline is omitted.
8589

8690
For backwards compatibility reasons, if a `string` options is passed
8791
in, then it is assumed to be the `section` value.

lib/ini.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const encode = (obj, opt) => {
1616
} else {
1717
opt = opt || Object.create(null)
1818
opt.whitespace = opt.whitespace === true
19+
opt.newline = opt.newline === true
1920
}
2021

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

3637
if (opt.section && out.length) {
37-
out = '[' + safe(opt.section) + ']' + eol + out
38+
out = '[' + safe(opt.section) + ']' + (opt.newline ? eol + eol : eol) + out
3839
}
3940

4041
for (const k of children) {
4142
const nk = dotSplit(k).join('\\.')
4243
const section = (opt.section ? opt.section + '.' : '') + nk
43-
const { whitespace } = opt
44+
const { whitespace, newline } = opt
4445
const child = encode(obj[k], {
4546
section,
4647
whitespace,
48+
newline,
4749
})
4850
if (out.length && child.length) {
4951
out += eol

tap-snapshots/test/foo.js.test.cjs

+12
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ noHashComment=this\\# this is not a comment
110110
111111
`
112112

113+
exports[`test/foo.js TAP encode with newline > must match snapshot 1`] = `
114+
[log]
115+
116+
type=file
117+
118+
[log.level]
119+
120+
label=debug
121+
value=10
122+
123+
`
124+
113125
exports[`test/foo.js TAP encode with option > must match snapshot 1`] = `
114126
[prefix.log]
115127
type=file

test/foo.js

+8
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ test('encode with whitespace', function (t) {
4444
t.matchSnapshot(e)
4545
t.end()
4646
})
47+
48+
test('encode with newline', function (t) {
49+
const obj = { log: { type: 'file', level: { label: 'debug', value: 10 } } }
50+
const e = i.encode(obj, { newline: true })
51+
52+
t.matchSnapshot(e)
53+
t.end()
54+
})

0 commit comments

Comments
 (0)