Skip to content

Commit 39cac0c

Browse files
authored
[PATCH] - fix bug that caused default values to be ignored (#11)
1 parent 46ae1a7 commit 39cac0c

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
## 3.0.1 (13/10/17)
3+
* Fix bug that caused default values to be ignored
4+
15
## 3.0.0 (13/10/17)
26
* Public API no longer is a function, instead exposes two functions, `mock` and `get`
37
* Drop support for Node.js versions less than 4.0
@@ -10,7 +14,7 @@
1014
* Use `standard` for code quality and formatting
1115

1216
## 2.4.3 (5/04/17)
13-
* Update with build, coverage, and version information badges.
17+
* Update with build, coverage, and version information badges
1418

1519
## 2.4.2 (19/12/2016)
1620
* Fix TypeScript definition file

lib/variable.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ function generateAccessor (container, varName, defValue, accessorPath) {
3232
let value = container[varName]
3333

3434
if (typeof value === 'undefined') {
35-
// Need to simply return since no value is available. If a value needed to
36-
// be available required() should be called
37-
return
35+
if (typeof defValue === 'undefined') {
36+
// Need to return since no value is available. If a value needed to
37+
// be available required() should be called, or a default passed
38+
return
39+
}
40+
41+
// Assign the default as the value since process.env does not contain
42+
// the desired variable
43+
value = defValue
3844
}
3945

4046
const args = [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "env-var",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Solution for loading and sanatizing environment variables in node.js with correct typings",
55
"main": "env-var.js",
66
"typings": "env-var.d.ts",

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ describe('env-var', function () {
4040
})
4141
})
4242

43+
describe('default values', function () {
44+
it('should return the default', function () {
45+
const ret = mod.get('XXX_NOT_DEFINED', 'default').asString()
46+
47+
expect(ret).to.equal('default')
48+
})
49+
})
50+
4351
describe('#asString', function () {
4452
it('should return a string', function () {
4553
expect(mod.get('STRING').asString()).to.be.a('string')

0 commit comments

Comments
 (0)