|
| 1 | +/** |
| 2 | + * See generate.md |
| 3 | + */ |
| 4 | +const { performance } = require('perf_hooks'); |
| 5 | +const { generateBasicTable, generateComplexTable, generateComplexRow } = require('../lib/generate'); |
| 6 | + |
| 7 | +const argv = process.argv; |
| 8 | + |
| 9 | +const timeScales = [ |
| 10 | + ['millisecond', 1000], |
| 11 | + ['second', 60], |
| 12 | + ['minute', 60], |
| 13 | +]; |
| 14 | +const duration = (v, scales = [...timeScales]) => { |
| 15 | + const [unit, min] = scales.shift(); |
| 16 | + if (v > min && scales.length) { |
| 17 | + return duration(v / min, scales); |
| 18 | + } |
| 19 | + let locale = undefined; |
| 20 | + if (process.env.LANG) { |
| 21 | + const userLocale = process.env.LANG.match(/[a-z]{2}_[A-Z]{2}/).shift(); |
| 22 | + if (userLocale.match(/^[a-z]{2}[-_][A-Z]{2}$/)) { |
| 23 | + locale = userLocale.replace(/_/, '-'); |
| 24 | + } |
| 25 | + } |
| 26 | + return v.toLocaleString(locale, { style: 'unit', unit }); |
| 27 | +}; |
| 28 | + |
| 29 | +const argVal = (idx, def = 10) => { |
| 30 | + if (argv[idx] && argv[idx].match(/^[0-9]+$/)) { |
| 31 | + return parseInt(argv[idx], 10); |
| 32 | + } |
| 33 | + return def; |
| 34 | +}; |
| 35 | +const optEnabled = (opt) => argv.indexOf(opt) > -1; |
| 36 | +const optValue = (opt) => { |
| 37 | + const idx = argv.indexOf(opt); |
| 38 | + return idx > -1 ? argv[idx + 1] : 0; |
| 39 | +}; |
| 40 | + |
| 41 | +const logMemory = (text = '') => { |
| 42 | + let suffix = 'kb'; |
| 43 | + let used = process.memoryUsage().heapUsed / 1024; |
| 44 | + if (used % 1024 > 1) { |
| 45 | + used = used / 1024; |
| 46 | + suffix = 'mb'; |
| 47 | + } |
| 48 | + return `Memory usage ${text}: ${used}${suffix}`; |
| 49 | +}; |
| 50 | + |
| 51 | +const printHelp = () => { |
| 52 | + console.log(`node scripts/generate [ROWS = 10] [COLS = 10]`); |
| 53 | + [ |
| 54 | + ['--print', 'Print the generated table to the screen.'], |
| 55 | + ['--dump', 'Print the generated table code to the screen.'], |
| 56 | + ['--complex', 'Generate a complex table (basic tables are generated by default)'], |
| 57 | + ['--debug', 'Print table debugging output (warnings only).'], |
| 58 | + ].forEach(([opt, desc]) => console.log(` ${opt} ${desc}`)); |
| 59 | +}; |
| 60 | + |
| 61 | +const dumpTable = (t) => { |
| 62 | + const lines = []; |
| 63 | + lines.push(`const table = new Table();`); |
| 64 | + lines.push(`table.push(`); |
| 65 | + t.forEach((row) => { |
| 66 | + if (row.length) { |
| 67 | + let prefix = ' '; |
| 68 | + let suffix = ''; |
| 69 | + const multiLine = row.length > 1 && row.some((v) => v.content !== undefined); |
| 70 | + if (multiLine) { |
| 71 | + lines.push(' ['); |
| 72 | + } |
| 73 | + const cellLines = []; |
| 74 | + row.forEach((cell) => { |
| 75 | + if (cell.content) { |
| 76 | + const attrib = []; |
| 77 | + Object.entries(cell).forEach(([k, v]) => { |
| 78 | + if (!['style'].includes(k)) { |
| 79 | + attrib.push(`${k}: ${typeof v === 'string' ? `'${v}'` : v}`); |
| 80 | + } |
| 81 | + }); |
| 82 | + cellLines.push(`{ ${attrib.join(', ')} },`); |
| 83 | + } else { |
| 84 | + cellLines.push(`${typeof cell === 'string' ? `'${cell}'` : cell}`); |
| 85 | + } |
| 86 | + }); |
| 87 | + if (multiLine) { |
| 88 | + cellLines.forEach((cl) => lines.push([prefix, cl, suffix].join(''))); |
| 89 | + lines.push(' ],'); |
| 90 | + } else { |
| 91 | + lines.push(` [${cellLines.join(',')}]`); |
| 92 | + } |
| 93 | + } else { |
| 94 | + lines.push(' [],'); |
| 95 | + } |
| 96 | + }); |
| 97 | + lines.push(');'); |
| 98 | + lines.push('console.log(table.toString());'); |
| 99 | + return lines.forEach((line) => console.log(line)); |
| 100 | +}; |
| 101 | + |
| 102 | +if (optEnabled('--help')) { |
| 103 | + printHelp(); |
| 104 | + process.exit(0); |
| 105 | +} |
| 106 | + |
| 107 | +const results = []; |
| 108 | +results.push(logMemory('at startup')); |
| 109 | + |
| 110 | +const rows = argVal(2); |
| 111 | +const cols = argVal(3); |
| 112 | + |
| 113 | +const maxRowSpan = rows > 10 ? Math.ceil(Math.round(rows * 0.1)) : Math.ceil(rows / 2); |
| 114 | +const maxColSpan = cols; |
| 115 | + |
| 116 | +const complex = optEnabled('--complex'); |
| 117 | + |
| 118 | +console.log(`Generating ${complex ? 'complex' : 'basic'} table with ${rows} rows and ${cols} columns:`); |
| 119 | + |
| 120 | +if (complex) { |
| 121 | + console.log(`Max rowSpan: ${maxRowSpan}`, `Max colSpan ${maxColSpan}`); |
| 122 | +} |
| 123 | + |
| 124 | +const options = { |
| 125 | + tableOptions: {}, |
| 126 | +}; |
| 127 | + |
| 128 | +if (optEnabled('--compact')) { |
| 129 | + options.tableOptions.style = { compact: true }; |
| 130 | +} |
| 131 | + |
| 132 | +if (optEnabled('--head')) { |
| 133 | + const head = generateComplexRow(0, 1, cols, {}, { maxCols: cols - 1 }); |
| 134 | + options.tableOptions.head = head; |
| 135 | +} |
| 136 | + |
| 137 | +const colWidth = optValue('--col-width'); |
| 138 | +if (colWidth) { |
| 139 | + options.tableOptions.colWidths = []; |
| 140 | + for (let i = 0; i < cols; i++) { |
| 141 | + options.tableOptions.colWidths.push(parseInt(colWidth, 10)); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// console.log(`table: ${rows} rows X ${cols} columns; ${rows * cols} total cells`); |
| 146 | +// console.time('build table'); |
| 147 | +const buildStart = performance.now(); |
| 148 | +const table = complex ? generateComplexTable(rows, cols, options) : generateBasicTable(rows, cols, options); |
| 149 | +// console.timeEnd('build table'); |
| 150 | +results.push(logMemory('after table build')); |
| 151 | + |
| 152 | +results.push(`table built in ${duration(performance.now() - buildStart)}`); |
| 153 | + |
| 154 | +const start = performance.now(); |
| 155 | +const output = table.toString(); |
| 156 | +results.push(logMemory('after table rendered')); |
| 157 | +results.push(`table rendered in ${duration(performance.now() - start)}`); |
| 158 | +if (optEnabled('--print')) console.log(output); |
| 159 | +if (optEnabled('--dump')) dumpTable(table); |
| 160 | +results.forEach((result) => console.log(result)); |
0 commit comments