diff --git a/lib/ini.js b/lib/ini.js index beb390d..19c338d 100644 --- a/lib/ini.js +++ b/lib/ini.js @@ -52,7 +52,8 @@ const encode = (obj, opt = {}) => { } else if (val && typeof val === 'object') { children.push(k) } else { - out += safe(k).padEnd(padToChars, ' ') + separator + safe(val) + eol + const safeKey = opt.bracketedArray && k.endsWith('[]') ? JSON.stringify(k) : safe(k) + out += safeKey.padEnd(padToChars, ' ') + separator + safe(val) + eol } } @@ -135,7 +136,8 @@ const decode = (str, opt = {}) => { const keyRaw = unsafe(match[2]) let isArray if (opt.bracketedArray) { - isArray = keyRaw.length > 2 && keyRaw.slice(-2) === '[]' + const rawKey = match[2].trim() + isArray = !isQuoted(rawKey) && rawKey.length > 2 && rawKey.slice(-2) === '[]' } else { duplicates[keyRaw] = (duplicates?.[keyRaw] || 0) + 1 isArray = duplicates[keyRaw] > 1 diff --git a/test/bar.js b/test/bar.js index 8f7eb23..babd6b2 100644 --- a/test/bar.js +++ b/test/bar.js @@ -8,6 +8,7 @@ var data = { string: { drink: 'white russian' }, boolean: { isTrue: true }, 'nested boolean': { theDude: { abides: true, rugCount: 1 } }, + 'key ending in []': { 'foo[]': 'bar' }, } test('parse(stringify(x)) is same as x', function (t) {