{"schemaVersion":1,"contentVersion":"3.4.0","id":"intermediate.10","languages":{"javascript":{"id":"intermediate.10","title":"Build one repeatable change","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p><strong>Before this lesson:</strong> Insertion doors and character search.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h3>Example</h3><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p>","teaches":["dot","door-A","door-O","door-i"],"introduces":[".","A","O","i","j","k","0","$"],"constraints":[],"drills":[{"task":"Turn off the two lines that log every event, so they stay in the file for debugging.","hint":"Make the change on the first line, then move to the next and press the dot.","mode":"jsx","start":"import { getConsent } from \"../privacy/consent\";\n\nconst ENDPOINT = \"https://stats.northwind.shop/collect\";\n\n// Sends one analytics event, if the visitor said yes to statistics.\nexport function track(name, props = {}) {\n  console.log(\"track\", name);\n  console.log(\"props\", props);\n  if (!getConsent().statistics) return;\n  const body = JSON.stringify({ name, props, page: location.pathname });\n  navigator.sendBeacon(ENDPOINT, body);\n}","startCursor":{"line":6,"ch":2},"paths":[{"keys":["I","// ","<Esc>","j","."],"tier":"idiomatic","technique":"dot","core":["dot","door-I"],"note":"The dot repeats your last change, here everything from I to Esc."}],"target":"import { getConsent } from \"../privacy/consent\";\n\nconst ENDPOINT = \"https://stats.northwind.shop/collect\";\n\n// Sends one analytics event, if the visitor said yes to statistics.\nexport function track(name, props = {}) {\n  // console.log(\"track\", name);\n  // console.log(\"props\", props);\n  if (!getConsent().statistics) return;\n  const body = JSON.stringify({ name, props, page: location.pathname });\n  navigator.sendBeacon(ENDPOINT, body);\n}","language":"javascript","evidence":"guided","canonicalKey":"intermediate.10#0","canonicalFingerprint":"7l9qh1"},{"task":"An order needs a customer and a total. Make <code>customer_id</code> and <code>total</code> required by ending both with <code>.notNullable()</code>.","hint":"A adds text at the end of the line, however long it is, so the dot works on any line.","mode":"jsx","start":"// Each order stores its own total, so a later price change can't alter old orders.\nexports.up = (knex) =>\n  knex.schema.createTable(\"orders\", (table) => {\n    table.increments(\"id\")\n    table.integer(\"customer_id\").references(\"customers.id\")\n    table.string(\"number\").unique()\n    table.decimal(\"total\", 10, 2)\n    table.string(\"currency\", 3).defaultTo(\"EUR\")\n    table.string(\"status\").defaultTo(\"open\")\n    table.timestamps(true, true)\n  })\n\nexports.down = (knex) => knex.schema.dropTable(\"orders\")","startCursor":{"line":3,"ch":4},"paths":[{"keys":["j","A",".notNullable()","<Esc>","2","j","."],"tier":"idiomatic","technique":"door-A","core":["door-A","dot"],"note":"A includes the jump to the end of the line, so the dot adds the same text to a longer line too."}],"target":"// Each order stores its own total, so a later price change can't alter old orders.\nexports.up = (knex) =>\n  knex.schema.createTable(\"orders\", (table) => {\n    table.increments(\"id\")\n    table.integer(\"customer_id\").references(\"customers.id\").notNullable()\n    table.string(\"number\").unique()\n    table.decimal(\"total\", 10, 2).notNullable()\n    table.string(\"currency\", 3).defaultTo(\"EUR\")\n    table.string(\"status\").defaultTo(\"open\")\n    table.timestamps(true, true)\n  })\n\nexports.down = (knex) => knex.schema.dropTable(\"orders\")","language":"javascript","evidence":"guided","canonicalKey":"intermediate.10#1","canonicalFingerprint":"hyabwl"},{"task":"<code>formatPrice</code> and <code>formatRefund</code> are going away. Put <code>/** @deprecated Use formatMoney. */</code> on the line above each of them.","hint":"O opens a line above the cursor. The dot opens another one above the line you're on.","mode":"jsx","start":"const formatters = new Map();\n\n// One Intl formatter per currency, made the first time it's needed.\nexport function formatMoney(cents, currency = \"EUR\") {\n  if (!formatters.has(currency)) {\n    formatters.set(currency, new Intl.NumberFormat(\"en-IE\", { style: \"currency\", currency }));\n  }\n  return formatters.get(currency).format(cents / 100);\n}\n\nexport function formatPrice(product) {\n  return formatMoney(product.priceCents, product.currency);\n}\n\nexport function formatRefund(refund) {\n  return \"-\" + formatMoney(refund.cents, refund.currency);\n}","startCursor":{"line":10,"ch":37},"paths":[{"keys":["O","/** @deprecated Use formatMoney. */","<Esc>","}","j","."],"tier":"idiomatic","technique":"door-O","core":["door-O","dot"],"note":"The dot repeats the whole O change, the new line and its text."}],"target":"const formatters = new Map();\n\n// One Intl formatter per currency, made the first time it's needed.\nexport function formatMoney(cents, currency = \"EUR\") {\n  if (!formatters.has(currency)) {\n    formatters.set(currency, new Intl.NumberFormat(\"en-IE\", { style: \"currency\", currency }));\n  }\n  return formatters.get(currency).format(cents / 100);\n}\n\n/** @deprecated Use formatMoney. */\nexport function formatPrice(product) {\n  return formatMoney(product.priceCents, product.currency);\n}\n\n/** @deprecated Use formatMoney. */\nexport function formatRefund(refund) {\n  return \"-\" + formatMoney(refund.cents, refund.currency);\n}","language":"javascript","evidence":"guided","canonicalKey":"intermediate.10#2","canonicalFingerprint":"1ka149x"},{"task":"<code>Date()</code> without <code>new</code> ignores its argument and returns today as a string. Add <code>new</code> before both <code>Date</code> calls.","hint":"i types where the cursor is, so put the cursor in the right place before you press the dot.","mode":"jsx","start":"import { startOfDay, endOfDay } from \"./days\";\n\n// The range the sales report covers. The picker gives us ISO dates.\nexport function reportRange(range) {\n  if (!range.from || !range.to) {\n    throw new Error(\"Pick a start and an end date\");\n  }\n  return { start: Date(range.from), end: Date(range.to) };\n}\n\nexport const wholeDay = (day) => ({ start: startOfDay(day), end: endOfDay(day) });","startCursor":{"line":7,"ch":58},"paths":[{"keys":["F","D","i","new ","<Esc>",";","."],"tier":"idiomatic","technique":"door-i","core":["door-i","dot"],"note":"The dot repeats the insert where the cursor is now, so move first."}],"target":"import { startOfDay, endOfDay } from \"./days\";\n\n// The range the sales report covers. The picker gives us ISO dates.\nexport function reportRange(range) {\n  if (!range.from || !range.to) {\n    throw new Error(\"Pick a start and an end date\");\n  }\n  return { start: new Date(range.from), end: new Date(range.to) };\n}\n\nexport const wholeDay = (day) => ({ start: startOfDay(day), end: endOfDay(day) });","language":"javascript","evidence":"guided","canonicalKey":"intermediate.10#3","canonicalFingerprint":"1ah75j9"}]},"python":{"id":"intermediate.10","title":"Build one repeatable change","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p><strong>Before this lesson:</strong> Insertion doors and character search.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h3>Example</h3><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p>","teaches":["dot","door-A","door-O","door-i"],"introduces":[".","A","O","i","j","k","0","$"],"constraints":[],"drills":[{"task":"Turn off the two lines that print every webhook, so they stay in the file for debugging.","hint":"Make the change on the first line, then move to the next and press the dot.","mode":"python","start":"import json\n\nfrom django.http import HttpResponse\n\nfrom .handlers import HANDLERS\n\n\ndef stripe_webhook(request):\n    event = json.loads(request.body)\n    print(\"webhook\", event[\"type\"])\n    print(\"payload\", event[\"data\"])\n    handler = HANDLERS.get(event[\"type\"])\n    if handler:\n        handler(event[\"data\"][\"object\"])\n    return HttpResponse(status=200)","startCursor":{"line":9,"ch":4},"paths":[{"keys":["I","# ","<Esc>","j","."],"tier":"idiomatic","technique":"dot","core":["dot","door-I"],"note":"The dot repeats your last change, here everything from I to Esc."}],"target":"import json\n\nfrom django.http import HttpResponse\n\nfrom .handlers import HANDLERS\n\n\ndef stripe_webhook(request):\n    event = json.loads(request.body)\n    # print(\"webhook\", event[\"type\"])\n    # print(\"payload\", event[\"data\"])\n    handler = HANDLERS.get(event[\"type\"])\n    if handler:\n        handler(event[\"data\"][\"object\"])\n    return HttpResponse(status=200)","language":"python","evidence":"guided","canonicalKey":"intermediate.10#0","canonicalFingerprint":"7l9qh1"},{"task":"Mypy can't find types for <code>stripe</code> and <code>redis</code>. End both of those imports with <code>  # type: ignore</code>.","hint":"A adds text at the end of the line, however long it is, so the dot works on any line.","mode":"python","start":"\"\"\"Payment helpers shared by checkout and the refund admin.\"\"\"\nfrom decimal import Decimal\n\nimport stripe\nfrom django.db import transaction\nfrom redis import Redis\n\nfrom shop.config import payment_settings\n\nstripe.api_key = payment_settings.stripe_secret_key\nlocks = Redis.from_url(payment_settings.redis_url)\n\n\ndef to_cents(amount: Decimal) -> int:\n    return int(amount * 100)","startCursor":{"line":0,"ch":0},"paths":[{"keys":["3","j","A","  # type: ignore","<Esc>","2","j","."],"tier":"idiomatic","technique":"door-A","core":["door-A","dot"],"note":"A includes the jump to the end of the line, so the dot adds the same text to a longer line too."}],"target":"\"\"\"Payment helpers shared by checkout and the refund admin.\"\"\"\nfrom decimal import Decimal\n\nimport stripe  # type: ignore\nfrom django.db import transaction\nfrom redis import Redis  # type: ignore\n\nfrom shop.config import payment_settings\n\nstripe.api_key = payment_settings.stripe_secret_key\nlocks = Redis.from_url(payment_settings.redis_url)\n\n\ndef to_cents(amount: Decimal) -> int:\n    return int(amount * 100)","language":"python","evidence":"guided","canonicalKey":"intermediate.10#1","canonicalFingerprint":"hyabwl"},{"task":"Both refund tests call the payment sandbox. Mark each of them with <code>@pytest.mark.slow</code> on the line above.","hint":"O opens a line above the cursor. The dot opens another one above the line you're on.","mode":"python","start":"import pytest\n\nfrom shop.payments.refunds import parse_amount, refund\n\n\ndef test_refunds_the_whole_order(paid_order):\n    result = refund(paid_order)\n    assert result.status == \"succeeded\"\n\n\ndef test_refunds_part_of_an_order(paid_order):\n    result = refund(paid_order, amount=500)\n    assert result.amount == 500\n\n\ndef test_parses_amounts_with_a_comma():\n    assert parse_amount(\"12,50\") == 1250","startCursor":{"line":5,"ch":39},"paths":[{"keys":["O","@pytest.mark.slow","<Esc>","6","j","."],"tier":"idiomatic","technique":"door-O","core":["door-O","dot"],"note":"The dot repeats the whole O change, the new line and its text."}],"target":"import pytest\n\nfrom shop.payments.refunds import parse_amount, refund\n\n\n@pytest.mark.slow\ndef test_refunds_the_whole_order(paid_order):\n    result = refund(paid_order)\n    assert result.status == \"succeeded\"\n\n\n@pytest.mark.slow\ndef test_refunds_part_of_an_order(paid_order):\n    result = refund(paid_order, amount=500)\n    assert result.amount == 500\n\n\ndef test_parses_amounts_with_a_comma():\n    assert parse_amount(\"12,50\") == 1250","language":"python","evidence":"guided","canonicalKey":"intermediate.10#2","canonicalFingerprint":"1ka149x"},{"task":"<code>get_cart</code> and <code>get_customer</code> are coroutines now. Await both, as the shipping quote does.","hint":"i types where the cursor is, so put the cursor in the right place before you press the dot.","mode":"python","start":"from django.http import JsonResponse\n\nfrom .cart import get_cart\nfrom .customers import get_customer\nfrom .shipping import quote_shipping\n\n\nasync def checkout_summary(request, cart_id):\n    customer_id = request.session[\"customer_id\"]\n    cart, customer = get_cart(cart_id), get_customer(customer_id)\n    shipping = await quote_shipping(cart, customer.address)\n    return JsonResponse({\"cart\": cart.as_dict(), \"shipping\": shipping})","startCursor":{"line":9,"ch":64},"paths":[{"keys":["F","g","i","await ","<Esc>",";","."],"tier":"idiomatic","technique":"door-i","core":["door-i","dot"],"note":"The dot repeats the insert where the cursor is now, so move first."}],"target":"from django.http import JsonResponse\n\nfrom .cart import get_cart\nfrom .customers import get_customer\nfrom .shipping import quote_shipping\n\n\nasync def checkout_summary(request, cart_id):\n    customer_id = request.session[\"customer_id\"]\n    cart, customer = await get_cart(cart_id), await get_customer(customer_id)\n    shipping = await quote_shipping(cart, customer.address)\n    return JsonResponse({\"cart\": cart.as_dict(), \"shipping\": shipping})","language":"python","evidence":"guided","canonicalKey":"intermediate.10#3","canonicalFingerprint":"1ah75j9"}]},"text":{"id":"intermediate.10","title":"Build one repeatable change","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p><strong>Before this lesson:</strong> Insertion doors and character search.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h3>Example</h3><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p>","teaches":["dot","door-A","door-O","door-i"],"introduces":[".","A","O","i","j","k","0","$"],"constraints":[],"drills":[{"task":"Mail isn't set up for local work. Comment out the two Mailgun lines.","hint":"Make the change on the first line, then move to the next and press the dot.","mode":"text/plain","start":"# Local settings for the shop. Copy this file to .env and fill in the blanks.\nDATABASE_URL=postgres://localhost:5432/shop\nREDIS_URL=redis://localhost:6379\nSTRIPE_SECRET_KEY=sk_test_\nSTRIPE_WEBHOOK_SECRET=whsec_\nMAILGUN_DOMAIN=mg.northwind.shop\nMAILGUN_API_KEY=\nSENTRY_DSN=\nLOG_LEVEL=debug","startCursor":{"line":5,"ch":0},"paths":[{"keys":["I","# ","<Esc>","j","."],"tier":"idiomatic","technique":"dot","core":["dot","door-I"],"note":"The dot repeats your last change, here everything from I to Esc."}],"target":"# Local settings for the shop. Copy this file to .env and fill in the blanks.\nDATABASE_URL=postgres://localhost:5432/shop\nREDIS_URL=redis://localhost:6379\nSTRIPE_SECRET_KEY=sk_test_\nSTRIPE_WEBHOOK_SECRET=whsec_\n# MAILGUN_DOMAIN=mg.northwind.shop\n# MAILGUN_API_KEY=\nSENTRY_DSN=\nLOG_LEVEL=debug","language":"text","evidence":"guided","canonicalKey":"intermediate.10#0","canonicalFingerprint":"7l9qh1"},{"task":"Dana is taking the changelog and the deploy. End those two items with <code> (Dana)</code>.","hint":"A adds text at the end of the line, however long it is, so the dot works on any line.","mode":"text/plain","start":"# Release checklist\n\nWork through this list on release day, in order.\n\n- [ ] Freeze the main branch\n- [ ] Run the full test suite\n- [ ] Update the changelog\n- [ ] Tag the release\n- [ ] Deploy to production\n- [ ] Post in #shop-releases","startCursor":{"line":5,"ch":6},"paths":[{"keys":["j","A"," (Dana)","<Esc>","2","j","."],"tier":"idiomatic","technique":"door-A","core":["door-A","dot"],"note":"A includes the jump to the end of the line, so the dot adds the same text to a longer line too."}],"target":"# Release checklist\n\nWork through this list on release day, in order.\n\n- [ ] Freeze the main branch\n- [ ] Run the full test suite\n- [ ] Update the changelog (Dana)\n- [ ] Tag the release\n- [ ] Deploy to production (Dana)\n- [ ] Post in #shop-releases","language":"text","evidence":"guided","canonicalKey":"intermediate.10#1","canonicalFingerprint":"hyabwl"},{"task":"Put a <code>---</code> line above the last two questions, so each shows as its own section.","hint":"O opens a line above the cursor. The dot opens another one above the line you're on.","mode":"text/plain","start":"# Help with orders\n\n## Where is my order?\nOrders ship within two working days. You'll get an email with a tracking link.\n\n## Can I change my address?\nYes, until the order ships. Reply to your order email with the new address.\n\n## How do I send something back?\nUse the returns form within 30 days. Refunds take up to five working days.","startCursor":{"line":5,"ch":20},"paths":[{"keys":["O","---","<Esc>","}","j","."],"tier":"idiomatic","technique":"door-O","core":["door-O","dot"],"note":"The dot repeats the whole O change, the new line and its text."}],"target":"# Help with orders\n\n## Where is my order?\nOrders ship within two working days. You'll get an email with a tracking link.\n\n---\n## Can I change my address?\nYes, until the order ships. Reply to your order email with the new address.\n\n---\n## How do I send something back?\nUse the returns form within 30 days. Refunds take up to five working days.","language":"text","evidence":"guided","canonicalKey":"intermediate.10#2","canonicalFingerprint":"1ka149x"},{"task":"Mention Dana and Kemal with an <code>@</code> before each name, so Slack lets them know.","hint":"i types where the cursor is, so put the cursor in the right place before you press the dot.","mode":"text/plain","start":"Release 4.2 is out.\n\nWhat's new:\n- Search is faster on the catalog pages\n- Checkout remembers your addresses\n- Every email is in Dutch too\n\nThanks Dana and Kemal for the release notes, and Priya for testing it all.\nQuestions go to #shop-releases.","startCursor":{"line":8,"ch":0},"paths":[{"keys":["k","w","i","@","<Esc>","f","K","."],"tier":"idiomatic","technique":"door-i","core":["door-i","dot"],"note":"The dot repeats the insert where the cursor is now, so move first."}],"target":"Release 4.2 is out.\n\nWhat's new:\n- Search is faster on the catalog pages\n- Checkout remembers your addresses\n- Every email is in Dutch too\n\nThanks @Dana and @Kemal for the release notes, and Priya for testing it all.\nQuestions go to #shop-releases.","language":"text","evidence":"guided","canonicalKey":"intermediate.10#3","canonicalFingerprint":"1ah75j9"}]}},"concepts":{"javascript":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Insertion doors and character search.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h2 class=\"prose-subhead\">Example</h2><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p></div></details>"},"python":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Insertion doors and character search.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h2 class=\"prose-subhead\">Example</h2><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p></div></details>"},"text":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Insertion doors and character search.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Make a change once, in a way the dot can repeat.</p><p>The dot repeats your last complete change, from the command that starts it to Esc. A includes the jump to the end of the line, so it works on lines of any length. I does the same for the start of the line. i types where the cursor is, so move to the right place before you repeat it. O opens a line above the one you're on. The dot repeats the change, not the moves you made before it.</p><h2 class=\"prose-subhead\">Example</h2><p>Press A, type a status at the end of the line and press Esc. Then press j and the dot. It works even if the next line is a different length, because A does the jump to the end for you.</p><p>Use Start over to try again.</p></div></details>"}}}