|
1 | 1 | import options from './options' |
2 | 2 |
|
3 | | -function NumberFormat(input, opt = options) { |
4 | | - this.options = opt |
5 | | - this.input = input || this.options.null_value |
6 | | - |
| 3 | +function NumberFormat(opt = options) { |
| 4 | + this.options = Object.assign(options, opt) |
| 5 | + this.input = this.options.null_value |
| 6 | + this.isClean = false |
| 7 | + this.clean = () => { |
| 8 | + this.isClean = true |
| 9 | + return this |
| 10 | + } |
| 11 | + this.negative = () => { |
| 12 | + const negetive = (this.input.toString().indexOf('-') >= 0 && this.numberOnly() > 0) ? '-' : '' |
| 13 | + return negetive |
| 14 | + } |
7 | 15 | this.numbers = () => { |
8 | | - let number = this.input |
9 | 16 | if (typeof this.input === 'number') { |
10 | | - number = this.input.toFixed(this.options.precision) |
| 17 | + this.numbers = this.input.toFixed(this.options.precision) |
11 | 18 | } else { |
12 | | - number = this.numberOnly() |
| 19 | + this.numbers = this.numberOnly(this.input) |
13 | 20 | } |
14 | | - return number |
| 21 | + return this.numbers |
15 | 22 | } |
16 | | - |
17 | | - this.numberOnly = (clean = false) => { |
| 23 | + this.numberOnly = () => { |
18 | 24 | const regExp = new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi') |
19 | | - const numbers = this.input.toString().replace(regExp, '') |
20 | | - if (clean) { |
21 | | - const parts = numbers.split(this.options.decimal) |
| 25 | + this.numbers = this.input.toString().replace(regExp, '') |
| 26 | + if (this.isClean) { |
| 27 | + const parts = this.numbers.split(this.options.decimal) |
22 | 28 | return parts.length > 1 && parts[1] ? parts.join(this.options.decimal) : parts[0] |
23 | 29 | } |
24 | | - return numbers |
| 30 | + return this.numbers |
25 | 31 | } |
26 | | - |
27 | | - this.negative = (this.input.toString().indexOf('-') >= 0 && this.numbers() > 0) ? '-' : '' |
28 | | - |
29 | 32 | this.parts = () => { |
30 | 33 | const parts = this.numbers().toString().split(this.options.decimal) |
31 | | - parts[0] = this.negative + (Number(parts[0]) ? Number(parts[0]) : 0) |
| 34 | + parts[0] = this.negative() + (Number(parts[0]) ? Number(parts[0]) : 0) |
32 | 35 | if (parts.length > 1) { |
33 | 36 | parts[1] = parts[1].slice(0, this.options.precision) |
34 | 37 | } |
35 | 38 | return parts |
36 | 39 | } |
37 | | - |
38 | | - this.addSeparator = (clean) => { |
| 40 | + this.addSeparator = () => { |
39 | 41 | const parts = this.parts() |
40 | 42 | parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`) |
41 | | - if (clean) { |
| 43 | + if (this.isClean) { |
42 | 44 | return parts[1] && parts[1].length > 0 ? parts.join(this.options.decimal) : parts[0] |
43 | 45 | } |
44 | 46 | return parts.join(this.options.decimal) |
45 | 47 | } |
46 | | - |
47 | | - this.format = (clean = false) => this.options.prefix + this.addSeparator(clean) + this.options.suffix |
48 | | - |
49 | | - this.unformat = (clean = true) => this.negative + this.numberOnly(clean) |
| 48 | + this.format = (input) => { |
| 49 | + this.input = input |
| 50 | + return this.options.prefix + this.addSeparator() + this.options.suffix |
| 51 | + } |
| 52 | + this.unformat = (input) => { |
| 53 | + this.input = input |
| 54 | + return this.negative() + this.numberOnly() |
| 55 | + } |
50 | 56 | } |
51 | 57 |
|
52 | 58 | function setCursor(el, position) { |
|
0 commit comments