|
1 | | -import options from "./options"; |
| 1 | +import options from './options' |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Number format function |
5 | 5 | * @param {Object} options |
6 | 6 | */ |
7 | 7 | export default function NumberFormat(config = options) { |
8 | | - this.options = Object.assign(options, config); |
9 | | - this.input = ""; |
10 | | - this.number = ""; |
11 | | - this.isClean = false; |
| 8 | + this.options = Object.assign(options, config) |
| 9 | + this.input = '' |
| 10 | + this.number = '' |
| 11 | + this.isClean = false |
12 | 12 |
|
13 | 13 | this.isNull = (input = this.input) => |
14 | | - !this.numberOnly(input, new RegExp("[^0-9]+", "gi")); |
| 14 | + !this.numberOnly(input, new RegExp('[^0-9]+', 'gi')) |
15 | 15 |
|
16 | 16 | this.clean = (clean = false) => { |
17 | | - this.isClean = clean; |
18 | | - return this; |
19 | | - }; |
| 17 | + this.isClean = clean |
| 18 | + return this |
| 19 | + } |
20 | 20 |
|
21 | 21 | this.sign = () => { |
22 | 22 | const sign = |
23 | | - this.input.toString().indexOf("-") >= 0 && this.realNumber() > 0 |
24 | | - ? "-" |
25 | | - : ""; |
26 | | - return sign; |
27 | | - }; |
| 23 | + this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0 |
| 24 | + ? '-' |
| 25 | + : '' |
| 26 | + return sign |
| 27 | + } |
28 | 28 |
|
29 | 29 | function between(min, n, max) { |
30 | | - return Math.max(min, Math.min(n, max)); |
| 30 | + return Math.max(min, Math.min(n, max)) |
31 | 31 | } |
32 | 32 |
|
33 | 33 | // Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed |
34 | 34 | function fixed(precision) { |
35 | | - return between(0, precision, 20); |
| 35 | + return between(0, precision, 20) |
36 | 36 | } |
37 | 37 |
|
38 | 38 | function toFixed(numbers, precision) { |
39 | 39 | // eslint-disable-next-line no-restricted-properties |
40 | | - var exp = Math.pow(10, precision); |
41 | | - var float = parseFloat(numbers) / exp || 0; |
42 | | - return float.toFixed(fixed(precision)); |
| 40 | + var exp = Math.pow(10, precision) |
| 41 | + var float = parseFloat(numbers) / exp || 0 |
| 42 | + return float.toFixed(fixed(precision)) |
43 | 43 | } |
44 | 44 |
|
45 | | - this.toNumber = (string) => Number(string); |
| 45 | + this.toNumber = (string) => Number(string) |
46 | 46 |
|
47 | | - this.numberOnly = (string, regExp) => string.toString().replace(regExp, ""); |
| 47 | + this.numberOnly = (string, regExp) => string.toString().replace(regExp, '') |
48 | 48 |
|
49 | | - this.isNegative = this.sign() === "-"; |
| 49 | + this.isNegative = this.sign() === '-' |
50 | 50 |
|
51 | 51 | this.numbers = () => { |
52 | 52 | if (this.options.reverseFill) { |
53 | 53 | this.number = toFixed( |
54 | 54 | this.numberOnly(this.input, /\D+/g), |
55 | 55 | this.options.precision |
56 | | - ).replace(".", this.options.decimal); |
57 | | - } else if (typeof this.input === "number") { |
| 56 | + ).replace('.', this.options.decimal) |
| 57 | + } else if (typeof this.input === 'number') { |
58 | 58 | this.number = this.parts( |
59 | | - this.input.toString().replace("-", ""), |
60 | | - "." |
61 | | - ).join(this.options.decimal); |
| 59 | + this.input.toString().replace('-', ''), |
| 60 | + '.' |
| 61 | + ).join(this.options.decimal) |
62 | 62 | } else { |
63 | 63 | this.number = this.numberOnly( |
64 | 64 | this.input, |
65 | | - new RegExp(`[^0-9\\${this.options.decimal}]+`, "gi") |
66 | | - ); |
67 | | - this.number = this.parts(this.number).join(this.options.decimal); |
| 65 | + new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi') |
| 66 | + ) |
| 67 | + this.number = this.parts(this.number).join(this.options.decimal) |
68 | 68 | } |
69 | | - return this.number; |
70 | | - }; |
| 69 | + return this.number |
| 70 | + } |
71 | 71 |
|
72 | 72 | this.realNumber = () => { |
73 | | - return this.numbers().toString().replace(this.options.decimal, "."); |
74 | | - }; |
| 73 | + return this.numbers().toString().replace(this.options.decimal, '.') |
| 74 | + } |
75 | 75 |
|
76 | | - this.parts = (number = "", decimal = this.options.decimal) => { |
77 | | - var parts = number.toString().split(decimal); |
78 | | - parts[0] = this.toNumber(parts[0]) || 0; |
| 76 | + this.parts = (number = '', decimal = this.options.decimal) => { |
| 77 | + var parts = number.toString().split(decimal) |
| 78 | + parts[0] = this.toNumber(parts[0]) || 0 |
79 | 79 |
|
80 | 80 | if (parts.length > 1) { |
81 | | - parts[1] = parts.slice(1, parts.length).join(""); |
82 | | - parts = parts.slice(0, 2); |
| 81 | + parts[1] = parts.slice(1, parts.length).join('') |
| 82 | + parts = parts.slice(0, 2) |
83 | 83 | } |
84 | 84 |
|
85 | 85 | if (this.isClean) { |
86 | | - const newNumber = this.toNumber(parts.join(".")).toFixed( |
| 86 | + const newNumber = this.toNumber(parts.join('.')).toFixed( |
87 | 87 | this.options.precision |
88 | | - ); |
89 | | - const cleanNumber = this.toNumber(newNumber); |
| 88 | + ) |
| 89 | + const cleanNumber = this.toNumber(newNumber) |
90 | 90 | const minimumDigits = cleanNumber.toFixed( |
91 | 91 | this.options.minimumFractionDigits |
92 | | - ); |
| 92 | + ) |
93 | 93 |
|
94 | 94 | if ( |
95 | 95 | this.options.minimumFractionDigits && |
96 | 96 | this.options.minimumFractionDigits >= 0 && |
97 | 97 | cleanNumber.toString().length < minimumDigits.length |
98 | 98 | ) { |
99 | | - parts = minimumDigits.toString().split("."); |
| 99 | + parts = minimumDigits.toString().split('.') |
100 | 100 | } else { |
101 | | - parts = cleanNumber.toString().split("."); |
| 101 | + parts = cleanNumber.toString().split('.') |
102 | 102 | } |
103 | 103 | } |
104 | 104 |
|
105 | | - return parts.slice(0, 2); |
106 | | - }; |
| 105 | + return parts.slice(0, 2) |
| 106 | + } |
107 | 107 |
|
108 | 108 | this.addSeparator = () => { |
109 | | - var parts = this.numbers().split(this.options.decimal); |
| 109 | + var parts = this.numbers().split(this.options.decimal) |
110 | 110 | parts[0] = parts[0] |
111 | 111 | .toString() |
112 | | - .replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`); |
113 | | - return parts.join(this.options.decimal); |
114 | | - }; |
| 112 | + .replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`) |
| 113 | + return parts.join(this.options.decimal) |
| 114 | + } |
115 | 115 |
|
116 | 116 | /** |
117 | 117 | * Format the input with default config if there is no constructor config |
118 | 118 | * @param {Number, String} input |
119 | 119 | * @return {String} |
120 | 120 | */ |
121 | | - this.format = (input) => { |
122 | | - if (input === "") return this.options.nullValue; |
123 | | - this.input = input || this.options.nullValue; |
| 121 | + this.format = (input = '') => { |
| 122 | + this.input = input |
124 | 123 | if (this.isNull() && !this.options.reverseFill) |
125 | | - return this.options.nullValue; |
| 124 | + return this.options.nullValue |
126 | 125 | return ( |
127 | 126 | this.sign() + |
128 | 127 | this.options.prefix + |
129 | 128 | this.addSeparator() + |
130 | 129 | this.options.suffix |
131 | | - ); |
132 | | - }; |
| 130 | + ) |
| 131 | + } |
133 | 132 |
|
134 | 133 | /** |
135 | 134 | * Unformat the input with default config if there is no constructor config |
136 | 135 | * @param {Number, String} input |
137 | 136 | * @return {String} |
138 | 137 | */ |
139 | | - this.unformat = (input) => { |
140 | | - if (input === "") return this.options.nullValue; |
141 | | - this.input = input || this.options.nullValue; |
| 138 | + this.unformat = (input = '') => { |
| 139 | + this.input = input |
142 | 140 | if (this.isNull() && !this.options.reverseFill) |
143 | | - return this.options.nullValue; |
144 | | - return this.sign() + this.realNumber(); |
145 | | - }; |
| 141 | + return this.options.nullValue |
| 142 | + return this.sign() + this.realNumber() |
| 143 | + } |
146 | 144 | } |
0 commit comments