1

I was trying out new things in node, So here I want to read two files and check if all the words in the files are the same.

Example: Two similar files where one file has some extra words added to it.

So after checking both the files, if the words of one file don't exist in the other file, then those words should be printed out

Here is what I have tried so far, But this doesn't seem to work as expected and isn't the right way

let newData = [];
let oldData = [];

(async () => {
  await new Promise(resolve => {
    fs.readFile(
      pathname.join(__dirname, "public", "hello.txt"),
      (err, data) => {
        const wordsInFile = data.toString().toLowerCase();
        // Here we should split string by some delimiter for further words processing. Assume, that your file looks like this "word1 word2". We'll use space delimiter.
        newData = wordsInFile.split(" ");
        resolve();
      }
    );
  });

  // here is the same code for the second file. Or you can move code to another function.
  await new Promise(resolve => {
    fs.readFile(pathname.join(__dirname, "public", "test.txt"), (err, data) => {
      const wordsInFile = data.toString().toLowerCase();
      // Here we should split string by some delimiter for further words processing. Assume, that your file looks like this "word1 word2". We'll use space delimiter.
      newData = wordsInFile.split(" ");
      resolve();
    });
  });
  // And now we can use Set from ES6, to find intersection/difference etc.
  const wordsNotInFileAndNotInFileTwo = new Set(
    [...newData].filter(x => !oldData.has(x))
  );
  console.log(wordsNotInFileAndNotInFileTwo);

  // But you can simple iterate two arrays to achieve your goal.
})();

Any help would be appreciated

Thanveer Shah
  • 3,250
  • 2
  • 15
  • 31

2 Answers2

2

Nest readFile:

fs.readFile(pathname.join(__dirname, "public", "hello.txt"), (err, data) => {
  newData = data.toString().toLowerCase();
  fs.readFile(pathname.join(__dirname, "public", "test.txt"), (err, data) => {
    oldData = data.toString().toLowerCase();
    if (newData === oldData) {
      console.log("Same");
    } else {
      console.log("Not Same");
      const a = newData.split(' ')
      const b = oldData.split(' ')

      const result = a.filter(w => !oldData.includes(w));
      console.log(result.join(' '));
    }
  });
});

This way, callback in inner function has access to variables set in the outer function.

1565986223
  • 6,420
  • 2
  • 20
  • 33
0

The first point is asynchronous behaviour of fs.readFile function.
In short, you have to await while each file would be read. Or, at least, one of files.
Here is example with IIFE, but you can use any asynchronous function or use promises.

let newData = [];
let oldData = [];

(async() => {
  await new Promise(resolve => {
    fs.readFile(pathname.join(__dirname, "public", "hello.txt"), (err, data) => {
      const wordsInFile = data.toString().toLowerCase();
      // Here we should split string by some delimiter for further words processing. Assume, that your file looks like this "word1 word2". We'll use space delimiter.
      newData = wordsInFile.split(' ');
      resolve();
    })
  })

  // here is the same code for the second file. Or you can move code to another function.

  // And now we can use Set from ES6, to find intersection/difference etc.
  const wordsNotInFileAndNotInFileTwo = new Set([...newData].filter(x => !oldData.has(x)));
 // But you can simple iterate two arrays to achieve your goal.
})()

Here is nice article about Set.

To sum up:
First, you need to await while file would be read.
Second, you can find words difference via Set.

Grynets
  • 2,477
  • 1
  • 17
  • 41