forked from markedjs/marked
/
load.js
132 lines (118 loc) · 4.19 KB
/
load.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
129
130
131
132
'use strict';
const fs = require('fs');
const path = require('path');
const fm = require('front-matter');
function node4Polyfills() {
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
if (!String.prototype.padEnd) {
// eslint-disable-next-line no-extend-native
String.prototype.padEnd = function padEnd(targetLength, padString) {
targetLength = targetLength >> 0; // floor if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
}
return String(this) + padString.slice(0, targetLength);
}
};
}
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
// eslint-disable-next-line no-extend-native
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; // truncate if number, or convert non-number to 0;
padString = String(typeof padString !== 'undefined' ? padString : ' ');
if (this.length >= targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
}
node4Polyfills();
function outputCompletionTable(title, specs) {
let longestName = 0;
let maxSpecs = 0;
for (const section in specs) {
longestName = Math.max(section.length, longestName);
maxSpecs = Math.max(specs[section].total, maxSpecs);
}
const maxSpecsLen = ('' + maxSpecs).length;
const spaces = maxSpecsLen * 2 + longestName + 11;
console.log('-'.padEnd(spaces + 4, '-'));
console.log(`| ${title.padStart(Math.ceil((spaces + title.length) / 2)).padEnd(spaces)} |`);
console.log(`| ${' '.padEnd(spaces)} |`);
for (const section in specs) {
console.log(`| ${section.padEnd(longestName)} ${('' + specs[section].pass).padStart(maxSpecsLen)} of ${('' + specs[section].total).padStart(maxSpecsLen)} ${(100 * specs[section].pass / specs[section].total).toFixed().padStart(4)}% |`);
}
console.log('-'.padEnd(spaces + 4, '-'));
console.log();
}
function loadFiles(dir) {
const files = fs.readdirSync(dir);
return files.reduce((obj, file) => {
const ext = path.extname(file);
const name = path.basename(file, ext);
const absFile = path.join(dir, file);
let specs;
switch (ext) {
case '.md': {
const content = fm(fs.readFileSync(absFile, 'utf8'));
const skip = content.attributes.skip;
delete content.attributes.skip;
const only = content.attributes.only;
delete content.attributes.only;
specs = [{
section: name,
markdown: content.body,
html: fs.readFileSync(absFile.replace(/[^.]+$/, 'html'), 'utf8'),
options: content.attributes,
only,
skip
}];
break;
}
case '.js':
case '.json': {
specs = require(absFile);
if (!Array.isArray(specs)) {
specs = [specs];
}
break;
}
default:
return obj;
}
for (const spec of specs) {
if (!spec.section) {
spec.section = name;
}
if (!obj[spec.section]) {
obj[spec.section] = {
total: 0,
pass: 0,
specs: []
};
}
obj[spec.section].total++;
obj[spec.section].pass += spec.shouldFail ? 0 : 1;
obj[spec.section].specs.push(spec);
}
return obj;
}, {});
}
module.exports = {
outputCompletionTable,
loadFiles
};