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

Conversation

knikolov-nuvolo
Copy link
Contributor

@knikolov-nuvolo knikolov-nuvolo commented Oct 6, 2021

Closes plop/issues/284

Check for choice value type, before comparing it with the bypass value.
When the value is not a string, check for key, name, and index (as usual).

Add unit tests.

Comment on lines 22 to 50
// answer is string
[
{ bypassValue: 'eh', expectedAnswer: 'eh' }, // by value
{ bypassValue: 'b', expectedAnswer: 'bee' }, // by key
{ bypassValue: 'c', expectedAnswer: 'see' }, // by name
{ bypassValue: 'd', expectedAnswer: 'd' }, // by value prop
{ bypassValue: 'e', expectedAnswer: 'e' }, // by name, no value
{ bypassValue: '0', expectedAnswer: 'eh' }, // by index - value
{ bypassValue: '1', expectedAnswer: 'bee' }, // by index - key
{ bypassValue: '2', expectedAnswer: 'see' }, // by index - name
{ bypassValue: '3', expectedAnswer: 'd' }, // by index - value prop
{ bypassValue: '4', expectedAnswer: 'e' }, // by index - name, no value
{ bypassValue: 4, expectedAnswer: 'e' }, // by index number
].forEach(testCase => {
const [, value] = promptBypass(prompts, [testCase.bypassValue], plop);
t.is(value.list, testCase.expectedAnswer);
});

// answer is object
const objValue = { prop: 'value' };
[
{ bypassValue: 'f', expectedAnswer: objValue }, // by key
{ bypassValue: 'ff', expectedAnswer: objValue }, // by name
{ bypassValue: '5', expectedAnswer: objValue }, // by index
{ bypassValue: 5, expectedAnswer: objValue }, // by index number
].forEach(testCase => {
const [, value] = promptBypass(prompts, [testCase.bypassValue], plop);
t.deepEqual(value.list, testCase.expectedAnswer);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deeply appreciate you having added unit tests for this functionality!

However, can we revert the migration of tests to use forEach (both here and below). While DRY code is useful in application logic, I try my best to discourage adding any un-neccisary level of complexity to unit/integration tests, even if it means copy+pasting large swaths of code.

IMO it makes tests easier to immediately grok and less potential to need to debug the tests later on

Comment on lines 30 to 38
function choiceMatchesValue (choice, choiceIdx, value) {
return [
choice,
choice.value,
choice.key,
choice.name,
choiceIdx.toString()
].some(choiceValue => checkChoiceValue(choiceValue, value));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find this logic a bit hard to parse in terms of early-return (despite understanding some usage).

I think I'd rather move this back to using logical ORs. What do you think of this?

Suggested change
function choiceMatchesValue (choice, choiceIdx, value) {
return [
choice,
choice.value,
choice.key,
choice.name,
choiceIdx.toString()
].some(choiceValue => checkChoiceValue(choiceValue, value));
}
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)
}

@crutchcorn
Copy link
Member

Hey hey @knikolov-nuvolo! Thanks a ton for submitting a PR! 😊 Left a few thoughts - once addressed we can merge!

@knikolov-nuvolo
Copy link
Contributor Author

Thanks for the feedback. I made the requested changes :)

Copy link
Member

@crutchcorn crutchcorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey hey! Super super sorry for not communicating more.

Past few days have been JAMMED packed. This is awesome and ready-to-go (I actually read thru code on my phone the other night).

However, I don't yet have time (and won't for a few days, still) to merge in the PR and release 😅 I'll try my best to tackle this no later than Friday.

Please feel free to ping me if I miss this deadline. That all said, I have this email marked as unread (which I notoriously never have unread emails so I can bug myself with stuff like this) and is at the top of my TODO list that I look at often. :)

@knikolov-nuvolo
Copy link
Contributor Author

knikolov-nuvolo commented Oct 11, 2021

No worries, and thanks for the feedback :)

As I mentioned in the issue, we have a temp solution for now. I'll be on vacation for most of this week, so whenever you find time.

@crutchcorn crutchcorn merged commit 0b6f291 into plopjs:master Oct 18, 2021
@crutchcorn
Copy link
Member

Pushed as-of node-plop@0.26.3 and plop@2.7.5

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot bypass list prompt when its value is an object
2 participants