From 57dc2b4ba6569de5850511f687d7c7b05a623a24 Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Mon, 5 Feb 2018 10:08:07 -0600 Subject: [PATCH] Apply optional transformer to input before accepted (#642) closes #359 --- README.md | 5 +++-- lib/prompts/input.js | 3 +++ test/specs/prompts/input.js | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d62fd196..525e597fc 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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) diff --git a/lib/prompts/input.js b/lib/prompts/input.js index bb31d67ec..d9e40c0e1 100644 --- a/lib/prompts/input.js +++ b/lib/prompts/input.js @@ -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; } diff --git a/test/specs/prompts/input.js b/test/specs/prompts/input.js index 718183e32..a4694a1c2 100644 --- a/test/specs/prompts/input.js +++ b/test/specs/prompts/input.js @@ -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); + }); });