getenv
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,3 @@ | ||
var util = require("util"); | ||
function _value(varName, fallback) { | ||
@@ -6,3 +8,3 @@ var value = process.env[varName]; | ||
throw new Error('GetEnv.Nonexistent: ' + varName + ' does not exist ' + | ||
'and now fallback value provided.'); | ||
'and no fallback value provided.'); | ||
} | ||
@@ -51,4 +53,8 @@ return '' + fallback; | ||
return function(varName, fallback) { | ||
var value = _value(varName, fallback); | ||
return convert[type](value); | ||
if(typeof varName == 'string') { // default | ||
var value = _value(varName, fallback); | ||
return convert[type](value); | ||
} else { // multibert! | ||
return getenv.multi(varName); | ||
} | ||
}; | ||
@@ -72,2 +78,27 @@ }; | ||
getenv.multi = function multi(spec) { | ||
var key, value; | ||
var result = {}; | ||
for(var key in spec) { | ||
var value = spec[key]; | ||
if(util.isArray(value)) { // default value & typecast | ||
switch(value.length) { | ||
case 1: // no default value | ||
case 2: // no type casting | ||
result[key] = getenv(value[0], value[1]); // dirty, when case 1: value[1] is undefined | ||
break; | ||
case 3: // with typecast | ||
result[key] = getenv[value[2]](value[0], value[1]); | ||
break; | ||
default: // wtf? | ||
throw('getenv.multi(): invalid spec'); | ||
break; | ||
} | ||
} else { // value or throw | ||
result[key] = getenv(value); | ||
} | ||
} | ||
return result; | ||
}; | ||
module.exports = getenv; |
@@ -7,5 +7,6 @@ { | ||
"Moritz von Hase", | ||
"Jonas Dohse <jonas@dohse.ch>" | ||
"Jonas Dohse <jonas@dohse.ch>", | ||
"Jan Lehnardt" | ||
], | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/ctavan/node-getenv", | ||
@@ -12,0 +13,0 @@ "repository": { |
@@ -87,4 +87,21 @@ # getenv | ||
### env.multi({spec}) | ||
Return a list of environment variables based on a `spec`: | ||
```javascript | ||
var config = getenv.multi({ | ||
foo: "FOO", // throws if FOO doesn't exist | ||
bar: ["BAR", "defaultval"], // set a default value | ||
baz: ["BAZ", "defaultval", "string"], // parse into type | ||
quux: ["QUUX", undefined, "integer"] // parse & throw | ||
}); | ||
``` | ||
## Changelog | ||
### v0.3.0 | ||
- Add getenv.multi() support. | ||
### v0.2.0 | ||
@@ -101,2 +118,3 @@ - Rename git repository | ||
- Jonas Dohse <jonas@dohse.ch> | ||
- Jan Lehnardt (@janl): `getenv.multi()` support. | ||
@@ -103,0 +121,0 @@ ## License |
@@ -5,3 +5,3 @@ var assert = require('assert'); | ||
//Setting getenv vars for testing | ||
// Setting env vars for testing | ||
process.env.TEST_GETENV_EMPTY_STRING = ''; | ||
@@ -379,2 +379,108 @@ process.env.TEST_GETENV_STRING = 'This is a string.'; | ||
tests['getenv.multi([string]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: 'TEST_GETENV_STRING' // throws when nonexistant | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: process.env.TEST_GETENV_STRING | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv([string]) multiple env vars shortcut'] = function() { | ||
var spec = { | ||
foo: 'TEST_GETENV_STRING' // throws when nonexistant | ||
}; | ||
var config = getenv(spec); | ||
var expect = { | ||
foo: process.env.TEST_GETENV_STRING | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/throw]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: 'TEST_GETENV_NONEXISTENT' // throws when nonexistant | ||
}; | ||
assert.throws(function() { | ||
var config = getenv.multi(spec); | ||
}); | ||
}; | ||
tests['getenv.multi([string/typecast]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_STRING', undefined, 'string'] | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: process.env.TEST_GETENV_STRING | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/typecast/defaultval]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_NONEXISTENT', 'default', 'string'] // throws when nonexistant | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: 'default' | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/typecast/throw]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_NONEXISTENT', undefined, 'string'] // throws when nonexistant | ||
}; | ||
assert.throws(function() { | ||
var config = getenv.multi(spec); | ||
}); | ||
}; | ||
tests['getenv.multi([string/defaultval]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_STRING', 'default'] // throws when nonexistant | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: process.env.TEST_GETENV_STRING | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/defaultval/throw]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_NONEXISTENT', 'default'] // throws when nonexistant | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: 'default' | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/single]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_STRING'] // throws when nonexistant | ||
}; | ||
var config = getenv.multi(spec); | ||
var expect = { | ||
foo: process.env.TEST_GETENV_STRING | ||
}; | ||
assert.deepEqual(expect, config); | ||
}; | ||
tests['getenv.multi([string/single/throw]) multiple env vars'] = function() { | ||
var spec = { | ||
foo: ['TEST_GETENV_NONEXISTENT'] // throws when nonexistant | ||
}; | ||
assert.throws(function() { | ||
var config = getenv.multi(spec); | ||
}); | ||
}; | ||
Object.keys(tests).forEach(function(key) { | ||
@@ -381,0 +487,0 @@ console.log('Test: %s', key); |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
18985
496
122
38