-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoney-formatter.js
61 lines (52 loc) · 1.74 KB
/
money-formatter.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
55
56
57
58
59
60
61
theme.Currency = (function () {
var moneyFormat = theme.moneyFormat;
function formatMoney(cents, format) {
if (typeof cents === "string") {
cents = cents.replace(".", "");
}
var value = "";
var placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
var formatString = format || moneyFormat;
function formatWithDelimiters(number, precision, thousands, decimal) {
thousands = thousands || ",";
decimal = decimal || ".";
if (isNaN(number) || number === null) {
return 0;
}
number = (number / 100.0).toFixed(precision);
var parts = number.split(".");
var dollarsAmount = parts[0].replace(
/(\d)(?=(\d\d\d)+(?!\d))/g,
"$1" + thousands
);
var centsAmount = parts[1] ? decimal + parts[1] : "";
return dollarsAmount + centsAmount;
}
switch (formatString.match(placeholderRegex)[1]) {
case "amount":
value = formatWithDelimiters(cents, 2);
break;
case "amount_no_decimals":
value = formatWithDelimiters(cents, 0);
break;
case "amount_with_comma_separator":
value = formatWithDelimiters(cents, 2, ".", ",");
break;
case "amount_no_decimals_with_comma_separator":
value = formatWithDelimiters(cents, 0, ".", ",");
break;
case "amount_no_decimals_with_space_separator":
value = formatWithDelimiters(cents, 0, " ");
break;
case "amount_with_apostrophe_separator":
value = formatWithDelimiters(cents, 2, "'");
break;
}
return formatString.replace(placeholderRegex, value);
}
return {
formatMoney: formatMoney,
};
})();
const currency = data.currency;
const price = theme.Currency.formatMoney(product.price, theme.moneyFormat);