diff --git a/CHANGES.md b/CHANGES.md index 9c38282..64a813b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ # Changes ## Unreleased +- Added itemCount(items), which returns the total quantity across all items, ignoring quantities of 0 or less. +- Added discount(items, percent), which returns the total including tax after taking a percentage off the subtotal. ## 1.2.0 - total() rounds through roundCents(). diff --git a/README.md b/README.md new file mode 100644 index 0000000..a379a46 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# cart-app + +A small, pure-function shopping cart library. An item is `{ sku: string, price: number, qty: number }`, with `price` as the unit price in dollars. + +## API (`src/lib/cart.js`) + +- `subtotal(items)`: returns the sum of price times quantity, before discounts and tax. +- `total(items)`: returns the cart total in dollars including tax, rounded to cents. +- `itemCount(items)`: returns the total quantity across all items, ignoring quantities of 0 or less. +- `discount(items, percent)`: returns the total in dollars including tax, after taking `percent` off the subtotal. + +## Running the checks + +- `npm test` runs the unit tests. +- `npm run check` runs lint and then the tests. Run this before considering a change done. diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..228bf6f 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,14 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +/** Returns the total quantity across all items, ignoring quantities of 0 or less. */ +export function itemCount(items) { + return items.reduce((sum, item) => (item.qty > 0 ? sum + item.qty : sum), 0); +} + +/** Returns the total in dollars including tax, after taking percent off the subtotal. */ +export function discount(items, percent) { + const discountedSubtotal = subtotal(items) * (1 - percent / 100); + return roundCents(discountedSubtotal * (1 + TAX_RATE)); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..03b928f 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 { discount, itemCount, subtotal, total } from '../../src/lib/cart.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -11,3 +11,31 @@ test('subtotal adds price times quantity', () => { test('total applies tax and rounds to cents', () => { assert.equal(total(items), 27); }); + +test('discount takes the percentage off the subtotal before applying tax', () => { + assert.equal(discount(items, 10), 24.3); +}); + +test('discount of zero percent equals the regular total', () => { + assert.equal(discount(items, 0), total(items)); +}); + +test('discount of one hundred percent is free', () => { + assert.equal(discount(items, 100), 0); +}); + +test('discount of an empty cart is zero', () => { + assert.equal(discount([], 10), 0); +}); + +test('itemCount adds up the quantity of every item', () => { + assert.equal(itemCount(items), 3); +}); + +test('itemCount of an empty cart is zero', () => { + assert.equal(itemCount([]), 0); +}); + +test('itemCount ignores zero and negative quantities', () => { + assert.equal(itemCount([{ price: 1, qty: 0 }, { price: 1, qty: -2 }, { price: 1, qty: 4 }]), 4); +});