diff --git a/CHANGES.md b/CHANGES.md index 9c38282..9274869 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased +- Add README.md documenting the cart functions and how to run the checks. +- Add discount(items, percent) returning the total including tax after taking a percentage off the subtotal before tax. +- Add itemCount(items) returning the total quantity across items. +- Add splitBill(items, people) to split the cart total (including tax) into per-person shares that sum to the total. +- Add checkout(items, { coupon }) returning subtotal, discount, tax and total; supports 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..3b1b0b7 --- /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)` – sum of price times quantity, before tax. +- `total(items)` – 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 on unknown codes). +- `splitBill(items, people)` – splits the total (including tax) into an array of per-person shares that sum to the total. +- `itemCount(items)` – total quantity across items, ignoring qty 0 or less. +- `discount(items, percent)` – total including tax after taking `percent` off the subtotal before tax. + +## Running the checks + +- `npm test` – run the unit tests. +- `npm run check` – run the money-rounding lint, then the tests. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..88fd4b1 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,53 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +// Total including tax after taking `percent` off the subtotal (before tax). +export function discount(items, percent) { + if (!Number.isFinite(percent) || percent < 0 || percent > 100) { + throw new Error(`Invalid discount percent: ${percent}`); + } + const discounted = subtotal(items) * (1 - percent / 100); + return roundCents(discounted * (1 + TAX_RATE)); +} + +// Total quantity across items; qty of 0 or less is ignored, as in checkout(). +export function itemCount(items) { + return items.reduce((sum, item) => (item.qty > 0 ? sum + item.qty : sum), 0); +} + +// Splits total(items) between people; shares differ by at most a cent and sum to the total. +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new Error(`Invalid number of people: ${people}`); + } + const whole = total(items); + const shares = []; + let allocated = 0; + for (let i = 1; i <= people; i++) { + const cumulative = roundCents((whole * i) / people); + shares.push(roundCents(cumulative - allocated)); + allocated = cumulative; + } + return shares; +} + +export function checkout(items, { coupon } = {}) { + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const code = (coupon ?? '').trim(); + let discount = 0; + switch (code.toUpperCase()) { + case '': + break; + case 'SAVE10': + discount = roundCents(sub * 0.1); + break; + case 'FLAT5': + discount = roundCents(Math.min(5, sub)); + break; + default: + throw new Error(`Unknown coupon: ${code}`); + } + const tax = roundCents((sub - discount) * TAX_RATE); + return { subtotal: sub, discount, tax, total: roundCents(sub - discount + tax) }; +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..57192a9 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 }]; @@ -11,3 +11,71 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +test('discount takes the percentage off before tax', () => { + assert.equal(discount(items, 10), 24.3); + assert.equal(discount(items, 0), total(items)); + assert.equal(discount(items, 100), 0); +}); + +test('discount rejects invalid percentages', () => { + for (const p of [-1, 101, NaN, undefined]) { + assert.throws(() => discount(items, p), /Invalid discount percent/); + } +}); + +test('itemCount sums 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('splitBill splits evenly when divisible', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); + assert.deepEqual(splitBill(items, 1), [27]); +}); + +test('splitBill distributes leftover cents and sums to the total', () => { + 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.equal(roundSum(odd), 1.08); + assert.ok(Math.max(...odd) - Math.min(...odd) < 0.011); +}); + +test('splitBill rejects invalid people counts', () => { + for (const n of [0, -1, 1.5, NaN, undefined]) { + assert.throws(() => splitBill(items, n), /Invalid number of people/); + } +}); + +function roundSum(xs) { + return Math.round(xs.reduce((a, b) => a + b, 0) * 100) / 100; +} + +test('checkout without coupon', () => { + assert.deepEqual(checkout(items), { subtotal: 25, discount: 0, tax: 2, total: 27 }); + assert.equal(checkout(items, { coupon: ' ' }).discount, 0); +}); + +test('checkout ignores items with qty 0 or less', () => { + const r = checkout([...items, { price: 100, qty: 0 }, { price: 100, qty: -2 }]); + 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.equal(checkout(items, { coupon: 'flat5' }).discount, 5); + const r = checkout([{ price: 3, qty: 1 }], { coupon: 'FLAT5' }); + assert.deepEqual(r, { subtotal: 3, discount: 3, tax: 0, total: 0 }); +}); + +test('checkout rejects unknown coupons', () => { + assert.throws(() => checkout(items, { coupon: ' bogus ' }), { message: 'Unknown coupon: bogus' }); +});