From 85ae50891fb6d31d6e5bd8f82f4a8f86da19c036 Mon Sep 17 00:00:00 2001 From: HocVoNgThai <161827816+HocVoNgThai@users.noreply.github.com> Date: Sun, 1 Nov 2026 05:50:29 +0700 Subject: [PATCH] fix: round-trip section names containing closing brackets --- lib/ini.js | 2 +- test/section-brackets.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/section-brackets.js diff --git a/lib/ini.js b/lib/ini.js index beb390d..5a5bc68 100644 --- a/lib/ini.js +++ b/lib/ini.js @@ -109,7 +109,7 @@ const decode = (str, opt = {}) => { let p = out let section = null // section |key = value - const re = /^\[([^\]]*)\]\s*$|^([^=]+)(=(.*))?$/i + const re = /^\[(.*)\]\s*$|^([^=]+)(=(.*))?$/i const lines = str.split(/[\r\n]+/g) const duplicates = {} diff --git a/test/section-brackets.js b/test/section-brackets.js new file mode 100644 index 0000000..ce6bef4 --- /dev/null +++ b/test/section-brackets.js @@ -0,0 +1,16 @@ +const t = require('tap') +const ini = require('../') + +for (const section of ['plain', 'profile]archive', '[profile]', 'nested.profile]archive']) { + t.test(`roundtrip section ${JSON.stringify(section)}`, t => { + const input = { [section]: { name: 'sample' } } + const text = ini.stringify(input) + t.same(ini.parse(text), input) + t.end() + }) +} + +t.test('a trailing assignment is not a section header', t => { + t.same(ini.parse('[profile]=sample'), { '[profile]': 'sample' }) + t.end() +})