diff --git a/CHANGES.md b/CHANGES.md index 9c38282..3f23923 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased +- Add discount(items, percent): total with the percentage taken off the subtotal before tax. ## 1.2.0 - total() rounds through roundCents(). diff --git a/src/lib/cart.js b/src/lib/cart.js index 385fd69..7d5e535 100644 --- a/src/lib/cart.js +++ b/src/lib/cart.js @@ -8,3 +8,8 @@ export function subtotal(items) { export function total(items) { return roundCents(subtotal(items) * (1 + TAX_RATE)); } + +export function discount(items, percent) { + const discounted = subtotal(items) * (1 - percent / 100); + return roundCents(discounted * (1 + TAX_RATE)); +}