diff --git a/src/summary.js b/src/summary.js new file mode 100644 index 0000000..4a4cc5d --- /dev/null +++ b/src/summary.js @@ -0,0 +1,9 @@ +import { applyDiscount, subtotal } from "./cart.js"; +import { salesTax } from "./tax.js"; + +export function orderSummary(items, { discountPercent = 0, taxRatePercent = 0 } = {}) { + const itemsTotal = subtotal(items); + const discounted = applyDiscount(itemsTotal, discountPercent); + const tax = salesTax(discounted, taxRatePercent); + return { itemsTotal, discounted, tax, total: discounted + tax }; +} diff --git a/src/summary.test.js b/src/summary.test.js new file mode 100644 index 0000000..ddd1b99 --- /dev/null +++ b/src/summary.test.js @@ -0,0 +1,8 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { orderSummary } from "./summary.js"; + +test("orderSummary applies the discount before tax", () => { + const summary = orderSummary([{ price: 1000, quantity: 2 }], { discountPercent: 10, taxRatePercent: 10 }); + assert.deepEqual(summary, { itemsTotal: 2000, discounted: 1800, tax: 180, total: 1980 }); +});