Skip to content

Commit

Permalink
Added app.extend()
Browse files Browse the repository at this point in the history
Added app.extend(), which provides an easy way to update certain properties of a setting.
  • Loading branch information
boycce committed Feb 22, 2018
1 parent 351396f commit 7e33e1d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,50 @@ app.set = function set(setting, val) {
return this;
};

/**
* Extend `setting` with `val`, or return `setting`'s value.
*
* app.set('foo', { 'bar' : 1 })
* app.expand('foo', { 'baz' : 1 });
* app.expand('foo');
* // => { "bar" : 1, "baz" : 1 }
*
* Mounted servers inherit their parent server's settings.
*
* @param {String} setting
* @param {*} [val]
* @return {Server} for chaining
* @public
*/

app.extend = function(setting, val) {
if (arguments.length === 1) {
// app.get(setting)
return this.settings[setting];
}

if (typeof val === 'undefined') {
return this;
} else if (val === null || typeof val !== 'object') {
throw new Error('value needs to be an object');
}

debug('extend "%s" with %o', setting, val);

var currentSetting = this.settings[setting];

// no object yet, set value
if (currentSetting === null || typeof currentSetting !== 'object') {
this.settings[setting] = val;
return this;
}

// extend value
this.settings[setting] = Object.assign(this.settings[setting], val);

return this;
};

/**
* Return the app's absolute pathname
* based on the parent(s) that have
Expand Down
32 changes: 32 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,38 @@ describe('config', function () {
})
})

describe('.extend()', function () {
it('should set if value is nonexistent', function () {
var app = express();
app.extend('foo', { 'bar' : 1 });
assert.deepEqual(app.get('foo'), { 'bar' : 1 });
})

it('should extend an object', function () {
var app = express();
app.extend('foo', { 'bar' : 1 });
app.extend('foo', { 'baz' : 1 });
assert.deepEqual(app.get('foo'), { 'bar' : 1, 'baz' : 1 });
})

it('should return the app', function () {
var app = express();
assert.equal(app.extend('foo', { 'bar' : 1 }), app);
})

it('should return the app when undefined', function () {
var app = express();
assert.equal(app.extend('foo', undefined), app);
})

it('should thorw on bad value', function () {
var app = express();
(function(){
app.extend('foo', null);
}).should.throw('value needs to be an object');
})
})

describe('.enable()', function(){
it('should set the value to true', function(){
var app = express();
Expand Down

0 comments on commit 7e33e1d

Please sign in to comment.