namespace
diff
namespace diff
Compare text and arrays, and create, parse and apply unified diffs.
interface FilePatch
What a patch changes in one file. A property is absent when the patch does not say.
interface Hunk
One
@@section of a unified diff.- lines: string[]
Each line without its
, after" "(kept),"-"(removed) or"+"(added)."\\ No newline at end of file"follows a line that has no
. - newStart: number
The first line of the hunk in the new text, from 1. If
newLinesis 0, the line after the hunk. - oldStart: number
The first line of the hunk in the old text, from 1. If
oldLinesis 0, the line after the hunk.
interface HunksOptions
- context?: number
The number of unchanged lines before and after each change, like
diff -U. An integer from 0, orInfinityfor the whole file. - maxEditLength?: number
Return
undefinedwhen the diff adds and removes more than this many tokens in total. An integer from 0, orInfinity.
interface Options
- maxEditLength?: number
Return
undefinedwhen the diff adds and removes more than this many tokens in total. An integer from 0, orInfinity.
interface PatchOptions
- context?: number
The number of unchanged lines before and after each change, like
diff -U. An integer from 0, orInfinityfor the whole file. - maxEditLength?: number
Return
undefinedwhen the diff adds and removes more than this many tokens in total. An integer from 0, orInfinity. - 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 tonewFileName. 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.
- source: string,): undefined | string;
Apply a patch for one file to
source.The lines that a hunk keeps and removes have to match exactly. If they don't match at
oldStart, Bun uses the nearest line where they do. Ifsourceonly has\r
line endings and the patch only has
, or the reverse, the patch is converted first.@param patchA unified diff, or hunks from hunks or parsePatch
@returnsThe patched text, or
undefinedif a hunk does not match - oldArray: readonly T[],newArray: readonly T[],
Compare two arrays element by element, with
===.@returnsThe changes, or
undefinedif there are more thanmaxEditLengthedits - oldText: string,newText: string,
Compare two strings by Unicode code point.
@returnsThe changes, or
undefinedif there are more thanmaxEditLengthedits - oldText: string,newText: string,): string;
Compare two strings line by line and format the result as a unified diff, like
diff -u.@returnsThe patch,
""if the strings are equal, orundefinedif there are more thanmaxEditLengtheditsBun.diff.createPatch("a\nb\n", "a\nc\n", { oldFileName: "file.txt" }); // --- file.txt // +++ file.txt // @@ -1,2 +1,2 @@ // a // -b // +coldText: string,newText: string,): undefined | string;Compare two strings line by line and format the result as a unified diff, like
diff -u.@returnsThe patch,
""if the strings are equal, orundefinedif there are more thanmaxEditLengtheditsBun.diff.createPatch("a\nb\n", "a\nc\n", { oldFileName: "file.txt" }); // --- file.txt // +++ file.txt // @@ -1,2 +1,2 @@ // a // -b // +c - oldText: string,newText: string,
Compare two strings line by line and group the changes into the hunks of a unified diff.
@returnsThe hunks, or
undefinedif there are more thanmaxEditLengtheditsoldText: string,newText: string,Compare two strings line by line and group the changes into the hunks of a unified diff.
@returnsThe hunks, or
undefinedif there are more thanmaxEditLengthedits - oldText: string,newText: string,
Compare two strings line by line. A line includes its
or\r
.@returnsThe changes, or
undefinedif there are more thanmaxEditLengtheditsBun.diff.lines("a\nb\n", "a\nc\n"); // [ // { value: "a\n", added: false, removed: false, count: 1 }, // { value: "b\n", added: false, removed: true, count: 1 }, // { value: "c\n", added: true, removed: false, count: 1 }, // ]oldText: string,newText: string,Compare two strings line by line. A line includes its
or\r
.@returnsThe changes, or
undefinedif there are more thanmaxEditLengtheditsBun.diff.lines("a\nb\n", "a\nc\n"); // [ // { value: "a\n", added: false, removed: false, count: 1 }, // { value: "b\n", added: false, removed: true, count: 1 }, // { value: "c\n", added: true, removed: false, count: 1 }, // ] - patch: string
Parse a unified diff, as written by
diff -u,git diff,git format-patch,svn difforhg diff.@returnsOne entry for each file
- oldText: string,newText: string,
Compare two strings word by word. A token is a word of Latin letters, digits and
_, a run of spaces, a line ending, or any other single character.@returnsThe changes, or
undefinedif there are more thanmaxEditLengtheditsoldText: string,newText: string,Compare two strings word by word. A token is a word of Latin letters, digits and
_, a run of spaces, a line ending, or any other single character.@returnsThe changes, or
undefinedif there are more thanmaxEditLengthedits