diff --git a/CHANGES.md b/CHANGES.md index 9c38282..84fa3f0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ # Changes ## Unreleased +- 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 taxed total; shares sum exactly to total(). +- Add itemCount(items) returning the total quantity across all items. +- Add discount(items, percent) returning the taxed total after taking the percentage off the subtotal before tax. +- Add README.md documenting the cart.js API and how to run the checks. ## 1.2.0 - total() rounds through roundCents(). diff --git a/README.md b/README.md new file mode 100644 index 0000000..28ab7d8 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# cart-app + +## API (`src/lib/cart.js`) + +- `subtotal(items)` – sum of `price * qty` across all items, before tax. +- `itemCount(items)` – total quantity across all items. +- `checkout(items, { coupon })` – returns `{ subtotal, discount, tax, total }`; supports `SAVE10` and `FLAT5` coupons, throws on unknown codes. +- `total(items)` – subtotal plus tax, rounded to cents. +- `discount(items, percent)` – taxed total after taking `percent` (0–100) off the subtotal before tax. +- `splitBill(items, people)` – array of per-person shares of `total()` that sum exactly to it. + +## Running the checks + +- `npm run check` – runs the money-rounding lint (`scripts/check.mjs`), then the tests. +- `npm test` – runs only the tests (`node --test`). +- `npm run gen` – regenerates `src/generated/rates.js`. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..2d44c84 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -5,6 +5,46 @@ 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 checkout(items, { coupon } = {}) { + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const code = (coupon ?? '').trim(); + let rawDiscount = 0; + if (code !== '') { + const key = code.toUpperCase(); + if (key === 'SAVE10') rawDiscount = sub * 0.1; + else if (key === 'FLAT5') rawDiscount = Math.min(5, sub); + else throw new Error(`Unknown coupon: ${code}`); + } + const discount = roundCents(rawDiscount); + const tax = roundCents((sub - rawDiscount) * TAX_RATE); + return { subtotal: sub, discount, tax, total: roundCents(sub - discount + tax) }; +} + 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 sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + return roundCents(sub * (1 - percent / 100) * (1 + TAX_RATE)); +} + +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new Error(`Invalid number of people: ${people}`); + } + const whole = total(items); + const share = roundCents(whole / people); + // Rounding each share can leave the sum off by a few cents; fix up the first shares. + const diffCents = roundCents((whole - roundCents(share * people)) * 100); + const step = diffCents < 0 ? -0.01 : 0.01; + const adjusted = Math.abs(diffCents); + return Array.from({ length: people }, (_, i) => (i < adjusted ? roundCents(share + step) : share)); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..3671664 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 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', () => { + 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 small = checkout([{ price: 3, qty: 1 }], { coupon: 'FLAT5' }); + assert.deepEqual(small, { 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), 27); + assert.equal(discount(items, 100), 0); +}); + +test('discount ignores items with qty 0 or less', () => { + assert.equal(discount([...items, { price: 100, qty: -2 }], 10), 24.3); +}); + +test('discount rejects invalid percents', () => { + assert.throws(() => discount(items, -1), { message: 'Invalid discount percent: -1' }); + assert.throws(() => discount(items, 101), /Invalid discount percent/); + assert.throws(() => discount(items, NaN), /Invalid discount percent/); +}); + +test('splitBill splits the taxed total evenly', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); + assert.deepEqual(splitBill([{ price: 10, qty: 1 }], 3), [3.6, 3.6, 3.6]); +}); + +test('splitBill adjusts the first shares by a cent so shares sum to the total', () => { + assert.deepEqual(splitBill([{ price: 1.01, qty: 1 }], 2), [0.54, 0.55]); + assert.deepEqual(splitBill([{ price: 10, qty: 1 }], 7), [1.55, 1.55, 1.54, 1.54, 1.54, 1.54, 1.54]); +}); + +test('splitBill rejects invalid people counts', () => { + assert.throws(() => splitBill(items, 0), { message: 'Invalid number of people: 0' }); + assert.throws(() => splitBill(items, 1.5), /Invalid number of people/); +});