Skip to content

Commit

Permalink
Pass answers object to transformer functions (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
raingerber authored and SBoudrias committed Mar 26, 2018
1 parent 11c52fd commit 9bed102
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 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 return the transformed value to be displayed to the user. The transformation only impacts what is shown while editing. It does not impact 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.
- **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
2 changes: 1 addition & 1 deletion lib/prompts/input.js
Expand Up @@ -46,7 +46,7 @@ class InputPrompt extends Base {
if (this.status === 'answered') {
message += chalk.cyan(this.answer);
} else if (transformer) {
message += transformer(this.rl.line);
message += transformer(this.rl.line, this.answers);
} else {
message += this.rl.line;
}
Expand Down
21 changes: 21 additions & 0 deletions test/specs/prompts/input.js
Expand Up @@ -55,4 +55,25 @@ describe('`input` prompt', function() {
done();
}, 10);
});

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

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 9bed102

Please sign in to comment.