diffparser is a robust JavaScript library specifically engineered for parsing unified diff format strings, which are the standard output for showing changes between files in version control systems like Git. Currently stable at version 2.0.1, the project appears to follow a feature-driven release cadence, with its latest major iteration focusing on modern JavaScript (ES2015) syntax and module patterns. A primary differentiator of diffparser is its capability to generate position information for individual changes, which is a critical requirement for integrating with services such as the GitHub Comments API for precise inline commenting. Furthermore, it offers an optional but powerful rename detection feature, allowing users to accurately identify files that have been renamed within a diff. This package serves as an actively maintained, ES2015-native spiritual successor to the earlier parse-diff project, providing a clean API and a comprehensive test suite for reliable diff processing in both Node.js and browser environments.
npm install diffparserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing a basic unified diff and an example with rename detection.
Migrate your import statements from `const parse = require('diffparser');` to `import parse from 'diffparser';`. Ensure your Node.js environment or build setup supports ESM.Always validate or sanitize diff inputs to ensure they conform to the unified diff specification. Use robust error handling around the parse function.
When parsing diffs that might contain file renames, pass `{ findRenames: true }` as the second argument to the `parse` function: `parse(diffString, { findRenames: true })`.Update your import statement from `const parse = require('diffparser');` to `import parse from 'diffparser';`.Verify that the input string strictly adheres to the unified diff format. Check for missing headers, corrupted chunks, or unexpected line endings.
Call the `parse` function with the `findRenames` option set to `true`: `parse(diffString, { findRenames: true });`No dependency data recorded yet.