Skip to content

Commit

Permalink
Default value for expand can be 'help' (#457)
Browse files Browse the repository at this point in the history
* Default value for expand can be 'help'

* Make help the default option if no default key is provided

* tiny change requested by SBoudrias

* simplifying even more :)

* using `choices.realLength`
  • Loading branch information
ihorskyi authored and SBoudrias committed Nov 24, 2016
1 parent 2ad1824 commit ae73e86
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ default must the choice `index` in the array)
#### Expand - `{type: 'expand'}`

Take `type`, `name`, `message`, `choices`[, `default`] properties. (Note that
default must be the choice `index` in the array)
default must be the choice `index` in the array. If `default` key not provided, then `help` will be used as default choice)

Note that the `choices` object will take an extra parameter called `key` for the `expand` prompt. This parameter must be a single (lowercased) character. The `h` option is added by the prompt and shouldn't be defined by the user.

Expand Down
2 changes: 1 addition & 1 deletion lib/prompts/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Prompt.prototype.validateChoices = function (choices) {
* @return {String} The rendered choices key string
*/
Prompt.prototype.generateChoicesString = function (choices, defaultIndex) {
var defIndex = 0;
var defIndex = choices.realLength - 1;
if (_.isNumber(defaultIndex) && this.opt.choices.getChoice(defaultIndex)) {
defIndex = defaultIndex;
}
Expand Down
16 changes: 8 additions & 8 deletions test/specs/prompts/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ describe('`expand` prompt', function () {
expect(mkPrompt).to.throw(/Reserved key error/);
});

it('should take the first choice by default', function (done) {
this.expand.run().then(function (answer) {
expect(answer).to.equal('acab');
done();
});
this.rl.emit('line');
});

it('should allow false as a value', function () {
var promise = this.expand.run();

Expand Down Expand Up @@ -133,6 +125,14 @@ describe('`expand` prompt', function () {
expect(this.rl.output.__raw__).to.contain('(aBcdh)');
});

it('should display and capitalize the default choice H (Help) `key` if none provided', function () {
delete this.fixture.default;
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();

expect(this.rl.output.__raw__).to.contain('(abcdH)');
});

it('should \'autocomplete\' the user input', function (done) {
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
Expand Down

0 comments on commit ae73e86

Please sign in to comment.