All files / src/elements table.ts

100% Statements 30/30
100% Branches 5/5
100% Functions 1/1
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
type Table<Columns extends string> = {
  rows: Array<Record<Columns, string>>;
};
 
/**
 * Creates a markdown table block.
 * @param rows - An array of objects representing the rows of the table.
 * @returns A markdown table string.
 * @example
 * ```typescript
 * table<'email' | 'description'>({
 *  rows: [
 *    {
 *      email: 'test@example.com',
 *      description: 'test email used in service',
 *    },
 *  ],
 * });
 * ```
 */
export const table = <Columns extends string>({ rows }: Table<Columns>) => {
  const columns = Object.keys(rows[0]) as Columns[];
  return [
    `| ${columns.map((header) => header.toUpperCase()).join(' | ')} |`,
    `| ${columns.map(() => '---').join(' | ')} |`,
    ...rows.map(
      (row) => `| ${columns.map((header) => row[header]).join(' | ')} |`,
    ),
  ].join('\n');
};