Skip to content

Commit a7758f1

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

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/output.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,25 @@ 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(
96+
// eslint-disable-line no-console
97+
`Warning: unknown column(s): ${invalid.join(", ")}. Available: ${[...allKeys].join(", ")}`
98+
);
99+
}
100+
selectedColumns = columns.filter((c) => allKeys.has(c));
101+
if (selectedColumns.length === 0) {
102+
selectedColumns = [...allKeys];
103+
}
86104
} else {
87105
const allKeys = new Set<string>();
88106
for (const item of arrayData) {

0 commit comments

Comments
 (0)