Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return boolean values instead of "true"/"false" strings. #247

Closed
wants to merge 1 commit into from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ _Alias: `load`_

`config` will read your .env file, parse the contents, assign it to
[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.

```js
const result = dotenv.config()
Expand Down Expand Up @@ -135,6 +135,7 @@ line'}
```
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
- whitespace is removed from both ends of the value (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO=" some value "` becomes `{FOO: 'some value'}`)
- booleans are maintained (`TRUTHY_VALUE=true` becomes `{TRUTHY_VALUE: true}`)

## FAQ

Expand Down
7 changes: 7 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function parse (src) {
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

// set booleans appropriately instead of boolean strings
if (value === 'true') {
value = true
} else if (value === 'false') {
value = false
}

obj[key] = value
}
})
Expand Down
2 changes: 2 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
INCLUDE_SPACE=some spaced out string
USERNAME="therealnerdybeast@example.tld"
FALSEY_VALUE=false
TRUTHY_VALUE=true
6 changes: 6 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,11 @@ describe('dotenv', function () {
parsed.should.have.property('USERNAME', 'therealnerdybeast@example.tld')
done()
})

it('parses booleans correctly', function (done) {
parsed.should.have.property('FALSEY_VALUE', false)
parsed.should.have.property('TRUTHY_VALUE', true)
done()
})
})
})