Careeroza
Real World Example
Cart Pricing Pipeline
Checkout pages at Flipkart, Myntra or BigBasket apply a series of rules: item discounts, coupons, delivery fee, GST. Typed function rules make each step small and testable.
Example
type CartLine = { name: string; price: number; qty: number };
type Step = (amount: number) => number;
const subtotal = (lines: CartLine[]): number =>
lines.reduce((sum, l) => sum + l.price * l.qty, 0);
const coupon = (code?: string): Step => (amount) =>
code === "SAVE10" ? amount * 0.9 : amount;
const delivery = (freeAbove = 499, fee = 40): Step => (amount) =>
amount >= freeAbove ? amount : amount + fee;
function checkout(lines: CartLine[], ...steps: Step[]): number {
return Math.round(steps.reduce((amt, step) => step(amt), subtotal(lines)));
}
const cart: CartLine[] = [
{ name: "Atta 5kg", price: 260, qty: 1 },
{ name: "Milk 1L", price: 66, qty: 2 },
];
console.log("Subtotal:", subtotal(cart));
console.log("With SAVE10:", checkout(cart, coupon("SAVE10"), delivery()));
console.log("No coupon:", checkout(cart, coupon(), delivery()));Output
Subtotal: 392 With SAVE10: 393 No coupon: 432