From 8535305ea20bb79c0e613d38a050935f68569c82 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Fri, 12 Apr 2019 12:56:15 +0800 Subject: [PATCH] Add number support for choices prompt (#796) * Add number support for choice prompt * Update readme --- README.md | 2 +- packages/inquirer/lib/objects/choice.js | 10 +++++----- packages/inquirer/test/specs/objects/choices.js | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dd28635f7..1f03655f7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 634e6bcd1..76f932933 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -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 { @@ -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, diff --git a/packages/inquirer/test/specs/objects/choices.js b/packages/inquirer/test/specs/objects/choices.js index bd168d71b..4bd3b9514 100644 --- a/packages/inquirer/test/specs/objects/choices.js +++ b/packages/inquirer/test/specs/objects/choices.js @@ -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]);