Skip to content

Commit

Permalink
env({lowerCase:true}) option to make it possible to get() keys in low…
Browse files Browse the repository at this point in the history
…er case
  • Loading branch information
olalonde committed Aug 4, 2015
1 parent 372521b commit 8a21ef3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/nconf/stores/env.js
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
}
Expand All @@ -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]);
}
});

Expand Down
22 changes: 22 additions & 0 deletions test/complete-test.js
Expand Up @@ -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);

0 comments on commit 8a21ef3

Please sign in to comment.