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