diff options
Diffstat (limited to 'graphs/js/gallery/gallery.js')
| -rw-r--r-- | graphs/js/gallery/gallery.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/graphs/js/gallery/gallery.js b/graphs/js/gallery/gallery.js new file mode 100644 index 0000000..8446846 --- /dev/null +++ b/graphs/js/gallery/gallery.js @@ -0,0 +1,54 @@ +const fs = require("fs"); +const path = require("path"); + +function extract(directoryPath) { + const mail = new RegExp( + "([a-z0-9_.+-]+)@([a-z0-9.-]+)\\.(([a-zA-Z0-9]|\\.){2,})", + "g", + ); + + try { + let emails = []; + const dirs = fs.readdirSync(directoryPath, { withFileTypes: true }); + + for (const dir of dirs) { + if (dir.isFile()) { + const data = fs.readFileSync( + path.join(directoryPath, dir.name), + "utf8", + ); + const matches = data.match(mail); + + if (matches) { + emails = [...emails, ...matches]; + } + } else if (dir.isDirectory()) { + emails = [ + ...emails, + ...extract(path.join(directoryPath, dir.name)), + ]; + } + } + + return emails; + } catch { + throw new Error("The directory does not exist"); + } +} + +/* + +fzhfeklzjfh@dakljhlzekj.f +fkzjefh@aaa0.fr +dejkzd@djkelh.fr +djhezklf0587dzedez@gmail................ +xavier.login@epita.fr +xavier.login@epita.fR + +*/ + +module.exports = { + extract, +}; + +console.log(extract("gallery")); |
