als-statistics
Version:
Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co
33 lines (29 loc) • 1.24 kB
JavaScript
import round from './round.js';
const transpose = (arr) => (arr && arr.length) ? arr[0].map((_, i) => arr.map(row => row[i])) : [];
const esc = s => String(s)
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
.replace(/"/g,'"').replace(/'/g,''');
export function htmlTable(rows = [], headers = [], options = {}) {
const {
firstColHeader = true,
header, description,
style = 'text-align:left;border: 1px solid black; padding:5px;',
fixed = 8, transposeValues=false
} = options
if(transposeValues) rows = transpose(rows)
headers = headers.length === 0 ? '' : /*html*/`<thead>
<tr>${headers.map(v => /*html*/`<th>${esc(v)}</th>`).join('')}</tr>
</thead>`
const body = /*html*/`<tbody>
${rows.map(row => /*html*/`<tr>
${row.map((v, i) => firstColHeader && i === 0
? /*html*/`<th>${round(v, fixed)}</th>`
: /*html*/`<td>${round(v, fixed)}</td>`).join('')}
</tr>`).join('')}
</tbody>`
return /*html*/`<div>
${header ? /*html*/`<h3>${esc(header)}</h3>` : ''}
${description ? /*html*/`<p>${esc(description)}</p>` : ''}
<table style="${style}">${headers}${body}</table>
</div>`
}