diff --git a/CHANGES.md b/CHANGES.md index 9c38282..56b9e85 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased +- Add README.md documenting the cart module's exported functions and how to run the checks. +- Add discount(items, percent) to the cart module: returns the total including tax after taking the percentage off the subtotal before tax. +- Add checkout() to the cart module: returns subtotal, discount, tax and total, with SAVE10 and FLAT5 coupons. +- Add splitBill(items, people) to the cart module: splits the total (including tax) between people, with shares that sum exactly to the total. +- Add itemCount(items) to the cart module: returns the total quantity across all items. ## 1.2.0 - total() rounds through roundCents(). diff --git a/README.md b/README.md new file mode 100644 index 0000000..a06b85b --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# cart-app + +## Cart API (`src/lib/cart.js`) + +- `subtotal(items)`: sum of `price * qty` over all items, before tax. +- `itemCount(items)`: total quantity across all items. +- `total(items)`: subtotal plus tax, rounded to cents. +- `discount(items, percent)`: total including tax after taking `percent` (0–100) off the subtotal; throws on invalid percent. +- `splitBill(items, people)`: splits the total into `people` shares in dollars that sum exactly to the total; throws unless `people` is a positive integer. +- `checkout(items, { coupon })`: returns `{ subtotal, discount, tax, total }`, ignoring items with qty 0; supports coupons `SAVE10` and `FLAT5` (case-insensitive) and throws on unknown coupons. + +## Running the checks + +- `npm run check`: runs the lint step (`scripts/check.mjs`, which enforces `roundCents()` for money) and then the tests. +- `npm test`: runs the tests only. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..511dd27 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -5,6 +5,51 @@ export function subtotal(items) { return items.reduce((sum, item) => sum + item.price * item.qty, 0); } +export function itemCount(items) { + return items.reduce((sum, item) => sum + item.qty, 0); +} + export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function discount(items, percent) { + if (typeof percent !== 'number' || !(percent >= 0 && percent <= 100)) { + throw new Error('percent must be a number between 0 and 100'); + } + // Take the percentage off the subtotal first, then apply tax. + return roundCents(subtotal(items) * (1 - percent / 100) * (1 + TAX_RATE)); +} + +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new Error('people must be a positive integer'); + } + // total() is already rounded to cents, so this is a whole number of cents. + const cents = roundCents(total(items) * 100); + const extra = cents % people; + const base = (cents - extra) / people; + // The first `extra` people pay one cent more so shares add up to the total. + return Array.from({ length: people }, (_, i) => roundCents((base + (i < extra ? 1 : 0)) / 100)); +} + +export function checkout(items, { coupon } = {}) { + const active = items.filter((item) => item.qty > 0); + const rawSubtotal = subtotal(active); + const code = (coupon ?? '').trim(); + const key = code.toUpperCase(); + + let rawDiscount = 0; + if (key === 'SAVE10') { + rawDiscount = rawSubtotal * 0.1; + } else if (key === 'FLAT5') { + rawDiscount = Math.min(5, rawSubtotal); + } else if (key !== '') { + throw new Error(`Unknown coupon: ${code}`); + } + + const sub = roundCents(rawSubtotal); + const discount = roundCents(rawDiscount); + const tax = roundCents((rawSubtotal - rawDiscount) * 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..de846eb 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,66 @@ test('subtotal adds price times quantity', () => { assert.equal(subtotal(items), 25); }); +test('itemCount sums quantities across items', () => { + assert.equal(itemCount(items), 3); + assert.equal(itemCount([]), 0); +}); + 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: ' ' }), { subtotal: 25, discount: 0, tax: 2, total: 27 }); +}); + +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 takes $5 off, capped at 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('checkout rejects unknown coupons', () => { + assert.throws(() => checkout(items, { coupon: ' bogus ' }), { message: 'Unknown coupon: bogus' }); +}); + +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, '10', undefined]) { + assert.throws(() => discount(items, p), { message: 'percent must be a number between 0 and 100' }); + } +}); + +test('splitBill splits the total evenly', () => { + assert.deepEqual(splitBill(items, 1), [27]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill gives leftover cents to the first people so shares sum to the total', () => { + const one = [{ price: 10, qty: 1 }]; + const shares = splitBill(one, 3); + assert.equal(shares.length, 3); + assert.equal(Math.round(shares.reduce((a, b) => a + b, 0) * 100), Math.round(total(one) * 100)); + assert.ok(Math.max(...shares) - Math.min(...shares) <= 0.01 + 1e-9); + assert.deepEqual(splitBill([{ price: 0.02, qty: 1 }], 3), [0.01, 0.01, 0]); +}); + +test('splitBill rejects invalid people counts', () => { + for (const n of [0, -1, 1.5, NaN]) { + assert.throws(() => splitBill(items, n), { message: 'people must be a positive integer' }); + } +});