From 734d6d0e6e42929caa33c9b629f9555efd69f3b0 Mon Sep 17 00:00:00 2001 From: Jakub Szymczak Date: Fri, 9 Feb 2024 17:08:30 +0100 Subject: [PATCH 1/5] migrate markdownTable --- .../{markdownTable.js => markdownTable.ts} | 349 +++++++++--------- 1 file changed, 169 insertions(+), 180 deletions(-) rename tests/e2e/compare/output/{markdownTable.js => markdownTable.ts} (54%) diff --git a/tests/e2e/compare/output/markdownTable.js b/tests/e2e/compare/output/markdownTable.ts similarity index 54% rename from tests/e2e/compare/output/markdownTable.js rename to tests/e2e/compare/output/markdownTable.ts index 3b580bcfc1167..eea4297048adc 100644 --- a/tests/e2e/compare/output/markdownTable.js +++ b/tests/e2e/compare/output/markdownTable.ts @@ -1,150 +1,158 @@ /* eslint-disable */ // copied from https://raw.githubusercontent.com/wooorm/markdown-table/main/index.js, turned into cmjs -/** - * @typedef Options - * Configuration (optional). - * @property {string|null|Array} [align] - * One style for all columns, or styles for their respective columns. - * Each style is either `'l'` (left), `'r'` (right), or `'c'` (center). - * Other values are treated as `''`, which doesn’t place the colon in the - * alignment row but does align left. - * *Only the lowercased first character is used, so `Right` is fine.* - * @property {boolean} [padding=true] - * Whether to add a space of padding between delimiters and cells. - * - * When `true`, there is padding: - * - * ```markdown - * | Alpha | B | - * | ----- | ----- | - * | C | Delta | - * ``` - * - * When `false`, there is no padding: - * - * ```markdown - * |Alpha|B | - * |-----|-----| - * |C |Delta| - * ``` - * @property {boolean} [delimiterStart=true] - * Whether to begin each row with the delimiter. - * - * > 👉 **Note**: please don’t use this: it could create fragile structures - * > that aren’t understandable to some markdown parsers. - * - * When `true`, there are starting delimiters: - * - * ```markdown - * | Alpha | B | - * | ----- | ----- | - * | C | Delta | - * ``` - * - * When `false`, there are no starting delimiters: - * - * ```markdown - * Alpha | B | - * ----- | ----- | - * C | Delta | - * ``` - * @property {boolean} [delimiterEnd=true] - * Whether to end each row with the delimiter. - * - * > 👉 **Note**: please don’t use this: it could create fragile structures - * > that aren’t understandable to some markdown parsers. - * - * When `true`, there are ending delimiters: - * - * ```markdown - * | Alpha | B | - * | ----- | ----- | - * | C | Delta | - * ``` - * - * When `false`, there are no ending delimiters: - * - * ```markdown - * | Alpha | B - * | ----- | ----- - * | C | Delta - * ``` - * @property {boolean} [alignDelimiters=true] - * Whether to align the delimiters. - * By default, they are aligned: - * - * ```markdown - * | Alpha | B | - * | ----- | ----- | - * | C | Delta | - * ``` - * - * Pass `false` to make them staggered: - * - * ```markdown - * | Alpha | B | - * | - | - | - * | C | Delta | - * ``` - * @property {(value: string) => number} [stringLength] - * Function to detect the length of table cell content. - * This is used when aligning the delimiters (`|`) between table cells. - * Full-width characters and emoji mess up delimiter alignment when viewing - * the markdown source. - * To fix this, you can pass this function, which receives the cell content - * and returns its “visible” size. - * Note that what is and isn’t visible depends on where the text is displayed. - * - * Without such a function, the following: - * - * ```js - * markdownTable([ - * ['Alpha', 'Bravo'], - * ['中文', 'Charlie'], - * ['👩‍❤️‍👩', 'Delta'] - * ]) - * ``` - * - * Yields: - * - * ```markdown - * | Alpha | Bravo | - * | - | - | - * | 中文 | Charlie | - * | 👩‍❤️‍👩 | Delta | - * ``` - * - * With [`string-width`](https://github.com/sindresorhus/string-width): - * - * ```js - * import stringWidth from 'string-width' - * - * markdownTable( - * [ - * ['Alpha', 'Bravo'], - * ['中文', 'Charlie'], - * ['👩‍❤️‍👩', 'Delta'] - * ], - * {stringLength: stringWidth} - * ) - * ``` - * - * Yields: - * - * ```markdown - * | Alpha | Bravo | - * | ----- | ------- | - * | 中文 | Charlie | - * | 👩‍❤️‍👩 | Delta | - * ``` - */ - -/** - * @typedef {Options} MarkdownTableOptions - * @todo - * Remove next major. - */ +type MarkdownTableOptions = { + /** + * One style for all columns, or styles for their respective columns. + * Each style is either `'l'` (left), `'r'` (right), or `'c'` (center). + * Other values are treated as `''`, which doesn’t place the colon in the + * alignment row but does align left. + * *Only the lowercased first character is used, so `Right` is fine.* + */ + align?: string | null | Array; + + /** + * Whether to add a space of padding between delimiters and cells. + * + * When `true`, there is padding: + * + * ```markdown + * | Alpha | B | + * | ----- | ----- | + * | C | Delta | + * ``` + * + * When `false`, there is no padding: + * + * ```markdown + * |Alpha|B | + * |-----|-----| + * |C |Delta| + * ``` + */ + padding?: boolean; + + /** + * Whether to begin each row with the delimiter. + * + * > 👉 **Note**: please don’t use this: it could create fragile structures + * > that aren’t understandable to some markdown parsers. + * + * When `true`, there are starting delimiters: + * + * ```markdown + * | Alpha | B | + * | ----- | ----- | + * | C | Delta | + * ``` + * + * When `false`, there are no starting delimiters: + * + * ```markdown + * Alpha | B | + * ----- | ----- | + * C | Delta | + * ``` + */ + delimiterStart?: boolean; + /** + * Whether to end each row with the delimiter. + * + * > 👉 **Note**: please don’t use this: it could create fragile structures + * > that aren’t understandable to some markdown parsers. + * + * When `true`, there are ending delimiters: + * + * ```markdown + * | Alpha | B | + * | ----- | ----- | + * | C | Delta | + * ``` + * + * When `false`, there are no ending delimiters: + * + * ```markdown + * | Alpha | B + * | ----- | ----- + * | C | Delta + * ``` + */ + delimiterEnd?: boolean; + + /** + * Whether to align the delimiters. + * By default, they are aligned: + * + * ```markdown + * | Alpha | B | + * | ----- | ----- | + * | C | Delta | + * ``` + * + * Pass `false` to make them staggered: + * + * ```markdown + * | Alpha | B | + * | - | - | + * | C | Delta | + * ``` + */ + alignDelimiters?: boolean; + + /** + * Function to detect the length of table cell content. + * This is used when aligning the delimiters (`|`) between table cells. + * Full-width characters and emoji mess up delimiter alignment when viewing + * the markdown source. + * To fix this, you can pass this function, which receives the cell content + * and returns its “visible” size. + * Note that what is and isn’t visible depends on where the text is displayed. + * + * Without such a function, the following: + * + * ```js + * markdownTable([ + * ['Alpha', 'Bravo'], + * ['中文', 'Charlie'], + * ['👩‍❤️‍👩', 'Delta'] + * ]) + * ``` + * + * Yields: + * + * ```markdown + * | Alpha | Bravo | + * | - | - | + * | 中文 | Charlie | + * | 👩‍❤️‍👩 | Delta | + * ``` + * + * With [`string-width`](https://github.com/sindresorhus/string-width): + * + * ```js + * import stringWidth from 'string-width' + * + * markdownTable( + * [ + * ['Alpha', 'Bravo'], + * ['中文', 'Charlie'], + * ['👩‍❤️‍👩', 'Delta'] + * ], + * {stringLength: stringWidth} + * ) + * ``` + * + * Yields: + * + * ```markdown + * | Alpha | Bravo | + * | ----- | ------- | + * | 中文 | Charlie | + * | 👩‍❤️‍👩 | Delta | + * ``` + */ + stringLength?: (value: string) => number; +}; /** * Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table.. @@ -155,27 +163,24 @@ * Configuration (optional). * @returns {string} */ -function markdownTable(table, options = {}) { +function markdownTable(table: Array>, options: MarkdownTableOptions = {}) { const align = (options.align || []).concat(); const stringLength = options.stringLength || defaultStringLength; - /** @type {Array} Character codes as symbols for alignment per column. */ - const alignments = []; - /** @type {Array>} Cells per row. */ - const cellMatrix = []; - /** @type {Array>} Sizes of each cell per row. */ - const sizeMatrix = []; - /** @type {Array} */ - const longestCellByColumn = []; + /** Character codes as symbols for alignment per column. */ + const alignments: Array = []; + /** Cells per row. */ + const cellMatrix: Array> = []; + /** Sizes of each cell per row. */ + const sizeMatrix: Array> = []; + const longestCellByColumn: Array = []; let mostCellsPerRow = 0; let rowIndex = -1; // This is a superfluous loop if we don’t align delimiters, but otherwise we’d // do superfluous work when aligning, so optimize for aligning. while (++rowIndex < table.length) { - /** @type {Array} */ - const row = []; - /** @type {Array} */ - const sizes = []; + const row: Array = []; + const sizes: Array = []; let columnIndex = -1; if (table[rowIndex].length > mostCellsPerRow) { @@ -218,10 +223,8 @@ function markdownTable(table, options = {}) { // Inject the alignment row. columnIndex = -1; - /** @type {Array} */ - const row = []; - /** @type {Array} */ - const sizes = []; + const row: Array = []; + const sizes: Array = []; while (++columnIndex < mostCellsPerRow) { const code = alignments[columnIndex]; @@ -260,15 +263,13 @@ function markdownTable(table, options = {}) { sizeMatrix.splice(1, 0, sizes); rowIndex = -1; - /** @type {Array} */ - const lines = []; + const lines: Array = []; while (++rowIndex < cellMatrix.length) { const row = cellMatrix[rowIndex]; const sizes = sizeMatrix[rowIndex]; columnIndex = -1; - /** @type {Array} */ - const line = []; + const line: Array = []; while (++columnIndex < mostCellsPerRow) { const cell = row[columnIndex] || ''; @@ -333,27 +334,15 @@ function markdownTable(table, options = {}) { return lines.join('\n'); } -/** - * @param {string|null|undefined} [value] - * @returns {string} - */ -function serialize(value) { +function serialize(value: string | null | undefined): string { return value === null || value === undefined ? '' : String(value); } -/** - * @param {string} value - * @returns {number} - */ -function defaultStringLength(value) { +function defaultStringLength(value: string): number { return value.length; } -/** - * @param {string|null|undefined} value - * @returns {number} - */ -function toAlignment(value) { +function toAlignment(value: string | null | undefined): number { const code = typeof value === 'string' ? value.codePointAt(0) : 0; return code === 67 /* `C` */ || code === 99 /* `c` */ From 9e2f5125573999601a33e3e0abb443307dd4dde9 Mon Sep 17 00:00:00 2001 From: Jakub Szymczak Date: Fri, 9 Feb 2024 17:39:53 +0100 Subject: [PATCH 2/5] switch from module export to export default --- tests/e2e/compare/output/markdownTable.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/compare/output/markdownTable.ts b/tests/e2e/compare/output/markdownTable.ts index eea4297048adc..a22e0a5d3366e 100644 --- a/tests/e2e/compare/output/markdownTable.ts +++ b/tests/e2e/compare/output/markdownTable.ts @@ -354,4 +354,4 @@ function toAlignment(value: string | null | undefined): number { : 0; } -module.exports = markdownTable; +export default markdownTable; From a13ecf200b13e6796097a96d3d0541b9b1ea87bb Mon Sep 17 00:00:00 2001 From: Jakub Szymczak Date: Mon, 12 Feb 2024 10:49:44 +0100 Subject: [PATCH 3/5] fix PR comments --- tests/e2e/compare/output/markdownTable.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/e2e/compare/output/markdownTable.ts b/tests/e2e/compare/output/markdownTable.ts index a22e0a5d3366e..e22325e4d8d12 100644 --- a/tests/e2e/compare/output/markdownTable.ts +++ b/tests/e2e/compare/output/markdownTable.ts @@ -167,20 +167,20 @@ function markdownTable(table: Array>, options: const align = (options.align || []).concat(); const stringLength = options.stringLength || defaultStringLength; /** Character codes as symbols for alignment per column. */ - const alignments: Array = []; + const alignments: number[] = []; /** Cells per row. */ const cellMatrix: Array> = []; /** Sizes of each cell per row. */ const sizeMatrix: Array> = []; - const longestCellByColumn: Array = []; + const longestCellByColumn: number[] = []; let mostCellsPerRow = 0; let rowIndex = -1; // This is a superfluous loop if we don’t align delimiters, but otherwise we’d // do superfluous work when aligning, so optimize for aligning. while (++rowIndex < table.length) { - const row: Array = []; - const sizes: Array = []; + const row: string[] = []; + const sizes: number[] = []; let columnIndex = -1; if (table[rowIndex].length > mostCellsPerRow) { @@ -223,8 +223,8 @@ function markdownTable(table: Array>, options: // Inject the alignment row. columnIndex = -1; - const row: Array = []; - const sizes: Array = []; + const row: string[] = []; + const sizes: number[] = []; while (++columnIndex < mostCellsPerRow) { const code = alignments[columnIndex]; @@ -263,13 +263,13 @@ function markdownTable(table: Array>, options: sizeMatrix.splice(1, 0, sizes); rowIndex = -1; - const lines: Array = []; + const lines: string[] = []; while (++rowIndex < cellMatrix.length) { const row = cellMatrix[rowIndex]; const sizes = sizeMatrix[rowIndex]; columnIndex = -1; - const line: Array = []; + const line: string[] = []; while (++columnIndex < mostCellsPerRow) { const cell = row[columnIndex] || ''; From 9bad094e91a4c7b0fd923363da2445d0440584e4 Mon Sep 17 00:00:00 2001 From: Jakub Szymczak Date: Tue, 13 Feb 2024 13:55:17 +0100 Subject: [PATCH 4/5] fix PR comments --- tests/e2e/compare/output/markdownTable.ts | 73 +++++++++++------------ 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/tests/e2e/compare/output/markdownTable.ts b/tests/e2e/compare/output/markdownTable.ts index e22325e4d8d12..51f0beeb6979d 100644 --- a/tests/e2e/compare/output/markdownTable.ts +++ b/tests/e2e/compare/output/markdownTable.ts @@ -1,4 +1,3 @@ -/* eslint-disable */ // copied from https://raw.githubusercontent.com/wooorm/markdown-table/main/index.js, turned into cmjs type MarkdownTableOptions = { @@ -154,24 +153,42 @@ type MarkdownTableOptions = { stringLength?: (value: string) => number; }; -/** - * Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table.. - * - * @param {Array>} table - * Table data (matrix of strings). - * @param {Options} [options] - * Configuration (optional). - * @returns {string} - */ +function serialize(value: string | null | undefined): string { + return value === null || value === undefined ? '' : String(value); +} + +function defaultStringLength(value: string): number { + return value.length; +} + +function toAlignment(value: string | null | undefined): number { + const code = typeof value === 'string' ? value.codePointAt(0) : 0; + + if (code === 67 /* `C` */ || code === 99 /* `c` */) { + return 99; /* `c` */ + } + + if (code === 76 /* `L` */ || code === 108 /* `l` */) { + return 108; /* `l` */ + } + + if (code === 82 /* `R` */ || code === 114 /* `r` */) { + return 114; /* `r` */ + } + + return 0; +} + +/** Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table.. */ function markdownTable(table: Array>, options: MarkdownTableOptions = {}) { - const align = (options.align || []).concat(); - const stringLength = options.stringLength || defaultStringLength; + const align = (options.align ?? []).concat(); + const stringLength = options.stringLength ?? defaultStringLength; /** Character codes as symbols for alignment per column. */ const alignments: number[] = []; /** Cells per row. */ - const cellMatrix: Array> = []; + const cellMatrix: string[][] = []; /** Sizes of each cell per row. */ - const sizeMatrix: Array> = []; + const sizeMatrix: number[][] = []; const longestCellByColumn: number[] = []; let mostCellsPerRow = 0; let rowIndex = -1; @@ -266,18 +283,18 @@ function markdownTable(table: Array>, options: const lines: string[] = []; while (++rowIndex < cellMatrix.length) { - const row = cellMatrix[rowIndex]; - const sizes = sizeMatrix[rowIndex]; + const matrixRow = cellMatrix[rowIndex]; + const matrixSizes = sizeMatrix[rowIndex]; columnIndex = -1; const line: string[] = []; while (++columnIndex < mostCellsPerRow) { - const cell = row[columnIndex] || ''; + const cell = matrixRow[columnIndex] || ''; let before = ''; let after = ''; if (options.alignDelimiters !== false) { - const size = longestCellByColumn[columnIndex] - (sizes[columnIndex] || 0); + const size = longestCellByColumn[columnIndex] - (matrixSizes[columnIndex] || 0); const code = alignments[columnIndex]; if (code === 114 /* `r` */) { @@ -334,24 +351,4 @@ function markdownTable(table: Array>, options: return lines.join('\n'); } -function serialize(value: string | null | undefined): string { - return value === null || value === undefined ? '' : String(value); -} - -function defaultStringLength(value: string): number { - return value.length; -} - -function toAlignment(value: string | null | undefined): number { - const code = typeof value === 'string' ? value.codePointAt(0) : 0; - - return code === 67 /* `C` */ || code === 99 /* `c` */ - ? 99 /* `c` */ - : code === 76 /* `L` */ || code === 108 /* `l` */ - ? 108 /* `l` */ - : code === 82 /* `R` */ || code === 114 /* `r` */ - ? 114 /* `r` */ - : 0; -} - export default markdownTable; From de2111c187b2cd6371a4425f1cd293b2961840ea Mon Sep 17 00:00:00 2001 From: Jakub Szymczak Date: Tue, 13 Feb 2024 16:04:15 +0100 Subject: [PATCH 5/5] fix bugs --- tests/e2e/compare/output/markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/compare/output/markdown.js b/tests/e2e/compare/output/markdown.js index 2015d8de6cc39..112c6780d4995 100644 --- a/tests/e2e/compare/output/markdown.js +++ b/tests/e2e/compare/output/markdown.js @@ -1,9 +1,9 @@ // From: https://raw.githubusercontent.com/callstack/reassure/main/packages/reassure-compare/src/output/markdown.ts +import markdownTable from './markdownTable'; const fs = require('node:fs/promises'); const path = require('path'); const _ = require('underscore'); -const markdownTable = require('./markdownTable'); const {formatDuration, formatPercent, formatDurationDiffChange} = require('./format'); const Logger = require('../../utils/logger');