Skip to content

Commit

Permalink
added transform feature for confirm prompts (#1212)
Browse files Browse the repository at this point in the history
* added transform feature for confirm prompts

* added the documentation and refactored code for better readability and scalability

* Fix documentation

---------

Co-authored-by: Harsh Jain <harsh.jain@springworks.in>
Co-authored-by: Simon Boudrias <admin@simonboudrias.com>
  • Loading branch information
3 people committed Apr 23, 2023
1 parent 8891098 commit aeab01d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Choices whose property `disabled` is truthy will be unselectable. If `disabled`

#### Confirm - `{type: 'confirm'}`

Take `type`, `name`, `message`, [`default`] properties. `default` is expected to be a boolean if used.
Take `type`, `name`, `message`, [`default`, `transformer`] properties. `default` is expected to be a boolean if used.

![Confirm prompt](https://cdn.rawgit.com/SBoudrias/Inquirer.js/28ae8337ba51d93e359ef4f7ee24e79b69898962/assets/screenshots/confirm.svg)

Expand Down Expand Up @@ -390,7 +390,7 @@ look at issues found on other command line - feel free to report any!

<a name="issues"></a>

- **nodemon** - Makes the arrow keys print gibrish on list prompts.
- **nodemon** - Makes the arrow keys print gibrish on list prompts.
Workaround: Add `{ stdin : false }` in the configuration file or pass `--no-stdin` in the CLI.
Please refer to [this issue](https://github.com/SBoudrias/Inquirer.js/issues/844#issuecomment-736675867)

Expand Down
1 change: 1 addition & 0 deletions packages/inquirer/examples/pizza.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const questions = [
name: 'toBeDelivered',
message: 'Is this for delivery?',
default: false,
transformer: (answer) => (answer ? '👍' : '👎'),
},
{
type: 'input',
Expand Down
1 change: 1 addition & 0 deletions packages/inquirer/lib/prompts/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class Prompt {
when: () => true,
suffix: '',
prefix: chalk.green('?'),
transformer: (val) => val,
});

// Make sure name is present
Expand Down
7 changes: 6 additions & 1 deletion packages/inquirer/lib/prompts/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default class ConfirmPrompt extends Base {

if (typeof answer === 'boolean') {
message += chalk.cyan(answer ? 'Yes' : 'No');
} else if (answer) {
message += answer;
} else {
message += this.rl.line;
}
Expand All @@ -78,7 +80,10 @@ export default class ConfirmPrompt extends Base {
onEnd(input) {
this.status = 'answered';

const output = this.opt.filter(input);
let output = this.opt.filter(input);
if (this.opt.transformer) {
output = this.opt.transformer(output);
}
this.render(output);

this.screen.done();
Expand Down
14 changes: 14 additions & 0 deletions packages/inquirer/test/specs/prompts/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@ describe('`confirm` prompt', () => {

this.rl.emit('line', 'bla bla foo');
});

it('should tranform the output based on the boolean value', function (done) {
this.fixture.transformer = (value) => (value ? '👍' : '👎');
const confirmOutput = new Confirm(this.fixture, this.rl);
confirmOutput
.run()
.then((answer) => {
expect(answer).to.equal('👍');
done();
})
.catch((err) => console.log(err));

this.rl.emit('line', 'y');
});
});

0 comments on commit aeab01d

Please sign in to comment.