Skip to content

Commit

Permalink
Use name as default message (#636)
Browse files Browse the repository at this point in the history
* Use name as default message

* Document default value for `message`

* Update tests to expect default messages
  • Loading branch information
stuartpb authored and SBoudrias committed May 14, 2018
1 parent ff89798 commit 8e0e459
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -107,7 +107,7 @@ A question object is a `hash` containing question related values:
- **type**: (String) Type of the prompt. Defaults: `input` - Possible values: `input`, `confirm`,
`list`, `rawlist`, `expand`, `checkbox`, `password`, `editor`
- **name**: (String) The name to use when storing the answer in the answers hash. If the name contains periods, it will define a path in the answers hash.
- **message**: (String|Function) The question to print. If defined as a function, the first parameter will be the current inquirer session answers.
- **message**: (String|Function) The question to print. If defined as a function, the first parameter will be the current inquirer session answers. Defaults to the value of `name` (followed by a colon).
- **default**: (String|Number|Boolean|Array|Function) Default value(s) to use if nothing is entered, or a function that returns the default value(s). If defined as a function, the first parameter will be the current inquirer session answers.
- **choices**: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers.
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).
Expand Down
10 changes: 6 additions & 4 deletions lib/prompts/base.js
Expand Up @@ -28,14 +28,16 @@ class Prompt {
prefix: chalk.green('?')
});

// Check to make sure prompt requirements are there
if (!this.opt.message) {
this.throwParamError('message');
}
// Make sure name is present
if (!this.opt.name) {
this.throwParamError('name');
}

// Set default message if no message defined
if (!this.opt.message) {
this.opt.message = this.opt.name + ':';
}

// Normalize choices
if (Array.isArray(this.opt.choices)) {
this.opt.choices = new Choices(this.opt.choices, answers);
Expand Down
16 changes: 9 additions & 7 deletions test/specs/api.js
Expand Up @@ -324,6 +324,15 @@ var tests = {

expect(this.rl.output.__raw__).to.contain(this.fixture.message);
});
it('should default to name for message', function() {
this.fixture.name = 'testfoobarbarfoobar';
delete this.fixture.message;

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

expect(this.rl.output.__raw__).to.contain(this.fixture.name + ':');
});
});
},

Expand All @@ -344,13 +353,6 @@ var tests = {

requiredValues: function() {
describe('Missing value', function() {
it('`message` should throw', function() {
expect(() => {
delete this.fixture.message;
return new this.Prompt(this.fixture, this.rl);
}).to.throw(/message/);
});

it('`name` should throw', function() {
expect(() => {
delete this.fixture.name;
Expand Down

0 comments on commit 8e0e459

Please sign in to comment.