diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..fb45a16 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,13 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new RangeError('people must be a positive integer'); + } + const totalCents = Math.round(total(items) * 100); + const base = Math.floor(totalCents / people); + const extra = totalCents - base * people; + return Array.from({ length: people }, (_, i) => (base + (i < extra ? 1 : 0)) / 100); +} diff --git a/tests/lib/splitbill.test.js b/tests/lib/splitbill.test.js new file mode 100644 index 0000000..7181ee6 --- /dev/null +++ b/tests/lib/splitbill.test.js @@ -0,0 +1,80 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { splitBill, total } from '../../src/lib/cart.js'; +import { roundCents } from '../../src/lib/money.js'; + +// Shares are compared in whole cents so float noise can't hide a lost or extra cent. +const toCents = (amount) => Math.round(amount * 100); +const sumCents = (shares) => shares.reduce((sum, share) => sum + toCents(share), 0); + +const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; // total 27.00 with tax + +test('splitBill returns one share per person', () => { + assert.equal(splitBill(items, 3).length, 3); +}); + +test('splitBill splits an even total into equal shares including tax', () => { + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); + assert.deepEqual(splitBill(items, 4), [6.75, 6.75, 6.75, 6.75]); +}); + +test('splitBill gives a single person the whole total', () => { + assert.deepEqual(splitBill(items, 1), [total(items)]); +}); + +test('splitBill shares always add up to exactly the cart total', () => { + for (const people of [1, 2, 3, 4, 5, 6, 7, 9, 11, 13]) { + assert.equal(sumCents(splitBill(items, people)), toCents(total(items)), `people=${people}`); + } +}); + +test('splitBill does not lose or invent cents on an uneven split', () => { + // 27.00 / 7 = 3.857...: five people pay 3.86 and two pay 3.85. + const shares = splitBill(items, 7); + assert.equal(sumCents(shares), 2700); + assert.deepEqual([...shares].sort((a, b) => b - a), [3.86, 3.86, 3.86, 3.86, 3.86, 3.85, 3.85]); +}); + +test('splitBill shares are rounded to cents', () => { + for (const share of splitBill(items, 7)) { + assert.equal(share, roundCents(share)); + } +}); + +test('splitBill shares differ by at most one cent', () => { + const shares = splitBill([{ price: 0.99, qty: 3 }], 4); // total 3.21 + const cents = shares.map(toCents); + assert.ok(Math.max(...cents) - Math.min(...cents) <= 1, `shares: ${shares}`); + assert.equal(sumCents(shares), toCents(total([{ price: 0.99, qty: 3 }]))); +}); + +test('splitBill handles a total smaller than one cent per person', () => { + const tiny = [{ price: 0.01, qty: 1 }]; // total 0.01 + const shares = splitBill(tiny, 3); + assert.equal(shares.length, 3); + assert.equal(sumCents(shares), 1); + assert.ok(shares.every((share) => share >= 0)); +}); + +test('splitBill of an empty cart gives everyone zero', () => { + assert.deepEqual(splitBill([], 3), [0, 0, 0]); +}); + +test('splitBill does not mutate the items', () => { + const input = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; + const copy = structuredClone(input); + splitBill(input, 3); + assert.deepEqual(input, copy); +}); + +test('splitBill rejects zero people', () => { + assert.throws(() => splitBill(items, 0)); +}); + +test('splitBill rejects a negative number of people', () => { + assert.throws(() => splitBill(items, -2)); +}); + +test('splitBill rejects a fractional number of people', () => { + assert.throws(() => splitBill(items, 2.5)); +});