diff --git a/CHANGES.md b/CHANGES.md index 9c38282..23fa809 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add splitBill(items, people): splits the taxed total into per-person shares that sum exactly to total(). ## 1.2.0 - total() rounds through roundCents(). diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..7bb5e8c 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,17 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +// Splits total(items) between `people` (a positive integer head count). +// Shares are whole cents that sum exactly to the total; leftover cents go +// one each to the first people, so shares differ by at most one cent. +export function splitBill(items, people) { + if (!Number.isInteger(people) || people < 1) { + throw new RangeError('people must be a positive integer'); + } + // total() is already rounded to cents, so trunc(x + 0.5) just removes float noise. + const cents = Math.trunc(total(items) * 100 + 0.5); + const extra = cents % people; + const base = (cents - extra) / people; + return Array.from({ length: people }, (_, i) => roundCents((base + (i < extra ? 1 : 0)) / 100)); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..9d01590 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, splitBill } from '../../src/lib/cart.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -11,3 +11,41 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +const sum = (xs) => xs.reduce((a, b) => a + b, 0); + +test('splitBill splits the taxed total evenly', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill with one person returns the whole total', () => { + assert.deepEqual(splitBill(items, 1), [27]); +}); + +test('splitBill distributes leftover cents so shares sum exactly to the total', () => { + const shares = splitBill([{ price: 1, qty: 1 }], 5); // total 1.08 + assert.deepEqual(shares, [0.22, 0.22, 0.22, 0.21, 0.21]); + assert.equal(Math.round(sum(shares) * 100), 108); +}); + +test('splitBill shares never differ by more than a cent', () => { + const cart = [{ price: 9.99, qty: 3 }]; + const shares = splitBill(cart, 7); + assert.equal(shares.length, 7); + assert.ok(Math.max(...shares) - Math.min(...shares) < 0.011); + assert.equal(Math.round(sum(shares) * 100), Math.round(total(cart) * 100)); +}); + +test('splitBill with an empty cart gives everyone zero', () => { + assert.deepEqual(splitBill([], 3), [0, 0, 0]); +}); + +test('splitBill with more people than cents gives some people zero', () => { + assert.deepEqual(splitBill([{ price: 0.01, qty: 1 }], 3), [0.01, 0, 0]); +}); + +test('splitBill rejects invalid people counts', () => { + for (const bad of [0, -2, 1.5, NaN, undefined, '3']) { + assert.throws(() => splitBill(items, bad), RangeError); + } +});