Skip to content

Commit

Permalink
Merge pull request #101 from pelias/add-get-function
Browse files Browse the repository at this point in the history
feat(get): add get helper method
  • Loading branch information
orangejulius committed Oct 2, 2018
2 parents 249cf27 + 0f6a15c commit 0ce6057
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ function generate( schema, deep ){
deep = true;
}

const getFunction = function get(key) {
return _.get(this, key);
};

const config = getConfig(deep);

// set 'get' convenience function on returned object
Object.defineProperty(config, 'get', {
value: getFunction,
enumerable: false // allows comparison to `expected.json` in tests
});

if (_.isObject(schema)) {
const result = Joi.validate(config, schema);

Expand Down
14 changes: 14 additions & 0 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ module.exports.generate.validate = (test) => {

});

test('get function allows getting keys which are set and undefined for unset', (t) => {
// set the PELIAS_CONFIG env var
process.env.PELIAS_CONFIG = path.resolve( __dirname + '/../config/env.json' );
const c = config.generate();

t.equals(c.get('logger.level'), 'debug', 'get can get keys that exist');
t.equals(c.get('deeply.nested.path.that.does.not.exist'), undefined, 'get returns undefined for non-existent nested paths');

// unset the PELIAS_CONFIG env var
delete process.env.PELIAS_CONFIG;

t.end();
});

};

module.exports.all = function (tape) {
Expand Down

0 comments on commit 0ce6057

Please sign in to comment.