diff --git a/CHANGES.md b/CHANGES.md index 9c38282..5d8762f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add splitBill(items, people): splits total() into per-person shares that add up exactly. ## 1.2.0 - total() rounds through roundCents(). diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..f0ddd39 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,16 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +// `people` is a head count or an array of people. Shares are whole cents that add up +// exactly to total(items); leftover cents go one each to the first people. +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 totalCents = roundCents(total(items) * 100); + const base = Math.trunc(totalCents / count); + const extra = totalCents - base * count; + return Array.from({ length: count }, (_, i) => (base + (i < extra ? 1 : 0)) / 100); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..456b9dc 100644 --- a/tests/lib/cart.test.js +++ b/tests/lib/cart.test.js @@ -1,6 +1,7 @@ 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'; +import { roundCents } from '../../src/lib/money.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -11,3 +12,41 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +test('splitBill splits the taxed total evenly', () => { + assert.deepEqual(splitBill(items, ['a', 'b']), [13.5, 13.5]); +}); + +test('splitBill accepts a head count instead of a list', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill gives one person the whole total', () => { + assert.deepEqual(splitBill(items, ['a']), [27]); +}); + +test('splitBill hands leftover cents to the first people', () => { + // 1.01 * 1.08 = 1.09 -> 0.37, 0.36, 0.36 + assert.deepEqual(splitBill([{ price: 1.01, qty: 1 }], 3), [0.37, 0.36, 0.36]); +}); + +test('splitBill shares always add up to the total', () => { + const cart = [{ price: 10, qty: 1 }]; + const shares = splitBill(cart, 7); + assert.equal(shares.length, 7); + assert.equal(roundCents(shares.reduce((a, b) => a + b, 0)), total(cart)); +}); + +test('splitBill on an empty cart returns zero shares', () => { + assert.deepEqual(splitBill([], 2), [0, 0]); +}); + +test('splitBill with more people than cents leaves some with zero', () => { + assert.deepEqual(splitBill([{ price: 0.01, qty: 1 }], 3), [0.01, 0, 0]); +}); + +test('splitBill rejects zero, negative, fractional, or empty people', () => { + for (const bad of [0, -1, 1.5, [], undefined, NaN]) { + assert.throws(() => splitBill(items, bad), RangeError); + } +});