diff --git a/CHANGES.md b/CHANGES.md index 9c38282..e00acf4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add checkout() with SAVE10 and FLAT5 coupons; returns subtotal, discount, tax and total. ## 1.2.0 - total() rounds through roundCents(). diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..7a76db0 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,27 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function checkout(items, { coupon } = {}) { + const counted = items.filter((item) => item.qty > 0); + const sub = roundCents(subtotal(counted)); + const discount = roundCents(couponDiscount(coupon, sub)); + const tax = roundCents((sub - discount) * TAX_RATE); + // The parts are already whole cents; roundCents only strips float noise from the sum. + const total = roundCents(sub - discount + tax); + return { subtotal: sub, discount, tax, total }; +} + +function couponDiscount(coupon, sub) { + const code = (coupon ?? '').trim(); + switch (code.toUpperCase()) { + case '': + return 0; + case 'SAVE10': + return sub * 0.1; + case 'FLAT5': + return Math.min(5, sub); + default: + throw new Error(`Unknown coupon: ${code}`); + } +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..140a815 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, checkout } from '../../src/lib/cart.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -11,3 +11,56 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +test('checkout without a coupon', () => { + assert.deepEqual(checkout(items), { subtotal: 25, discount: 0, tax: 2, total: 27 }); +}); + +test('checkout ignores items with qty 0 or less', () => { + const withEmpty = [...items, { price: 100, qty: 0 }, { price: 100, qty: -2 }]; + assert.deepEqual(checkout(withEmpty), { subtotal: 25, discount: 0, tax: 2, total: 27 }); +}); + +test('checkout with no items', () => { + assert.deepEqual(checkout([]), { subtotal: 0, discount: 0, tax: 0, total: 0 }); +}); + +test('SAVE10 takes 10% off before tax', () => { + assert.deepEqual(checkout(items, { coupon: 'SAVE10' }), { + subtotal: 25, discount: 2.5, tax: 1.8, total: 24.3, + }); +}); + +test('FLAT5 takes $5 off', () => { + assert.deepEqual(checkout(items, { coupon: 'FLAT5' }), { + subtotal: 25, discount: 5, tax: 1.6, total: 21.6, + }); +}); + +test('FLAT5 never takes more than the subtotal', () => { + assert.deepEqual(checkout([{ price: 3, qty: 1 }], { coupon: 'FLAT5' }), { + subtotal: 3, discount: 3, tax: 0, total: 0, + }); +}); + +test('coupons are case-insensitive and ignore surrounding spaces', () => { + assert.equal(checkout(items, { coupon: ' save10 ' }).discount, 2.5); + assert.equal(checkout(items, { coupon: 'Flat5' }).discount, 5); +}); + +test('empty or missing coupon means no discount', () => { + assert.equal(checkout(items, { coupon: '' }).discount, 0); + assert.equal(checkout(items, { coupon: ' ' }).discount, 0); + assert.equal(checkout(items, {}).discount, 0); +}); + +test('unknown coupon throws with the trimmed code', () => { + assert.throws(() => checkout(items, { coupon: ' Bogus ' }), { + message: 'Unknown coupon: Bogus', + }); +}); + +test('subtotal, discount and tax are rounded to cents', () => { + const result = checkout([{ price: 3.333, qty: 3 }], { coupon: 'SAVE10' }); + assert.deepEqual(result, { subtotal: 10, discount: 1, tax: 0.72, total: 9.72 }); +});