Skip to content

Commit

Permalink
Feat(inquirer): Make Choices class iterable + add .some() support
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Apr 25, 2024
1 parent 6a18ae8 commit b5428e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/inquirer/lib/objects/choices.js
Expand Up @@ -47,6 +47,15 @@ export default class Choices {
});
}

[Symbol.iterator]() {
const data = this.choices;
let index = -1;

return {
next: () => ({ value: data[++index], done: !(index in data) }),
};
}

/**
* Get a valid choice from the collection
* @param {Number} selector The selected choice index
Expand Down Expand Up @@ -106,6 +115,10 @@ export default class Choices {
return this.choices.find(func);
}

some(func) {
return this.choices.some(func);
}

push(...args) {
const objs = args.map((val) => new Choice(val));
this.choices.push(...objs);
Expand Down
10 changes: 5 additions & 5 deletions packages/inquirer/lib/prompts/checkbox.js
Expand Up @@ -20,11 +20,11 @@ export default class CheckboxPrompt extends Base {
}

if (Array.isArray(this.opt.default)) {
this.opt.choices.forEach(function (choice) {
if (this.opt.default.indexOf(choice.value) >= 0) {
for (const choice of this.opt.choices) {
if (this.opt.default.includes(choice.value)) {
choice.checked = true;
}
}, this);
}
}

this.pointer = 0;
Expand Down Expand Up @@ -192,8 +192,8 @@ export default class CheckboxPrompt extends Base {
}

onAllKey() {
const shouldBeChecked = Boolean(
this.opt.choices.find((choice) => choice.type !== 'separator' && !choice.checked),
const shouldBeChecked = this.opt.choices.some(
(choice) => choice.type !== 'separator' && !choice.checked,
);

this.opt.choices.forEach((choice) => {
Expand Down

0 comments on commit b5428e9

Please sign in to comment.