Skip to content

Commit 0b1b5f9

Browse files
authoredDec 1, 2016
fix: inner objects in configs had their keys appended to top-level key when dot-notation was disabled (#72)
1 parent 0f0fb2d commit 0b1b5f9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
 

‎index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,10 @@ function parse (args, opts) {
452452
var value = config[key]
453453
var fullKey = prev ? prev + '.' + key : key
454454

455-
if (Object.prototype.toString.call(value) === '[object Object]') {
455+
// if the value is an inner object and we have dot-notation
456+
// enabled, treat inner objects in config the same as
457+
// heavily nested dot notations (foo.bar.apple).
458+
if (typeof value === 'object' && !Array.isArray(value) && configuration['dot-notation']) {
456459
// if the value is an object but not an array, check nested object
457460
setConfigObject(value, fullKey)
458461
} else {

‎test/yargs-parser.js

+25
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,31 @@ describe('yargs-parser', function () {
18861886
expect(parsed['foo.bar']).to.equal('banana')
18871887
expect(parsed).not.to.include.keys('f.bar')
18881888
})
1889+
1890+
// addresses https://github.com/yargs/yargs/issues/716
1891+
it('does not append nested-object keys from config to top-level key', function () {
1892+
var parsed = parser([], {
1893+
alias: {
1894+
'foo': ['f']
1895+
},
1896+
configuration: {
1897+
'dot-notation': false
1898+
},
1899+
configObjects: [
1900+
{
1901+
'website.com': {
1902+
a: 'b',
1903+
b: 'c'
1904+
}
1905+
}
1906+
]
1907+
})
1908+
1909+
parsed['website.com'].should.deep.equal({
1910+
a: 'b',
1911+
b: 'c'
1912+
})
1913+
})
18891914
})
18901915

18911916
describe('parse numbers', function () {

0 commit comments

Comments
 (0)