Skip to content

Commit

Permalink
feat: add strip-aliased and strip-dashed configuration options. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and bcoe committed May 5, 2019
1 parent 0ae7fcb commit a3936aa
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
43 changes: 43 additions & 0 deletions README.md
Expand Up @@ -340,6 +340,49 @@ node example.js -a run b -x y
{ _: [ 'run', 'b', '-x', 'y' ], a: true }
```
### strip aliased
* default: `false`
* key: `strip-aliased`
Should aliases be removed before returning results?
_If disabled:_
```sh
node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1, 'test-alias': 1, testAlias: 1 }
```
_If enabled:_
```sh
node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1 }
```
### strip dashed
* default: `false`
* key: `strip-dashed`
Should dashed keys be removed before returning results? This option has no effect if
`camel-case-exansion` is disabled.
_If disabled:_
```sh
node example.js --test-field 1
{ _: [], 'test-field': 1, testField: 1 }
```
_If enabled:_
```sh
node example.js --test-field 1
{ _: [], testField: 1 }
```
## Special Thanks
The yargs project evolves from optimist and minimist. It owes its
Expand Down
21 changes: 20 additions & 1 deletion index.js
Expand Up @@ -24,7 +24,9 @@ function parse (args, opts) {
'populate--': false,
'combine-arrays': false,
'set-placeholder-key': false,
'halt-at-non-option': false
'halt-at-non-option': false,
'strip-aliased': false,
'strip-dashed': false
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -331,6 +333,23 @@ function parse (args, opts) {
argv[notFlagsArgv].push(key)
})

if (configuration['camel-case-expansion'] && configuration['strip-dashed']) {
Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => {
delete argv[key]
})
}

if (configuration['strip-aliased']) {
// XXX Switch to [].concat(...Object.values(aliases)) once node.js 6 is dropped
;[].concat(...Object.keys(aliases).map(k => aliases[k])).forEach(alias => {
if (configuration['camel-case-expansion']) {
delete argv[alias.split('.').map(prop => camelCase(prop)).join('.')]
}

delete argv[alias]
})
}

// how many arguments should we consume, based
// on the nargs option?
function eatNargs (i, key, args) {
Expand Down
67 changes: 67 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2874,4 +2874,71 @@ describe('yargs-parser', function () {
})
argv.bar.should.equal('hello')
})

describe('stripping', function () {
it('strip-dashed removes expected fields from argv', function () {
const argv = parser([ '--test-value', '1' ], {
number: ['test-value'],
alias: {
'test-value': ['alt-test']
},
configuration: {
'strip-dashed': true
}
})
argv.should.deep.equal({
_: [],
'testValue': 1,
'altTest': 1
})
})

it('strip-aliased removes expected fields from argv', function () {
const argv = parser([ '--test-value', '1' ], {
number: ['test-value'],
alias: {
'test-value': ['alt-test']
},
configuration: {
'strip-aliased': true
}
})
argv.should.deep.equal({
_: [],
'test-value': 1,
'testValue': 1
})
})

it('strip-aliased and strip-dashed combined removes expected fields from argv', function () {
const argv = parser([ '--test-value', '1' ], {
number: ['test-value'],
alias: {
'test-value': ['alt-test']
},
configuration: {
'strip-aliased': true,
'strip-dashed': true
}
})
argv.should.deep.equal({
_: [],
'testValue': 1
})
})

it('ignores strip-dashed if camel-case-expansion is disabled', function () {
const argv = parser([ '--test-value', '1' ], {
number: ['test-value'],
configuration: {
'camel-case-expansion': false,
'strip-dashed': true
}
})
argv.should.deep.equal({
_: [],
'test-value': 1
})
})
})
})

0 comments on commit a3936aa

Please sign in to comment.