diff --git a/index.js b/index.js index d8a87d0..0ed4800 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,17 @@ module.exports = class Conf { } merge (obj) { - return _.merge(this.config, obj) + return _.mergeWith(this.config, obj, (configValue, overrideValue) => { + // Do not apply merge on non-plain objects + if (typeof overrideValue === 'object' && overrideValue.constructor !== Object) { + return overrideValue + } + + // Do not apply merge on arrays + if (Array.isArray(overrideValue)) { + return overrideValue + } + }) } toString () { @@ -61,11 +71,12 @@ function loadFile (parts, opts) { } function getEnvVariables () { - return _.reduce(process.env, function (obj, val, key) { + const env = {} + for (const key of Object.keys(process.env)) { const k = key.split('__').join('/') - pointer.set(obj, strToPointer(k), val) - return obj - }, {}) + pointer.set(env, strToPointer(k), process.env[key]) + } + return env } function strToPointer (str) { diff --git a/test/unit/conf_tests.js b/test/unit/conf_tests.js index c3a37c2..3da000a 100644 --- a/test/unit/conf_tests.js +++ b/test/unit/conf_tests.js @@ -155,6 +155,27 @@ describe('The Conf', () => { expect(config.get('added')).to.be.true }) + it('does not merge arrays', function () { + const config = new Conf({foo: ['foo', 'bar']}) + + config.merge({foo: ['quz']}) + expect(config.get('foo')).to.deep.equal(['quz']) + }) + + it('does not merge class instances, keeps the original object', function () { + class Foo { + constructor () { + this.foo = 'foo' + } + } + + const config = new Conf({foo: {bar: 'bar'}}) + + const foo = new Foo() + config.merge({foo}) + expect(config.get('foo')).to.equal(foo) + expect(foo.bar).to.equal(undefined) + }) }) describe('set:', () => {