Skip to content

feat: getCodeDiff - #52

Merged
DoneDeal0 merged 1 commit into
mainfrom
getCodeDiff
Sep 19, 2026
Merged

DoneDeal0 merged 1 commit into
mainfrom
getCodeDiff

Conversation

@DoneDeal0

@DoneDeal0 DoneDeal0 commented Sep 2, 2026 •

Copy link
Copy Markdown
Owner

GETCODEDIFF

get-code-diff-demo
import { getCodeDiff } from "@donedeal0/superdiff";

Compares two codes and returns a structured diff: lines first, then the tokens of each changed line. Whitespace is kept, so indentation changes are reported too.

To diff files, read them first: getCodeDiff(await previousFile.text(), await currentFile.text()).

FORMAT

Input

  previousCode: string | null | undefined,
  currentCode: string | null | undefined,

Output

type CodeDiff = {
  type: "code";
  status: "added" | "deleted" | "equal" | "updated";
  diff: {
    value: string;
    previousValue?: string;
    line: number | null;
    previousLine: number | null;
    status: "added" | "deleted" | "equal" | "updated";
    diff?: {
      value: string;
      previousValue?: string;
      status: "added" | "deleted" | "equal" | "updated";
    }[];
  }[];
};

Lines start at 1. line is null if the line was deleted, previousLine is null if it was added. diff holds the token changes of an updated line.

USAGE

Input

getCodeDiff(
- "const MAX = 280;\n\nfunction preview(post) {\n  return post.body;\n}",
+ "const MAX_LENGTH = 320;\n\nfunction preview(post) {\n  const text = post.body;\n  return text.slice(0, MAX_LENGTH);\n}"
);

Output

{
      type: "code",
+     status: "updated",
      diff: [
        {
+         value: "const MAX_LENGTH = 320;",
+         previousValue: "const MAX = 280;",
          line: 1,
          previousLine: 1,
+         status: "updated",
          diff: [
            { value: "const", status: "equal" },
+           { value: " MAX_LENGTH", previousValue: " MAX", status: "updated" },
            { value: " =", status: "equal" },
+           { value: " 320", previousValue: " 280", status: "updated" },
            { value: ";", status: "equal" },
          ],
        },
        {
          value: "",
          previousValue: "",
          line: 2,
          previousLine: 2,
          status: "equal",
        },
        {
          value: "function preview(post) {",
          previousValue: "function preview(post) {",
          line: 3,
          previousLine: 3,
          status: "equal",
        },
        {
+         value: "  const text = post.body;",
+         previousValue: "  return post.body;",
          line: 4,
          previousLine: 4,
+         status: "updated",
          diff: [
+           { value: "  const", previousValue: "  return", status: "updated" },
+           { value: " text", status: "added" },
+           { value: " =", status: "added" },
            { value: " post", status: "equal" },
            { value: ".", status: "equal" },
            { value: "body", status: "equal" },
            { value: ";", status: "equal" },
          ],
        },
        {
+         value: "  return text.slice(0, MAX_LENGTH);",
          line: 5,
          previousLine: null,
+         status: "added",
        },
        {
          value: "}",
          previousValue: "}",
          line: 6,
          previousLine: 5,
          status: "equal",
        },
      ],
}

@DoneDeal0 DoneDeal0 self-assigned this Sep 2, 2026
@DoneDeal0
DoneDeal0 force-pushed the getCodeDiff branch 3 times, most recently from 41216f0 to ef85b9d Compare September 4, 2026 20:05
@DoneDeal0
DoneDeal0 force-pushed the getCodeDiff branch 3 times, most recently from 55ff03e to 7035492 Compare September 18, 2026 08:21
@DoneDeal0
DoneDeal0 marked this pull request as ready for review September 18, 2026 10:47
@DoneDeal0
DoneDeal0 merged commit 130bf4a into main Sep 19, 2026
1 check passed
@DoneDeal0
DoneDeal0 deleted the getCodeDiff branch September 19, 2026 09:28
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 4.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant