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
8 changes: 6 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ function parse (src) {

// Expand newlines if double quoted
if (maybeQuote === '"') {
value = value.replace(/\\n/g, '\n')
value = value.replace(/\\r/g, '\r')
value = value.replace(/\\([\s\S])/g, function (_, ch) {
if (ch === 'n') return '\n'
if (ch === 'r') return '\r'
if (ch === '\\') return '\\'
return '\\' + ch
})
}

// Add to object
Expand Down
3 changes: 3 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS=`double "quotes" and single 'quotes' w
EXPAND_NEWLINES="expand\nnew\nlines"
DONT_EXPAND_UNQUOTED=dontexpand\nnewlines
DONT_EXPAND_SQUOTED='dontexpand\nnewlines'
ESCAPED_BACKSLASH_N="keep\\nraw"
ESCAPED_BACKSLASH_R="keep\\rraw"
ESCAPED_DOUBLE_BACKSLASH="keep\\\\slashes"
# COMMENTS=work
INLINE_COMMENTS=inline comments # work #very #well
INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work
Expand Down
5 changes: 5 additions & 0 deletions tests/test-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ t.equal(parsed.EXPAND_NEWLINES, 'expand\nnew\nlines', 'expands newlines but only
t.equal(parsed.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted')

t.equal(parsed.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted')
t.equal(parsed.ESCAPED_BACKSLASH_N, 'keep\\nraw', 'treats \\\\n in double quotes as literal backslash-n, not newline')

t.equal(parsed.ESCAPED_BACKSLASH_R, 'keep\\rraw', 'treats \\\\r in double quotes as literal backslash-r, not carriage return')

t.equal(parsed.ESCAPED_DOUBLE_BACKSLASH, 'keep\\\\slashes', 'collapses \\\\\\\\ in double quotes to \\\\')

t.notOk(parsed.COMMENTS, 'ignores commented lines')

Expand Down