import { formatPrice } from './money.js'; // Every receipt line is exactly this wide, so the till printer never wraps. export const WIDTH = 28; export function receiptLine(name, cents) { const price = formatPrice(cents); return name.padEnd(WIDTH - price.length, '.') + price; } export function receipt(items) { const total = items.reduce((sum, item) => sum + item.cents, 0); const lines = items.map((item) => receiptLine(item.name, item.cents)); return [...lines, receiptLine('TOTAL', total)].join('\n'); } if (import.meta.url === `file://${process.argv[1]}`) { console.log(receipt([ { name: 'Coffee beans', cents: 1899 }, { name: 'Espresso machine', cents: 124950 }, ])); }