Legacy JavaScript to modern ES2023 refactor
Views
10.9K
Copies
2.6K
Likes
1.3K
Comments
0
Copy rate
24.0%
Prompt
You are a senior JavaScript engineer. Refactor the legacy JavaScript snippet below into idiomatic modern ES2023.
Legacy source:
[legacy_source]
Target runtime: [target_runtime]
Apply every one of these transformations where applicable:
- Replace every `var` with `const` (or `let` only if reassigned)
- Replace `function` expressions with arrow functions when `this` is not needed
- Replace callback-based async (`fn(err, data) => ...`) with `async`/`await` over Promises
- Replace `.then().catch()` chains with `try`/`catch` inside `async` functions
- Use optional chaining (`?.`) and nullish coalescing (`??`) instead of manual truthy checks
- Use object/array destructuring for access patterns
- Use template literals instead of string concatenation
- Use `for...of` over manual index loops; use `Array.prototype.at(-1)` for last element
- Use `Object.groupBy`, `Array.prototype.findLast`, `Array.prototype.toSorted` where relevant
- Use top-level `await` if target runtime supports ES modules
- Replace `JSON.parse(JSON.stringify(x))` with `structuredClone(x)`
- Add JSDoc types on every exported function
Output format:
- Return only the final .js file
- No prose, no markdown fences
- End with a short `// Summary of changes` comment block listing every transformation appliedCustomise this prompt
Fill in 2 variables to personalise this prompt
Preview
You are a senior JavaScript engineer. Refactor the legacy JavaScript snippet below into idiomatic modern ES2023.
Legacy source:
[legacy_source]
Target runtime: [target_runtime]
Apply every one of these transformations where applicable:
- Replace every `var` with `const` (or `let` only if reassigned)
- Replace `function` expressions with arrow functions when `this` is not needed
- Replace callback-based async (`fn(err, data) => ...`) with `async`/`await` over Promises
- Replace `.then().catch()` chains with `try`/`catch` inside `async` functions
- Use optional chaining (`?.`) and nullish coalescing (`??`) instead of manual truthy checks
- Use object/array destructuring for access patterns
- Use template literals instead of string concatenation
- Use `for...of` over manual index loops; use `Array.prototype.at(-1)` for last element
- Use `Object.groupBy`, `Array.prototype.findLast`, `Array.prototype.toSorted` where relevant
- Use top-level `await` if target runtime supports ES modules
- Replace `JSON.parse(JSON.stringify(x))` with `structuredClone(x)`
- Add JSDoc types on every exported function
Output format:
- Return only the final .js file
- No prose, no markdown fences
- End with a short `// Summary of changes` comment block listing every transformation applied