Skip to content

Commit c95146a

Browse files
committed
Merge pull request #6 from keybase/master
Adding boolish support
2 parents 8801017 + 9e8e899 commit c95146a

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ Return as float number.
7878

7979
### env.bool(name, [fallback])
8080

81-
Return as boolean.
81+
Return as boolean. Only allows true/false as valid values.
82+
83+
### env.boolish(name, [fallback])
84+
85+
Return as boolean. Allows true/false/1/0 as valid values.
8286

8387
### env.array(name, [type], [fallback])
8488

@@ -120,6 +124,9 @@ if (process.env.NODE_ENV === 'production') {
120124

121125
## Changelog
122126

127+
### v0.5.1
128+
- Added getenv.boolish() support.
129+
123130
### v0.5.0
124131
- Add getenv.url() support.
125132

lib/getenv.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function _value(varName, fallback) {
1111
'and no fallback value provided.');
1212
}
1313
if (fallbacksDisabled) {
14-
throw new Error('GetEnv.DisabledFallbacks: ' + varName + ' relying on fallback ' +
14+
throw new Error('GetEnv.DisabledFallbacks: ' + varName + ' relying on fallback ' +
1515
'when fallbacks have been disabled');
1616
}
1717
return '' + fallback;
@@ -50,7 +50,20 @@ var convert = {
5050
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
5151
}
5252

53-
return (value === 'true');
53+
return (value === 'true')
54+
},
55+
boolish: function(value) {
56+
try {
57+
return convert.bool(value)
58+
}
59+
catch(err) {
60+
var isBool = (value === '1' || value === '0');
61+
if (!isBool) {
62+
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
63+
}
64+
65+
return (value === '1');
66+
}
5467
},
5568
url: url.parse
5669
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"Jan Lehnardt",
99
"Tim Ruffles <[email protected]>"
1010
],
11-
"version": "0.5.0",
11+
"version": "0.5.1",
1212
"homepage": "https://github.com/ctavan/node-getenv",
1313
"repository": {
1414
"type": "git",

test/getenv.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,41 @@ tests['getenv.bool() invalid input'] = function() {
221221
});
222222
};
223223

224+
tests['getenv.boolish() valid input'] = function() {
225+
var data = [{
226+
varName: 'TEST_GETENV_FALSE',
227+
expected: false
228+
}, {
229+
varName: 'TEST_GETENV_TRUE',
230+
expected: true
231+
}, {
232+
varName: 'TEST_GETENV_NOT_REALLY_FALSE',
233+
expected: false
234+
}, {
235+
varName: 'TEST_GETENV_NOT_REALLY_TRUE',
236+
expected: true
237+
}];
238+
239+
data.forEach(function(item) {
240+
var boolVar = getenv.boolish(item.varName);
241+
assert.strictEqual(boolVar, item.expected);
242+
});
243+
};
244+
245+
246+
tests['getenv.boolish() invalid input'] = function() {
247+
var data = [
248+
{ varName: 'TEST_GETENV_STRING' },
249+
{ varName: 'TEST_GETENV_EMPTY_STRING' }
250+
];
251+
252+
data.forEach(function(item) {
253+
assert.throws(function() {
254+
var boolVar = getenv.boolish(item.varName);
255+
});
256+
});
257+
};
258+
224259

225260
tests['getenv.bool() nonexistent variable'] = function() {
226261
assert.throws(function() {

0 commit comments

Comments
 (0)