|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const fm = require('front-matter'); |
| 6 | + |
| 7 | +function node4Polyfills() { |
| 8 | + // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js |
| 9 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd |
| 10 | + if (!String.prototype.padEnd) { |
| 11 | + // eslint-disable-next-line no-extend-native |
| 12 | + String.prototype.padEnd = function padEnd(targetLength, padString) { |
| 13 | + targetLength = targetLength >> 0; // floor if number or convert non-number to 0; |
| 14 | + padString = String((typeof padString !== 'undefined' ? padString : ' ')); |
| 15 | + if (this.length > targetLength) { |
| 16 | + return String(this); |
| 17 | + } else { |
| 18 | + targetLength = targetLength - this.length; |
| 19 | + if (targetLength > padString.length) { |
| 20 | + padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed |
| 21 | + } |
| 22 | + return String(this) + padString.slice(0, targetLength); |
| 23 | + } |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js |
| 28 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart |
| 29 | + if (!String.prototype.padStart) { |
| 30 | + // eslint-disable-next-line no-extend-native |
| 31 | + String.prototype.padStart = function padStart(targetLength, padString) { |
| 32 | + targetLength = targetLength >> 0; // truncate if number, or convert non-number to 0; |
| 33 | + padString = String(typeof padString !== 'undefined' ? padString : ' '); |
| 34 | + if (this.length >= targetLength) { |
| 35 | + return String(this); |
| 36 | + } else { |
| 37 | + targetLength = targetLength - this.length; |
| 38 | + if (targetLength > padString.length) { |
| 39 | + padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed |
| 40 | + } |
| 41 | + return padString.slice(0, targetLength) + String(this); |
| 42 | + } |
| 43 | + }; |
| 44 | + } |
| 45 | +} |
| 46 | +node4Polyfills(); |
| 47 | + |
| 48 | +function outputCompletionTable(title, specs) { |
| 49 | + let longestName = 0; |
| 50 | + let maxSpecs = 0; |
| 51 | + |
| 52 | + for (const section in specs) { |
| 53 | + longestName = Math.max(section.length, longestName); |
| 54 | + maxSpecs = Math.max(specs[section].total, maxSpecs); |
| 55 | + } |
| 56 | + |
| 57 | + const maxSpecsLen = ('' + maxSpecs).length; |
| 58 | + const spaces = maxSpecsLen * 2 + longestName + 11; |
| 59 | + |
| 60 | + console.log('-'.padEnd(spaces + 4, '-')); |
| 61 | + console.log(`| ${title.padStart(Math.ceil((spaces + title.length) / 2)).padEnd(spaces)} |`); |
| 62 | + console.log(`| ${' '.padEnd(spaces)} |`); |
| 63 | + for (const section in specs) { |
| 64 | + 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)}% |`); |
| 65 | + } |
| 66 | + console.log('-'.padEnd(spaces + 4, '-')); |
| 67 | + console.log(); |
| 68 | +} |
| 69 | + |
| 70 | +function loadFiles(dir) { |
| 71 | + const files = fs.readdirSync(dir); |
| 72 | + |
| 73 | + return files.reduce((obj, file) => { |
| 74 | + const ext = path.extname(file); |
| 75 | + const name = path.basename(file, ext); |
| 76 | + const absFile = path.join(dir, file); |
| 77 | + let specs; |
| 78 | + |
| 79 | + switch (ext) { |
| 80 | + case '.md': { |
| 81 | + const content = fm(fs.readFileSync(absFile, 'utf8')); |
| 82 | + specs = [{ |
| 83 | + section: name, |
| 84 | + example: 1, |
| 85 | + markdown: content.body, |
| 86 | + html: fs.readFileSync(absFile.replace(/[^.]+$/, 'html'), 'utf8'), |
| 87 | + options: content.attributes |
| 88 | + }]; |
| 89 | + break; |
| 90 | + } |
| 91 | + case '.js': |
| 92 | + case '.json': { |
| 93 | + specs = require(absFile); |
| 94 | + if (!Array.isArray(specs)) { |
| 95 | + specs = [specs]; |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + default: |
| 100 | + return obj; |
| 101 | + } |
| 102 | + |
| 103 | + for (const spec of specs) { |
| 104 | + if (!spec.section) { |
| 105 | + spec.section = name; |
| 106 | + } |
| 107 | + if (!obj[spec.section]) { |
| 108 | + obj[spec.section] = { |
| 109 | + total: 0, |
| 110 | + pass: 0, |
| 111 | + specs: [] |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + obj[spec.section].total++; |
| 116 | + obj[spec.section].pass += spec.shouldFail ? 0 : 1; |
| 117 | + obj[spec.section].specs.push(spec); |
| 118 | + } |
| 119 | + |
| 120 | + return obj; |
| 121 | + }, {}); |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = { |
| 125 | + outputCompletionTable, |
| 126 | + loadFiles |
| 127 | +}; |
0 commit comments