From 505b3c75b3ddc56df53439ed2c501daf621ca776 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Mon, 5 Jun 2023 14:19:38 +0200 Subject: [PATCH] fallback argument in convertUnit Fixes #70 --- analysis.js | 1 + stores/hofer.js | 11 +++++------ stores/spar.js | 17 +++++++++-------- stores/utils.js | 9 +++++++-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/analysis.js b/analysis.js index 817cb75..46a547c 100644 --- a/analysis.js +++ b/analysis.js @@ -200,6 +200,7 @@ exports.updateData = async function (dataDir, done) { try { const storeItems = await stores[store].fetchData(); writeJSON(`${dataDir}/${store}-${today}.json`, storeItems, FILE_COMPRESSOR); + // const storeItems = readJSON(`${dataDir}/${store}-${today}.json.${FILE_COMPRESSOR}`); const storeItemsCanonical = getCanonicalFor(store, storeItems, today); console.log(`Fetched ${store.toUpperCase()} data, took ${(performance.now() - start) / 1000} seconds`); resolve(storeItemsCanonical); diff --git a/stores/hofer.js b/stores/hofer.js index 986f07f..7967d1b 100644 --- a/stores/hofer.js +++ b/stores/hofer.js @@ -12,11 +12,6 @@ exports.getCanonical = function (item, today) { // try to read quantity and unit from product name const name = item.ProductName; let [quantity, unit] = utils.parseUnitAndQuantityAtEnd(name); - if (units[unit] === undefined) { - // fallback: use given quantity and unit (including packaging) - quantity = item.Unit; - unit = item.UnitType; - } return utils.convertUnit( { @@ -31,7 +26,11 @@ exports.getCanonical = function (item, today) { url: `${item.CategorySEOName}/${item.SEOName}`, }, units, - "hofer" + "hofer", + { + quantity: item.Unit, + unit: item.UnitType, + } ); }; diff --git a/stores/spar.js b/stores/spar.js index 057ab4e..7e71155 100644 --- a/stores/spar.js +++ b/stores/spar.js @@ -28,13 +28,13 @@ exports.getCanonical = function (item, today) { quantity = q[0]; unit = q[1]; } - if (!(unit in units) && !(unit in utils.globalUnits)) { - // use price per unit to calculate quantity (less accurate) - let [unitPrice, unit_] = item.masterValues["price-per-unit"].split("/"); - unitPrice = parseFloat(unitPrice.replace("€", "")); - quantity = parseFloat((price / unitPrice).toFixed(3)); - unit = unit_.toLowerCase(); - } + + let [unitPrice, unit_] = item.masterValues["price-per-unit"].split("/"); + unitPrice = parseFloat(unitPrice.replace("€", "")); + const fallback = { + quantity: parseFloat((price / unitPrice).toFixed(3)), + unit: unit_.toLowerCase(), + }; return utils.convertUnit( { @@ -50,7 +50,8 @@ exports.getCanonical = function (item, today) { url: item.masterValues.url, }, units, - "spar" + "spar", + fallback ); }; diff --git a/stores/utils.js b/stores/utils.js index 6f536b2..43e9095 100644 --- a/stores/utils.js +++ b/stores/utils.js @@ -24,7 +24,7 @@ exports.globalUnits = { wg: { unit: "wg", factor: 1 }, }; -exports.convertUnit = function (item, units, store) { +exports.convertUnit = function (item, units, store, fallback) { if (typeof item.quantity == "string") item.quantity = parseFloat(item.quantity.replace(",", ".")); let unit = item.unit; @@ -32,7 +32,12 @@ exports.convertUnit = function (item, units, store) { const conv = unit in exports.globalUnits ? exports.globalUnits[unit] : units[unit]; if (conv === undefined) { - console.error(`Unknown unit in ${store}: '${unit}' in item ${item.name}`); + if (fallback) { + item.quantity = fallback.quantity; + item.unit = fallback.unit; + } else { + console.error(`Unknown unit in ${store}: '${unit}' in item ${item.name}`); + } return item; }