-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext-searcher.js
128 lines (111 loc) · 3.36 KB
/
text-searcher.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const assert = require('assert');
function thrownArgumentException(text, term) {
if (typeof term !== 'string' || typeof text !== 'string') {
throw new Error('Each argument, must be a string');
}
}
function search(term, text) {
thrownArgumentException(term, text);
return (new RegExp(term)).test(text);
}
assert.ok(search('foo', 'foobar'));
assert.throws(() => {
search(1, 10);
}, {
name: 'Error',
message: 'Each argument, must be a string'
});
function times(term, text) {
thrownArgumentException(term, text);
return text.match((new RegExp(term, 'g'))).length;
}
assert.equal(times('baz', 'bazfoobarbaz'), 2);
assert.throws(() => {
times(1, 10);
}, {
name: 'Error',
message: 'Each argument, must be a string'
});
function match(term, text) {
thrownArgumentException(term, text);
return text.match(new RegExp(term)).input;
}
assert.equal(match('baz', 'foobarbaz'), 'foobarbaz');
assert.throws(() => {
match(1, 10);
}, {
name: 'Error',
message: 'Each argument, must be a string'
});
class Text {
constructor(content) {
this.content = content;
this.setContent = this.setContent.bind(this);
this.getContent = this.getContent.bind(this);
this.search = this.search.bind(this);
this.times = this.times.bind(this);
this.match = this.match.bind(this);
}
setContent(value) {
this.content = value;
return this;
}
getContent() {
return this.content;
}
search(term) { return search(term, this.getContent()); }
times(term) { return times(term, this.getContent()); }
match(term) { return match(term, this.getContent()); }
}
let txt = new Text('foobarbaz');
assert.ok(txt instanceof Text);
assert.equal(txt.content, 'foobarbaz', 'Text content not settled');
assert.ok(txt.setContent('foobarbazbuzz') instanceof Text);
assert.equal(txt.getContent(), 'foobarbazbuzz');
assert.ok(txt.search('buzz'));
txt.setContent('fuzzbarfuzzbuzzfuzz');
assert.equal(txt.times('fuzz'), 3);
assert.equal(txt.match('buzz'), 'fuzzbarfuzzbuzzfuzz');
const isTTY = process.stdin.isTTY;
const { Transform } = require('stream');
const args = process.argv.slice(2);
function textMatchContentTransformFactory(filePath='') {
const opts = {
transform(raw, encoding, callback) {
let text = new Text(raw.toString());
if (text.search(args[0])) {
if (!!filePath) console.log(filePath);
this.push(Buffer.from(text.match(args[0])));
}
callback();
}
};
return new Transform(opts);
}
const fs = require('fs');
const path = require('path');
function traverse(dirPath, dirs=[]) {
let dir = fs.readdirSync(dirPath, {
withFileTypes: true
});
let nestedDirs = dir.filter(curr => curr.isDirectory() &&
!(curr.name.indexOf('.') === 0));
let nestedFiles = dir.filter(curr => curr.isFile() &&
!(curr.name.indexOf('.') === 0));
for (let file of nestedFiles) {
let curr = path.resolve(dirPath, file.name);
let currStream = fs.createReadStream(curr);
currStream.pipe(textMatchContentTransformFactory(curr)).pipe(process.stdout);
}
for (let entrypoint of nestedDirs) {
let curr = path.resolve(dirPath, entrypoint.name);
traverse(curr);
}
}
if (!isTTY) {
process.stdin.pipe(textMatchContentTransformFactory()).pipe(process.stdout);
} else if (isTTY) {
// traverse directories
traverse(process.cwd());
}