env({lowerCase:true}) option to make it possible to get() keys in lower case

master
Olivier Lalonde 2015-08-04 18:56:43 +08:00
parent 372521b124
commit 8a21ef36d5
2 changed files with 34 additions and 3 deletions

View File

@ -23,6 +23,7 @@ var Env = exports.Env = function (options) {
this.readOnly = true;
this.whitelist = options.whitelist || [];
this.separator = options.separator || '';
this.lowerCase = options.lowerCase || false;
if (typeof options.match === 'function'
&& typeof options !== 'string') {
@ -56,8 +57,16 @@ Env.prototype.loadSync = function () {
Env.prototype.loadEnv = function () {
var self = this;
var env = process.env;
if (this.lowerCase) {
Object.keys(env).forEach(function (key) {
env[key.toLowerCase()] = env[key];
});
}
this.readOnly = false;
Object.keys(process.env).filter(function (key) {
Object.keys(env).filter(function (key) {
if (self.match && self.whitelist.length) {
return key.match(self.match) || self.whitelist.indexOf(key) !== -1
}
@ -69,10 +78,10 @@ Env.prototype.loadEnv = function () {
}
}).forEach(function (key) {
if (self.separator) {
self.set(common.key.apply(common, key.split(self.separator)), process.env[key]);
self.set(common.key.apply(common, key.split(self.separator)), env[key]);
}
else {
self.set(key, process.env[key]);
self.set(key, env[key]);
}
});

View File

@ -123,4 +123,26 @@ vows.describe('nconf/multiple-stores').addBatch({
nconf.remove('env');
}
}
}).addBatch({
// Threw this in it's own batch to make sure it's run separately from the
// sync check
"When using env with lowerCase:true": {
topic: function () {
var that = this;
helpers.cp(complete, completeTest, function () {
nconf.env({ lowerCase: true });
that.callback();
});
},
"env vars": {
"keys also available as lower case": function () {
Object.keys(process.env).forEach(function (key) {
assert.equal(nconf.get(key.toLowerCase()), process.env[key]);
});
}
},
teardown: function () {
nconf.remove('env');
}
}
}).export(module);