Skip to content

Commit 06203f8

Browse files
authored
Handle questions as a plain object {[name]: question} (#885)
1 parent 6717092 commit 06203f8

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/inquirer/lib/ui/prompt.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ class PromptUI extends Base {
3232

3333
// Make sure questions is an array.
3434
if (_.isPlainObject(questions)) {
35-
questions = [questions];
35+
// It's either an object of questions or a single question
36+
questions = Object.values(questions).every(
37+
(v) => _.isPlainObject(v) && v.name === undefined
38+
)
39+
? Object.entries(questions).map(([name, question]) => ({ name, ...question }))
40+
: [questions];
3641
}
3742

3843
// Create an observable, unless we received one as parameter.

packages/inquirer/test/specs/inquirer.js

+20
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ describe('inquirer.prompt', () => {
9797
});
9898
});
9999

100+
it('should take a prompts nested object and return answers', async function () {
101+
var prompts = {
102+
q1: {
103+
type: 'confirm',
104+
message: 'message',
105+
},
106+
q2: {
107+
type: 'input',
108+
message: 'message',
109+
default: 'Foo',
110+
},
111+
};
112+
113+
var promise = this.prompt(prompts);
114+
autosubmit(promise.ui);
115+
const { q1, q2 } = await promise;
116+
expect(q1).to.equal(true);
117+
expect(q2).to.equal('Foo');
118+
});
119+
100120
it('should take a prompts array with nested names', function () {
101121
const prompts = [
102122
{

0 commit comments

Comments
 (0)