Skip to content

Commit 6a172e6

Browse files
committed
updated number format class
1 parent c2e4c99 commit 6a172e6

7 files changed

+344
-317
lines changed

src/number-format.js

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,144 @@
1-
import options from "./options";
1+
import options from './options'
22

33
/**
44
* Number format function
55
* @param {Object} options
66
*/
77
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
1212

1313
this.isNull = (input = this.input) =>
14-
!this.numberOnly(input, new RegExp("[^0-9]+", "gi"));
14+
!this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
1515

1616
this.clean = (clean = false) => {
17-
this.isClean = clean;
18-
return this;
19-
};
17+
this.isClean = clean
18+
return this
19+
}
2020

2121
this.sign = () => {
2222
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+
}
2828

2929
function between(min, n, max) {
30-
return Math.max(min, Math.min(n, max));
30+
return Math.max(min, Math.min(n, max))
3131
}
3232

3333
// Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
3434
function fixed(precision) {
35-
return between(0, precision, 20);
35+
return between(0, precision, 20)
3636
}
3737

3838
function toFixed(numbers, precision) {
3939
// 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))
4343
}
4444

45-
this.toNumber = (string) => Number(string);
45+
this.toNumber = (string) => Number(string)
4646

47-
this.numberOnly = (string, regExp) => string.toString().replace(regExp, "");
47+
this.numberOnly = (string, regExp) => string.toString().replace(regExp, '')
4848

49-
this.isNegative = this.sign() === "-";
49+
this.isNegative = this.sign() === '-'
5050

5151
this.numbers = () => {
5252
if (this.options.reverseFill) {
5353
this.number = toFixed(
5454
this.numberOnly(this.input, /\D+/g),
5555
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') {
5858
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)
6262
} else {
6363
this.number = this.numberOnly(
6464
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)
6868
}
69-
return this.number;
70-
};
69+
return this.number
70+
}
7171

7272
this.realNumber = () => {
73-
return this.numbers().toString().replace(this.options.decimal, ".");
74-
};
73+
return this.numbers().toString().replace(this.options.decimal, '.')
74+
}
7575

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
7979

8080
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)
8383
}
8484

8585
if (this.isClean) {
86-
const newNumber = this.toNumber(parts.join(".")).toFixed(
86+
const newNumber = this.toNumber(parts.join('.')).toFixed(
8787
this.options.precision
88-
);
89-
const cleanNumber = this.toNumber(newNumber);
88+
)
89+
const cleanNumber = this.toNumber(newNumber)
9090
const minimumDigits = cleanNumber.toFixed(
9191
this.options.minimumFractionDigits
92-
);
92+
)
9393

9494
if (
9595
this.options.minimumFractionDigits &&
9696
this.options.minimumFractionDigits >= 0 &&
9797
cleanNumber.toString().length < minimumDigits.length
9898
) {
99-
parts = minimumDigits.toString().split(".");
99+
parts = minimumDigits.toString().split('.')
100100
} else {
101-
parts = cleanNumber.toString().split(".");
101+
parts = cleanNumber.toString().split('.')
102102
}
103103
}
104104

105-
return parts.slice(0, 2);
106-
};
105+
return parts.slice(0, 2)
106+
}
107107

108108
this.addSeparator = () => {
109-
var parts = this.numbers().split(this.options.decimal);
109+
var parts = this.numbers().split(this.options.decimal)
110110
parts[0] = parts[0]
111111
.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+
}
115115

116116
/**
117117
* Format the input with default config if there is no constructor config
118118
* @param {Number, String} input
119119
* @return {String}
120120
*/
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
124123
if (this.isNull() && !this.options.reverseFill)
125-
return this.options.nullValue;
124+
return this.options.nullValue
126125
return (
127126
this.sign() +
128127
this.options.prefix +
129128
this.addSeparator() +
130129
this.options.suffix
131-
);
132-
};
130+
)
131+
}
133132

134133
/**
135134
* Unformat the input with default config if there is no constructor config
136135
* @param {Number, String} input
137136
* @return {String}
138137
*/
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
142140
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+
}
146144
}

0 commit comments

Comments
 (0)