diff --git a/CHANGES.md b/CHANGES.md index 9c38282..3828667 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 all items (qty 0 or less is ignored). +- Add splitBill(items, people) to split the cart total (including tax) between people; the first person absorbs any rounding difference so shares add up 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..e261727 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# cart-app + +Cart calculations. Items look like `{ price, qty }`. Money is rounded to cents with `roundCents()` from `src/lib/money.js`. + +## API (`src/lib/cart.js`) + +- `subtotal(items)` – sum of `price * qty` across items, before tax. +- `itemCount(items)` – total quantity across items, ignoring qty 0 or less. +- `total(items)` – subtotal plus tax, rounded to cents. +- `discount(items, percent)` – total including tax after taking `percent` (0-100) off the subtotal before tax; throws on an invalid percent. +- `splitBill(items, people)` – array of each person's share of the total, with the first person absorbing any rounding difference; throws unless `people` is a positive integer. +- `checkout(items, { coupon } = {})` – returns `{ subtotal, discount, tax, total }`, ignoring qty 0 or less; supports coupons `SAVE10` (10% off) and `FLAT5` ($5 off, capped at the subtotal), and throws `Unknown coupon: ` for any other code. + +## Checks + +- `npm test` – run the tests. +- `npm run check` – run the rounding lint (`scripts/check.mjs`), then the tests. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..5de9aa6 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -5,6 +5,54 @@ 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)); } + +// Total including tax after taking `percent` (0-100) off the subtotal, before tax. +export function discount(items, percent) { + if (typeof percent !== 'number' || !(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 (including tax) evenly; the cents lost or gained by rounding each +// share are applied to the first person so the shares always add up 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 share = roundCents(whole / people); + const shares = Array.from({ length: people }, () => share); + shares[0] = roundCents(shares[0] + (whole - roundCents(share * people))); + return shares; +} + +function discountFor(sub, coupon) { + const code = (coupon ?? '').trim(); + if (code === '') return 0; + switch (code.toUpperCase()) { + case 'SAVE10': + return sub * 0.1; + case 'FLAT5': + return Math.min(5, sub); + default: + throw new Error(`Unknown coupon: ${code}`); + } +} + +export function checkout(items, { coupon } = {}) { + const sub = roundCents(subtotal(items.filter((item) => item.qty > 0))); + const discount = roundCents(discountFor(sub, coupon)); + 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..0ec98e3 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,79 @@ test('subtotal adds price times quantity', () => { 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 }); +}); + +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 takes $5 off, capped at 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 treats empty or blank coupon as none', () => { + assert.equal(checkout(items, { coupon: '' }).discount, 0); + assert.equal(checkout(items, { coupon: ' ' }).discount, 0); +}); + +test('checkout throws on unknown coupon with trimmed code', () => { + assert.throws(() => checkout(items, { coupon: ' bogus ' }), { message: 'Unknown coupon: bogus' }); +}); + +test('itemCount sums quantities across items', () => { + 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: -2 }]), 3); +}); + +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: 0 }], 10), 24.3); +}); + +test('discount rejects a percent outside 0-100', () => { + assert.throws(() => discount(items, -1), { message: 'Invalid percent: -1' }); + assert.throws(() => discount(items, 101), /Invalid percent/); + assert.throws(() => discount(items, NaN), /Invalid percent/); +}); + +test('splitBill splits evenly when possible', () => { + assert.deepEqual(splitBill(items, 1), [27]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill shares always add up to the total', () => { + const cart = [{ price: 1, qty: 1 }]; + const shares = splitBill(cart, 7); + assert.equal(shares.length, 7); + assert.equal(shares.reduce((a, b) => a + b, 0).toFixed(2), '1.08'); +}); + +test('splitBill puts the rounding difference on the first person', () => { + const cart = [{ price: 1, qty: 1 }]; + assert.deepEqual(splitBill(cart, 7), [0.18, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15]); + assert.deepEqual(splitBill(cart, 5), [0.2, 0.22, 0.22, 0.22, 0.22]); +}); + +test('splitBill rejects an invalid number of people', () => { + assert.throws(() => splitBill(items, 0), { message: 'Invalid number of people: 0' }); + assert.throws(() => splitBill(items, 2.5), /Invalid number of people/); +});