Skip to content

Commit

Permalink
Add number support for choices prompt (#796)
Browse files Browse the repository at this point in the history
* Add number support for choice prompt

* Update readme
  • Loading branch information
LitoMore authored and SBoudrias committed Apr 12, 2019
1 parent 0e93c0a commit 8535305
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -119,7 +119,7 @@ A question object is a `hash` containing question related values:
- **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).
Array values can be simple `numbers`, `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, answers hash and option flags, and return a transformed value to display to the user. The transformation only impacts what is shown while editing. It does not modify the answers hash.
Expand Down
10 changes: 5 additions & 5 deletions packages/inquirer/lib/objects/choice.js
Expand Up @@ -5,8 +5,8 @@ var _ = require('lodash');
* Choice object
* Normalize input as choice object
* @constructor
* @param {String|Object} val Choice value. If an object is passed, it should contains
* at least one of `value` or `name` property
* @param {Number|String|Object} val Choice value. If an object is passed, it should contains
* at least one of `value` or `name` property
*/

module.exports = class Choice {
Expand All @@ -16,10 +16,10 @@ module.exports = class Choice {
return val;
}

if (_.isString(val)) {
this.name = val;
if (_.isString(val) || _.isNumber(val)) {
this.name = String(val);
this.value = val;
this.short = val;
this.short = String(val);
} else {
_.extend(this, val, {
name: val.name || val.value,
Expand Down
5 changes: 5 additions & 0 deletions packages/inquirer/test/specs/objects/choices.js
Expand Up @@ -11,6 +11,11 @@ describe('Choices collection', function() {
expect(choices.getChoice(1)).to.be.instanceOf(Choice);
});

it('should support for number', function() {
var choices = new Choices([1, 2, 3, 4]);
expect(choices.getChoice(0).value).to.equal(1);
});

it('should not process Separator object', function() {
var sep = new inquirer.Separator();
var choices = new Choices(['Bar', sep]);
Expand Down

0 comments on commit 8535305

Please sign in to comment.