diff --git a/lib/locator.js b/lib/locator.js index 5e7aaf0..0779453 100644 --- a/lib/locator.js +++ b/lib/locator.js @@ -28,6 +28,14 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'} return envPrefix + subKey; } + function getEnvPrefix(prefixes, envSubKey) { + if (typeof prefixes === 'string') { + return prefixes + envSubKey + '_'; + } + + return prefixes.map(prefix => prefix + envSubKey + '_'); + } + function getNested(option, {namePrefix, envPrefix, cliPrefix}) { return (subKey) => { const envSubKey = _.snakeCase(subKey); @@ -48,7 +56,7 @@ module.exports = function({options, env, argv, envPrefix = '', cliPrefix = '--'} }, { namePrefix: newName, - envPrefix: `${envName}_`, + envPrefix: getEnvPrefix(envPrefix, envSubKey), cliPrefix: `${cliFlag}-` } ); diff --git a/test/locator.js b/test/locator.js index d7b8102..66ac4c4 100644 --- a/test/locator.js +++ b/test/locator.js @@ -239,4 +239,17 @@ describe('locator', () => { assert.propertyVal(childPointer, 'envVar', 'some env value'); }); + + it('should support multiple prefixes with deep nesting', () => { + const pointer = locatorWithEnv( + { + 'bar_some_deep_nested_option': 'some value' + }, + {envPrefix: ['foo_', 'bar_']} + ); + + const childPointer = pointer.nested('some').nested('deep').nested('nested').nested('option'); + + assert.propertyVal(childPointer, 'envVar', 'some value'); + }); });