diff --git a/CHANGES.md b/CHANGES.md index 9c38282..b0648c6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased +- Added README.md documenting the cart functions and how to run the checks. +- discount(items, percent) returns the total including tax after taking the percentage off the subtotal before tax. +- itemCount(items) returns the total quantity across items, ignoring items with qty 0 or less. +- splitBill(items, people) splits the cart total, including tax, into per-person shares that sum exactly to the total. +- checkout(items, { coupon }) returns subtotal, discount, tax and total, supporting SAVE10 and FLAT5 coupons. ## 1.2.0 - total() rounds through roundCents(). diff --git a/README.md b/README.md new file mode 100644 index 0000000..db3c229 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# cart-app + +Cart helpers live in `src/lib/cart.js`. Items look like `{ price, qty }`. + +## Functions + +- `subtotal(items)` returns the sum of price times qty, before tax. +- `itemCount(items)` returns the total quantity across items, ignoring items with qty 0 or less. +- `total(items)` returns the subtotal plus tax, rounded to cents. +- `checkout(items, { coupon } = {})` returns `{ subtotal, discount, tax, total }`, ignoring items with qty 0 or less and applying an optional `SAVE10` or `FLAT5` coupon (throws `Unknown coupon: ` otherwise). +- `discount(items, percent)` returns the total including tax after taking `percent` (0 to 100) off the subtotal before tax. +- `splitBill(items, people)` returns an array of each person's share of the total including tax, with shares summing exactly to the total. + +## Running the checks + +- `npm test` runs the unit tests. +- `npm run check` runs the money-rounding lint (`scripts/check.mjs`) and then the tests. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..bec5dda 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -5,6 +5,52 @@ export function subtotal(items) { return items.reduce((sum, item) => sum + item.price * item.qty, 0); } +export function itemCount(items) { + return items.reduce((sum, item) => (item.qty > 0 ? sum + item.qty : sum), 0); +} + export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function checkout(items, { coupon } = {}) { + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const code = (coupon ?? '').trim(); + let discount = 0; + if (code) { + const key = code.toUpperCase(); + if (key === 'SAVE10') discount = roundCents(sub * 0.1); + else if (key === 'FLAT5') discount = Math.min(5, sub); + else throw new Error(`Unknown coupon: ${code}`); + } + const tax = roundCents((sub - discount) * TAX_RATE); + return { subtotal: sub, discount, tax, total: roundCents(sub - discount + tax) }; +} + +// Total including tax after taking `percent` off the subtotal; rounds like checkout(). +export function discount(items, percent) { + if (!Number.isFinite(percent) || percent < 0 || percent > 100) { + throw new Error(`Invalid percent: ${percent}`); + } + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const off = roundCents(sub * percent / 100); + const tax = roundCents((sub - off) * TAX_RATE); + return roundCents(sub - off + tax); +} + +// Splits the cart total (with tax) evenly; leftover cents go to the first people so shares sum to the total. +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new Error(`Invalid number of people: ${people}`); + } + const { total: amount } = checkout(items); + const base = roundCents(amount / people); + const shares = new Array(people).fill(base); + let remainder = roundCents(amount - base * people); + const step = remainder > 0 ? 0.01 : -0.01; + for (let i = 0; Math.abs(remainder) > 0.005; i++) { + shares[i] = roundCents(shares[i] + step); + remainder = roundCents(remainder - step); + } + return shares; +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..84d8cf7 100644 --- a/tests/lib/cart.test.js +++ b/tests/lib/cart.test.js @@ -1,6 +1,6 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { subtotal, total } from '../../src/lib/cart.js'; +import { subtotal, total, checkout, splitBill, itemCount, discount } from '../../src/lib/cart.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -8,6 +8,79 @@ test('subtotal adds price times quantity', () => { assert.equal(subtotal(items), 25); }); +test('itemCount adds up quantities', () => { + assert.equal(itemCount(items), 3); + assert.equal(itemCount([]), 0); +}); + +test('itemCount ignores items with qty 0 or less', () => { + assert.equal(itemCount([...items, { price: 1, qty: 0 }, { price: 1, qty: -4 }]), 3); +}); + test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +test('checkout without coupon', () => { + assert.deepEqual(checkout(items), { subtotal: 25, discount: 0, tax: 2, total: 27 }); + assert.deepEqual(checkout(items, { coupon: ' ' }), checkout(items)); +}); + +test('checkout ignores items with qty 0 or less', () => { + const r = checkout([...items, { price: 100, qty: 0 }, { price: 100, qty: -1 }]); + assert.equal(r.subtotal, 25); +}); + +test('checkout SAVE10 is case-insensitive and trimmed', () => { + assert.deepEqual(checkout(items, { coupon: ' save10 ' }), { subtotal: 25, discount: 2.5, tax: 1.8, total: 24.3 }); +}); + +test('checkout FLAT5 never exceeds subtotal', () => { + assert.deepEqual(checkout(items, { coupon: 'flat5' }), { subtotal: 25, discount: 5, tax: 1.6, total: 21.6 }); + assert.deepEqual(checkout([{ price: 3, qty: 1 }], { coupon: 'FLAT5' }), { subtotal: 3, discount: 3, tax: 0, total: 0 }); +}); + +test('splitBill divides the total evenly', () => { + assert.deepEqual(splitBill(items, 1), [27]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill hands leftover cents to the first people', () => { + const shares = splitBill([{ price: 10, qty: 1 }], 3); // total 10.80 + assert.deepEqual(shares, [3.6, 3.6, 3.6]); + const odd = splitBill([{ price: 1, qty: 1 }], 7); // total 1.08 + assert.deepEqual(odd, [0.16, 0.16, 0.16, 0.15, 0.15, 0.15, 0.15]); + assert.equal(odd.reduce((a, b) => a + b, 0).toFixed(2), '1.08'); +}); + +test('splitBill rounds down when the even share rounds up', () => { + const shares = splitBill([{ price: 0.1, qty: 1 }], 4); // total 0.11 + assert.deepEqual(shares, [0.02, 0.03, 0.03, 0.03]); +}); + +test('splitBill rejects invalid people counts', () => { + for (const n of [0, -2, 1.5, NaN, undefined]) { + assert.throws(() => splitBill(items, n), /Invalid number of people/); + } +}); + +test('discount takes the percentage off before tax', () => { + assert.equal(discount(items, 10), 24.3); // 25 - 2.50 = 22.50, plus 1.80 tax + assert.equal(discount(items, 0), 27); + assert.equal(discount(items, 100), 0); +}); + +test('discount matches checkout with SAVE10 and ignores qty 0 or less', () => { + const cart = [...items, { price: 50, qty: 0 }]; + assert.equal(discount(cart, 10), checkout(cart, { coupon: 'SAVE10' }).total); +}); + +test('discount rejects invalid percentages', () => { + for (const p of [-1, 101, NaN, undefined]) { + assert.throws(() => discount(items, p), /Invalid percent/); + } +}); + +test('checkout rejects unknown coupons', () => { + assert.throws(() => checkout(items, { coupon: ' bogus ' }), { message: 'Unknown coupon: bogus' }); +});