forked from vuejs-tips/v-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
54 lines (45 loc) · 1.44 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function toStr(value) {
return value ? value.toString() : '';
}
function onlyNumbers(input) {
return toStr(input).replace(/\D+/g, '') || '0';
}
function between(min, n, max) {
return Math.max(min, Math.min(n, max));
}
function fixed(precision) {
return between(0, precision, 20);
}
function numbersToCurrency(numbers, precision) {
const exp = 10 ** precision;
const float = parseFloat(numbers) / exp;
return float.toFixed(fixed(precision));
}
function addThousandSeparator(integer, separator) {
return integer.replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${separator}`);
}
function joinIntegerAndDecimal(integer, decimal, separator) {
return decimal ? integer + separator + decimal : integer;
}
function format(input = 0, opt) {
const value = typeof input === 'number' ? input.toFixed(fixed(opt.precision)) : (input || '0');
const numbers = onlyNumbers(value);
const currency = numbersToCurrency(numbers, opt.precision);
const parts = toStr(currency).split('.');
const [integerDirty, decimal] = parts;
const integer = addThousandSeparator(integerDirty, opt.thousands);
return [
opt.prefix,
joinIntegerAndDecimal(integer, decimal, opt.decimal)
].join('');
}
function setCursor(el, position) {
function setSelectionRange() {
el.setSelectionRange(position, position);
}
if (el === document.activeElement) {
setSelectionRange();
setTimeout(setSelectionRange, 1); // Android Fix
}
}
export { format, setCursor };