Merge pull request #13 from christav/set-via-object-literal
Set via object literal
This commit is contained in:
Коммит
3563911a65
23
README.md
23
README.md
|
@ -18,13 +18,19 @@ c.configure('development', function (c) {
|
|||
});
|
||||
|
||||
c.configure('production', function (c) {
|
||||
c.set('settingTwo', 'prodValue');
|
||||
// You can also pass in an object literal to set a bunch
|
||||
// of values at once
|
||||
c.set({
|
||||
settingTwo: 'prodValue'
|
||||
prodOnly: 'prodSpecific'
|
||||
});
|
||||
});
|
||||
|
||||
c('development').get('settingOne').should.equal('devValue');
|
||||
|
||||
process.env.NODE_ENV = 'production';
|
||||
c.default.get('settingTwo').should.equal('prodValue');
|
||||
c.default.get('prodOnly').should.equal('prodSpecific');
|
||||
```
|
||||
|
||||
The previous code shows picking up the default environment from the NODE_ENV environment variable.
|
||||
|
@ -64,6 +70,21 @@ c2.configure('development', function (c) {
|
|||
c2.get('settingOne').should.equal('This value came from a function');
|
||||
```
|
||||
|
||||
or, if you use the object literal version of set, values that are functions
|
||||
will be treated as if you called setFunc for that value:
|
||||
|
||||
```javascript
|
||||
var c2 = envconf.createConfig();
|
||||
|
||||
c2.configure('development', function (c) {
|
||||
c.set({
|
||||
settingOne: function () { return 'This value also came from a function'; }
|
||||
});
|
||||
});
|
||||
|
||||
c2.get('settingOne').should.equal('This value also came from a function');
|
||||
```
|
||||
|
||||
This can be handy if you want to have a default value that you need to derive from
|
||||
ambient state.
|
||||
|
||||
|
|
12
lib/index.js
12
lib/index.js
|
@ -56,7 +56,17 @@ function Config(parent, defaultEnvVar, customizer) {
|
|||
};
|
||||
|
||||
config.set = function (setting, value) {
|
||||
settings[setting] = function () { return value; };
|
||||
if (arguments.length === 2) {
|
||||
settings[setting] = function () { return value; };
|
||||
} else {
|
||||
Object.keys(setting).forEach(function (key) {
|
||||
if (typeof setting[key] === 'function') {
|
||||
settings[key] = setting[key];
|
||||
} else {
|
||||
settings[key] = function () { return setting[key]; };
|
||||
}
|
||||
});
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
10
package.json
10
package.json
|
@ -13,18 +13,18 @@
|
|||
"keywords": ["config", "configuration", "environment"],
|
||||
"licenses": [ { "type": "Apache 2.0", "url": "http://www.apache.org/licenses/LICENSE-2.0" } ],
|
||||
"devDependencies": {
|
||||
"mocha": "~1.12.0",
|
||||
"should": "~1.2.2",
|
||||
"mocha": "~1",
|
||||
"should": "~3",
|
||||
"sinon": "*",
|
||||
"jshint": "*"
|
||||
},
|
||||
"homepage": "https://github.com/WindowsAzure/envconf",
|
||||
"homepage": "https://github.com/Azure/envconf",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:WindowsAzure/envconf.git"
|
||||
"url": "git@github.com:Azure/envconf.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "http://github.com/WindowsAzure/envconf/issues"
|
||||
"url": "http://github.com/Azure/envconf/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm -s run-script jshint && npm -s run-script unit",
|
||||
|
|
|
@ -61,6 +61,33 @@ describe('Config', function () {
|
|||
c.get('secondSetting').should.equal(37);
|
||||
});
|
||||
|
||||
it('should store settings via object literal', function () {
|
||||
var c = envconf.createConfig();
|
||||
c.configure(function (c) {
|
||||
c.set({
|
||||
settingOne: 'aValue',
|
||||
secondSetting: 37
|
||||
});
|
||||
});
|
||||
|
||||
c.get('settingOne').should.equal('aValue');
|
||||
c.get('secondSetting').should.equal(37);
|
||||
});
|
||||
|
||||
it('should set a function in setting via object literal', function () {
|
||||
var c = envconf.createConfig();
|
||||
var f = sinon.stub().returns('hello');
|
||||
|
||||
c.set({
|
||||
settingOne: 'aValue',
|
||||
settingTwo: f
|
||||
});
|
||||
|
||||
c.get('settingOne').should.equal('aValue');
|
||||
c.get('settingTwo').should.equal('hello');
|
||||
f.callCount.should.equal(1);
|
||||
});
|
||||
|
||||
it('should store settings in environments', function () {
|
||||
var c = envconf.createConfig();
|
||||
|
||||
|
@ -419,4 +446,4 @@ describe('Setting getters', function () {
|
|||
c.set('child', 'child value');
|
||||
c.get('root').should.equal('root: child value');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче