-
Notifications
You must be signed in to change notification settings - Fork 3
/
hail.js
41 lines (36 loc) · 1.21 KB
/
hail.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
var fs = require('fs');
var falafel = require('falafel');
var esprima = require('esprima');
var log = require('book');
function hail(street, policeman, output) {
this.street = fs.readFileSync(street, 'utf8');
this.policeman = fs.readFileSync(policeman, 'utf8');
this.output = output;
};
hail.prototype.countOccurences = function(str, value){
var regExp = new RegExp(value, "gi");
return str.match(regExp) ? str.match(regExp).length : 0;
}
hail.prototype.call = function(callback) {
log.info('Inserting street ...');
var self = this;
var updated = false;
var count = self.countOccurences(JSON.stringify(esprima.parse(self.policeman)), 'FunctionDeclaration');
var i = 0;
var toIns = Math.abs(Math.floor(Math.random() * count - 5) + 1);
var nodeParent = '';
var hailed = falafel(self.policeman, function(node) {
if(node.type === 'FunctionDeclaration' && updated == false) {
if(i == toIns) {
log.info('Inserted street after the %sth FunctionDeclaration', i);
node.update(node.source() + self.street.substring(0, self.street.length -1));
nodeParent = node.parent.parent.source();
}
i++;
}
});
updated = true;
fs.writeFileSync(self.output, hailed);
callback(nodeParent);
};
module.exports = hail;