diff --git a/src/prompt-bypass.js b/src/prompt-bypass.js index a84cb5a..e86fe21 100644 --- a/src/prompt-bypass.js +++ b/src/prompt-bypass.js @@ -19,23 +19,21 @@ const getChoiceValue = (choice) => { return choice; }; +// check if the choice value matches the bypass value +function checkChoiceValue(choiceValue, value) { + return typeof choiceValue === 'string' + && choiceValue.toLowerCase() === value.toLowerCase(); +} + // check if a bypass value matches some aspect of // a particular choice option (index, value, key, etc) -const choiceMatchesValue = (choice, choiceIdx, value) => { - const choiceValue = getChoiceValue(choice); - - const valueMatchesChoice = choiceValue && choiceValue.toLowerCase() === value.toLowerCase(); - const valueMatchesChoiceKey = typeof choice.key === 'string' && choice.key.toLowerCase() === value.toLowerCase(); - const valueMatchesChoiceName = typeof choice.name === 'string' && choice.name.toLowerCase() === value.toLowerCase(); - const valueMatchesChoiceIndex = choiceIdx.toString() === value; - - return ( - valueMatchesChoice - || valueMatchesChoiceKey - || valueMatchesChoiceName - || valueMatchesChoiceIndex - ); -}; +function choiceMatchesValue (choice, choiceIdx, value) { + return checkChoiceValue(choice, value) + || checkChoiceValue(choice.value, value) + || checkChoiceValue(choice.key, value) + || checkChoiceValue(choice.name, value) + || checkChoiceValue(choiceIdx.toString(), value); +} // check if a value matches a particular set of flagged input options const isFlag = (list, v) => list.includes(v.toLowerCase()); diff --git a/tests/prompt-bypass-list.ava.js b/tests/prompt-bypass-list.ava.js index 8260f8a..0b4cf43 100644 --- a/tests/prompt-bypass-list.ava.js +++ b/tests/prompt-bypass-list.ava.js @@ -6,13 +6,15 @@ const plop = nodePlop(); const prompts = [{ type:'list', - name:'list', message:'listMsg', + name:'list', + message:'listMsg', choices: [ 'eh', {key: 'b', value:'bee'}, {name: 'c', value: 'see'}, {value: 'd'}, - {name: 'e'} + {name: 'e'}, + {key: 'f', name: 'ff', value: { prop: 'value'}} ] }]; @@ -49,9 +51,19 @@ test('verify good bypass input', function (t) { const [, byIndexNumber] = promptBypass(prompts, [4], plop); t.is(byIndexNumber.list, 'e'); + + const [, byIndexNumberObject] = promptBypass(prompts, [5], plop); + t.deepEqual(byIndexNumberObject.list, { prop: 'value' }); + + const [, byKeyObject] = promptBypass(prompts, 'f', plop); + t.deepEqual(byKeyObject.list, { prop: 'value' }); + + const [, byNameObject] = promptBypass(prompts, 'ff', plop); + t.deepEqual(byNameObject.list, { prop: 'value' }); }); test('verify bad bypass input', function (t) { t.throws(() => promptBypass(prompts, ['asdf'], plop)); - t.throws(() => promptBypass(prompts, ['5'], plop)); + t.throws(() => promptBypass(prompts, ['6'], plop)); + t.throws(() => promptBypass(prompts, [6], plop)); });