Skip to content

Commit

Permalink
Added a new function to retrieve the current configuration as a JavaS…
Browse files Browse the repository at this point in the history
…cript object.
  • Loading branch information
Ashley Davis committed Apr 7, 2016
1 parent 0f273fd commit 2aa07f5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
13 changes: 13 additions & 0 deletions confucious.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,17 @@ module.exports = function () {
this.pushEnv = function () {
this.push(process.env);
};

//
// Get the current configuration as a JavaScript object.
//
this.toObject = function () {
var base = {};

valuesStack.forEach(function (value) {
base = extend(base, value);
});

return base;
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "confucious",
"version": "0.0.9",
"version": "0.0.10",
"description": "App configuration management. Kind of like nconf, but easier to use, predicable and more flexible.",
"main": "index.js",
"scripts": {
Expand Down
94 changes: 94 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ describe('confucious', function () {

expect(testObject.get('some-key')).to.equal('smeg');
});

it('can get configuration as JavaScript object', function () {

testObject.set('someKey', 'smeg');

expect(testObject.toObject()).to.eql({
someKey: 'smeg',
});

});
});

describe('nested key/values', function () {
Expand Down Expand Up @@ -128,6 +138,27 @@ describe('confucious', function () {

expect(testObject.get('some-key:something:something')).to.be.undefined;
});

it('can get nested configuration as JavaScript object', function () {

var obj = {
'something': {
'something': 'dark side',
},
};

testObject.set('someKey', obj);

expect(testObject.toObject()).to.eql({
someKey: {
something: {
something: 'dark side',
},
},
});

});

});

describe('stack', function () {
Expand Down Expand Up @@ -243,6 +274,39 @@ describe('confucious', function () {

testObject.pop();
});

it('can get stacked configuration as JavaScript object', function () {

testObject.push({
someKey1: 'smeg1',
});

testObject.push({
someKey2: 'smeg2',
});

expect(testObject.toObject()).to.eql({
someKey1: 'smeg1',
someKey2: 'smeg2',
});

});

it('can stacked configuration overrides when retreived as JavaScript object', function () {

testObject.push({
someKey: 'smeg1',
});

testObject.push({
someKey: 'smeg2',
});

expect(testObject.toObject()).to.eql({
someKey: 'smeg2',
});

});
});

describe('globals', function () {
Expand Down Expand Up @@ -344,5 +408,35 @@ describe('confucious', function () {

expect(testObject.get('some-global-key')).to.equal('some-value');
});

it('can get global configuration as JavaScript object', function () {

testObject.setGlobal('someGlobalKey', 'some-value');

testObject.push({
'someStackedKey': 'some-other-value',
});

expect(testObject.toObject()).to.eql({
someGlobalKey: 'some-value',
someStackedKey: 'some-other-value',
});

});

it('can local configuration overrides global configuration when retreived as JavaScript object', function () {

testObject.setGlobal('someGlobalKey', 'some-value');

testObject.push({
'someGlobalKey': 'some-other-value',
});

expect(testObject.toObject()).to.eql({
someGlobalKey: 'some-other-value',
});

});

});
});

0 comments on commit 2aa07f5

Please sign in to comment.