number.js 668 B

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