From 5b9b968e591d9157376db32098b75aa6f91a6bb4 Mon Sep 17 00:00:00 2001 From: Felipe-BP Date: Mon, 18 Apr 2022 20:11:49 -0300 Subject: [PATCH] fix: now number prompt could accept negative floats --- packages/inquirer/lib/prompts/number.js | 2 +- packages/inquirer/test/specs/prompts/number.js | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/inquirer/lib/prompts/number.js b/packages/inquirer/lib/prompts/number.js index ccabc018a..d502ccde8 100644 --- a/packages/inquirer/lib/prompts/number.js +++ b/packages/inquirer/lib/prompts/number.js @@ -14,7 +14,7 @@ class NumberPrompt extends Input { if (input && typeof input === 'string') { input = input.trim(); // Match a number in the input - const numberMatch = input.match(/(^-?\d+|^\d+\.\d*|^\d*\.\d+)(e\d+)?$/); + const numberMatch = input.match(/(^-?\d+|^-?\d+\.\d*|^\d*\.\d+)(e\d+)?$/); // If a number is found, return that input. if (numberMatch) { return Number(numberMatch[0]); diff --git a/packages/inquirer/test/specs/prompts/number.js b/packages/inquirer/test/specs/prompts/number.js index db03fff6a..c075d1706 100644 --- a/packages/inquirer/test/specs/prompts/number.js +++ b/packages/inquirer/test/specs/prompts/number.js @@ -40,7 +40,7 @@ describe('`number` prompt', () => { this.rl.emit('line', '42'); }); - it('should parse negative numbers', function (done) { + it('should parse a negative integer', function (done) { this.number.run().then((answer) => { expect(answer).to.equal(-363); done(); @@ -49,7 +49,7 @@ describe('`number` prompt', () => { this.rl.emit('line', '-363'); }); - it('should parse a regular float', function (done) { + it('should parse a positive float', function (done) { this.number.run().then((answer) => { expect(answer).to.be.closeTo(4353.43, ACCEPTABLE_ERROR); done(); @@ -58,6 +58,15 @@ describe('`number` prompt', () => { this.rl.emit('line', '4353.43'); }); + it('should parse a negative float', function (done) { + this.number.run().then((answer) => { + expect(answer).to.be.closeTo(-4353.43, ACCEPTABLE_ERROR); + done(); + }); + + this.rl.emit('line', '-4353.43'); + }); + it('should parse a float with no digits before the decimal', function (done) { this.number.run().then((answer) => { expect(answer).to.be.closeTo(0.01264, ACCEPTABLE_ERROR);