UNPKG

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

163 lines (133 loc) 4.29 kB
import { describe, it } from 'node:test'; import assert from 'node:assert'; import { htmlTable } from '../../lib/utils/html-table.js'; // Утилита: схлопываем пробелы и переносы между тегами, // чтобы сравнивать именно разметку, а не форматирование. function normalize(html) { return String(html) .replace(/\s+/g, ' ') .replace(/>\s+</g, '><') .trim(); } describe('htmlTable (simple string comparison)', () => { it('renders with headers (default options)', () => { const cols = [ [1, 1.2, 1.23], [2, 2.5, 2.51], ]; const headers = ['H1', 'H2', 'H3']; const actual = htmlTable(cols, headers); const expected = ` <div> <table style="text-align:left;border: 1px solid black; padding:5px;"><thead> <tr><th>H1</th><th>H2</th><th>H3</th></tr> </thead><tbody> <tr> <th>1</th><td>1.2</td><td>1.2</td> </tr><tr> <th>2</th><td>2.5</td><td>2.5</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); it('renders without <thead> when headers are empty', () => { const cols = [ [1, 10.7], [2, 20.2], ]; const headers = []; const actual = htmlTable(cols, headers); const expected = ` <div> <table style="text-align:left;border: 1px solid black; padding:5px;"><tbody> <tr> <th>1</th><td>10.7</td> </tr><tr> <th>2</th><td>20.2</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); it('uses <td> for first column when firstColHeader = false', () => { const cols = [ [1, 1.2], [2, 2.5], ]; const headers = ['A', 'B']; const actual = htmlTable(cols, headers, { firstColHeader: false }); const expected = ` <div> <table style="text-align:left;border: 1px solid black; padding:5px;"><thead> <tr><th>A</th><th>B</th></tr> </thead><tbody> <tr> <td>1</td><td>1.2</td> </tr><tr> <td>2</td><td>2.5</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); it('renders header and description, and applies custom style', () => { const cols = [ [10, 0, 0.00056], // 0.00056 -> "0.0006" по твоей round ]; const headers = []; // без thead const options = { header: 'My Title', description: 'My Description', style: 'border:1px solid;', }; const actual = htmlTable(cols, headers, options); const expected = ` <div> <h3>My Title</h3> <p>My Description</p> <table style="border:1px solid;"><tbody> <tr> <th>10</th><td>0</td><td>0.0006</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); it('respects fixed option (caps precision)', () => { const cols = [ [1, 0.00056], // при fixed=3 -> "0.001" ]; const headers = ['H1', 'H2']; const actual = htmlTable(cols, headers, { fixed: 3 }); const expected = ` <div> <table style="text-align:left;border: 1px solid black; padding:5px;"><thead> <tr><th>H1</th><th>H2</th></tr> </thead><tbody> <tr> <th>1</th><td>0.001</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); it('handles zeros/null/undefined as 0 via round', () => { const cols = [ [1, 0, null, undefined], ]; const headers = ['C1', 'C2', 'C3', 'C4']; const actual = htmlTable(cols, headers); const expected = ` <div> <table style="text-align:left;border: 1px solid black; padding:5px;"><thead> <tr><th>C1</th><th>C2</th><th>C3</th><th>C4</th></tr> </thead><tbody> <tr> <th>1</th><td>0</td><td>0</td><td>0</td> </tr> </tbody></table> </div>`.trim(); assert.strictEqual(normalize(actual), normalize(expected)); }); });