{"size": 822, "ext": "js", "max_stars_repo_path": "NodeJS/Observer/main.js", "max_stars_repo_name": "RyabovNick/databasecourse2018p2", "max_stars_repo_stars_event_min_datetime": "2019-10-25T06:44:38.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-22T20:11:54.000Z", "max_issues_repo_path": "NodeJS/Observer/main.js", "max_issues_repo_name": "RyabovNick/databasecourse2018p2", "max_issues_repo_issues_event_min_datetime": "2020-10-31T09:09:16.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T10:41:28.000Z", "max_forks_repo_path": "NodeJS/Observer/main.js", "max_forks_repo_name": "RyabovNick/databasecourse2018p2", "max_forks_repo_forks_event_min_datetime": "2019-09-21T11:22:13.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-18T18:07:23.000Z"}
{"max_stars_count": 3, "max_issues_count": 9, "max_forks_count": 3, "avg_line_length": 27.4, "max_line_length": 68, "alphanum_fraction": 0.6094890511}
"use strict";

const EventEmitter = require("events").EventEmitter;
const fs = require("fs");

function findPattern(files, regex) {
  const emitter = new EventEmitter();
  files.forEach(function(file) {
    fs.readFile(file, "utf8", (err, content) => {
      if (err) return emitter.emit("error", err);

      emitter.emit("fileread", file);
      let match;
      if ((match = content.match(regex)))
        match.forEach(elem => emitter.emit("found", file, elem));
    });
  });
  return emitter;
}

findPattern(
  ["./NodeJS/Observer/fileA.txt", "./NodeJS/Observer/fileB.json"],
  /hello \w+/g
)
  .on("fileread", file => console.log(file + " was read"))
  .on("found", (file, match) =>
    console.log('Matched "' + match + '" in file ' + file)
  )
  .on("error", err => console.log("Error emitted: " + err.message));
