diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..a7325db 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 cents = Math.round(total(items) * 100); + const base = Math.floor(cents / people); + const extra = cents - 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..4a6484b --- /dev/null +++ b/tests/lib/splitbill.test.js @@ -0,0 +1,93 @@ +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'; + +const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; // total() === 27 + +// Work in integer cents so float noise doesn't hide (or fake) a missing cent. +const toCents = (amount) => Math.round(amount * 100); +const sumCents = (shares) => shares.reduce((sum, s) => sum + toCents(s), 0); + +function assertValidSplit(shares, cartItems, people) { + assert.ok(Array.isArray(shares), 'returns an array'); + assert.equal(shares.length, people, 'one share per person'); + for (const s of shares) { + assert.equal(typeof s, 'number'); + assert.equal(s, roundCents(s), `share ${s} is rounded to whole cents`); + } + assert.equal(sumCents(shares), toCents(total(cartItems)), 'shares add up to the taxed total'); + const cents = shares.map(toCents); + assert.ok(Math.max(...cents) - Math.min(...cents) <= 1, 'shares differ by at most one cent'); +} + +test('splitBill splits the taxed total evenly between people', () => { + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill includes tax, not just the subtotal', () => { + const shares = splitBill([{ price: 10, qty: 1 }], 2); + assert.deepEqual(shares, [5.4, 5.4]); +}); + +test('splitBill gives the whole total to a single person', () => { + assert.deepEqual(splitBill(items, 1), [total(items)]); +}); + +test('splitBill does not lose or invent cents when the total does not divide evenly', () => { + // 1.00 + 8% tax = 1.08, which does not split into 5 equal whole-cent shares. + const cartItems = [{ price: 1, qty: 1 }]; + const shares = splitBill(cartItems, 5); + assertValidSplit(shares, cartItems, 5); + assert.deepEqual([...shares].sort((a, b) => a - b), [0.21, 0.21, 0.22, 0.22, 0.22]); +}); + +test('splitBill handles a remainder of one cent', () => { + // 27.01 can't be split three ways evenly: 9.00 + 9.00 + 9.01 style. + const cartItems = [{ price: 25.01, qty: 1 }]; // 25.01 * 1.08 = 27.0108 -> 27.01 + assert.equal(total(cartItems), 27.01); + const shares = splitBill(cartItems, 3); + assertValidSplit(shares, cartItems, 3); + assert.deepEqual([...shares].sort((a, b) => a - b), [9, 9, 9.01]); +}); + +test('splitBill keeps invariants across a range of carts and group sizes', () => { + const carts = [ + items, + [{ price: 0.99, qty: 3 }], + [{ price: 19.99, qty: 1 }, { price: 3.33, qty: 7 }], + [{ price: 0.01, qty: 1 }], + ]; + for (const cartItems of carts) { + for (let people = 1; people <= 7; people++) { + assertValidSplit(splitBill(cartItems, people), cartItems, people); + } + } +}); + +test('splitBill handles more people than cents in the total', () => { + // 0.01 * 1.08 = 0.0108 -> 0.01; only one person can pay anything. + const cartItems = [{ price: 0.01, qty: 1 }]; + const shares = splitBill(cartItems, 3); + assertValidSplit(shares, cartItems, 3); + assert.deepEqual([...shares].sort((a, b) => a - b), [0, 0, 0.01]); +}); + +test('splitBill returns all zero shares for an empty cart', () => { + assert.deepEqual(splitBill([], 3), [0, 0, 0]); +}); + +test('splitBill does not mutate the items passed in', () => { + const cartItems = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; + const snapshot = structuredClone(cartItems); + splitBill(cartItems, 4); + assert.deepEqual(cartItems, snapshot); +}); + +test('splitBill rejects a non-positive or non-integer number of people', () => { + assert.throws(() => splitBill(items, 0)); + assert.throws(() => splitBill(items, -2)); + assert.throws(() => splitBill(items, 2.5)); + assert.throws(() => splitBill(items, NaN)); +});