namespace

diff

namespace diff

Compare text and arrays, and create, parse and apply unified diffs.

  • interface Change<T = string>

    A run of tokens that were kept, removed or added.

    • added: boolean

      The run is only in the new input.

    • count: number

      The number of tokens (lines, words, characters or elements) in value.

    • removed: boolean

      The run is only in the old input.

    • value: T

      The tokens of the run: joined for text, an array for arrays.

  • interface FilePatch

    What a patch changes in one file. A property is absent when the patch does not say.

    • index?: string

      The name in an Index: or diff -r line.

    • isBinary?: boolean

      Git reports a change to a binary file, which has no hunks.

    • isCopy?: boolean
    • isCreate?: boolean
    • isDelete?: boolean
    • isGit?: boolean

      The section starts with diff --git.

    • isRename?: boolean
    • newFileName?: string
    • newHeader?: string

      The text after the name in the +++ line.

    • newMode?: string

      The file mode after the change, in octal, like "100755". Git only.

    • oldFileName?: string
    • oldHeader?: string

      The text after the name in the --- line.

    • oldMode?: string

      The file mode before the change, in octal, like "100644". Git only.

  • 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

      .

    • newLines: number

      The number of lines the hunk keeps or adds.

    • newStart: number

      The first line of the hunk in the new text, from 1. If newLines is 0, the line after the hunk.

    • oldLines: number

      The number of lines the hunk keeps or removes.

    • oldStart: number

      The first line of the hunk in the old text, from 1. If oldLines is 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, 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.

  • interface Options

    • maxEditLength?: number

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

  • 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.

  • function applyPatch(
    source: string,
    patch: string | Pick<Hunk, 'oldStart' | 'lines'>[]
    ): 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. If source only has

    \r
    line endings and the patch only has

    , or the reverse, the patch is converted first.

    @param patch

    A unified diff, or hunks from hunks or parsePatch

    @returns

    The patched text, or undefined if a hunk does not match

  • function arrays<T>(
    oldArray: readonly T[],
    newArray: readonly T[],
    options?: Options & { maxEditLength: undefined }
    ): Change<T[]>[];

    Compare two arrays element by element, with ===.

    @returns

    The changes, or undefined if there are more than maxEditLength edits

    function arrays<T>(
    oldArray: readonly T[],
    newArray: readonly T[],
    options?: Options
    ): undefined | Change<T[]>[];

    Compare two arrays element by element, with ===.

    @returns

    The changes, or undefined if there are more than maxEditLength edits

  • function chars(
    oldText: string,
    newText: string,
    options?: Options & { maxEditLength: undefined }
    ): Change<string>[];

    Compare two strings by Unicode code point.

    @returns

    The changes, or undefined if there are more than maxEditLength edits

    function chars(
    oldText: string,
    newText: string,
    options?: Options
    ): undefined | Change<string>[];

    Compare two strings by Unicode code point.

    @returns

    The changes, or undefined if there are more than maxEditLength edits

  • 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
  • function hunks(
    oldText: string,
    newText: string,
    options?: HunksOptions & { maxEditLength: undefined }
    ): Hunk[];

    Compare two strings line by line and group the changes into the hunks of a unified diff.

    @returns

    The hunks, or undefined if there are more than maxEditLength edits

    function hunks(
    oldText: string,
    newText: string,
    options?: HunksOptions
    ): undefined | Hunk[];

    Compare two strings line by line and group the changes into the hunks of a unified diff.

    @returns

    The hunks, or undefined if there are more than maxEditLength edits

  • function lines(
    oldText: string,
    newText: string,
    options?: Options & { maxEditLength: undefined }
    ): Change<string>[];

    Compare two strings line by line. A line includes its


    or
    \r
    .

    @returns

    The changes, or undefined if there are more than maxEditLength edits

    Bun.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 },
    // ]
    function lines(
    oldText: string,
    newText: string,
    options?: Options
    ): undefined | Change<string>[];

    Compare two strings line by line. A line includes its


    or
    \r
    .

    @returns

    The changes, or undefined if there are more than maxEditLength edits

    Bun.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 },
    // ]
  • function parsePatch(
    patch: string
    ): FilePatch[];

    Parse a unified diff, as written by diff -u, git diff, git format-patch, svn diff or hg diff.

    @returns

    One entry for each file

  • function words(
    oldText: string,
    newText: string,
    options?: Options & { maxEditLength: undefined }
    ): Change<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.

    @returns

    The changes, or undefined if there are more than maxEditLength edits

    function words(
    oldText: string,
    newText: string,
    options?: Options
    ): undefined | Change<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.

    @returns

    The changes, or undefined if there are more than maxEditLength edits