Skip to content

danielduarte/diffparse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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.

const parser = require('@tandil/diffparse');

parser.parseDiffFile('./examples/example1.diff').then(diff => {
  console.log(diff);
});

Sync

const parser = require('@tandil/diffparse');

const diff = parser.parseDiffFileSync('./examples/example1.diff');
console.log(diff);

Parse from string

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

const parser = require('@tandil/diffparse');

const diffStr = `diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2ccbe46
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules/`;

const diff = parser.parseDiffString(diffStr);

console.log(diff);

Outputs

{
  header: [],
  files: [
    {
      header: 'diff --git a/.gitignore b/.gitignore',
      fileMode: 'new file mode 100644',
      oldMode: null,
      newMode: null,
      index: 'index 0000000..2ccbe46',
      oldFile: '--- /dev/null',
      newFile: '+++ b/.gitignore',
      chunks: [
        {
          header: '@@ -0,0 +1 @@',
          content: [
            '+/node_modules/'
          ]
        }
      ]
    }
  ],
  errors: []
}

Having issues?

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

About

Simple parser for Diff files (unified diff format)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published