-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordr_data.js
46 lines (42 loc) · 1.16 KB
/
ordr_data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const fn = "ordr.csv";
const fs = require("fs");
const menu = require("./menu_data.js");
const menu_is = Object.values(menu).map((v) => v.shortname);
const csv = require("csv-parse/lib/sync.js");
const stack = [];
const bumpd = [];
function writeData(stack, bumpd) {
var s = stack.concat(bumpd).map((ordr) => `${ordr.tble},${menu_is.map((n) => ordr.plte[n] || 0).join(",")}\n`);
fs.writeFileSync(fn, `tble,${menu_is.join(",")}\n${s.join("")}`, { encoding: "ascii" });
}
function appendData(ordr) {
fs.appendFileSync(fn, `${ordr.tble},${menu_is.map((n) => ordr.plte[n] || 0).join(",")}\n`);
}
if (fs.existsSync(fn))
for (let ordr of csv(
fs.readFileSync(fn, "utf-8", {
skip_empty_lines: true,
columns: true,
})
)) {
let plte = {};
for (let n of menu_is) if (ordr[n]) plte[n] = ordr[n];
(ordr.bump == "True" ? bumpd : stack).push({ tble: ordr.tble, plte: plte });
}
module.exports = {
stack: stack,
add(...ordrs) {
return stack.push(...ordrs);
},
bump(idx) {
var i = stack.splice(idx, 1)[0];
while (bumpd.length >= 13) bumpd.shift();
if (i) bumpd.push(i);
return i;
},
unbump() {
var i = bumpd.pop();
if (i) stack.unshift(i);
return i;
},
};