-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
40 lines (39 loc) · 1.12 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
// dependencies
var fs = require("fs");
exports.read = function (filePath) {
var text = fs.readFileSync(filePath, 'utf8', function (err, data) {
if (err)
throw (err);
});
return text;
};
exports.filter = function (fileName, text) {
var pattern = new RegExp("(\/\/...*\b)", "ig");
var lines = text.split(/\n/);
var title = 'From ' + fileName + '\n';
var listOfLines = [title];
for (var i = 0; i < lines.length; i++) {
if (lines[i].indexOf('// ') >= 0) {
var newLine = lines[i].replace('\/\/ ', '');
listOfLines.push('Line ' + i + ': ' + newLine);
}
}
return listOfLines;
};
exports.write = function (fileToWrite, filteredText) {
function appendLine(line) {
fs.appendFileSync(fileToWrite, line, encoding = 'utf8', function (
err) {
if (err) throw (err);
});
}
for (var i = 0; i < filteredText.length; i++) {
var line = filteredText[i] + '\n';
appendLine(line);
}
};
exports.extract = function (fileToRead, newFile) { // params taken as strings
var text = exports.read(fileToRead);
text = exports.filter(fileToRead, text);
exports.write(newFile, text);
};