parse-diff is a lightweight and focused JavaScript library designed to parse unified diff format strings, typically generated by version control systems like Git. It transforms a raw diff string into a structured JavaScript array of file objects, each containing chunks of changes and individual line modifications, including metadata like additions, deletions, and file paths. The current stable version is 0.12.0, published in January 2023, indicating a mature and stable project with an infrequent release cadence. While other parsers exist (e.g., `diffparser`, `parse-git-diff`), `parse-diff` is known for its simplicity and direct approach to the unified diff format, making it suitable for applications requiring programmatic access to diff data. It ships with TypeScript types, enhancing developer experience for TypeScript projects.
npm install parse-diffVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse a simple unified diff string and iterate through the resulting file, chunk, and change objects, logging their properties.
Always pin `parse-diff` to an exact version (e.g., `"parse-diff": "0.12.0"`) or use a tilde (`~0.12.0`) in `package.json` to prevent unexpected breaking changes with minor updates. Thoroughly test your application after any upgrade.
Ensure the input `diffString` strictly adheres to the unified diff format. Pre-validate input if it comes from untrusted or variable sources. For Git-specific diffs, ensure `git diff --unified` output is used.
Consider breaking down extremely large diffs into smaller, manageable chunks if possible, or explore streaming parsers if available for very large scale diff processing. Profile memory usage when dealing with large inputs.
For ESM/TypeScript, use `import parse from 'parse-diff';`. For CommonJS, use `const parse = require('parse-diff');`.If encountering this error, ensure your project's module resolution is consistent. If your project is primarily CommonJS, verify `parse-diff` is installed in a compatible version. If you are in an ESM context, always use `import parse from 'parse-diff';`.
Double-check the input diff for strict adherence to the unified diff format. Use a reliable source for diff strings (e.g., `git diff`). Consider adding pre-parsing validation or sanitization steps for user-provided diffs.
No dependency data recorded yet.