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