nyc-bookstores/index.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

import { load } from "cheerio";
import { readFile, writeFile } from "fs";
import stores from "./stores.json" assert { type: "json" };
2023-03-07 22:56:58 +00:00
function TableViewTemplate(rows) {
let table = "<table>";
2023-03-07 22:56:58 +00:00
rows.forEach((row, key) => {
row.rowNumber = key;
table = table + TableRowTemplate(row);
});
return table + "</table>";
}
function TableRowTemplate({ rowNumber, name, address, city }) {
return `
<tr id="${rowNumber}" class="spotRow">
<td class="name">${name}</td>
<td>${address}, ${city}</td>
</tr>`;
}
readFile("./index.html", function (err, data) {
if (err) {
throw err;
}
const $ = load(data);
stores.sort(function (a, b) {
var aname = a.name.toLowerCase();
var bname = b.name.toLowerCase();
return aname === bname ? 0 : +(aname > bname) || -1;
});
2023-03-07 22:56:58 +00:00
$("#Stores").html(TableViewTemplate(stores));
$("#storeCount").html(stores.length);
2022-03-19 20:28:26 +00:00
$("#updatedOn").html(
new Date().toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
})
);
const cssurl = $("link[type='text/css']").attr("href").split("?")[0];
2022-03-20 14:43:25 +00:00
$("link[type='text/css']").attr("href", cssurl + "?" + new Date().getTime());
writeFile("./index.html", $.html(), (err) => {
if (err) throw err;
console.log("Default view updated.");
});
});