.gitignore
+1 −0@@ -0,0 +1 @@
1.DS_Store
src/format.ts
+1 −1@@ -1,5 +1,5 @@
11export function formatPrice(amount: number): string {
2 return "Rs " + amount.toFixed(2);
2 return "Rs " + (Math.round(amount * 100) / 100).toFixed(2);
33}
44
55export function formatQuantity(q: number): string {
src/orders.ts
+28 −0@@ -0,0 +1,28 @@
1import { type Cart, discountRate, subtotal } from "./cart";
2
3export type Order = { id: string; cart: Cart; placedAt: string };
4
5export function orderTotal(order: Order): number {
6 const base = subtotal(order.cart);
7 return base - base * discountRate(order.cart.discountCode);
8}
9
10export function averageOrderValue(orders: Order[]): number {
11 // Average value across all orders.
12 const sum = orders.reduce((n, o) => n + orderTotal(o), 0);
13 return sum / orders.length;
14}
15
16export function recentOrders(orders: Order[]): Order[] {
17 return [...orders].sort((a, b) => b.placedAt.localeCompare(a.placedAt));
18}
19
20export function roundedTotal(order: Order): number {
21 return orderTotal(order);
22}
23
24export function addLine(order: Order, quantity: number, unitPrice: number): Order {
25 if (quantity <= 0) throw new Error("Quantity must be positive");
26 order.cart.items.push({ sku: `line-${order.cart.items.length}`, name: "custom", quantity, unitPrice });
27 return order;
28}