diff options
| -rw-r--r-- | lib/utils.js | 76 |
1 files changed, 37 insertions, 39 deletions
diff --git a/lib/utils.js b/lib/utils.js index 389f8a57..b5492f72 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -86,48 +86,47 @@ export let f = (s, currency) => .replace("CA", ""); export function formatReceipt(items, currency) { -function wrapText(text, maxWidth) { - const words = text.split(" "); - const lines = []; - let currentLine = ""; - words.forEach((word) => { - if (currentLine.length + word.length + 1 > maxWidth) { - lines.push(currentLine); - currentLine = word; - } else { - currentLine += (currentLine.length > 0 ? " " : "") + word; - } - }); - if (currentLine) lines.push(currentLine); - return lines; -} + function wrapText(text, maxWidth) { + const words = text.split(" "); + const lines = []; + let currentLine = ""; + words.forEach((word) => { + if (currentLine.length + word.length + 1 > maxWidth) { + lines.push(currentLine); + currentLine = word; + } else { + currentLine += (currentLine.length > 0 ? " " : "") + word; + } + }); + if (currentLine) lines.push(currentLine); + return lines; + } -function calculateColumnWidths(items) { - let maxQuantityLength = 0; - let maxPriceLength = 0; + function calculateColumnWidths(items) { + let maxQuantityLength = 0; + let maxPriceLength = 0; - items.forEach((item) => { - const quantityLength = String(item.quantity).length; - if (quantityLength > maxQuantityLength) { - maxQuantityLength = quantityLength; - } + items.forEach((item) => { + const quantityLength = String(item.quantity).length; + if (quantityLength > maxQuantityLength) { + maxQuantityLength = quantityLength; + } - const priceLength = f(item.price * item.quantity, currency).length; - if (priceLength > maxPriceLength) { - maxPriceLength = priceLength; - } - }); + const priceLength = f(item.price * item.quantity, currency).length; + if (priceLength > maxPriceLength) { + maxPriceLength = priceLength; + } + }); - // Add padding to the widths for aesthetic spacing - return { - quantityColumnWidth: maxQuantityLength + 1, // Space after quantity - priceColumnWidth: maxPriceLength + 1, // Space before price - }; -} + // Add padding to the widths for aesthetic spacing + return { + quantityColumnWidth: maxQuantityLength + 1, // Space after quantity + priceColumnWidth: maxPriceLength + 1, // Space before price + }; + } const maxLineWidth = 32; - const { quantityColumnWidth, priceColumnWidth } = calculateColumnWidths( - items, - ); + const { quantityColumnWidth, priceColumnWidth } = + calculateColumnWidths(items); const nameColumnWidth = maxLineWidth - quantityColumnWidth - priceColumnWidth; return items @@ -149,6 +148,5 @@ function calculateColumnWidths(items) { return fullLines.join("\n"); }) - .join("\n\n"); + .join("\n"); } - |
