From 0b1b5f9c3861e70955f86ba0d2e89da9e80ca901 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Thu, 1 Dec 2016 10:20:21 -0800 Subject: [PATCH] fix: inner objects in configs had their keys appended to top-level key when dot-notation was disabled (#72) --- index.js | 5 ++++- test/yargs-parser.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1031bf02..cdb5fc55 100644 --- a/index.js +++ b/index.js @@ -452,7 +452,10 @@ function parse (args, opts) { var value = config[key] var fullKey = prev ? prev + '.' + key : key - if (Object.prototype.toString.call(value) === '[object Object]') { + // if the value is an inner object and we have dot-notation + // enabled, treat inner objects in config the same as + // heavily nested dot notations (foo.bar.apple). + if (typeof value === 'object' && !Array.isArray(value) && configuration['dot-notation']) { // if the value is an object but not an array, check nested object setConfigObject(value, fullKey) } else { diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 0e1778a9..5d4243aa 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1886,6 +1886,31 @@ describe('yargs-parser', function () { expect(parsed['foo.bar']).to.equal('banana') expect(parsed).not.to.include.keys('f.bar') }) + + // addresses https://github.com/yargs/yargs/issues/716 + it('does not append nested-object keys from config to top-level key', function () { + var parsed = parser([], { + alias: { + 'foo': ['f'] + }, + configuration: { + 'dot-notation': false + }, + configObjects: [ + { + 'website.com': { + a: 'b', + b: 'c' + } + } + ] + }) + + parsed['website.com'].should.deep.equal({ + a: 'b', + b: 'c' + }) + }) }) describe('parse numbers', function () {