Skip to content

Commit 55ff070

Browse files
authored
Feat(inquirer): Make Choices class iterable + add .some() support (#1399)
1 parent c34cfec commit 55ff070

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/inquirer/lib/objects/choices.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ export default class Choices {
4747
});
4848
}
4949

50+
[Symbol.iterator]() {
51+
const data = this.choices;
52+
let index = -1;
53+
54+
return {
55+
next: () => ({ value: data[++index], done: !(index in data) }),
56+
};
57+
}
58+
5059
/**
5160
* Get a valid choice from the collection
5261
* @param {Number} selector The selected choice index
@@ -106,6 +115,10 @@ export default class Choices {
106115
return this.choices.find(func);
107116
}
108117

118+
some(func) {
119+
return this.choices.some(func);
120+
}
121+
109122
push(...args) {
110123
const objs = args.map((val) => new Choice(val));
111124
this.choices.push(...objs);

packages/inquirer/lib/prompts/checkbox.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export default class CheckboxPrompt extends Base {
2020
}
2121

2222
if (Array.isArray(this.opt.default)) {
23-
this.opt.choices.forEach(function (choice) {
24-
if (this.opt.default.indexOf(choice.value) >= 0) {
23+
for (const choice of this.opt.choices) {
24+
if (this.opt.default.includes(choice.value)) {
2525
choice.checked = true;
2626
}
27-
}, this);
27+
}
2828
}
2929

3030
this.pointer = 0;
@@ -192,8 +192,8 @@ export default class CheckboxPrompt extends Base {
192192
}
193193

194194
onAllKey() {
195-
const shouldBeChecked = Boolean(
196-
this.opt.choices.find((choice) => choice.type !== 'separator' && !choice.checked),
195+
const shouldBeChecked = this.opt.choices.some(
196+
(choice) => choice.type !== 'separator' && !choice.checked,
197197
);
198198

199199
this.opt.choices.forEach((choice) => {

0 commit comments

Comments
 (0)