39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
exports.convertUnit = function (item, units, store) {
|
|
if (!(item.unit in units)) {
|
|
console.error(`Unknown unit in ${store}: '${item.unit}' in item ${item.name}`);
|
|
return item;
|
|
}
|
|
|
|
if (typeof item.quantity == "string") item.quantity = parseFloat(item.quantity.replace(",", "."));
|
|
|
|
const conv = units[item.unit];
|
|
item.quantity = conv.factor * item.quantity;
|
|
item.unit = conv.unit;
|
|
|
|
if (item.isWeighted && (item.unit == "g" || item.unit == "ml")) {
|
|
item.price = (100 * item.price) / item.quantity;
|
|
item.quantity = 100;
|
|
}
|
|
return item;
|
|
};
|
|
|
|
exports.parseUnitAndQuantityAtEnd = function (name) {
|
|
let unit,
|
|
quantity = 1;
|
|
const nameTokens = name.trim().replaceAll("(", "").replaceAll(")", "").replaceAll(",", ".").split(" ");
|
|
const lastToken = nameTokens[nameTokens.length - 1];
|
|
const secondLastToken = nameTokens.length >= 2 ? nameTokens[nameTokens.length - 2] : null;
|
|
|
|
const token = parseFloat(lastToken) ? lastToken : secondLastToken + lastToken;
|
|
const regex = /^([0-9.x]+)(.*)$/;
|
|
const matches = token.match(regex);
|
|
if (matches) {
|
|
matches[1].split("x").forEach((q) => {
|
|
quantity = quantity * parseFloat(q);
|
|
});
|
|
unit = matches[2];
|
|
return [quantity, unit.toLowerCase()];
|
|
}
|
|
return [undefined, undefined];
|
|
};
|