Skip to content

Commit

Permalink
Merge pull request #194 from kcarra/handle-quotes
Browse files Browse the repository at this point in the history
normalize quotes for values in .env when parsing to json
  • Loading branch information
kcarra committed Sep 8, 2022
2 parents 8e7eeb7 + 251f466 commit f39a8f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function parse(src: string): Data {
const match = line.match(/^([^=:#]+?)[=:](.*)/)
if (match) {
const key = match[1].trim()
const value = match[2].trim()
const value = match[2].trim().replace(/['"]+/g, '')
result[key] = value
}
}
Expand Down
16 changes: 15 additions & 1 deletion source/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Import
import { equal, errorEqual } from 'assert-helpers'
import { deepEqual, equal, errorEqual } from 'assert-helpers'
import kava from 'kava'
import safeps from 'safeps'
import { resolve } from 'path'
import { readJSON } from '@bevry/json'
import { parse } from './index.js'

import filedirname from 'filedirname'
const [file, dir] = filedirname()
Expand Down Expand Up @@ -40,4 +41,17 @@ kava.suite('envfile', function (suite, test) {
done()
})
})

test('quotes should be preserved and normalized', function (done) {
const str = `name="bob"\nplanet="earth"\nrace='human'`
const expected = {
name: 'bob',
planet: 'earth',
race: 'human',
}
const result = parse(str)

deepEqual(result, expected)
done()
})
})

0 comments on commit f39a8f4

Please sign in to comment.