Skip to content

Commit

Permalink
Chore: Clean anti-patterns of early returns in long functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Oct 8, 2023
1 parent ba042f9 commit f6a3c50
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
27 changes: 16 additions & 11 deletions packages/checkbox/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ export default createPrompt(
setStatus('done');
done(items.filter(isChecked).map((choice) => choice.value));
} else if (isUpKey(key) || isDownKey(key)) {
if (!loop && active === bounds.first && isUpKey(key)) return;
if (!loop && active === bounds.last && isDownKey(key)) return;
const offset = isUpKey(key) ? -1 : 1;
let next = active;
do {
next = (next + offset + items.length) % items.length;
} while (!isSelectable(items[next]!));
setActive(next);
if (
loop ||
(isUpKey(key) && active !== bounds.first) ||
(isDownKey(key) && active !== bounds.last)
) {
const offset = isUpKey(key) ? -1 : 1;
let next = active;
do {
next = (next + offset + items.length) % items.length;
} while (!isSelectable(items[next]!));
setActive(next);
}
} else if (isSpaceKey(key)) {
setShowHelpTip(false);
setItems(items.map((choice, i) => (i === active ? toggle(choice) : choice)));
Expand All @@ -124,9 +128,10 @@ export default createPrompt(
// Adjust index to start at 1
const position = Number(key.name) - 1;
const item = items[position];
if (item == null || !isSelectable(item)) return;
setActive(position);
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice)));
if (item != null && isSelectable(item)) {
setActive(position);
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice)));
}
}
});

Expand Down
12 changes: 4 additions & 8 deletions packages/core/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,12 @@ describe('createPrompt()', () => {
useKeypress((event: KeypressEvent) => {
if (isEnterKey(event)) {
done(lastKeypress);
return;
}

// Space will just trigger a re-render
if (isSpaceKey(event)) {
} else if (isSpaceKey(event)) {
// Space will just trigger a re-render
setIndex(index + 1);
return;
} else {
setLastKeypress(event.name);
}

setLastKeypress(event.name);
});

return `${config.message} ${displayKeypress}`;
Expand Down
6 changes: 3 additions & 3 deletions packages/inquirer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ A question object is a `hash` containing question related values:
if (typeof input !== 'number') {
// Pass the return value in the done callback
done('You need to provide a number');
return;
} else {
// Pass the return value in the done callback
done(null, true);
}
// Pass the return value in the done callback
done(null, true);
}, 3000);
}
}
Expand Down
25 changes: 15 additions & 10 deletions packages/select/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,24 @@ export default createPrompt(
setStatus('done');
done(selectedChoice.value);
} else if (isUpKey(key) || isDownKey(key)) {
if (!loop && active === bounds.first && isUpKey(key)) return;
if (!loop && active === bounds.last && isDownKey(key)) return;
const offset = isUpKey(key) ? -1 : 1;
let next = active;
do {
next = (next + offset + items.length) % items.length;
} while (!isSelectable(items[next]!));
setActive(next);
if (
loop ||
(isUpKey(key) && active !== bounds.first) ||
(isDownKey(key) && active !== bounds.last)
) {
const offset = isUpKey(key) ? -1 : 1;
let next = active;
do {
next = (next + offset + items.length) % items.length;
} while (!isSelectable(items[next]!));
setActive(next);
}
} else if (isNumberKey(key)) {
const position = Number(key.name) - 1;
const item = items[position];
if (item == null || !isSelectable(item)) return;
setActive(position);
if (item != null && isSelectable(item)) {
setActive(position);
}
}
});

Expand Down

0 comments on commit f6a3c50

Please sign in to comment.