// The restock sheet exactly as the form sent it - buyers only send fields they touched.
const lines = [
{ sku: "USB-C-1M", qty: 24, note: "Fast mover" },
{ sku: "HDMI-2M", qty: 0, note: "Overstocked in Berlin" },
{ sku: "CASE-13", note: "Ship before Friday" },
{ sku: "MOUSE-PRO", qty: 7, note: "" },
];
// Fill in only the fields the buyer left out. Right now it does more than that - fix it.
function normalize(line) {
return {
sku: line.sku,
qty: line.qty || 10,
note: line.note || "Standard restock",
};
}
// Already correct. Leave this one alone.
function totalUnits(items) {
return items.reduce((sum, line) => sum + normalize(line).qty, 0);
}
for (const line of lines.map(normalize)) {
console.log(`${line.sku} x${line.qty} ${line.note}`);
}
console.log("Total units to order:", totalUnits(lines));