diff --git a/CHANGES.md b/CHANGES.md index 9c38282..91b3c44 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased +- Add README.md documenting the cart API and how to run the checks. +- Add checkout(items, { coupon }) returning subtotal, discount, tax and total; supports SAVE10 and FLAT5 coupons. +- Add splitBill(items, people) returning each person's share of the cart total (including tax); shares sum exactly to total(). +- Add itemCount(items) returning the total quantity across all items. +- Add discount(items, percent) returning the total including tax after taking percent off the subtotal before tax; throws if percent is outside 0–100. ## 1.2.0 - total() rounds through roundCents(). diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b507e0 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# cart-app + +## API (`src/lib/cart.js`) + +- `subtotal(items)` — sum of `price * qty` across 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 if out of range. +- `splitBill(items, people)` — array of per-person shares of `total()` that sum exactly to it; throws if `people` is not a positive integer. +- `checkout(items, { coupon })` — returns `{ subtotal, discount, tax, total }`; supports `SAVE10` and `FLAT5` coupons, throws on unknown codes. + +## Checks + +- `npm run check` — runs `scripts/check.mjs`, then the tests. +- `npm test` — runs the tests only (`node --test`). +- `npm run gen` — regenerates `src/generated/rates.js`; requires access to the internal pricing service (ask the pricing team for a re-export otherwise). diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..49fce9a 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -5,6 +5,50 @@ 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(`Invalid discount percent: ${percent}`); + } + const discounted = subtotal(items) * (1 - percent / 100); + return roundCents(discounted * (1 + TAX_RATE)); +} + +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new Error(`Invalid number of people: ${people}`); + } + const cents = Math.round(total(items) * 100); + const base = Math.floor(cents / people); + const extra = cents - base * people; + // The first `extra` people pay one cent more so shares sum exactly to the total. + return Array.from({ length: people }, (_, i) => (base + (i < extra ? 1 : 0)) / 100); +} + +export function checkout(items, { coupon } = {}) { + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const code = (coupon ?? '').trim(); + let rawDiscount = 0; + switch (code.toUpperCase()) { + case '': + break; + case 'SAVE10': + rawDiscount = sub * 0.1; + break; + case 'FLAT5': + rawDiscount = Math.min(5, sub); + break; + default: + throw new Error(`Unknown coupon: ${code}`); + } + const discount = roundCents(rawDiscount); + 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..d0e82c8 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,72 @@ test('subtotal adds price times quantity', () => { assert.equal(subtotal(items), 25); }); +test('itemCount sums quantities across all 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 returns subtotal, tax and total', () => { + assert.deepEqual(checkout(items), { 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 takes 10% off, 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 but never more than the 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 treats empty or blank coupon as no discount', () => { + assert.equal(checkout(items, { coupon: '' }).discount, 0); + assert.equal(checkout(items, { coupon: ' ' }).discount, 0); + assert.equal(checkout(items, {}).discount, 0); +}); + +test('checkout throws on unknown coupon with the trimmed code', () => { + assert.throws(() => checkout(items, { coupon: ' bogus ' }), { message: 'Unknown coupon: bogus' }); +}); + +test('discount takes the percentage off the subtotal before tax', () => { + assert.equal(discount(items, 10), 24.3); + assert.equal(discount(items, 50), 13.5); +}); + +test('discount of 0 equals total and 100 gives 0', () => { + assert.equal(discount(items, 0), total(items)); + assert.equal(discount(items, 100), 0); +}); + +test('discount throws on an invalid percent', () => { + assert.throws(() => discount(items, -1), { message: 'Invalid discount percent: -1' }); + assert.throws(() => discount(items, 101), { message: 'Invalid discount percent: 101' }); + assert.throws(() => discount(items, NaN), { message: 'Invalid discount percent: NaN' }); +}); + +test('splitBill splits the total evenly when it divides exactly', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); + assert.deepEqual(splitBill(items, 1), [27]); +}); + +test('splitBill gives leftover cents to the first people so shares sum to the total', () => { + const cart = [{ price: 10, qty: 1 }, { price: 0.05, qty: 1 }]; + const shares = splitBill(cart, 4); + assert.deepEqual(shares, [2.72, 2.71, 2.71, 2.71]); + assert.equal(Math.round(shares.reduce((a, b) => a + b, 0) * 100), Math.round(total(cart) * 100)); +}); + +test('splitBill throws on an invalid number of people', () => { + assert.throws(() => splitBill(items, 0), { message: 'Invalid number of people: 0' }); + assert.throws(() => splitBill(items, 1.5), { message: 'Invalid number of people: 1.5' }); +});