Last change
on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
699 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 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;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.