-
-
Save sgarmendia/566f3291a649d877cc258d8045f38b04 to your computer and use it in GitHub Desktop.
Solution - nodejs design pattterns - 3.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from "events"; | |
import { readFile } from "fs"; | |
class FindRegex extends EventEmitter { | |
constructor(regex) { | |
super(); | |
this.regex = regex; | |
this.files = []; | |
} | |
addFile(file) { | |
this.files.push(file); | |
return this; | |
} | |
find() { | |
process.nextTick(() => this.emit("findstart", this.files)); | |
for (const file of this.files) { | |
readFile(file, "utf8", (err, content) => { | |
if (err) { | |
return this.emit("error", err); | |
} | |
this.emit("fileread", file); | |
const match = content.match(this.regex); | |
if (match) { | |
match.forEach((el) => this.emit("found", file, el)); | |
} | |
}); | |
} | |
return this; | |
} | |
} | |
const find = new FindRegex(/test1/); | |
find | |
.addFile("file1.txt") | |
.addFile("file2.txt") | |
.find() | |
.on("findstart", () => { | |
console.log("find start"); | |
}) | |
.on("found", (file, match) => console.log(`Matched ${match} in file ${file}`)) | |
.on("fileread", () => console.log("file read ")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment