2024-07-15 14:32:15 +03:00
|
|
|
import currency from "currency.js";
|
|
|
|
|
|
|
|
export const convertPrice = async (price, curr, rate) => {
|
|
|
|
l("rate for", curr, "is", rate);
|
|
|
|
|
|
|
|
let useVedic = false;
|
|
|
|
|
|
|
|
switch (curr) {
|
|
|
|
case "inr":
|
|
|
|
case "php": {
|
|
|
|
useVedic = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "jpy": {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "zar": {
|
|
|
|
price = price.replace(",", ".");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "dkk": {
|
|
|
|
price = price.replace("kr.", "").replace(",", ".");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "pen": {
|
|
|
|
price = price.replace("S/.", "");
|
|
|
|
break;
|
|
|
|
}
|
2024-07-17 21:01:52 +03:00
|
|
|
case "try": {
|
|
|
|
price = price.replace(".", "").replace(",", ".");
|
|
|
|
break;
|
|
|
|
}
|
2024-07-15 14:32:15 +03:00
|
|
|
case "clp":
|
|
|
|
case "cop":
|
2024-07-17 21:01:52 +03:00
|
|
|
case "twd":
|
|
|
|
case "mxn": {
|
2024-07-15 14:32:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
price = price.replace(/[^\d,.-]/, "").replace(",", ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
const convertedPrice = currency(price, { useVedic }).divide(rate);
|
|
|
|
|
|
|
|
l("converted price for", curr, convertedPrice);
|
|
|
|
|
|
|
|
return {
|
|
|
|
intValue: convertedPrice.intValue,
|
|
|
|
formatted: convertedPrice.format(),
|
|
|
|
};
|
|
|
|
};
|