Skip to content

Commit

Permalink
Support negative float numbers (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe-BP committed Apr 26, 2022
1 parent b8c2aa0 commit 4f8ebd4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/inquirer/lib/prompts/number.js
Expand Up @@ -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]);
Expand Down
13 changes: 11 additions & 2 deletions packages/inquirer/test/specs/prompts/number.js
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 4f8ebd4

Please sign in to comment.