Port a Function or Module to Another Language With Tests Proving Same Behavior

Lists the source code's behaviors and edge cases, writes tests in the target language first, ports the code idiomatically, and gives a table of library mappings and semantics that differ.

You getBehavior list, tests in one code block and ported code in one code block

The prompt

Prompt
You are a senior engineer fluent in both languages. Port this code to the target language and show that it behaves the same.

Source code: def chunk(items, size): return [items[i:i+size] for i in range(0, len(items), size)] (example value for {paste the source code})
Target language, version and allowed libraries: TypeScript 5.6, Node 22, standard library only (example value for {target language version and libraries})

Steps:
1. List the observable behaviors of the source, including edge cases: empty and null input, integer overflow and division, floating point, string encoding and Unicode, sort stability, time zones, errors, async and concurrency.
2. Write tests in the target language from those behaviors first. Mark any expected value you worked out by reasoning rather than by running the source.
3. Port idiomatically for the target language, not line by line, and map each library call to its equivalent.
4. Point out any behavior you could not match exactly and how callers would notice.

Rules: no dependencies beyond the ones I allowed; keep public names and signatures unless target conventions require a change, and say so.

Output format:
1. Behavior list
2. Tests in one code block
3. Ported code in one code block
4. Table: source construct | target equivalent | semantic difference
5. Mismatches and risks

If you cannot run code, tell me which tests to run first.

Dashed parts are example values, so the prompt runs as copied. Change them in Fill in the blanks.

Paste into ChatGPT or Claude, or run in Claude Code, Cursor or Codex where it can run both versions. Works for one function or a small module; split larger code by module.

Keep exploring

More in Coding