Skip to content
Closed
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
25 changes: 25 additions & 0 deletions lib/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,31 @@ const safe = val => {

const unsafe = val => {
val = (val || '').trim()
if (val.charAt(0) === '"' || val.charAt(0) === "'") {
let quote = null
let escaped = false
let commentIndex = -1
for (let i = 0; i < val.length; i++) {
const c = val.charAt(i)
if (escaped) {
escaped = false
} else if (c === '\\') {
escaped = true
} else if (quote) {
if (c === quote) {
quote = null
}
} else if (c === '"' || c === "'") {
quote = c
} else if (c === ';' || c === '#') {
commentIndex = i
break
}
}
if (commentIndex !== -1) {
val = val.slice(0, commentIndex).trim()
}
}
if (isQuoted(val)) {
// remove the single quotes before calling JSON.parse
if (val.charAt(0) === "'") {
Expand Down
6 changes: 6 additions & 0 deletions test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ test('decode from file', function (t) {
t.end()
})

test('decode quoted values before inline comments', function (t) {
const d = i.decode('double = "value" ; comment\nsingle = \'value\' # comment\n')
t.same(d, { double: 'value', single: 'value' })
t.end()
})

test('encode from data', function (t) {
const d = i.decode(data)
const e = i.encode(d)
Expand Down