Skip to content

Commit

Permalink
Completed new behaviours for readFromObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat Carey committed Apr 3, 2015
1 parent 07c6141 commit 56c0e44
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ expect(myConfig.get('collection.nothing.this.would.usually.cause.problems')).toB
To use this with Node.JS or IO.JS you can do things like:

```javascript
var myConfig = JsConfig.readFromObject(process.env, ['MUST_BE_PRESENT', 'AN_ENV_VAR', 'NO_DEFAULT'], {
ANOTHER_ENV_VAR: 'the default value',
AN_OPTIONAL_FLAG: 'false'
});

```

This will give you an object which includes the environment variable values for MUST_BE_PRESENT, AN_ENV_VAR and NO_DEFAULT but blows up if one of them doesn't exist.
Expand Down
33 changes: 33 additions & 0 deletions spec/usageExamplesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ describe('Usage Examples', function () {
expect(actual.get('something.myVar')).toEqual('value A');
expect(actual.get('somethingElse.someRandomThing')).toEqual('value B');
});
it('support nested values', function () {
var myObj = {parent: {child: {someVar: 'some value'}}},
actual = JsConfig.readFromObject(myObj, {
something: {
myVar: 'parent.child.someVar'
}
});
expect(actual.get('something.myVar')).toEqual('some value');
});
it('should allow defaults for nonexistnet keys', function () {
process.env.EXISTS = 'true';
var actual = JsConfig.readFromObject(process.env, {
Expand All @@ -162,6 +171,30 @@ describe('Usage Examples', function () {
actual.assertExists('specific');
}).toThrow(new Error('JsConfig: missing key: "specific"'));
});
it('should complain when no object provided to read from', function () {
var jsConfig = new JsConfig();
expect(function () {
jsConfig.readFromObject();
}).toThrow(new Error('Couldn\'t "readFromObject" as no object was provided'));
});
it('should complain when non-object provided instead of object to read from', function () {
var jsConfig = new JsConfig();
expect(function () {
jsConfig.readFromObject('some string');
}).toThrow(new Error('Couldn\'t "readFromObject" as no object was provided'));
});
it('should complain when array provided instead of object to read from', function () {
var jsConfig = new JsConfig();
expect(function () {
jsConfig.readFromObject(['a', 'b']);
}).toThrow(new Error('Couldn\'t "readFromObject" as no object was provided'));
});
it('should complain when no mapper provided', function () {
var jsConfig = new JsConfig();
expect(function () {
jsConfig.readFromObject({}, ['a', 'b']);
}).toThrow(new Error('Couldn\'t "readFromObject" as no item definition was provided'));
});
it('should assert presence of keys', function () {
var actual = new JsConfig({facebook: {id: 123}});
expect(function () {
Expand Down
27 changes: 20 additions & 7 deletions src/JsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ var JsConfig = (function () {
}
}

function isObject(obj) {
return typeof obj === 'object' && obj.length === undefined;
}

function clone(original) {
var newObj = {};
if (typeof original !== 'object' || original === undefined) {
if (!isObject(original) || original === undefined) {
return original;
}
loop(original, function (val, key) {
Expand Down Expand Up @@ -97,7 +101,7 @@ var JsConfig = (function () {

function updateWithOverrides(data, output) {
loop(data, function (value, key) {
if (typeof value !== 'object') {
if (!isObject(value)) {
output[key] = value;
} else {
output[key] = output[key] || {};
Expand All @@ -117,22 +121,31 @@ var JsConfig = (function () {
};

this.readFromObject = function (obj, items) {
var self = this;
if (!isObject(obj)) {
throw new Error('Couldn\'t "readFromObject" as no object was provided');
}
if (!isObject(items)) {
throw new Error('Couldn\'t "readFromObject" as no item definition was provided');
}
var self = this, lookedUpValue;
function deepLoop(parent, combinedName) {
loop(parent, function (value, key) {
var name = combinedName ? combinedName + '.' : '';
name += key;
if (typeof value === 'object') {
if (isObject(value)) {
deepLoop(value, name);
} else {
if (obj.hasOwnProperty(value)) {
self.set(name, obj[value]);
lookedUpValue = lookupKeyInData(value, obj);
if (lookedUpValue) {
self.set(name, lookedUpValue);
}
}
});
}

deepLoop(items || {});
if (items) {
deepLoop(items);
}
};
}

Expand Down

0 comments on commit 56c0e44

Please sign in to comment.