Skip to content

Commit 5c010b6

Browse files
committed
simplify setKey logic
make compatible with Node 0.10 get back to 100% test coverage
1 parent e5c6ee2 commit 5c010b6

File tree

1 file changed

+12
-130
lines changed

1 file changed

+12
-130
lines changed

index.js

Lines changed: 12 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ function parse (args, opts) {
326326
argsToSet.push(args[ii])
327327
}
328328
if (multipleArrayFlag) {
329-
setArg(key, argsToSet.map(arg => processValue(key, arg)))
329+
setArg(key, argsToSet.map(function (arg) {
330+
return processValue(key, arg)
331+
}))
330332
} else {
331333
argsToSet.forEach(function (arg) {
332334
setArg(key, arg)
@@ -557,142 +559,22 @@ function parse (args, opts) {
557559
var key = keys[keys.length - 1]
558560

559561
var isTypeArray = checkAllAliases(key, flags.arrays)
560-
var isOldValueArray = Array.isArray(o[key])
561-
var isNewValueArray = Array.isArray(value)
562+
var isValueArray = Array.isArray(value)
562563
var duplicate = configuration['duplicate-arguments-array']
563-
var flatten = configuration['flatten-duplicate-arrays']
564564

565565
if (value === increment) {
566566
o[key] = increment(o[key])
567-
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
568-
o[key] = Array.isArray(value) ? value : [value]
569-
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
570-
o[key] = value
571567
} else if (Array.isArray(o[key])) {
572-
if (!duplicate && !flatten) {
573-
if (isTypeArray) {
574-
if (isOldValueArray) {
575-
if (isNewValueArray) {
576-
o[key] = value
577-
} else {
578-
o[key] = o[key].concat([value])
579-
}
580-
} else {
581-
if (isNewValueArray) {
582-
throw new Error('this should not happen')
583-
} else {
584-
throw new Error('this should not happen')
585-
}
586-
}
587-
} else {
588-
if (isOldValueArray) {
589-
if (isNewValueArray) {
590-
throw new Error('this should not happen')
591-
} else {
592-
throw new Error('this should not happen')
593-
}
594-
} else {
595-
if (isNewValueArray) {
596-
throw new Error('this should not happen')
597-
} else {
598-
o[key] = value
599-
}
600-
}
601-
}
602-
} else if (!duplicate && flatten) {
603-
if (isTypeArray) {
604-
if (isOldValueArray) {
605-
if (isNewValueArray) {
606-
o[key] = value
607-
} else {
608-
o[key] = o[key].concat([value])
609-
}
610-
} else {
611-
if (isNewValueArray) {
612-
throw new Error('this should not happen')
613-
} else {
614-
throw new Error('this should not happen')
615-
}
616-
}
617-
} else {
618-
if (isOldValueArray) {
619-
if (isNewValueArray) {
620-
throw new Error('this should not happen')
621-
} else {
622-
o[key] = value
623-
}
624-
} else {
625-
if (isNewValueArray) {
626-
throw new Error('this should not happen')
627-
} else {
628-
o[key] = value
629-
}
630-
}
631-
}
632-
} else if (duplicate && flatten) {
633-
if (isTypeArray) {
634-
if (isOldValueArray) {
635-
if (isNewValueArray) {
636-
o[key] = o[key].concat(value)
637-
} else {
638-
o[key] = o[key].concat([value])
639-
}
640-
} else {
641-
if (isNewValueArray) {
642-
throw new Error('this should not happen')
643-
} else {
644-
throw new Error('this should not happen')
645-
}
646-
}
647-
} else {
648-
if (isOldValueArray) {
649-
if (isNewValueArray) {
650-
throw new Error('this should not happen')
651-
} else {
652-
o[key] = o[key].concat([value])
653-
}
654-
} else {
655-
if (isNewValueArray) {
656-
throw new Error('this should not happen')
657-
} else {
658-
o[key] = o[key].concat([value])
659-
}
660-
}
661-
}
662-
} else if (duplicate && !flatten) {
663-
if (isTypeArray) {
664-
if (isOldValueArray) {
665-
if (isNewValueArray) {
666-
o[key] = [o[key]].concat([value])
667-
} else {
668-
o[key] = o[key].concat([value])
669-
}
670-
} else {
671-
if (isNewValueArray) {
672-
throw new Error('this should not happen')
673-
} else {
674-
throw new Error('this should not happen')
675-
}
676-
}
677-
} else {
678-
if (isOldValueArray) {
679-
if (isNewValueArray) {
680-
throw new Error('this should not happen')
681-
} else {
682-
o[key] = o[key].concat([value])
683-
}
684-
} else {
685-
if (isNewValueArray) {
686-
throw new Error('this should not happen')
687-
} else {
688-
o[key] = o[key].concat([value])
689-
}
690-
}
691-
}
568+
if (duplicate && isTypeArray && isValueArray) {
569+
o[key] = configuration['flatten-duplicate-arrays'] ? o[key].concat(value) : [o[key]].concat([value])
570+
} else if (!duplicate && Boolean(isTypeArray) === Boolean(isValueArray)) {
571+
o[key] = value
692572
} else {
693-
throw new Error('this should not happen')
573+
o[key] = o[key].concat([value])
694574
}
695-
} else if (configuration['duplicate-arguments-array']) {
575+
} else if (o[key] === undefined && isTypeArray) {
576+
o[key] = isValueArray ? value : [value]
577+
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts))) {
696578
o[key] = [ o[key], value ]
697579
} else {
698580
o[key] = value

0 commit comments

Comments
 (0)