From 1cb3b797e3dff9b6c9ffc454472b8ccff7c49e5a Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 3 Sep 2026 00:15:20 +0100 Subject: [PATCH] fix: preserve scalar keys ending in "[]" across stringify/parse --- lib/ini.js | 6 ++++-- test/bar.js | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) 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) {