-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
42 lines (36 loc) · 1.08 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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('start', 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(elem => this.emit('found', file, elem))
}
})
}
return this
}
}
new FindRegex(/hello \w+/)
.addFile('./data/fileA.txt')
.addFile('./data/fileB.json')
.find()
.on('found', (file, match) => console.log(`Matched "${match}"`))
.on('start', files => console.log(`The process starts with ${files}`))
.on('error', console.error);