Skip to content

Commit

Permalink
Apply optional transformer to input before accepted (#642)
Browse files Browse the repository at this point in the history
closes #359
  • Loading branch information
travi authored and SBoudrias committed Feb 5, 2018
1 parent 45f324d commit 57dc2b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -113,12 +113,13 @@ 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.
- **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.
- **suffix**: (String) Change the default _suffix_ message.

`default`, `choices`(if defined as functions), `validate`, `filter` and `when` functions can be called asynchronous. Either return a promise or use `this.async()` to get a callback you'll call with the final value.
`default`, `choices`(if defined as functions), `validate`, `filter` and `when` functions can be called asynchronously. Either return a promise or use `this.async()` to get a callback you'll call with the final value.

``` javascript
{
Expand Down Expand Up @@ -237,7 +238,7 @@ Take `type`, `name`, `message`[, `default`] properties. `default` is expected to

#### Input - `{type: 'input'}`

Take `type`, `name`, `message`[, `default`, `filter`, `validate`] properties.
Take `type`, `name`, `message`[, `default`, `filter`, `validate`, `transformer`] properties.

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

Expand Down
3 changes: 3 additions & 0 deletions lib/prompts/input.js
Expand Up @@ -41,9 +41,12 @@ class InputPrompt extends Base {
render(error) {
var bottomContent = '';
var message = this.getQuestion();
var transformer = this.opt.transformer;

if (this.status === 'answered') {
message += chalk.cyan(this.answer);
} else if (transformer) {
message += transformer(this.rl.line);
} else {
message += this.rl.line;
}
Expand Down
20 changes: 20 additions & 0 deletions test/specs/prompts/input.js
Expand Up @@ -35,4 +35,24 @@ describe('`input` prompt', function() {
expect(this.rl.output.__raw__).to.contain('pass');
});
});

it('should apply the provided transform to the value', function(done) {
this.fixture.transformer = function(value) {
return value
.split('')
.reverse()
.join('');
};

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

this.rl.line = 'Inquirer';
this.rl.input.emit('keypress');

setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('reriuqnI');
done();
}, 10);
});
});

0 comments on commit 57dc2b4

Please sign in to comment.