diff --git a/src/stringifyTableData.ts b/src/stringifyTableData.ts index ab7e299..7637452 100644 --- a/src/stringifyTableData.ts +++ b/src/stringifyTableData.ts @@ -5,7 +5,7 @@ import { normalizeString, } from './utils'; -export const stringifyTableData = (rows: unknown[][]): Row[] => { +export const stringifyTableData = (rows: ReadonlyArray): Row[] => { return rows.map((cells) => { return cells.map((cell) => { return normalizeString(String(cell)); diff --git a/src/table.ts b/src/table.ts index d4b3b4c..64315ee 100644 --- a/src/table.ts +++ b/src/table.ts @@ -38,7 +38,7 @@ import { validateTableData, } from './validateTableData'; -export const table = (data: unknown[][], userConfig: TableUserConfig = {}): string => { +export const table = (data: ReadonlyArray, userConfig: TableUserConfig = {}): string => { validateTableData(data); let rows = stringifyTableData(data); diff --git a/src/validateTableData.ts b/src/validateTableData.ts index fa5367d..1336ef4 100644 --- a/src/validateTableData.ts +++ b/src/validateTableData.ts @@ -2,7 +2,7 @@ import { normalizeString, } from './utils'; -export const validateTableData = (rows: unknown[][]): void => { +export const validateTableData = (rows: ReadonlyArray): void => { if (!Array.isArray(rows)) { throw new TypeError('Table data must be an array.'); } diff --git a/test/table.ts b/test/table.ts index b2f90b0..42dee3e 100644 --- a/test/table.ts +++ b/test/table.ts @@ -187,4 +187,21 @@ describe('drawTable', () => { ╚══════════╧══════════╧══════════╧══════════╝`); }); }); + + context('readonly array data type input', () => { + it('works properly', () => { + const dataReadonly = data as ReadonlyArray; + + const result = table(dataReadonly); + + expectTable(result, ` +╔═════════════╤═════════════╗ +║ Lorem ipsum │ dolor sit ║ +╟─────────────┼─────────────╢ +║ amet │ consectetur ║ +╟─────────────┼─────────────╢ +║ adipiscing │ elit ║ +╚═════════════╧═════════════╝`); + }); + }); });