diff-match-patch-es

  • Version 1.0.1
  • Published
  • 146 kB
  • No dependencies
  • Apache-2.0 license

Install

npm i diff-match-patch-es
yarn add diff-match-patch-es
pnpm add diff-match-patch-es

Overview

ESM and TypeScript rewrite of Google's diff-match-patch

Index

Variables

variable defaultOptions

const defaultOptions: Required<DiffMatchPathOptions>;

    variable DIFF_DELETE

    const DIFF_DELETE: number;

      variable DIFF_EQUAL

      const DIFF_EQUAL: number;

        variable DIFF_INSERT

        const DIFF_INSERT: number;

          Functions

          function diff

          diff: (
          text1: string,
          text2: string,
          options?: DiffMatchPathOptions | ResolvedOptions,
          opt_checklines?: boolean,
          opt_deadline?: number
          ) => Diff[];
          • Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.

            Parameter text1

            Old string to be diffed.

            Parameter text2

            New string to be diffed.

            Parameter options

            Diff options

            Parameter opt_checklines

            Optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff.

            Parameter opt_deadline

            Optional time when the diff should be complete by. Used internally for recursive calls. Users should set DiffTimeout instead. {Diff[]} Array of diff tuples.

          function diffCharsToLines

          diffCharsToLines: (diffs: Diff[], lineArray: string[]) => void;
          • Rehydrate the text in a diff from a string of line hashes to real lines of text.

            Parameter diffs

            Array of diff tuples.

            Parameter lineArray

            Array of unique strings.

          function diffCleanupEfficiency

          diffCleanupEfficiency: (diffs: Diff[], options?: DiffMatchPathOptions) => void;
          • Reduce the number of edits by eliminating operationally trivial equalities.

            Parameter diffs

            Array of diff tuples.

          function diffCleanupMerge

          diffCleanupMerge: (diffs: Diff[]) => void;
          • Reorder and merge like edit sections. Merge equalities. Any edit section can move as long as it doesn't cross an equality.

            Parameter diffs

            Array of diff tuples.

          function diffCleanupSemantic

          diffCleanupSemantic: (diffs: Diff[]) => void;
          • Reduce the number of edits by eliminating semantically trivial equalities.

            Parameter diffs

            Array of diff tuples.

          function diffCleanupSemanticLossless

          diffCleanupSemanticLossless: (diffs: Diff[]) => void;
          • Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary. e.g: The cat came. -> The cat came.

            Parameter diffs

            Array of diff tuples.

          function diffCommonPrefix

          diffCommonPrefix: (text1: string, text2: string) => number;
          • Determine the common prefix of two strings.

            Parameter text1

            First string.

            Parameter text2

            Second string. {number} The number of characters common to the start of each string.

          function diffCommonSuffix

          diffCommonSuffix: (text1: string, text2: string) => number;
          • Determine the common suffix of two strings.

            Parameter text1

            First string.

            Parameter text2

            Second string. {number} The number of characters common to the end of each string.

          function diffFromDelta

          diffFromDelta: (text1: string, delta: string) => Diff[];
          • Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.

            Parameter text1

            Source string for the diff.

            Parameter delta

            Delta text. {Diff[]} Array of diff tuples.

            Throws

            {!Error} If invalid input.

          function diffLevenshtein

          diffLevenshtein: (diffs: Diff[]) => number;
          • Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.

            Parameter diffs

            Array of diff tuples. {number} Number of changes.

          function diffLinesToChars

          diffLinesToChars: (
          text1: string,
          text2: string
          ) => { chars1: string; chars2: string; lineArray: string[] };
          • Split two texts into an array of strings. Reduce the texts to a string of hashes where each Unicode character represents one line.

            Parameter text1

            First string.

            Parameter text2

            Second string. {{chars1: string, chars2: string, lineArray: !Array.}} An object containing the encoded text1, the encoded text2 and the array of unique strings. The zeroth element of the array of unique strings is intentionally blank.

          function diffMain

          diffMain: (
          text1: string,
          text2: string,
          options?: DiffMatchPathOptions | ResolvedOptions,
          opt_checklines?: boolean,
          opt_deadline?: number
          ) => Diff[];
          • Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.

            Parameter text1

            Old string to be diffed.

            Parameter text2

            New string to be diffed.

            Parameter options

            Diff options

            Parameter opt_checklines

            Optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff.

            Parameter opt_deadline

            Optional time when the diff should be complete by. Used internally for recursive calls. Users should set DiffTimeout instead. {Diff[]} Array of diff tuples.

          function diffPrettyHtml

          diffPrettyHtml: (diffs: Diff[]) => string;
          • Convert a diff array into a pretty HTML report.

            Parameter diffs

            Array of diff tuples. {string} HTML representation.

          function diffText1

          diffText1: (diffs: Diff[]) => string;
          • Compute and return the source text (all equalities and deletions).

            Parameter diffs

            Array of diff tuples. {string} Source text.

          function diffText2

          diffText2: (diffs: Diff[]) => string;
          • Compute and return the destination text (all equalities and insertions).

            Parameter diffs

            Array of diff tuples. {string} Destination text.

          function diffToDelta

          diffToDelta: (diffs: Diff[]) => string;
          • Crush the diff into an encoded string which describes the operations required to transform text1 into text2. E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.

            Parameter diffs

            Array of diff tuples. {string} Delta text.

          function diffXIndex

          diffXIndex: (diffs: Diff[], loc: number) => number;
          • loc is a location in text1, compute and return the equivalent location in text2. e.g. 'The cat' vs 'The big cat', 1->1, 5->8

            Parameter diffs

            Array of diff tuples.

            Parameter loc

            Location within text1. {number} Location within text2.

          function match

          match: (
          text: string,
          pattern: string,
          loc: number,
          options?: DiffMatchPathOptions
          ) => number;
          • Locate the best instance of 'pattern' in 'text' near 'loc'.

            Parameter text

            The text to search.

            Parameter pattern

            The pattern to search for.

            Parameter loc

            The location to search around. {number} Best match index or -1.

          function matchAlphabet

          matchAlphabet: (pattern: string) => Record<string, number>;
          • Initialise the alphabet for the Bitap algorithm.

            Parameter pattern

            The text to encode. {!object} Hash of character locations.

          function matchBitap

          matchBitap: (
          text: string,
          pattern: string,
          loc: number,
          options?: DiffMatchPathOptions
          ) => number;
          • Locate the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm.

            Parameter text

            The text to search.

            Parameter pattern

            The pattern to search for.

            Parameter loc

            The location to search around.

            Parameter options

            The options {number} Best match index or -1.

          function matchMain

          matchMain: (
          text: string,
          pattern: string,
          loc: number,
          options?: DiffMatchPathOptions
          ) => number;
          • Locate the best instance of 'pattern' in 'text' near 'loc'.

            Parameter text

            The text to search.

            Parameter pattern

            The pattern to search for.

            Parameter loc

            The location to search around. {number} Best match index or -1.

          function patch

          patch: (
          a: string | Diff[],
          opt_b?: string | Diff[],
          opt_c?: string | Diff[],
          options?: DiffMatchPathOptions
          ) => Patch[];
          • Compute a list of patches to turn text1 into text2. Use diffs if provided, otherwise compute it ourselves. There are four ways to call this function, depending on what data is available to the caller: Method 1: a = text1, b = text2 Method 2: a = diffs Method 3 (optimal): a = text1, b = diffs Method 4 (deprecated, use method 3): a = text1, b = text2, c = diffs

            Parameter a

            text1 (methods 1,3,4) or Array of diff tuples for text1 to text2 (method 2).

            Parameter opt_b

            text2 (methods 1,4) or Array of diff tuples for text1 to text2 (method 3) or undefined (method 2).

            Parameter opt_c

            Array of diff tuples for text1 to text2 (method 4) or undefined (methods 1,2,3). {Patch[]} Array of Patch objects.

          function patchAddPadding

          patchAddPadding: (patches: Patch[], options?: DiffMatchPathOptions) => string;
          • Add some padding on text start and end so that edges can match something. Intended to be called only from within patch_apply.

            Parameter patches

            Array of Patch objects. {string} The padding string added to each side.

          function patchApply

          patchApply: (
          patches: Patch[],
          text: string,
          options?: DiffMatchPathOptions
          ) => (string | boolean[])[];
          • Merge a set of patches onto the text. Return a patched text, as well as a list of true/false values indicating which patches were applied.

            Parameter patches

            Array of Patch objects.

            Parameter text

            Old text. {!Array.<string|!Array.>} Two element Array, containing the new text and an array of boolean values.

          function patchDeepCopy

          patchDeepCopy: (patches: Patch[]) => Patch[];
          • Given an array of patches, return another array that is identical.

            Parameter patches

            Array of Patch objects. {Patch[]} Array of Patch objects.

          function patchFromText

          patchFromText: (textline: string) => Patch[];
          • Parse a textual representation of patches and return a list of Patch objects.

            Parameter textline

            Text representation of patches. {Patch[]} Array of Patch objects.

            Throws

            {!Error} If invalid input.

          function patchMake

          patchMake: (
          a: string | Diff[],
          opt_b?: string | Diff[],
          opt_c?: string | Diff[],
          options?: DiffMatchPathOptions
          ) => Patch[];
          • Compute a list of patches to turn text1 into text2. Use diffs if provided, otherwise compute it ourselves. There are four ways to call this function, depending on what data is available to the caller: Method 1: a = text1, b = text2 Method 2: a = diffs Method 3 (optimal): a = text1, b = diffs Method 4 (deprecated, use method 3): a = text1, b = text2, c = diffs

            Parameter a

            text1 (methods 1,3,4) or Array of diff tuples for text1 to text2 (method 2).

            Parameter opt_b

            text2 (methods 1,4) or Array of diff tuples for text1 to text2 (method 3) or undefined (method 2).

            Parameter opt_c

            Array of diff tuples for text1 to text2 (method 4) or undefined (methods 1,2,3). {Patch[]} Array of Patch objects.

          function patchSplitMax

          patchSplitMax: (patches: Patch[], options?: DiffMatchPathOptions) => void;
          • Look through the patches and break up any which are longer than the maximum limit of the match algorithm. Intended to be called only from within patch_apply.

            Parameter patches

            Array of Patch objects.

          function patchToText

          patchToText: (patches: Patch[]) => string;
          • Take a list of patches and return a textual representation.

            Parameter patches

            Array of Patch objects. {string} Text representation of patches.

          function resolveOptions

          resolveOptions: (options?: DiffMatchPathOptions) => ResolvedOptions;

            Interfaces

            interface DiffMatchPathOptions

            interface DiffMatchPathOptions {}

              property diffEditCost

              diffEditCost?: number;
              • Cost of an empty edit operation in terms of edit characters. 4

              property diffTimeout

              diffTimeout?: number;
              • Number of seconds to map a diff before giving up (0 for infinity). 1.0

              property matchDistance

              matchDistance?: number;
              • How far to search for a match (0 = exact location, 1000+ = broad match). 1000

              property matchMaxBits

              matchMaxBits?: number;
              • The number of bits in an int. 32

              property matchThreshold

              matchThreshold?: number;
              • At what point is no match declared (0.0 = perfection, 1.0 = very loose). 0.5

              property patchDeleteThreshold

              patchDeleteThreshold?: number;
              • When deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents. (0.0 = perfection, 1.0 = very loose). 0.5

              property patchMargin

              patchMargin?: number;
              • Chunk size for context length. 4

              interface Patch

              interface Patch {}

                property diffs

                diffs: Diff[];

                  property length1

                  length1: number;

                    property length2

                    length2: number;

                      property start1

                      start1: number;

                        property start2

                        start2: number;

                          Type Aliases

                          type Diff

                          type Diff = [DiffOperation, string];
                          • The data structure representing a diff is an array of tuples: [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']] which means: delete 'Hello', add 'Goodbye' and keep ' world.'

                          type DiffOperation

                          type DiffOperation = -1 | 0 | 1;
                          • DIFF_DELETE: -1

                            DIFF_INSERT: 1

                            DIFF_EQUAL: 0

                          type ResolvedOptions

                          type ResolvedOptions = Required<DiffMatchPathOptions>;

                            Package Files (1)

                            Dependencies (0)

                            No dependencies.

                            Dev Dependencies (15)

                            Peer Dependencies (0)

                            No peer dependencies.

                            Badge

                            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/diff-match-patch-es.

                            • Markdown
                              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/diff-match-patch-es)
                            • HTML
                              <a href="https://www.jsdocs.io/package/diff-match-patch-es"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>