Skip to content

Commit c0c8787

Browse files
dderevjanikmhamann
authored andcommitted
feat: add env prefix option (#428)
* feat: add env prefix option * chore: remove console.log from test
1 parent cb92a3d commit c0c8787

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/nconf/stores/env.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var Env = exports.Env = function (options) {
2020

2121
options = options || {};
2222
this.type = 'env';
23-
this.readOnly = true;
23+
this.readOnly = options.readOnly !== undefined ? options.readOnly : true;
2424
this.whitelist = options.whitelist || [];
2525
this.separator = options.separator || '';
2626
this.lowerCase = options.lowerCase || false;
@@ -61,6 +61,15 @@ Env.prototype.loadEnv = function () {
6161

6262
var env = process.env;
6363

64+
if (this.prefix) {
65+
env = {};
66+
Object.keys(process.env).forEach(function (key) {
67+
if (key.indexOf(self.prefix) === 0) {
68+
env[key.replace(self.prefix, '')] = process.env[key];
69+
}
70+
});
71+
}
72+
6473
if (this.lowerCase) {
6574
env = {};
6675
Object.keys(process.env).forEach(function (key) {
@@ -102,4 +111,3 @@ Env.prototype.loadEnv = function () {
102111
this.readOnly = true;
103112
return this.store;
104113
};
105-

test/stores/env.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* env-test.js: Tests for the nconf env store.
3+
*
4+
* (C) 2011, Charlie Robbins and the Contributors.
5+
*
6+
*/
7+
8+
var nconf = require('../../lib/nconf');
9+
10+
process.env.DEEP__NESTED__VALUE = 'foo';
11+
process.env.DEEP2__NESTED__VALUE = 'bar';
12+
13+
describe('nconf/stores/env, An instance of nconf.Env', () => {
14+
it("should have the correct methods defined", () => {
15+
var env = new nconf.Env();
16+
expect(typeof env.loadSync).toBe('function');
17+
expect(typeof env.loadEnv).toBe('function');
18+
expect(env.whitelist instanceof Array).toBeTruthy();
19+
expect(env.whitelist.length).toEqual(0);
20+
expect(env.inputSeparator).toEqual('__');
21+
});
22+
it("should have the correct methods defined and with readOnly false", () => {
23+
var env = new nconf.Env({readOnly: false});
24+
expect(typeof env.loadSync).toBe('function');
25+
expect(typeof env.loadEnv).toBe('function');
26+
expect(env.whitelist instanceof Array).toBeTruthy();
27+
expect(env.whitelist.length).toEqual(0);
28+
expect(env.inputSeparator).toEqual('__');
29+
expect(env.readOnly).toBe(false);
30+
});
31+
it("should be able to retrieve a value using the logical separator", () => {
32+
var env = new nconf.Env({accessSeparator: '.', inputSeparator: '__'});
33+
env.loadSync();
34+
35+
expect(env.accessSeparator).toBe('.');
36+
expect(env.get('DEEP.NESTED.VALUE')).toBe('foo');
37+
});
38+
it("should filter and strip prefix from environment variables", () => {
39+
var env = new nconf.Env({prefix: 'DEEP2__'});
40+
env.loadSync();
41+
expect(env.get('DEEP__NESTED__VALUE')).toBeUndefined();
42+
expect(env.get('NESTED__VALUE')).toBe('bar');
43+
});
44+
it("should filter and strip prefix from environment variables with input and access separator", () => {
45+
var env = new nconf.Env({prefix: 'DEEP2__', accessSeparator: '.', inputSeparator: '__' });
46+
env.loadSync();
47+
expect(env.get('DEEP.NESTED.VALUE')).toBeUndefined();
48+
expect(env.get('NESTED.VALUE')).toBe('bar');
49+
});
50+
});

0 commit comments

Comments
 (0)