diff --git a/site/js/misc.js b/site/js/misc.js index 8d50b2af..3b118037 100644 --- a/site/js/misc.js +++ b/site/js/misc.js @@ -1,35 +1,7 @@ const alasql = require("alasql"); +const globalUnits = require("../../stores/global-units"); const { stores, BUDGET_BRANDS } = require("../model/stores"); -const UNITS = { - "stk.": { unit: "stk", factor: 1 }, - stück: { unit: "stk", factor: 1 }, - blatt: { unit: "stk", factor: 1 }, - paar: { unit: "stk", factor: 1 }, - stk: { unit: "stk", factor: 1 }, - st: { unit: "stk", factor: 1 }, - teebeutel: { unit: "stk", factor: 1 }, - tücher: { unit: "stk", factor: 1 }, - rollen: { unit: "stk", factor: 1 }, - tabs: { unit: "stk", factor: 1 }, - mm: { unit: "cm", factor: 0.1 }, - cm: { unit: "cm", factor: 1 }, - zentimeter: { unit: "cm", factor: 1 }, - m: { unit: "cm", factor: 100 }, - meter: { unit: "cm", factor: 100 }, - g: { unit: "g", factor: 1 }, - gramm: { unit: "g", factor: 1 }, - dag: { unit: "g", factor: 10 }, - kg: { unit: "g", factor: 1000 }, - kilogramm: { unit: "g", factor: 1000 }, - ml: { unit: "ml", factor: 1 }, - milliliter: { unit: "ml", factor: 1 }, - dl: { unit: "ml", factor: 10 }, - cl: { unit: "ml", factor: 100 }, - l: { unit: "ml", factor: 1000 }, - liter: { unit: "ml", factor: 1000 }, - wg: { unit: "wg", factor: 1 }, -}; if (typeof window !== "undefined") { function setupLiveEdit() { @@ -157,7 +129,7 @@ exports.queryItems = (query, items, exactWord) => { const operators = ["<", "<=", ">", ">=", "="]; for (let i = 0; i < tokens.length; i++) { const token = tokens[i]; - let unit = UNITS[token]; + let unit = globalUnits.globalUnits[token]; if (unit && i > 0 && /^\d+(\.\d+)?$/.test(tokens[i - 1])) { newTokens.pop(); let operator = "="; diff --git a/stores/global-units.js b/stores/global-units.js new file mode 100644 index 00000000..08f3a3be --- /dev/null +++ b/stores/global-units.js @@ -0,0 +1,31 @@ +exports.globalUnits = { + "stk.": { unit: "stk", factor: 1 }, + blatt: { unit: "stk", factor: 1 }, + paar: { unit: "stk", factor: 1 }, + stk: { unit: "stk", factor: 1 }, + st: { unit: "stk", factor: 1 }, + teebeutel: { unit: "stk", factor: 1 }, + tücher: { unit: "stk", factor: 1 }, + rollen: { unit: "stk", factor: 1 }, + tabs: { unit: "stk", factor: 1 }, + stück: { unit: "stk", factor: 1 }, + mm: { unit: "cm", factor: 0.1 }, + cm: { unit: "cm", factor: 1 }, + zentimeter: { unit: "cm", factor: 1 }, + m: { unit: "cm", factor: 100 }, + meter: { unit: "cm", factor: 100 }, + g: { unit: "g", factor: 1 }, + gr: { unit: "g", factor: 1 }, + gramm: { unit: "g", factor: 1 }, + dag: { unit: "g", factor: 10 }, + kg: { unit: "g", factor: 1000 }, + kilogramm: { unit: "g", factor: 1000 }, + ml: { unit: "ml", factor: 1 }, + milliliter: { unit: "ml", factor: 1 }, + dl: { unit: "ml", factor: 10 }, + cl: { unit: "ml", factor: 100 }, + l: { unit: "ml", factor: 1000 }, + lt: { unit: "ml", factor: 1000 }, + liter: { unit: "ml", factor: 1000 }, + wg: { unit: "wg", factor: 1 }, +}; diff --git a/stores/utils.js b/stores/utils.js index 81381620..2001884b 100644 --- a/stores/utils.js +++ b/stores/utils.js @@ -1,5 +1,6 @@ const fs = require("fs"); const path = require("path"); +const globalUnits = require("./global-units"); exports.mergeAndSaveCategories = (store, categories) => { const mappingFile = path.join(__dirname, `${store}-categories.json`); @@ -32,45 +33,13 @@ exports.mergeAndSaveCategories = (store, categories) => { return categories; }; -exports.globalUnits = { - "stk.": { unit: "stk", factor: 1 }, - blatt: { unit: "stk", factor: 1 }, - paar: { unit: "stk", factor: 1 }, - stk: { unit: "stk", factor: 1 }, - st: { unit: "stk", factor: 1 }, - teebeutel: { unit: "stk", factor: 1 }, - tücher: { unit: "stk", factor: 1 }, - rollen: { unit: "stk", factor: 1 }, - tabs: { unit: "stk", factor: 1 }, - stück: { unit: "stk", factor: 1 }, - mm: { unit: "cm", factor: 0.1 }, - cm: { unit: "cm", factor: 1 }, - zentimeter: { unit: "cm", factor: 1 }, - m: { unit: "cm", factor: 100 }, - meter: { unit: "cm", factor: 100 }, - g: { unit: "g", factor: 1 }, - gr: { unit: "g", factor: 1 }, - gramm: { unit: "g", factor: 1 }, - dag: { unit: "g", factor: 10 }, - kg: { unit: "g", factor: 1000 }, - kilogramm: { unit: "g", factor: 1000 }, - ml: { unit: "ml", factor: 1 }, - milliliter: { unit: "ml", factor: 1 }, - dl: { unit: "ml", factor: 10 }, - cl: { unit: "ml", factor: 100 }, - l: { unit: "ml", factor: 1000 }, - lt: { unit: "ml", factor: 1000 }, - liter: { unit: "ml", factor: 1000 }, - wg: { unit: "wg", factor: 1 }, -}; - exports.convertUnit = function (item, units, store, fallback) { if (typeof item.quantity == "string") item.quantity = parseFloat(item.quantity.replace(",", ".")); let unit = item.unit; if (typeof unit === "string") unit = unit.toLowerCase(); - const conv = unit in exports.globalUnits ? exports.globalUnits[unit] : units[unit]; + const conv = unit in globalUnits.globalUnits ? globalUnits.globalUnits[unit] : units[unit]; if (conv === undefined) { if (fallback) { item.quantity = fallback.quantity;