4.8 KiB
cultures-generator.js
You are an expert senior JavaScript developer specializing in refactoring legacy code into modern, modular, and environment-agnostic libraries. You have a deep understanding of design patterns like dependency injection and the separation of concerns.
Your Goal:
Your task is to refactor a single JavaScript module from a legacy Fantasy Map Generator application. The goal is to migrate it from its old, browser-dependent format into a pure, headless-first ES module that will be part of a core generation engine. This engine must be able to run in any JavaScript environment, including Node.js, without any dependencies on a browser or DOM.
Architectural Context:
- Old Architecture: The original code is wrapped in an IIFE and attaches its exports to the global
windowobject. It directly reads from and mutates global state variables likepackandgrid, and directly accesses the DOM viabyId(). - New Architecture (Target):
- Core Engine: A collection of pure ES modules. It receives all necessary data (
pack,grid) and configuration as function arguments. It performs its logic and returns the newly generated data. It has zero knowledge of the browser. - Viewer/Client: The application responsible for all DOM interaction, UI, and rendering SVG based on the data object produced by the engine.
- Core Engine: A collection of pure ES modules. It receives all necessary data (
The Golden Rules of Refactoring for the Core Engine:
- No Globals: Remove the IIFE and the attachment to the
windowobject. - Use ES Modules: All exported functions and data must use the
exportkeyword. - Dependency Injection: Functions must not read from or mutate global state. All data they need (
pack,grid) must be passed in as arguments. - Introduce a
configObject:- When you find code that reads a value from the DOM (e.g.,
byId("statesNumber").value), this is a configuration parameter. - You must replace this DOM call with a property from a
configobject (e.g.,config.statesNumber). - Add this
configobject as a new argument to the function's signature.
- When you find code that reads a value from the DOM (e.g.,
- Return New Data: Instead of modifying an object in place (e.g.,
pack.cells.biome = ...), functions should create the new data and return it. The calling function will be responsible for merging this data into the main state object. - Pure functions: Functions should not have side effects. They should either return a new state object or a specific piece of data.
- Strict Separation of Concerns (Crucial):
- UI Input Reading: As per Rule #4, these
byId()calls are your guide to what properties theconfigobject needs. - Rendering Logic: Any code that writes to the DOM or SVG (e.g.,
d3.select,document.getElementById(...).innerHTML = ..., creating<path>elements, etc.) is considered rendering logic. - You must REMOVE all rendering logic from the engine module.
- UI Input Reading: As per Rule #4, these
- Maintain Style: Preserve the original code style, comments, and variable names as much as possible for consistency.
- Efficient Destructuring: When passing a utils object, only destructure the specific properties needed within the scope of the function that uses them, rather than destructuring the entire object at the top of every function. This improves clarity and reduces code repetition.
Concrete Example of Refactoring:
BEFORE (Legacy burgs-and-states.js):
// ...
function placeCapitals() {
// Direct DOM read - THIS IS A CONFIGURATION VALUE
let count = +byId("statesNumber").value;
// ...
}
// ...
AFTER (Refactored engine/modules/burgsAndStates.js):
// ...
// Dependencies, including the new `config` object, are injected.
export function placeCapitals(cells, graphWidth, graphHeight, config) {
// DOM read is replaced by a property from the `config` object.
let count = config.statesNumber;
// ...
// Returns the generated data
return { burgs, states };
}
// ...
Your Specific Task:
Now, please apply these principles to refactor the following module: cultures-generator.js.
File Content:
Instructions:
Provide a response in three parts:
- Refactored Code: The complete JavaScript code for the new ES module in ./cultures-generator.js
- Engine Dependencies:
- List the external modules the refactored code will need to
import(e.g.,Names,COA) in ./cultures-generator_external.md - List the new
configproperties you identified and used (e.g.,statesNumber,growthRate) in cultures-generator_config.md This is essential.
- List the external modules the refactored code will need to
- Removed Rendering/UI Logic: List all the code blocks related to DOM manipulation or SVG rendering that you have removed so they can be moved to the Viewer application into cultures-generator_render.md