Skip to content

Commit d28dc0d

Browse files
committed
Merge pull request #1 from janl/master
Add getenv.multi().
2 parents bf09617 + cea5861 commit d28dc0d

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ Return as boolean.
8484

8585
Split value of the environment variable at each comma and return the resulting array where each value has been typecast according to the `type` parameter. An array can be provided as `fallback`.
8686

87+
### env.multi({spec})
88+
89+
Return a list of environment variables based on a `spec`:
90+
91+
```javascript
92+
var config = getenv.multi({
93+
foo: "FOO", // throws if FOO doesn't exist
94+
bar: ["BAR", "defaultval"], // set a default value
95+
baz: ["BAZ", "defaultval", "string"], // parse into type
96+
quux: ["QUUX", undefined, "integer"] // parse & throw
97+
});
98+
99+
```
100+
87101
## Changelog
88102

89103
### v0.2.0

lib/getenv.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var util = require("util");
2+
13
function _value(varName, fallback) {
24
var value = process.env[varName];
35
if (value === undefined) {
@@ -47,8 +49,12 @@ var convert = {
4749

4850
function converter(type) {
4951
return function(varName, fallback) {
50-
var value = _value(varName, fallback);
51-
return convert[type](value);
52+
if(typeof varName == 'string') { // default
53+
var value = _value(varName, fallback);
54+
return convert[type](value);
55+
} else { // multibert!
56+
return getenv.multi(varName);
57+
}
5258
};
5359
};
5460

@@ -67,4 +73,29 @@ getenv.array = function array(varName, type, fallback) {
6773
return value.split(/\s*,\s*/).map(convert[type]);
6874
};
6975

76+
getenv.multi = function multi(spec) {
77+
var key, value;
78+
var result = {};
79+
for(var key in spec) {
80+
var value = spec[key];
81+
if(util.isArray(value)) { // default value & typecast
82+
switch(value.length) {
83+
case 1: // no default value
84+
case 2: // no type casting
85+
result[key] = getenv(value[0], value[1]); // dirty, when case 1: value[1] is undefined
86+
break;
87+
case 3: // with typecast
88+
result[key] = getenv[value[2]](value[0], value[1]);
89+
break;
90+
default: // wtf?
91+
throw('getenv.multi(): invalid spec');
92+
break;
93+
}
94+
} else { // value or throw
95+
result[key] = getenv(value);
96+
}
97+
}
98+
return result;
99+
};
100+
70101
module.exports = getenv;

test/getenv.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,112 @@ tests['getenv.array() nonexistent type'] = function() {
375375
});
376376
};
377377

378+
tests['getenv.multi([string]) multiple env vars'] = function() {
379+
var spec = {
380+
foo: 'TEST_GETENV_STRING' // throws when nonexistant
381+
};
382+
var config = getenv.multi(spec);
383+
var expect = {
384+
foo: process.env.TEST_GETENV_STRING
385+
};
386+
assert.deepEqual(expect, config);
387+
};
388+
389+
tests['getenv([string]) multiple env vars shortcut'] = function() {
390+
var spec = {
391+
foo: 'TEST_GETENV_STRING' // throws when nonexistant
392+
};
393+
var config = getenv(spec);
394+
var expect = {
395+
foo: process.env.TEST_GETENV_STRING
396+
};
397+
assert.deepEqual(expect, config);
398+
};
399+
400+
401+
tests['getenv.multi([string/throw]) multiple env vars'] = function() {
402+
var spec = {
403+
foo: 'TEST_GETENV_NONEXISTENT' // throws when nonexistant
404+
};
405+
assert.throws(function() {
406+
var config = getenv.multi(spec);
407+
});
408+
};
409+
410+
tests['getenv.multi([string/typecast]) multiple env vars'] = function() {
411+
var spec = {
412+
foo: ['TEST_GETENV_STRING', undefined, 'string']
413+
};
414+
var config = getenv.multi(spec);
415+
var expect = {
416+
foo: process.env.TEST_GETENV_STRING
417+
};
418+
assert.deepEqual(expect, config);
419+
};
420+
421+
tests['getenv.multi([string/typecast/defaultval]) multiple env vars'] = function() {
422+
var spec = {
423+
foo: ['TEST_GETENV_NONEXISTENT', 'default', 'string'] // throws when nonexistant
424+
};
425+
var config = getenv.multi(spec);
426+
var expect = {
427+
foo: 'default'
428+
};
429+
assert.deepEqual(expect, config);
430+
};
431+
432+
tests['getenv.multi([string/typecast/throw]) multiple env vars'] = function() {
433+
var spec = {
434+
foo: ['TEST_GETENV_NONEXISTENT', undefined, 'string'] // throws when nonexistant
435+
};
436+
assert.throws(function() {
437+
var config = getenv.multi(spec);
438+
});
439+
};
440+
441+
tests['getenv.multi([string/defaultval]) multiple env vars'] = function() {
442+
var spec = {
443+
foo: ['TEST_GETENV_STRING', 'default'] // throws when nonexistant
444+
};
445+
var config = getenv.multi(spec);
446+
var expect = {
447+
foo: process.env.TEST_GETENV_STRING
448+
};
449+
assert.deepEqual(expect, config);
450+
};
451+
452+
tests['getenv.multi([string/defaultval/throw]) multiple env vars'] = function() {
453+
var spec = {
454+
foo: ['TEST_GETENV_NONEXISTENT', 'default'] // throws when nonexistant
455+
};
456+
var config = getenv.multi(spec);
457+
var expect = {
458+
foo: 'default'
459+
};
460+
assert.deepEqual(expect, config);
461+
};
462+
463+
tests['getenv.multi([string/single]) multiple env vars'] = function() {
464+
var spec = {
465+
foo: ['TEST_GETENV_STRING'] // throws when nonexistant
466+
};
467+
var config = getenv.multi(spec);
468+
var expect = {
469+
foo: process.env.TEST_GETENV_STRING
470+
};
471+
assert.deepEqual(expect, config);
472+
};
473+
474+
tests['getenv.multi([string/single/throw]) multiple env vars'] = function() {
475+
var spec = {
476+
foo: ['TEST_GETENV_NONEXISTENT'] // throws when nonexistant
477+
};
478+
assert.throws(function() {
479+
var config = getenv.multi(spec);
480+
});
481+
};
482+
483+
378484
Object.keys(tests).forEach(function(key) {
379485
console.log('Test: %s', key);
380486
tests[key]();

0 commit comments

Comments
 (0)