diff --git a/CHANGES.md b/CHANGES.md index 9c38282..b70759a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add splitBill(items, people): splits the cart total (incl. tax) 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..1b53b9b 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,18 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +// Splits total(items) between `people` (an array of people, or a head count). +// Everyone pays the even share rounded to cents; the last person absorbs the +// rounding difference so the shares always sum exactly to the total. +export function splitBill(items, people) { + const count = Array.isArray(people) ? people.length : people; + if (!Number.isInteger(count) || count < 1) { + throw new RangeError('splitBill needs at least one person'); + } + const bill = total(items); + const share = roundCents(bill / count); + const shares = new Array(count - 1).fill(share); + shares.push(roundCents(bill - share * (count - 1))); + return shares; +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..fbd5afe 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,36 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +const sum = (a) => a.reduce((x, y) => x + y, 0); + +test('splitBill splits evenly when divisible', () => { + assert.deepEqual(splitBill(items, ['a', 'b']), [13.5, 13.5]); +}); + +test('splitBill shares sum exactly to the total when not divisible', () => { + const odd = [{ price: 10, qty: 1 }]; // 10.80 / 7 + const shares = splitBill(odd, [1, 2, 3, 4, 5, 6, 7]); + assert.equal(shares.length, 7); + assert.equal(Math.round(sum(shares) * 100), Math.round(total(odd) * 100)); + assert.ok(Math.max(...shares) - Math.min(...shares) <= 0.07 + 1e-9); +}); + +test('splitBill with one person returns the whole total', () => { + assert.deepEqual(splitBill(items, ['a']), [27]); +}); + +test('splitBill with empty cart gives zero shares', () => { + assert.deepEqual(splitBill([], ['a', 'b']), [0, 0]); +}); + +test('splitBill rejects no people or invalid counts', () => { + assert.throws(() => splitBill(items, []), RangeError); + assert.throws(() => splitBill(items, 0), RangeError); + assert.throws(() => splitBill(items, -2), RangeError); + assert.throws(() => splitBill(items, 1.5), RangeError); +}); + +test('splitBill accepts a head count', () => { + assert.deepEqual(splitBill(items, 2), [13.5, 13.5]); +});