Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negative float numbers #1103

Merged
merged 1 commit into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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