项目作者: danielduarte

项目描述 :
Simple parser for Diff files (unified diff format)
高级语言: JavaScript
项目地址: git://github.com/danielduarte/diffparse.git
创建时间: 2020-06-05T20:14:14Z
项目社区:https://github.com/danielduarte/diffparse

开源协议:MIT License

下载


@tandil/diffparse

@tandil/diffparse"">NPM Package Version
License
Conventional Commits

Simple parser for Diff files (unified diff format)

Features

  • Parse a diff file from its filepath
  • Parse a diff directly from a string
  • Error reporting: If there are parsing errors, the partial result is returned and the list of detected errors including line numbers

Usage

Parse from file

Async with Promises

Given a filepath it returns a Promise that resolves to a JS object with the diff content parsed and easily accessible programmatically.

  1. const parser = require('@tandil/diffparse');
  2. parser.parseDiffFile('./examples/example1.diff').then(diff => {
  3. console.log(diff);
  4. });

Sync

  1. const parser = require('@tandil/diffparse');
  2. const diff = parser.parseDiffFileSync('./examples/example1.diff');
  3. console.log(diff);

Parse from string

Given a diff string it returns a JS object with the diff content parsed and easily accessible programmatically.

  1. const parser = require('@tandil/diffparse');
  2. const diffStr = `diff --git a/.gitignore b/.gitignore
  3. new file mode 100644
  4. index 0000000..2ccbe46
  5. --- /dev/null
  6. +++ b/.gitignore
  7. @@ -0,0 +1 @@
  8. +/node_modules/`;
  9. const diff = parser.parseDiffString(diffStr);
  10. console.log(diff);

Outputs

  1. {
  2. header: [],
  3. files: [
  4. {
  5. header: 'diff --git a/.gitignore b/.gitignore',
  6. fileMode: 'new file mode 100644',
  7. oldMode: null,
  8. newMode: null,
  9. index: 'index 0000000..2ccbe46',
  10. oldFile: '--- /dev/null',
  11. newFile: '+++ b/.gitignore',
  12. chunks: [
  13. {
  14. header: '@@ -0,0 +1 @@',
  15. content: [
  16. '+/node_modules/'
  17. ]
  18. }
  19. ]
  20. }
  21. ],
  22. errors: []
  23. }

Having issues?

Feel free to report any issues or feature request in Github repo.