{"id":"code-dev-ts-if-chain-to-lookup-map","audience":"developers","category":"coding","track":"coding","technique":"constraints","title":"Refactor an if-else chain into a lookup map, plus one new rule","free":false,"task":"Refactor shippingZone into a lookup object named ZONE_BY_COUNTRY of type Record<string, string>, declared outside the function with one country per line. Every current country must map the same way, ops just asked for Norway (NO) to be EU, anything unknown stays INTL, and the trim/uppercase step stays. Keep the exported signature, no if/else or switch left, no imports. Return the code in exactly 1 code block of at most 20 lines, with under 20 words of text.","materialLabel":"The function and its tests (attached to your prompt automatically)","material":"// src/shipping.ts  (TypeScript 5.5, storefront, Pinecrest Outfitters)\n\nexport function shippingZone(country: string): string {\n  const c = country.trim().toUpperCase();\n  if (c === \"US\") {\n    return \"DOMESTIC\";\n  } else if (c === \"CA\" || c === \"MX\") {\n    return \"NORTH_AMERICA\";\n  } else if (c === \"DE\" || c === \"FR\" || c === \"NL\" || c === \"IE\") {\n    return \"EU\";\n  } else if (c === \"GB\") {\n    return \"UK\";\n  } else {\n    return \"INTL\";\n  }\n}\n\n// src/shipping.test.ts (excerpt)\n// expect(shippingZone(\"us\")).toBe(\"DOMESTIC\");\n// expect(shippingZone(\" mx \")).toBe(\"NORTH_AMERICA\");\n// expect(shippingZone(\"IE\")).toBe(\"EU\");\n// expect(shippingZone(\"GB\")).toBe(\"UK\");\n// expect(shippingZone(\"JP\")).toBe(\"INTL\");\n// expect(shippingZone(\"\")).toBe(\"INTL\");\n//\n// Used by checkout.ts, rates.ts and the admin \"zones\" report.\n// PR #418 comment (Wen): \"this chain gets longer every quarter, can we\n// make it data?\"","rules":[{"type":"codeBlocks","count":1},{"type":"maxCodeLines","max":20},{"type":"maxProseWords","max":20}],"mistakes":[{"title":"Letting the AI name it","why":"The AI picks names like zoneMap or ZONES, which won't match what the admin report will import.","fix":"Say: \"Name it ZONE_BY_COUNTRY, typed Record<string, string>, outside the function.\"","checks":["r0"]},{"title":"Missing the new country","why":"The AI only converts what's there; it can't know ops added Norway.","fix":"Add: \"Also map NO to EU.\"","checks":["r1"]},{"title":"Losing the default or the cleanup","why":"Rewrites sometimes drop trim().toUpperCase() or return undefined for unknown countries.","fix":"Add: \"Keep trim().toUpperCase(); unknown countries return INTL.\"","checks":["r1"]},{"title":"A rewrite with extras","why":"The AI adds an enum, a Zone type or a new helper file, and explains it all.","fix":"Add: \"Keep the signature, no imports, one code block, no text.\"","checks":["r2","codeBlocks","maxCodeLines","maxProseWords"]}],"promptHints":[{"label":"Names the object","patterns":["ZONE_BY_COUNTRY"]},{"label":"Adds the new country","patterns":["norway","\\bNO\\b"]},{"label":"Keeps the default","patterns":["INTL","default","unknown"]},{"label":"Keeps the cleanup","patterns":["trim","uppercase","toUpperCase"]},{"label":"Protects the signature","patterns":["signature","no imports","same (name|export)"]}],"hint":"A refactor should change shape, not results, except for the one change you actually want. Give the AI the name, the new rule and what must stay the same.","rubricLabels":["Declares const ZONE_BY_COUNTRY: Record<string, string> outside the function, with one country code per line","Maps US to DOMESTIC, CA and MX to NORTH_AMERICA, DE, FR, NL, IE and NO to EU, GB to UK, and returns INTL for anything else (e.g. ?? \"INTL\"), still applying trim().toUpperCase() first","Keeps export function shippingZone(country: string): string, with no if/else or switch left and no imports or libraries"]}