1 | ;
|
2 | /**
|
3 | * `input` type prompt
|
4 | */
|
5 |
|
6 | const Input = require('./input');
|
7 |
|
8 | /**
|
9 | * Extention of the Input prompt specifically for use with number inputs.
|
10 | */
|
11 |
|
12 | class NumberPrompt extends Input {
|
13 | filterInput(input) {
|
14 | if (input && typeof input === 'string') {
|
15 | input = input.trim();
|
16 | // Match a number in the input
|
17 | const numberMatch = input.match(/(^-?\d+|^\d+\.\d*|^\d*\.\d+)(e\d+)?$/);
|
18 | // If a number is found, return that input.
|
19 | if (numberMatch) {
|
20 | return Number(numberMatch[0]);
|
21 | }
|
22 | }
|
23 |
|
24 | // If the input was invalid return the default value.
|
25 | return this.opt.default == null ? NaN : this.opt.default;
|
26 | }
|
27 | }
|
28 |
|
29 | module.exports = NumberPrompt;
|