Skip to content

Commit fee65a7

Browse files
committed
warn on unknown columns
1 parent 51a6cd1 commit fee65a7

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/output.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,24 @@ function formatTable(data: unknown, columns?: string[]): string {
8282
if (firstItem && typeof firstItem === "object") {
8383
let selectedColumns: string[];
8484
if (columns && columns.length > 0) {
85-
selectedColumns = columns;
85+
const allKeys = new Set<string>();
86+
for (const item of arrayData) {
87+
if (item && typeof item === "object") {
88+
Object.keys(item as Record<string, unknown>).forEach((k) =>
89+
allKeys.add(k)
90+
);
91+
}
92+
}
93+
const invalid = columns.filter((c) => !allKeys.has(c));
94+
if (invalid.length > 0) {
95+
console.error( // eslint-disable-line no-console
96+
`Warning: unknown column(s): ${invalid.join(", ")}. Available: ${[...allKeys].join(", ")}`
97+
);
98+
}
99+
selectedColumns = columns.filter((c) => allKeys.has(c));
100+
if (selectedColumns.length === 0) {
101+
selectedColumns = [...allKeys];
102+
}
86103
} else {
87104
const allKeys = new Set<string>();
88105
for (const item of arrayData) {

0 commit comments

Comments
 (0)