0

I have two .js files with several functions defined in them and I wish to compare the contents of these two files. Is there an npm package for such a comparison or any other way to do this?

Sarmad
  • 141
  • 2
  • 15
  • What kind of content are you wanting to compare? Maybe take a look at https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Spangle Oct 07 '20 at 06:31
  • 1
    I can give you instructions and an example if you the files are just .txt files – Darkphoton Oct 07 '20 at 06:39
  • simple text comparison, to see if any of the functions are changed or added – Sarmad Oct 07 '20 at 06:41
  • 1
    Seems like you just want to run a `diff`. There are many developer tools packages that contain a diff tool that you can just run as a child_process. – jfriend00 Oct 07 '20 at 06:43
  • I've got the solution by reading these comments, thanks. I'll simply do a by-text search as it serves my purpose – Sarmad Oct 07 '20 at 06:52

1 Answers1

3

You just need to compare them as if they were 2 plain text files. You could do something like this:

function readFile(name) {
    return new Promise((resolve, reject) =>
        fs.readFile(name,  function (err, data) {
            if (err) { reject(err); }
            resolve(data);
        });
    });
}
Promise.all(readFile('file1'), readFile('file2')).then(data => {
  var file1 = data[0];
  var file2 = data[1];
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arkalex
  • 101
  • 6