Skip to content

Commit

Permalink
Merge pull request #132 from pelias/lodash_get
Browse files Browse the repository at this point in the history
support for lodash get defaultValue
  • Loading branch information
orangejulius authored Aug 7, 2020
2 parents 520923a + 6022289 commit 1dab5e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ function getConfig(deep) {
/*
* Because it's not enumberable, the get function has to be added every time after
* cloning an object with _.merge or _.assign
*
* see: https://lodash.com/docs/4.17.15#get
*/
function addGetFunction(object) {
const getFunction = function get(key) {
return _.get(this, key);
};

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

Expand Down
19 changes: 16 additions & 3 deletions test/generate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var path = require('path'),
config = require('../'),
defaults = require('../config/defaults');
const path = require('path');
const config = require('../');
const defaults = require('../config/defaults');
const Joi = require('@hapi/joi');

module.exports.generate = {};
Expand Down Expand Up @@ -271,6 +271,19 @@ module.exports.generate.validate = (test) => {
t.end();
});

test('get function supports defaultValue', (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('foo', 'DEFAULT'), 'DEFAULT', 'default value used for keys that do not exist');

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

t.end();
});

test('generateDefaults returns default config always', function(t) {
// set the PELIAS_CONFIG env var, this config should NOT be used
process.env.PELIAS_CONFIG = path.resolve( __dirname + '/../config/env.json' );
Expand Down

0 comments on commit 1dab5e0

Please sign in to comment.