function

diff.createPatch

function createPatch(
oldText: string,
newText: string,
options?: PatchOptions & { maxEditLength: undefined }
): string;

Compare two strings line by line and format the result as a unified diff, like diff -u.

@returns

The patch, "" if the strings are equal, or undefined if there are more than maxEditLength edits

Bun.diff.createPatch("a\nb\n", "a\nc\n", { oldFileName: "file.txt" });
// --- file.txt
// +++ file.txt
// @@ -1,2 +1,2 @@
//  a
// -b
// +c
function createPatch(
oldText: string,
newText: string,
options?: PatchOptions
): undefined | string;

Compare two strings line by line and format the result as a unified diff, like diff -u.

@returns

The patch, "" if the strings are equal, or undefined if there are more than maxEditLength edits

Bun.diff.createPatch("a\nb\n", "a\nc\n", { oldFileName: "file.txt" });
// --- file.txt
// +++ file.txt
// @@ -1,2 +1,2 @@
//  a
// -b
// +c

Referenced types

interface PatchOptions

  • context?: number

    The number of unchanged lines before and after each change, like diff -U. An integer from 0, or Infinity for the whole file.

  • maxEditLength?: number

    Return undefined when the diff adds and removes more than this many tokens in total. An integer from 0, or Infinity.

  • newFileName?: string

    The name in the +++ line. Defaults to oldFileName.

  • newHeader?: string

    Text after the name in the +++ line, such as a timestamp. It needs a file name and has to be one line.

  • oldFileName?: string

    The name in the --- line. Defaults to newFileName. Without either name, the patch has no --- and +++ lines.

  • oldHeader?: string

    Text after the name in the --- line, such as a timestamp. It needs a file name and has to be one line.