-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
52 lines (43 loc) · 1.37 KB
/
index.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
var es = require('event-stream');
var colors = require('colors');
module.exports = function(subString, opt) {
var substrings = typeof subString === "undefined" ? [] : subString;
opt = opt || {};
function occurrences(file,cb){
// file.contents = new Buffer(String(file.contents).toString());
if (file.isNull()) {
// Do nothing if no contents
}
if (file.isBuffer()) {
var string = file.contents.toString();
string = string.toString('utf8').split(/\r\n|[\n\r\u0085\u2028\u2029]/g);
var message = "";
var m = 0;
var n=0, pos=0;
var sc = 0;
while(sc < string.length){
for (var i = substrings.length - 1; i >= 0; i--) {
var step=substrings[i].length;
pos=string[sc].indexOf(substrings[i],pos);
if(pos>=0){
n++;
message = message + " "+ "["+(sc+1)+"] "+ substrings[i] + " ,\n";
};
};
sc++;
}
m = m + n;
var finalMessage = file.path+" ("+m+")\n" + message;
if (m === 0) {
if (opt && opt.logLevel !== 'warn') {
console.log(finalMessage[opt.color || 'green']);
}
} else {
console.log(finalMessage[opt.color || 'red']);
}
message = "";
cb(null,file);
}
}
return es.map(occurrences);
};