Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

number.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const StringPrompt = require('./string');
  3. class NumberPrompt extends StringPrompt {
  4. constructor(options = {}) {
  5. super({ style: 'number', ...options });
  6. this.min = this.isValue(options.min) ? this.toNumber(options.min) : -Infinity;
  7. this.max = this.isValue(options.max) ? this.toNumber(options.max) : Infinity;
  8. this.delay = options.delay != null ? options.delay : 1000;
  9. this.float = options.float !== false;
  10. this.round = options.round === true || options.float === false;
  11. this.major = options.major || 10;
  12. this.minor = options.minor || 1;
  13. this.initial = options.initial != null ? options.initial : '';
  14. this.input = String(this.initial);
  15. this.cursor = this.input.length;
  16. this.cursorShow();
  17. }
  18. append(ch) {
  19. if (!/[-+.]/.test(ch) || (ch === '.' && this.input.includes('.'))) {
  20. return this.alert('invalid number');
  21. }
  22. return super.append(ch);
  23. }
  24. number(ch) {
  25. return super.append(ch);
  26. }
  27. next() {
  28. if (this.input && this.input !== this.initial) return this.alert();
  29. if (!this.isValue(this.initial)) return this.alert();
  30. this.input = this.initial;
  31. this.cursor = String(this.initial).length;
  32. return this.render();
  33. }
  34. up(number) {
  35. let step = number || this.minor;
  36. let num = this.toNumber(this.input);
  37. if (num > this.max + step) return this.alert();
  38. this.input = `${num + step}`;
  39. return this.render();
  40. }
  41. down(number) {
  42. let step = number || this.minor;
  43. let num = this.toNumber(this.input);
  44. if (num < this.min - step) return this.alert();
  45. this.input = `${num - step}`;
  46. return this.render();
  47. }
  48. shiftDown() {
  49. return this.down(this.major);
  50. }
  51. shiftUp() {
  52. return this.up(this.major);
  53. }
  54. format(input = this.input) {
  55. if (typeof this.options.format === 'function') {
  56. return this.options.format.call(this, input);
  57. }
  58. return this.styles.info(input);
  59. }
  60. toNumber(value = '') {
  61. return this.float ? +value : Math.round(+value);
  62. }
  63. isValue(value) {
  64. return /^[-+]?[0-9]+((\.)|(\.[0-9]+))?$/.test(value);
  65. }
  66. submit() {
  67. let value = [this.input, this.initial].find(v => this.isValue(v));
  68. this.value = this.toNumber(value || 0);
  69. return super.submit();
  70. }
  71. }
  72. module.exports = NumberPrompt;