Skip to content

Commit

Permalink
Fix (Confirm): Set every other string fallback to default value (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore authored Jun 3, 2023
1 parent a9a0039 commit 9652548
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/confirm/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default createPrompt<boolean, ConfirmConfig>((config, done) => {

useKeypress((key, rl) => {
if (isEnterKey(key)) {
const answer = value ? /^y(es)?/i.test(value) : config.default !== false;
let answer = config.default !== false;
if (/^(y|yes)?/i.test(value)) answer = true;
else if (/^(n|no)?/i.test(value)) answer = false;
if (typeof config.transformer === 'function') {
setValue(config.transformer(answer));
} else {
Expand Down
7 changes: 3 additions & 4 deletions packages/inquirer/lib/prompts/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ export default class ConfirmPrompt extends Base {

Object.assign(this.opt, {
filter(input) {
let value = rawDefault;
if (input != null && input !== '') {
value = /^y(es)?/i.test(input);
if (/^y(es)?/i.test(input)) return true;
if (/^n(o)?/i.test(input)) return false;
}

return value;
return rawDefault;
},
});

Expand Down
38 changes: 37 additions & 1 deletion packages/inquirer/test/specs/prompts/confirm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe('`confirm` prompt', () => {
rl.emit('line', 'Yes');
}));

it("should parse 'N' value to boolean false", () =>
new Promise((done) => {
confirm.run().then((answer) => {
expect(answer).toEqual(false);
done();
});

rl.emit('line', 'N');
}));

it("should parse 'No' value to boolean false", () =>
new Promise((done) => {
confirm.run().then((answer) => {
Expand All @@ -85,9 +95,35 @@ describe('`confirm` prompt', () => {
rl.emit('line', 'No');
}));

it('should parse every other string value to boolean false', () =>
it('should parse every other string value to default (unset)', () =>
new Promise((done) => {
confirm.run().then((answer) => {
expect(answer).toEqual(true);
done();
});

rl.emit('line', 'bla bla foo');
}));

it('should parse every other string value to default (true)', () =>
new Promise((done) => {
fixture.default = true;
const trueConfirm = new Confirm(fixture, rl);

trueConfirm.run().then((answer) => {
expect(answer).toEqual(true);
done();
});

rl.emit('line', 'bla bla foo');
}));

it('should parse every other string value to default (false)', () =>
new Promise((done) => {
fixture.default = false;
const falseConfirm = new Confirm(fixture, rl);

falseConfirm.run().then((answer) => {
expect(answer).toEqual(false);
done();
});
Expand Down

0 comments on commit 9652548

Please sign in to comment.