Skip to content

Commit

Permalink
Adds string default option for list and rawlist (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Froren authored and SBoudrias committed Nov 8, 2017
1 parent ce4e858 commit b701876
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/prompts/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,16 @@ Prompt.prototype.validateChoices = function (choices) {
/**
* Generate a string out of the choices keys
* @param {Array} choices
* @param {Number} defaultIndex - the choice index to capitalize
* @param {Number|String} default - the choice index or name to capitalize
* @return {String} The rendered choices key string
*/
Prompt.prototype.generateChoicesString = function (choices, defaultIndex) {
Prompt.prototype.generateChoicesString = function (choices, defaultChoice) {
var defIndex = choices.realLength - 1;
if (_.isNumber(defaultIndex) && this.opt.choices.getChoice(defaultIndex)) {
defIndex = defaultIndex;
if (_.isNumber(defaultChoice) && this.opt.choices.getChoice(defaultChoice)) {
defIndex = defaultChoice;
} else if (_.isString(defaultChoice)) {
let index = _.findIndex(choices.realChoices, ({value}) => value === defaultChoice);
defIndex = (index === -1 ? defIndex : index);
}
var defStr = this.opt.choices.pluck('key');
this.rawDefault = defStr[defIndex];
Expand Down
2 changes: 1 addition & 1 deletion lib/prompts/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Prompt() {
if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
this.selected = def;
} else if (!_.isNumber(def) && def != null) {
let index = this.opt.choices.pluck('value').indexOf(def);
let index = _.findIndex(this.opt.choices.realChoices, ({value}) => value === def);
this.selected = Math.max(index, 0);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/prompts/rawlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function Prompt() {
var def = this.opt.default;
if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
this.selected = this.rawDefault = def;
} else if (!_.isNumber(def) && def != null) {
let index = _.findIndex(this.opt.choices.realChoices, ({value}) => value === def);
this.selected = this.rawDefault = Math.max(index, 0);
}

// Make sure no default is set (so it won't be printed)
Expand Down
27 changes: 27 additions & 0 deletions test/specs/prompts/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ describe('`expand` prompt', function () {
}.bind(this));
});

it('should use a string the `default` value', function (done) {
this.fixture.default = 'chile';
this.expand = new Expand(this.fixture, this.rl);

this.expand.run().then(function (answer) {
expect(answer).to.equal('chile');
done();
});
this.rl.emit('line');
});

it('should use the `default` argument value', function (done) {
this.fixture.default = 1;
this.expand = new Expand(this.fixture, this.rl);
Expand Down Expand Up @@ -125,6 +136,22 @@ describe('`expand` prompt', function () {
expect(this.rl.output.__raw__).to.contain('(aBcdh)');
});

it('should display and capitalize the default choice by name value', function () {
this.fixture.default = 'chile';
this.expand = new Expand(this.fixture, this.rl);

this.expand.run();
expect(this.rl.output.__raw__).to.contain('(abCdh)');
});

it('should display and capitalize the default choice H (Help) `key` if no string default matched', function () {
this.fixture.default = 'chile!';
this.expand = new Expand(this.fixture, this.rl);

this.expand.run();
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);
Expand Down
24 changes: 24 additions & 0 deletions test/specs/prompts/rawlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@ describe('`rawlist` prompt', function () {

this.rl.emit('line');
});

it('should allow string default being the value', function (done) {
this.fixture.default = 'bum';
var list = new Rawlist(this.fixture, this.rl);

list.run().then(function (answer) {
expect(answer).to.equal('bum');
done();
});

this.rl.emit('line');
});

it('shouldn\'t allow an invalid string default to change position', function (done) {
this.fixture.default = 'bumby';
var list = new Rawlist(this.fixture, this.rl);

list.run().then(function (answer) {
expect(answer).to.equal('foo');
done();
});

this.rl.emit('line');
});
});

0 comments on commit b701876

Please sign in to comment.