koboprice/lib/convertPrice.js

52 lines
1,014 B
JavaScript
Raw Permalink Normal View History

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;
}
case "try": {
price = price.replace(".", "").replace(",", ".");
break;
}
2024-07-15 14:32:15 +03:00
case "clp":
case "cop":
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(),
};
};