Skip to content

Instantly share code, notes, and snippets.

@Yuyz0112
Created January 8, 2018 06:53
Show Gist options
  • Save Yuyz0112/5c2a21ebdf27f19f222f7a0e63215a9e to your computer and use it in GitHub Desktop.
Save Yuyz0112/5c2a21ebdf27f19f222f7a0e63215a9e to your computer and use it in GitHub Desktop.
generate table
// Usage: node generate.js 10x300
const fs = require('fs');
const path = require('path');
const placeholder = (rowIndex, colIndex) => Math.random().toString(36).substring((rowIndex + colIndex) % 7);
const tpl = (col, row) => {
const colArr = new Array(col).fill();
const rowArr = new Array(row).fill();
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Table with ${col} columns and ${row} rows</title>
<style>
table {
width: 1200px;
table-layout: auto;
border-spacing: 0;
border-collapse: collapse;
}
td {
border: 1px solid grey;
padding: 1rem;
}
</style>
</head>
<body>
<table>
<tbody>
${rowArr.map((_, rowIndex) => `
<tr>
${colArr.map((_, colIndex) => `<td>${placeholder(rowIndex, colIndex)}</td>`).join('')}
</tr>
`).join('')}
</tbody>
</table>
</body>
<script>
let count = 100;
const tbody = document.querySelector('tbody');
const t = setInterval(() => {
count--;
const lastTr = document.querySelector('tr:last-child');
const colNum = lastTr.querySelectorAll('td').length;
let newTr = '<tr>';
for (let i = 0; i < colNum; i++) {
const td = '<td>' + Math.random().toString(36).substring(i % 7) + '</td>';
newTr += td;
}
newTr += '</tr>';
tbody.insertAdjacentHTML('beforeend', newTr);
if (count <= 0) {
console.log('done')
clearInterval(t);
}
}, 50);
</script>
</html>
`;
};
// example size param: 10x5
const sizeStr = process.argv[2];
const html = tpl(...sizeStr.split('x').map(s => parseInt(s, 10)));
const staticFolder = path.join(__dirname, '../statics');
fs.writeFileSync(path.resolve(staticFolder, `${sizeStr}.html`), html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment