-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Adds a NumberPrompt type for convenience (#654) #663
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
/** | ||
* `input` type prompt | ||
*/ | ||
|
||
var Input = require('./input'); | ||
|
||
/** | ||
* Extention of the Input prompt specifically for use with number inputs. | ||
*/ | ||
|
||
class NumberPrompt extends Input { | ||
filterInput(input) { | ||
if (input && typeof input === 'string') { | ||
input = input.trim(); | ||
// Match a number in the input | ||
let numberMatch = input.match(/(^-?\d+|^\d+\.\d*|^\d*\.\d+)(e\d+)?$/); | ||
// If a number is found, return that input. | ||
if (numberMatch) { | ||
return Number(numberMatch[0]); | ||
} | ||
} | ||
// If the input was invalid return the default value. | ||
return this.opt.default == null ? NaN : this.opt.default; | ||
} | ||
} | ||
|
||
module.exports = NumberPrompt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
var expect = require('chai').expect; | ||
var _ = require('lodash'); | ||
var ReadlineStub = require('../../helpers/readline'); | ||
var fixtures = require('../../helpers/fixtures'); | ||
|
||
var NumberPrompt = require('../../../lib/prompts/number'); | ||
|
||
const ACCEPTABLE_ERROR = 0.001; | ||
|
||
describe('`number` prompt', function() { | ||
beforeEach(function() { | ||
this.fixture = _.clone(fixtures.number); | ||
this.rl = new ReadlineStub(); | ||
this.number = new NumberPrompt(this.fixture, this.rl); | ||
}); | ||
|
||
it('should parse the largest number', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.equal(Number.MAX_SAFE_INTEGER); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', String(Number.MAX_SAFE_INTEGER)); | ||
}); | ||
|
||
it('should parse the smallest number', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.equal(Number.MIN_SAFE_INTEGER); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', String(Number.MIN_SAFE_INTEGER)); | ||
}); | ||
|
||
it('should parse an integer', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.equal(42); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', '42'); | ||
}); | ||
|
||
it('should parse negative numbers', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.equal(-363); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', '-363'); | ||
}); | ||
|
||
it('should parse a regular 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); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', '.01264'); | ||
}); | ||
|
||
it('should parse a float with no digits after the decimal', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.be.closeTo(1234.0, ACCEPTABLE_ERROR); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', '1234.'); | ||
}); | ||
|
||
it('should parse a float with exponents', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.be.closeTo(534e12, ACCEPTABLE_ERROR); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', '534e12'); | ||
}); | ||
|
||
it('should parse any other string as NaN', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.be.NaN; // eslint-disable-line no-unused-expressions | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', 'The cat'); | ||
}); | ||
|
||
it('should parse the empty string as NaN', function(done) { | ||
this.number.run().then(answer => { | ||
expect(answer).to.be.NaN; // eslint-disable-line no-unused-expressions | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', ''); | ||
}); | ||
|
||
it('should return default value if it is set on a bad input', function(done) { | ||
this.number.opt.default = 11; | ||
this.number.run().then(answer => { | ||
expect(answer).to.equal(11); | ||
done(); | ||
}); | ||
|
||
this.rl.emit('line', ''); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better than
parseFloat
in some way?