.js clean-up, add second pattern to template "engine" so we can also bundle .js files eventually. Not great for development, as line numbers no longer match.

This commit is contained in:
Mario Zechner 2023-06-04 03:30:05 +02:00
parent 21e7566cfa
commit 665b2b5c25
6 changed files with 40 additions and 37 deletions

View File

@ -45,7 +45,7 @@ function showResults(items, today) {
const storeCheckboxes = STORE_KEYS.map((store) => document.querySelector(`#${store}`));
const checkedStores = STORE_KEYS.filter((store, i) => storeCheckboxes[i].checked);
let changedItems = [];
for (item of items) {
for (const item of items) {
if (item.priceHistory.length < 2) continue;
for (let i = 0; i < item.priceHistory.length; i++) {
@ -62,7 +62,7 @@ function showResults(items, today) {
if (query.length >= 3)
changedItems = searchItems(changedItems, document.querySelector("#filter").value, checkedStores, false, 0, 10000, false, false);
document.querySelector("#numresults").innerText = "Resultate: " + changedItems.length + (total > changedItems.length ? " / " + total : "");
if (items.length > 0) return;
if (changedItems.length == 0) return;
const table = document.querySelector("#result");
table.innerHTML = "";
const header = dom(
@ -81,11 +81,13 @@ function showResults(items, today) {
table.appendChild(header);
for (item of changedItems) {
for (let item of changedItems) {
item = JSON.parse(JSON.stringify(item));
const itemDom = itemToDOM(item);
table.appendChild(itemDom);
}
table.classList.remove("hide");
}
load();

View File

@ -34,7 +34,7 @@ function showResults(items, _today) {
const storeCheckboxes = STORE_KEYS.map((store) => document.querySelector(`#${store}`));
const checkedStores = STORE_KEYS.filter((_store, i) => storeCheckboxes[i].checked);
let changedItems = [];
for (item of items) {
for (const item of items) {
if (item.priceHistory.length < 2) continue;
if (!checkedStores.includes(item.store)) continue;
@ -65,7 +65,7 @@ function showResults(items, _today) {
table.appendChild(header);
for (item of changedItems) {
for (let item of changedItems) {
item = JSON.parse(JSON.stringify(item));
const itemDom = itemToDOM(item);
table.appendChild(itemDom);

View File

@ -3,15 +3,15 @@ shoppingCarts.load();
async function load() {
const items = await loadItems();
lookup = {};
for (item of items) {
const lookup = {};
for (const item of items) {
lookup[item.store + item.id] = item;
}
// Update carts with latest price info
for (cart of shoppingCarts.carts) {
for (const cart of shoppingCarts.carts) {
const items = [];
for (cartItem of cart.items) {
for (const cartItem of cart.items) {
const item = lookup[cartItem.store + cartItem.id];
if (!item) continue;
items.push(item);
@ -60,7 +60,7 @@ async function load() {
const importedCarts = JSON.parse(contents);
for (const importedCart of importedCarts) {
const items = [];
for (cartItem of importedCart.items) {
for (const cartItem of importedCart.items) {
const item = lookup[cartItem.store + cartItem.id];
if (!item) continue;
items.push(item);
@ -108,7 +108,7 @@ function showCarts(lookup) {
let oldPrice = 0;
let currPrice = 0;
let link = encodeURIComponent(cart.name) + ";";
for (cartItem of cart.items) {
for (const cartItem of cart.items) {
const item = lookup[cartItem.store + cartItem.id];
if (!item) continue;
oldPrice += item.priceHistory[item.priceHistory.length - 1].price;

View File

@ -20,15 +20,15 @@ async function load() {
});
const dates = {};
for (item of items) {
for (price of item.priceHistory) {
for (const item of items) {
for (const price of item.priceHistory) {
dates[price.date] = dates[price.date] ? dates[price.date] + 1 : 1;
}
}
const dateNames = Object.keys(dates).sort((a, b) => b.localeCompare(a));
const dateSelection = document.querySelector("#dates");
for (dateName of dateNames) {
for (const dateName of dateNames) {
const option = dom("option", dateName + " (" + dates[dateName] + ")");
option.setAttribute("value", dateName);
dateSelection.appendChild(option);
@ -52,7 +52,7 @@ function showResults(items, today) {
const decreases = document.querySelector("#decreases").checked;
const fullHistory = document.querySelector("#fullhistory").checked;
const changedItems = [];
for (item of items) {
for (const item of items) {
if (item.priceHistory.length < 2) continue;
for (let i = 0; i < item.priceHistory.length; i++) {
@ -81,7 +81,7 @@ function showResults(items, today) {
table.appendChild(header);
for (item of changedItems) {
for (let item of changedItems) {
item = JSON.parse(JSON.stringify(item));
if (!fullHistory) {
let priceHistory = [];
@ -94,5 +94,4 @@ function showResults(items, today) {
}
document.querySelector("#results").innerText = "Resultate: " + changedItems.length;
}
load();

View File

@ -1068,10 +1068,10 @@ function setupLiveEdit() {
script.type = "text/javascript";
script.onload = () => {
let lastChangeTimestamp = null;
this.socket = io({ transports: ["websocket"] });
this.socket.on("connect", () => console.log("Connected"));
this.socket.on("disconnect", () => console.log("Disconnected"));
this.socket.on("message", (timestamp) => {
let socket = io({ transports: ["websocket"] });
socket.on("connect", () => console.log("Connected"));
socket.on("disconnect", () => console.log("Disconnected"));
socket.on("message", (timestamp) => {
if (lastChangeTimestamp != timestamp) {
setTimeout(() => location.reload(), 100);
lastChangeTimestamp = timestamp;

View File

@ -15,26 +15,28 @@ function deleteDirectory(directory) {
}
}
function replaceFileContents(string, fileDir) {
const pattern = /%%([^%]+)%%|\/\/\s*include\s*"([^"]+)"/g;
return string.replace(pattern, (_, filename1, filename2) => {
const filename = filename1 || filename2;
const filenamePath = path.join(fileDir, filename);
try {
const data = fs.readFileSync(filenamePath, "utf8");
const replacedData = replaceFileContents(data, path.dirname(filenamePath));
return replacedData;
} catch (error) {
console.error(`Error reading file "${filenamePath}":`, error);
return "";
}
});
}
function processFile(inputFile, outputFile) {
console.log(`${inputFile} -> ${outputFile}`);
const fileDir = path.dirname(inputFile);
const data = fs.readFileSync(inputFile, "utf8");
const regex = /%%([^%]+)%%/g;
let result;
let replacedData = data;
while ((result = regex.exec(data))) {
const match = result[0];
const filename = result[1];
const filenamePath = path.join(fileDir, filename);
try {
const fileContents = fs.readFileSync(filenamePath, "utf8");
replacedData = replacedData.replace(match, fileContents);
} catch (error) {
console.error(`Error reading file '${filenamePath}': ${error}`);
}
}
const replacedData = replaceFileContents(data, fileDir);
fs.writeFileSync(outputFile, replacedData);
}