diff --git a/CHANGES.md b/CHANGES.md index 9c38282..82eacd8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ # Changes ## Unreleased +- Add discount(items, percent): total after a percentage off, before tax. +- Tax rate is now 8.25%. ## 1.2.0 - total() rounds through roundCents(). diff --git a/src/generated/rates.js b/src/generated/rates.js index d60b834..f0ffeeb 100644 --- a/src/generated/rates.js +++ b/src/generated/rates.js @@ -1,2 +1,2 @@ // Exported from the pricing service. -export const TAX_RATE = 0.08; +export const TAX_RATE = 0.0825; diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..332f9ce 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,7 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function discount(items, percent) { + return roundCents(subtotal(items) * (1 - percent / 100) * (1 + TAX_RATE)); +} diff --git a/tests/lib/cart.test.js b/tests/lib/cart.test.js index 7a9eab3..d54b54f 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, discount } from '../../src/lib/cart.js'; const items = [{ price: 10, qty: 2 }, { price: 5, qty: 1 }]; @@ -9,5 +9,9 @@ test('subtotal adds price times quantity', () => { }); test('total applies tax and rounds to cents', () => { - assert.equal(total(items), 27); + assert.equal(total(items), 27.06); +}); + +test('discount takes percent off before tax', () => { + assert.equal(discount(items, 10), 24.36); });