Skip to content
Open
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
5 changes: 3 additions & 2 deletions lib/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const encode = (obj, opt = {}) => {
const eol = opt.platform === 'win32' ? '\r\n' : '\n'
const separator = opt.whitespace ? ' = ' : '='
const children = []
const hasSection = opt.section || opt.section === ''

const keys = opt.sort ? Object.keys(obj).sort() : Object.keys(obj)

Expand Down Expand Up @@ -56,13 +57,13 @@ const encode = (obj, opt = {}) => {
}
}

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

for (const k of children) {
const nk = splitSections(k, '.').join('\\.')
const section = (opt.section ? opt.section + '.' : '') + nk
const section = (hasSection ? opt.section + '.' : '') + nk
const child = encode(obj[k], {
...opt,
section,
Expand Down
24 changes: 24 additions & 0 deletions test/empty-section.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const ini = require('../')
const t = require('tap')

t.test('empty section names round trip', t => {
for (const text of [
'[]\na=1\n',
'top=value\n[]\na=1\n',
'[]\na[]=1\na[]=2\n[.child]\nb=3\n',
'[parent.]\na=1\n[parent..child]\nb=2\n',
]) {
const parsed = ini.parse(text)
t.same(ini.parse(ini.stringify(parsed)), parsed, text)
}
t.end()
})

t.test('explicit empty section option writes its header', t => {
const eol = require('os').EOL
t.equal(ini.stringify({ a: '1' }, { section: '', platform: 'linux' }), '[]\na=1\n')
t.equal(ini.stringify({ a: '1' }, ''), `[]${eol}a=1${eol}`)
t.equal(ini.stringify({ a: '1' }, { platform: 'linux' }), 'a=1\n')
t.equal(ini.stringify({}, { section: '' }), '')
t.end()
})