Skip to content

Commit

Permalink
fix: dot-notation now respects strip-dashed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Davidson committed Aug 11, 2020
1 parent 5f987aa commit 8f40f1f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/yargs-parser.ts
Expand Up @@ -414,9 +414,26 @@ export class YargsParser {
})

if (configuration['camel-case-expansion'] && configuration['strip-dashed']) {
Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => {
delete argv[key]
})
if (configuration['dot-notation']) {
Object.keys(flags.aliases)
.filter(key => key !== '--' && key.includes('-'))
.map(key => key.split('.'))
.forEach(keyPath => {
var obj = argv
for (const subkey of keyPath) {
if (subkey.includes('-')) {
delete obj[subkey]
break
} else {
obj = obj[subkey]
}
}
})
} else {
Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => {
delete argv[key]
})
}
}

if (configuration['strip-aliased']) {
Expand Down
26 changes: 26 additions & 0 deletions test/yargs-parser.cjs
Expand Up @@ -3636,6 +3636,32 @@ describe('yargs-parser', function () {
'test-value': 1
})
})

// See: https://github.com/yargs/yargs-parser/issues/223
it('strip-dashed removes subkeys made by dot-notation', function () {
var argv = parser(['--dashed-will-be-stripped', '--here.it-will-too',
'--foo.not-dashed.also-not-dashed', '--foo.definitley-not-dashed'], {
configuration: {
'strip-dashed': true
}
})
argv.should.not.have.property('dashed-will-be-stripped')
argv.here.should.not.have.property('it-will-too')
argv.here.should.have.property('itWillToo')
argv.foo.should.not.have.property('not-dashed')
argv.foo.notDashed.should.not.have.property('also-not-dashed')
argv.foo.notDashed.should.have.property('alsoNotDashed')
})

it('strip-dashed removes dotted keys with dot-notation disabled', function () {
var argv = parser(['--foo.not-dashed'], {
configuration: {
'strip-dashed': true,
'dot-notation': false
}
})
argv.should.not.have.property('foo.not-dashed')
})
})

describe('prototype collisions', () => {
Expand Down

0 comments on commit 8f40f1f

Please sign in to comment.