Skip to content

Commit

Permalink
fix: allow readonly array input for table data
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Mar 19, 2023
1 parent 28e8e6e commit bbc04d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/stringifyTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
normalizeString,
} from './utils';

export const stringifyTableData = (rows: unknown[][]): Row[] => {
export const stringifyTableData = (rows: ReadonlyArray<readonly unknown[]>): Row[] => {
return rows.map((cells) => {
return cells.map((cell) => {
return normalizeString(String(cell));
Expand Down
2 changes: 1 addition & 1 deletion src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
validateTableData,
} from './validateTableData';

export const table = (data: unknown[][], userConfig: TableUserConfig = {}): string => {
export const table = (data: ReadonlyArray<readonly unknown[]>, userConfig: TableUserConfig = {}): string => {
validateTableData(data);

let rows = stringifyTableData(data);
Expand Down
2 changes: 1 addition & 1 deletion src/validateTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
normalizeString,
} from './utils';

export const validateTableData = (rows: unknown[][]): void => {
export const validateTableData = (rows: ReadonlyArray<readonly unknown[]>): void => {
if (!Array.isArray(rows)) {
throw new TypeError('Table data must be an array.');
}
Expand Down
17 changes: 17 additions & 0 deletions test/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,21 @@ describe('drawTable', () => {
╚══════════╧══════════╧══════════╧══════════╝`);
});
});

context('readonly array data type input', () => {
it('works properly', () => {
const dataReadonly = data as ReadonlyArray<readonly string[]>;

const result = table(dataReadonly);

expectTable(result, `
╔═════════════╤═════════════╗
║ Lorem ipsum │ dolor sit ║
╟─────────────┼─────────────╢
║ amet │ consectetur ║
╟─────────────┼─────────────╢
║ adipiscing │ elit ║
╚═════════════╧═════════════╝`);
});
});
});

0 comments on commit bbc04d6

Please sign in to comment.