Skip to content

Commit

Permalink
Add support for transform the answer after accepted (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore authored and SBoudrias committed Jun 2, 2018
1 parent b9c32b6 commit e349dfb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -113,7 +113,7 @@ A question object is a `hash` containing question related values:
Array values can be simple `strings`, or `objects` containing a `name` (to display in list), a `value` (to save in the answers hash) and a `short` (to display after selection) properties. The choices array can also contain [a `Separator`](#separator).
- **validate**: (Function) Receive the user input and answers hash. Should return `true` if the value is valid, and an error message (`String`) otherwise. If `false` is returned, a default error message is provided.
- **filter**: (Function) Receive the user input and return the filtered value to be used inside the program. The value returned will be added to the _Answers_ hash.
- **transformer**: (Function) Receive the user input and answers hash, and return a transformed value to display to the user. The transformation only impacts what is shown while editing. It does not modify the answers hash.
- **transformer**: (Function) Receive the user input, answers hash and option flags, and return a transformed value to display to the user. The transformation only impacts what is shown while editing. It does not modify the answers hash.
- **when**: (Function, Boolean) Receive the current user answers hash and should return `true` or `false` depending on whether or not this question should be asked. The value can also be a simple boolean.
- **pageSize**: (Number) Change the number of lines that will be rendered when using `list`, `rawList`, `expand` or `checkbox`.
- **prefix**: (String) Change the default _prefix_ message.
Expand Down
8 changes: 6 additions & 2 deletions examples/input.js
Expand Up @@ -24,8 +24,12 @@ var questions = [
type: 'input',
name: 'fav_color',
message: "What's your favorite color",
transformer: function(color) {
return chalkPipe(color)(color);
transformer: function(color, answers, flags) {
const text = chalkPipe(color)(color);
if (flags.isFinal) {
return text + '!';
}
return text;
}
},
{
Expand Down
16 changes: 11 additions & 5 deletions lib/prompts/input.js
Expand Up @@ -43,15 +43,21 @@ class InputPrompt extends Base {

render(error) {
var bottomContent = '';
var appendContent = '';
var message = this.getQuestion();
var transformer = this.opt.transformer;
var isFinal = this.status === 'answered';

if (this.status === 'answered') {
message += chalk.cyan(this.answer);
} else if (transformer) {
message += transformer(this.rl.line, this.answers);
if (isFinal) {
appendContent = this.answer;
} else {
message += this.rl.line;
appendContent = this.rl.line;
}

if (transformer) {
message += transformer(appendContent, this.answers, { isFinal });
} else {
message += isFinal ? chalk.cyan(appendContent) : appendContent;
}

if (error) {
Expand Down
22 changes: 22 additions & 0 deletions test/specs/prompts/input.js
Expand Up @@ -76,4 +76,26 @@ describe('`input` prompt', function() {
done();
}, 200);
});

it('should use the flags object in the provided transformer', function(done) {
this.fixture.transformer = function(value, answers, flags) {
var text = answers.capitalize ? value.toUpperCase() : value;
if (flags.isFinal) return text + '!';
return text;
};

var answers = {
capitalize: true
};

var prompt = new Input(this.fixture, this.rl, answers);
prompt.run();

this.rl.line = 'inquirer';
this.rl.input.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('INQUIRER');
done();
}, 200);
});
});

0 comments on commit e349dfb

Please sign in to comment.