Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

feat: Prompt Bypass supports Object choice values #209

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/prompt-bypass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
18 changes: 15 additions & 3 deletions tests/prompt-bypass-list.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
]
}];

Expand Down Expand Up @@ -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));
});