Skip to content

Commit

Permalink
Handle questions as a plain object {[name]: question} (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
caub committed May 21, 2021
1 parent 6717092 commit 06203f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/inquirer/lib/ui/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class PromptUI extends Base {

// Make sure questions is an array.
if (_.isPlainObject(questions)) {
questions = [questions];
// It's either an object of questions or a single question
questions = Object.values(questions).every(
(v) => _.isPlainObject(v) && v.name === undefined
)
? Object.entries(questions).map(([name, question]) => ({ name, ...question }))
: [questions];
}

// Create an observable, unless we received one as parameter.
Expand Down
20 changes: 20 additions & 0 deletions packages/inquirer/test/specs/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ describe('inquirer.prompt', () => {
});
});

it('should take a prompts nested object and return answers', async function () {
var prompts = {
q1: {
type: 'confirm',
message: 'message',
},
q2: {
type: 'input',
message: 'message',
default: 'Foo',
},
};

var promise = this.prompt(prompts);
autosubmit(promise.ui);
const { q1, q2 } = await promise;
expect(q1).to.equal(true);
expect(q2).to.equal('Foo');
});

it('should take a prompts array with nested names', function () {
const prompts = [
{
Expand Down

0 comments on commit 06203f8

Please sign in to comment.