Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow readonly array input for table data #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/stringifyTableData.ts
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
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
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
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 ║
╚═════════════╧═════════════╝`);
});
});
});