diff --git a/CHANGES.md b/CHANGES.md index 9c38282..39dbb8a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add splitBill(items, people): splits the tax-inclusive 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..1f821a1 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 count or an array of people). +// Works in whole cents so shares always sum exactly to the total; leftover +// cents go one each to the first people. +export function splitBill(items, people) { + const n = Array.isArray(people) ? people.length : people; + if (!Number.isInteger(n) || n < 1) { + throw new RangeError('splitBill needs at least one person'); + } + const totalCents = roundCents(total(items) * 100); + const base = Math.trunc(totalCents / n); + const extra = totalCents - base * n; + return Array.from({ length: n }, (_, i) => roundCents((base + (i < extra ? 1 : 0)) / 100)); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..3da29f6 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,48 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +const sum = (a) => a.reduce((s, x) => s + x, 0); +const cents = (x) => Math.round(x * 100); + +test('splitBill splits 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 to total', () => { + const odd = [{ price: 10, qty: 1 }]; // total 10.80 + const shares = splitBill(odd, 7); + assert.equal(shares.length, 7); + assert.equal(cents(sum(shares)), 1080); + const cs = shares.map(cents); + assert.ok(Math.max(...cs) - Math.min(...cs) <= 1); +}); + +test('splitBill gives extra cents to the first people', () => { + const one = [{ price: 1, qty: 1 }]; // total 1.08 + assert.deepEqual(splitBill(one, 3), [0.36, 0.36, 0.36]); + assert.deepEqual(splitBill(one, 5), [0.22, 0.22, 0.22, 0.21, 0.21]); +}); + +test('splitBill accepts an array of people', () => { + assert.deepEqual(splitBill(items, ['a', 'b', 'c']), [9, 9, 9]); +}); + +test('splitBill with empty cart gives zero shares', () => { + assert.deepEqual(splitBill([], 2), [0, 0]); +}); + +test('splitBill with more people than cents', () => { + const tiny = [{ price: 0.01, qty: 1 }]; // total 0.01 + assert.deepEqual(splitBill(tiny, 3), [0.01, 0, 0]); +}); + +test('splitBill rejects invalid people', () => { + for (const bad of [0, -1, 1.5, NaN, [], undefined, null]) { + assert.throws(() => splitBill(items, bad), undefined, String(bad)); + } +});