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..18535c4 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -1,5 +1,5 @@ import { TAX_RATE } from '../generated/rates.js'; -import { roundCents } from './money.js'; +import { roundCents, splitCents } from './money.js'; export function subtotal(items) { return items.reduce((sum, item) => sum + item.price * item.qty, 0); @@ -8,3 +8,10 @@ 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'); + } + return splitCents(total(items), people); +} diff --git a/src/lib/money.js b/src/lib/money.js index 9f36d50..ad87849 100644 --- a/src/lib/money.js +++ b/src/lib/money.js @@ -2,3 +2,12 @@ export function roundCents(amount) { return Math.round((amount + Number.EPSILON) * 100) / 100; } + +// Split an amount into `parts` shares that sum exactly to the amount. +// Leftover cents go one each to the first shares. +export function splitCents(amount, parts) { + const totalCents = Math.round(amount * 100); + const base = Math.trunc(totalCents / parts); + const extra = totalCents - base * parts; + return Array.from({ length: parts }, (_, i) => (base + (i < extra ? 1 : 0)) / 100); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..efd5ce6 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,40 @@ 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); +const cents = (a) => Math.round(sum(a) * 100); + +test('splitBill splits evenly when divisible', () => { + assert.deepEqual(splitBill(items, 3), [9, 9, 9]); +}); + +test('splitBill gives one person the whole total', () => { + assert.deepEqual(splitBill(items, 1), [27]); +}); + +test('splitBill splits tax-inclusive total evenly', () => { + assert.deepEqual(splitBill([{ price: 10, qty: 1 }], 3), [3.6, 3.6, 3.6]); // total 10.80 +}); + +test('splitBill distributes leftover cents so shares sum to the total', () => { + const odd = splitBill([{ price: 1, qty: 1 }], 7); // total 1.08 + assert.equal(odd.length, 7); + assert.equal(cents(odd), 108); + assert.ok(Math.max(...odd) - Math.min(...odd) < 0.0101); +}); + +test('splitBill never loses a cent (1 cent among 3)', () => { + const shares = splitBill([{ price: 0.01, qty: 1 }], 3); // total 0.01 + assert.equal(cents(shares), 1); + assert.deepEqual(shares, [0.01, 0, 0]); +}); + +test('splitBill on an empty cart returns zero shares', () => { + assert.deepEqual(splitBill([], 4), [0, 0, 0, 0]); +}); + +test('splitBill rejects invalid people counts', () => { + for (const bad of [0, -1, 2.5, NaN, undefined, '3']) + assert.throws(() => splitBill(items, bad), RangeError); +});