-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathextractFile.js
58 lines (51 loc) · 1.8 KB
/
extractFile.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
// Assertion extractor, from file in current repo.
// Also sorts by ID (optional) and (optional) pulls in assertion text.
// First column is index of where it returns in results from Cheerio.
// Usage:
// % npm i cheerio
// % cd testing
// % node extractFile.js > assertions.csv
const fs = require('fs');
const cheerio = require('cheerio');
const docFilename = '../index.html'
const addAssertionTxt = true; // set true to add assertion text on last column
const sortData = true; // set to true to sort output data
fs.readFile(docFilename, 'utf8', (err,data) => {
if (err) {
console.error(err);
return;
}
// Load and parse HTML
const $ = cheerio.load(data);
// Extract assertion and put them into an array of objects
var arr_data = [];
$('.rfc2119-assertion').each(function (i) {
arr_data.push({
"html": $(this),
"index": i
});
});
// Optionally sort assertions by ID
if (sortData) {
// this is an in-place sort
arr_data.sort((a,b) => {
let sa = a.html.attr("id");
let sb = b.html.attr("id");
return (sa < sb) - (sa > sb); // case-sensitive order
});
}
// Initialize headers in output CSV file
console.log(`"Index","ID","Status","Comment"${addAssertionTxt?',"Assertion"':''}`);
// Output each row of array data into CSV, with optional assertion text
for (j = 0; j < arr_data.length; j++) {
let element = arr_data[j];
let i = element.index;
let id = element.html.attr('id');
let text = element.html.text();
let assertionTxt = "";
if (addAssertionTxt) {
assertionTxt = ',"'+text.trim().replace(/\r?\n/g,'').replace(/\s+/g,' ').replace(/"/g, '""')+'"';
}
console.log(`${i},"${id}","null","not testable with Assertion Tester"${assertionTxt}`);
}
});