UNPKG

jsonweaver

Version:

A simple utility to transform JSON data into CSV, XML, YAML, JSONLines and Markdown table formats.

16 lines (15 loc) 676 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMarkdownTable = void 0; const toMarkdownTable = (json) => { if (json.length === 0) { throw new Error('Input JSON array is empty.'); } const headers = Array.from(new Set(json.flatMap(Object.keys))); const rows = json.map(obj => headers.map(header => obj[header] || '')); const headerRow = `| ${headers.join(' | ')} |`; const separatorRow = `| ${headers.map(() => '--- ').join('| ')}|`; const dataRows = rows.map(row => `| ${row.join(' | ')} |`); return [headerRow, separatorRow, ...dataRows].join('\n'); }; exports.toMarkdownTable = toMarkdownTable;