From 965254809f0be8921bbf5c877303a2799960a73e Mon Sep 17 00:00:00 2001 From: LitoMore Date: Sun, 4 Jun 2023 02:50:48 +0800 Subject: [PATCH] Fix (Confirm): Set every other string fallback to default value (#1240) --- packages/confirm/src/index.mts | 4 +- packages/inquirer/lib/prompts/confirm.js | 7 ++-- .../test/specs/prompts/confirm.test.js | 38 ++++++++++++++++++- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/confirm/src/index.mts b/packages/confirm/src/index.mts index 0ed5ba046..d69f68cc4 100644 --- a/packages/confirm/src/index.mts +++ b/packages/confirm/src/index.mts @@ -22,7 +22,9 @@ export default createPrompt((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 { diff --git a/packages/inquirer/lib/prompts/confirm.js b/packages/inquirer/lib/prompts/confirm.js index 226ce32b8..ede22193d 100644 --- a/packages/inquirer/lib/prompts/confirm.js +++ b/packages/inquirer/lib/prompts/confirm.js @@ -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; }, }); diff --git a/packages/inquirer/test/specs/prompts/confirm.test.js b/packages/inquirer/test/specs/prompts/confirm.test.js index f60d17a7f..2c551c986 100644 --- a/packages/inquirer/test/specs/prompts/confirm.test.js +++ b/packages/inquirer/test/specs/prompts/confirm.test.js @@ -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) => { @@ -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(); });