{"schemaVersion":1,"contentVersion":"3.4.0","id":"intermediate.11","languages":{"javascript":{"id":"intermediate.11","title":"Search, change, inspect, repeat","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h3>Example</h3><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p>","teaches":["motion:gn"],"introduces":["search","n",".","gn","c"],"constraints":[],"drills":[{"task":"The money helper is called <code>formatMoney</code> now. Rename <code>fmt</code> in the import and in its four calls.","hint":"Search for the name, change the match with cgn, then press the dot for each of the others.","mode":"jsx","start":"import { fmt } from \"../lib/money\";\n\n// The summary beside the checkout form. It updates as the cart changes.\nexport function OrderSummary({ order, onEdit }) {\n  const hasDiscount = order.discountCents > 0;\n  const freeShipping = order.shippingCents === 0;\n\n  return (\n    <aside className=\"order-summary\">\n      <h2>Your order</h2>\n      <p>\n        {order.lines.length} items, {fmt(order.subtotalCents)}\n      </p>\n      <button type=\"button\" onClick={onEdit}>\n        Edit cart\n      </button>\n      {hasDiscount && (\n        <p className=\"order-discount\">\n          {order.discountCode}: -{fmt(order.discountCents)}\n        </p>\n      )}\n      <p>\n        Shipping: {freeShipping ? \"free\" : fmt(order.shippingCents)}\n      </p>\n      <p className=\"order-total\">\n        Total {fmt(order.totalCents)}\n      </p>\n    </aside>\n  );\n}","startCursor":{"line":3,"ch":0},"paths":[{"keys":["/","fmt","<CR>","c","g","n","formatMoney","<Esc>",".",".",".","."],"tier":"idiomatic","technique":"motion:gn","core":["motion:gn","dot","search:/"],"note":"cgn changes the match it finds and the dot changes the next one, wherever it is in the file."}],"target":"import { formatMoney } from \"../lib/money\";\n\n// The summary beside the checkout form. It updates as the cart changes.\nexport function OrderSummary({ order, onEdit }) {\n  const hasDiscount = order.discountCents > 0;\n  const freeShipping = order.shippingCents === 0;\n\n  return (\n    <aside className=\"order-summary\">\n      <h2>Your order</h2>\n      <p>\n        {order.lines.length} items, {formatMoney(order.subtotalCents)}\n      </p>\n      <button type=\"button\" onClick={onEdit}>\n        Edit cart\n      </button>\n      {hasDiscount && (\n        <p className=\"order-discount\">\n          {order.discountCode}: -{formatMoney(order.discountCents)}\n        </p>\n      )}\n      <p>\n        Shipping: {freeShipping ? \"free\" : formatMoney(order.shippingCents)}\n      </p>\n      <p className=\"order-total\">\n        Total {formatMoney(order.totalCents)}\n      </p>\n    </aside>\n  );\n}","language":"javascript","evidence":"guided","canonicalKey":"intermediate.11#0","canonicalFingerprint":"1c5tow9"}]},"python":{"id":"intermediate.11","title":"Search, change, inspect, repeat","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h3>Example</h3><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p>","teaches":["motion:gn"],"introduces":["search","n",".","gn","c"],"constraints":[],"drills":[{"task":"The money helper is called <code>format_money</code> now. Rename <code>fmt</code> in the import and in its four calls.","hint":"Search for the name, change the match with cgn, then press the dot for each of the others.","mode":"python","start":"from django.template import loader\n\nfrom shop.money import fmt\n\n\ndef receipt_lines(order):\n    \"\"\"The lines of the plain-text receipt, one per item and total.\"\"\"\n    lines = []\n    for item in order.items.all():\n        lines.append(f\"{item.quantity} x {item.name}  {fmt(item.total)}\")\n\n    if order.discount:\n        lines.append(f\"Discount ({order.discount_code})  -{fmt(order.discount)}\")\n\n    shipping = \"free\" if not order.shipping else fmt(order.shipping)\n    lines.append(f\"Shipping  {shipping}\")\n\n    lines.append(f\"Total  {fmt(order.total)}\")\n    return lines\n\n\ndef render_receipt(order):\n    return loader.render_to_string(\n        \"emails/receipt.txt\",\n        {\"order\": order, \"lines\": receipt_lines(order)},\n    )","startCursor":{"line":5,"ch":0},"paths":[{"keys":["/","fmt","<CR>","c","g","n","format_money","<Esc>",".",".",".","."],"tier":"idiomatic","technique":"motion:gn","core":["motion:gn","dot","search:/"],"note":"cgn changes the match it finds and the dot changes the next one, wherever it is in the file."}],"target":"from django.template import loader\n\nfrom shop.money import format_money\n\n\ndef receipt_lines(order):\n    \"\"\"The lines of the plain-text receipt, one per item and total.\"\"\"\n    lines = []\n    for item in order.items.all():\n        lines.append(f\"{item.quantity} x {item.name}  {format_money(item.total)}\")\n\n    if order.discount:\n        lines.append(f\"Discount ({order.discount_code})  -{format_money(order.discount)}\")\n\n    shipping = \"free\" if not order.shipping else format_money(order.shipping)\n    lines.append(f\"Shipping  {shipping}\")\n\n    lines.append(f\"Total  {format_money(order.total)}\")\n    return lines\n\n\ndef render_receipt(order):\n    return loader.render_to_string(\n        \"emails/receipt.txt\",\n        {\"order\": order, \"lines\": receipt_lines(order)},\n    )","language":"python","evidence":"guided","canonicalKey":"intermediate.11#0","canonicalFingerprint":"1c5tow9"}]},"text":{"id":"intermediate.11","title":"Search, change, inspect, repeat","moduleName":"Repeat, select and compose","tips":"","concept":"<p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h3>Example</h3><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p>","teaches":["motion:gn"],"introduces":["search","n",".","gn","c"],"constraints":[],"drills":[{"task":"The primary database is called <code>pg-main</code> now. Rename all five <code>db1</code>.","hint":"Search for the name, change the match with cgn, then press the dot for each of the others.","mode":"text/plain","start":"# The shop database\n\n## Where it runs\nThe shop uses one Postgres server, db1, in the Amsterdam data centre.\nA replica follows it with a few seconds of delay. Reports read from the replica.\n\n## Connecting\nAsk in #it-help for an account. You get read access to the replica only.\nWrites go through the app. Nobody connects to db1 by hand during opening hours.\n\n## Backups\nA full backup runs at 02:00 every night and is kept for 30 days.\nRestoring one takes about forty minutes, so test it once a quarter.\n\n## Slow queries\nAnything slower than 500 ms on db1 shows up in Sentry with its query plan.\nFix it in the app if you can. Add an index only after you've talked to Dana.\n\n## When it's down\nThe status page turns red after two failed health checks.\nFail over to the replica with the runbook in the ops repo, then promote it.\nOnce it's back, rebuild db1 from the newest backup and make it the replica.\nTell #shop-releases when db1 is a replica again.","startCursor":{"line":2,"ch":0},"paths":[{"keys":["/","db1","<CR>","c","g","n","pg-main","<Esc>",".",".",".","."],"tier":"idiomatic","technique":"motion:gn","core":["motion:gn","dot","search:/"],"note":"cgn changes the match it finds and the dot changes the next one, wherever it is in the file."}],"target":"# The shop database\n\n## Where it runs\nThe shop uses one Postgres server, pg-main, in the Amsterdam data centre.\nA replica follows it with a few seconds of delay. Reports read from the replica.\n\n## Connecting\nAsk in #it-help for an account. You get read access to the replica only.\nWrites go through the app. Nobody connects to pg-main by hand during opening hours.\n\n## Backups\nA full backup runs at 02:00 every night and is kept for 30 days.\nRestoring one takes about forty minutes, so test it once a quarter.\n\n## Slow queries\nAnything slower than 500 ms on pg-main shows up in Sentry with its query plan.\nFix it in the app if you can. Add an index only after you've talked to Dana.\n\n## When it's down\nThe status page turns red after two failed health checks.\nFail over to the replica with the runbook in the ops repo, then promote it.\nOnce it's back, rebuild pg-main from the newest backup and make it the replica.\nTell #shop-releases when pg-main is a replica again.","language":"text","evidence":"guided","canonicalKey":"intermediate.11#0","canonicalFingerprint":"1c5tow9"}]}},"concepts":{"javascript":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h2 class=\"prose-subhead\">Example</h2><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p></div></details>"},"python":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h2 class=\"prose-subhead\">Example</h2><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p></div></details>"},"text":{"lead":"","reminder":"<p class=\"lesson-prereq\"><strong>Before this lesson:</strong> Search reuse and repeatable changes.</p>","more":"<details class=\"lesson-more\"><summary>More about this lesson</summary><div class=\"lesson-more-body prose\"><p class=\"lead\">Repeat a change on some search matches and skip the ones that should stay.</p><p>Search, make a change, press n to look at the next match, then press the dot only where the same edit belongs. cgn makes finding the match part of the change, so the dot then finds and changes the next match in one go, however far down the file it is. Search for the whole name, because cgn changes only what the search matched. Stop before any match that should stay.</p><h2 class=\"prose-subhead\">Example</h2><p>If two current lines and an archive all say pending, change the two current lines and leave the archive. Check each match before you repeat.</p><p>Use Start over to try again.</p></div></details>"}}}