← Files TurnpointARCHIVED FILE
examples/checkout.json
1.96 KB · Nov 9, 2026 · 01:00 UTC
{
"title": "A safer path from cart to order",
"files": [
{
"path": "checkout.ts",
"before": "import { OrderStore, Cart } from './orders';\n\n/** Turn the customer's cart into an order. */\nexport function placeOrder(cart: Cart) {\n const store = new OrderStore();\n const total = calculateTotal(cart);\n return store.save(cart, total);\n}\n\n/** Add up the price of every item. */\nfunction calculateTotal(cart: Cart) {\n return cart.items.reduce((sum, item) => sum + item.price, 0);\n}\n",
"after": "import { OrderStore, Cart } from './orders';\n\n/** Turn the customer's cart into an order. */\nexport function placeOrder(cart: Cart) {\n if (!hasItems(cart)) return { error: 'Your cart is empty' };\n const store = new OrderStore();\n const total = calculateTotal(cart);\n return store.save(cart, total);\n}\n\n/** Check that there is something to order. */\nfunction hasItems(cart: Cart) {\n return cart.items.length > 0;\n}\n\n/** Add up the price of every item, including its quantity. */\nfunction calculateTotal(cart: Cart) {\n return cart.items.reduce((sum, item) => sum + item.price * item.quantity, 0);\n}\n"
},
{
"path": "orders.ts",
"before": "/** The items the customer wants to order. */\nexport interface Cart {\n items: { name: string; price: number; quantity: number }[];\n}\n\n/** Keep the details of an order together. */\nexport class OrderStore {\n /** Create an order record from a cart and its total. */\n save(cart: Cart, total: number) {\n return { items: cart.items, total, status: 'created' };\n }\n}\n",
"after": "/** The items the customer wants to order. */\nexport interface Cart {\n items: { name: string; price: number; quantity: number }[];\n}\n\n/** Keep the details of an order together. */\nexport class OrderStore {\n /** Create an order record from a cart and its total. */\n save(cart: Cart, total: number) {\n return { items: cart.items, total, status: 'created' };\n }\n}\n"
}
]
}
SHA-256: 3d9e64b672867c989726f96710b7f22ad6c6696d51cc4ec928125dac33face5f