{"id":10748,"library":"diff-match-patch-typescript","title":"diff-match-patch-typescript","description":"diff-match-patch-typescript is a direct, strongly-typed TypeScript port of Google's `diff-match-patch` library. It provides robust algorithms for computing differences between two texts, finding approximate string matches, and applying patches. This package, currently at version 1.1.3, aims for full functional parity with the original Java/JavaScript implementation, ensuring that developers leveraging TypeScript benefit from type safety and modern tooling while utilizing a widely-used and proven diffing solution. It's actively maintained with continuous integration, making it a reliable choice for text manipulation tasks where precise diffing, matching, and patching are required. Its core differentiators include its faithfulness to the original algorithm and its full TypeScript support.","status":"active","version":"1.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/nonoroazoro/diff-match-patch-typescript","tags":["javascript","diff-match-patch-typescript","diff-match-patch","diff","google-diff-match-patch","typescript-port","typescript"],"install":[{"cmd":"npm install diff-match-patch-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add diff-match-patch-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add diff-match-patch-typescript","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is written in TypeScript and primarily designed for ESM usage. CommonJS `require` is not the idiomatic way to import.","wrong":"const DiffMatchPatch = require('diff-match-patch-typescript').DiffMatchPatch;","symbol":"DiffMatchPatch","correct":"import { DiffMatchPatch } from 'diff-match-patch-typescript';"},{"note":"DiffOperation is an enum used to represent the type of change (EQUAL, DELETE, INSERT) in diff results. It should be imported directly.","wrong":"const { DiffOperation } = require('diff-match-patch-typescript');","symbol":"DiffOperation","correct":"import { DiffOperation } from 'diff-match-patch-typescript';"},{"note":"The main class `DiffMatchPatch` is a named export, not a default export.","wrong":"import DiffMatchPatch from 'diff-match-patch-typescript';","symbol":"default import (class)","correct":"import { DiffMatchPatch } from 'diff-match-patch-typescript';"}],"quickstart":{"code":"import { DiffMatchPatch, DiffOperation } from 'diff-match-patch-typescript';\n\nconst dmp = new DiffMatchPatch();\n\n// Example 1: Compute a diff between two strings\nconst text1 = 'The quick brown fox jumps over the lazy dog.';\nconst text2 = 'A quick brown cat jumps over the agile fox.';\nconsole.log('--- Diff Example ---');\nconst diffs = dmp.diff_main(text1, text2);\nconsole.log('Diff result:', JSON.stringify(diffs));\n// Expected output: array of [operation, text] tuples\n// e.g., [[-1,\"The\"],[1,\"A\"],[0,\" quick brown \"],[-1,\"fox\"],[1,\"cat\"],[0,\" jumps over the \"]...]\n\n// Example 2: Find a match within a text\nconst mainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';\nconst pattern = 'consectetur';\nconst startIndex = 0;\nconsole.log('\\n--- Match Example ---');\nconst position = dmp.match_main(mainText, pattern, startIndex);\nconsole.log(`Pattern '${pattern}' found at position: ${position}`);\n// Expected output: 28 (index of 'c' in 'consectetur')\n\n// Example 3: Apply a patch to a text\nconst originalText = 'Hello World';\nconst newDesiredText = 'Hello TypeScript';\nconsole.log('\\n--- Patch Example ---');\nconst patches = dmp.patch_make(originalText, newDesiredText);\nconsole.log('Generated patches:', JSON.stringify(patches));\nconst [patchedText, results] = dmp.patch_apply(patches, originalText);\nconsole.log(`Original: '${originalText}', Patched: '${patchedText}', Success: ${results[0]}`);\n// Expected output: newText: \"Hello TypeScript\", results: [true]","lang":"typescript","description":"This quickstart demonstrates the core functionalities: `diff_main` for comparing texts, `match_main` for finding a substring, and `patch_make`/`patch_apply` for creating and applying changes."},"warnings":[{"fix":"Review the `DiffMatchPatch` class configuration options (e.g., `dmp.diffTimeout = 0;` for infinite timeout) and adjust them based on your specific text characteristics and performance requirements.","message":"The `diff-match-patch` algorithm (and thus this port) uses configurable parameters like `diffTimeout` and `diffEditCost` that significantly affect diff accuracy and performance. Default values might not be optimal for all use cases, particularly very long texts or highly dissimilar strings, potentially leading to 'suboptimal' diffs if not tuned.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Familiarize yourself with the original `diff-match-patch` documentation if you encounter unexpected results, as the underlying algorithms and their tuning parameters are identical. Consider profiling performance for very large inputs.","message":"As a direct port, this library replicates the behavior of the original Google `diff-match-patch`, which was developed primarily for browser environments and may have nuances or performance characteristics that differ from more modern or specialized Node.js diffing libraries.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For most common text diffing tasks, this library remains robust. However, for highly specialized scenarios (e.g., real-time collaborative editing with granular character tracking), research newer algorithms like Myers diff or Longest Common Subsequence variants.","message":"While still functional, the original `google/diff-match-patch` library has not seen significant updates in many years. While this TypeScript port provides modern language benefits, the underlying algorithm itself is mature and not actively evolving. Developers might want to evaluate newer diffing algorithms for specific cutting-edge use cases.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you use `import { DiffMatchPatch } from 'diff-match-patch-typescript';` and then create an instance with `const dmp = new DiffMatchPatch();` before attempting to call methods like `dmp.diff_main`.","cause":"The `DiffMatchPatch` class was either not correctly imported or not instantiated before calling its methods.","error":"TypeError: dmp.diff_main is not a function"},{"fix":"Add `DiffOperation` to your import statement: `import { DiffMatchPatch, DiffOperation } from 'diff-match-patch-typescript';`.","cause":"The `DiffOperation` enum was used without being explicitly imported from the package.","error":"ReferenceError: DiffOperation is not defined"},{"fix":"Always use the `DiffOperation` enum for clarity and type safety: `if (op[0] === DiffOperation.DIFF_EQUAL) { ... }` instead of `if (op[0] === 0) { ... }`.","cause":"Attempting to manually assign or compare diff operation results using magic numbers (0, 1, -1) instead of the `DiffOperation` enum members.","error":"TS2345: Argument of type 'number' is not assignable to parameter of type 'DiffOperation'"}],"ecosystem":"npm"}