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: Generate larger tables more quickly #224

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
15 changes: 13 additions & 2 deletions src/spanningCellManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
} from './types/internal';
import {
areCellEqual,
findOriginalRowIndex,
flatten,
isCellInRange, sequence, sumArray,
} from './utils';

Expand All @@ -30,6 +30,8 @@ export type SpanningCellManager = {
inSameRange: (cell1: CellCoordinates, cell2: CellCoordinates) => boolean,
rowHeights: number[],
setRowHeights: (rowHeights: number[]) => void,
rowIndexMapping: number[],
setRowIndexMapping: (mappedRowHeights: number[]) => void,
};

export type SpanningCellParameters = {
Expand Down Expand Up @@ -114,9 +116,10 @@ export const createSpanningCellManager = (parameters: SpanningCellParameters): S
const rangeCache: Record<string, ResolvedRangeConfig | undefined> = {};

let rowHeights: number[] = [];
let rowIndexMapping: number[] = [];

return {getContainingRange: (cell, options) => {
const originalRow = options?.mapped ? findOriginalRowIndex(rowHeights, cell.row) : cell.row;
const originalRow = options?.mapped ? rowIndexMapping[cell.row] : cell.row;

const range = findRangeConfig({...cell,
row: originalRow}, ranges);
Expand All @@ -139,7 +142,15 @@ export const createSpanningCellManager = (parameters: SpanningCellParameters): S
return inSameRange(cell1, cell2, ranges);
},
rowHeights,
rowIndexMapping,
setRowHeights: (_rowHeights: number[]) => {
rowHeights = _rowHeights;
},
setRowIndexMapping: (mappedRowHeights: number[]) => {
rowIndexMapping = flatten(mappedRowHeights.map((height, index) => {
return Array.from({length: height}, () => {
return index;
});
}));
}};
};
1 change: 1 addition & 0 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const table = (data: unknown[][], userConfig: TableUserConfig = {}): stri
const rowHeights = calculateRowHeights(rows, config);

config.spanningCellManager.setRowHeights(rowHeights);
config.spanningCellManager.setRowIndexMapping(rowHeights);

rows = mapDataUsingRowHeights(rows, rowHeights, config);
rows = alignTableData(rows, config);
Expand Down
10 changes: 0 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,6 @@ export const flatten = <T>(array: T[][]): T[] => {
return ([] as T[]).concat(...array);
};

export const findOriginalRowIndex = (mappedRowHeights: number[], mappedRowIndex: number): number => {
const rowIndexMapping = flatten(mappedRowHeights.map((height, index) => {
return Array.from({length: height}, () => {
return index;
});
}));

return rowIndexMapping[mappedRowIndex];
};

export const calculateRangeCoordinate = (spanningCellConfig: SpanningCellConfig): RangeCoordinate => {
const {row, col, colSpan = 1, rowSpan = 1} = spanningCellConfig;

Expand Down