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() +})