Skip to content

Commit

Permalink
feat: allow not to flatten duplicate array types
Browse files Browse the repository at this point in the history
Lets you disable flattening of multiple array type values

```
yargsParser('-x a b -x c d', {array: ['x]})
// => {x: ['a', 'b', 'c', 'd']} // all flattened
```
```
yargsParser('-x a b -x c d', {array: ['x], configuration:{'flatten-duplicate-arrays': false}})
// => {x: [['a', 'b'], ['c', 'd']]} // multiple nested arrays
```
  • Loading branch information
laggingreflex committed Nov 25, 2016
1 parent d23b473 commit 58aaa30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
17 changes: 14 additions & 3 deletions index.js
Expand Up @@ -16,7 +16,8 @@ function parse (args, opts) {
'dot-notation': true,
'parse-numbers': true,
'boolean-negation': true,
'duplicate-arguments-array': true
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': true
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -311,15 +312,25 @@ function parse (args, opts) {
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
function eatArray (i, key, args) {
var start = i + 1
var argsToSet = []
var multipleArrayFlag = i > 0
for (var ii = i + 1; ii < args.length; ii++) {
if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
if (ii === start) {
setArg(key, defaultForType('array'))
}
multipleArrayFlag = true
break
}
i = ii
setArg(key, args[ii])
argsToSet.push(args[ii])
}
if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) {
setArg(key, argsToSet)
} else {
argsToSet.forEach(function (arg) {
setArg(key, arg)
})
}

return i
Expand Down Expand Up @@ -540,7 +551,7 @@ function parse (args, opts) {
if (value === increment) {
o[key] = increment(o[key])
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
o[key] = Array.isArray(value) ? value : [value]
o[key] = Array.isArray(value) && configuration['flatten-duplicate-arrays'] ? value : [value]
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
o[key] = value
} else if (Array.isArray(o[key])) {
Expand Down
23 changes: 23 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1954,6 +1954,29 @@ describe('yargs-parser', function () {
parsed['x'].should.equal('b')
})
})

describe('flatten duplicate arrays', function () {
it('flattens duplicate array type', function () {
var parsed = parser('-x a b -x c d', {
array: ['x'],
configuration: {
'flatten-duplicate-arrays': true
}
})

parsed['x'].should.deep.equal(['a', 'b', 'c', 'd'])
})
it('nests duplicate array types', function () {
var parsed = parser('-x a b -x c d', {
array: ['x'],
configuration: {
'flatten-duplicate-arrays': false
}
})

parsed['x'].should.deep.equal([['a', 'b'], ['c', 'd']])
})
})
})

// addresses: https://github.com/yargs/yargs-parser/issues/41
Expand Down

0 comments on commit 58aaa30

Please sign in to comment.