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
6 changes: 4 additions & 2 deletions lib/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ const decode = (str, opt = {}) => {
const out = Object.create(null)
let p = out
let section = null
// section |key = value
const re = /^\[([^\]]*)\]\s*$|^([^=]+)(=(.*))?$/i
// A key may contain `=` as long as it is quoted, which is what encode()
// emits for such keys via safe().
// section |key = value
const re = /^\[([^\]]*)\]\s*$|^("(?:\\.|[^"\\])*"\s*|[^=]+)(=(.*))?$/i
const lines = str.split(/[\r\n]+/g)
const duplicates = {}

Expand Down
34 changes: 34 additions & 0 deletions test/quoted-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const i = require('../')
const test = require('tap').test

test('decode a quoted key containing =', function (t) {
t.same(i.decode('"a=b"=c\n'), { 'a=b': 'c' })
t.same(i.decode('"key=with=eq"=v\n'), { 'key=with=eq': 'v' })
t.same(i.decode('"=lead"=v\n'), { '=lead': 'v' })
t.same(i.decode('"tr="=v\n'), { 'tr=': 'v' })
t.end()
})

test('encode then decode round-trips keys containing =', function (t) {
for (const obj of [
{ 'a=b': 'c' },
{ 'key=with=eq': 'v' },
{ '=lead': 'v' },
{ 'tr=': 'v' },
{ a: { 'b=c': 'd' } },
]) {
t.same(i.decode(i.encode(obj)), obj)
}
t.end()
})

test('an unquoted key still splits on the first =', function (t) {
t.same(i.decode('a=b=c\n'), { a: 'b=c' })
t.same(i.decode('a="x=y"\n'), { a: 'x=y' })
t.end()
})

test('a key with an unterminated quote is not treated as quoted', function (t) {
t.same(i.decode('"a=b\n'), { '"a': 'b' })
t.end()
})