Skip to content

Commit

Permalink
fix: Generate larger tables more quickly (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-allanson committed Mar 26, 2024
1 parent 28e8e6e commit 1a39e0c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/spanningCellManager.ts
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
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
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

0 comments on commit 1a39e0c

Please sign in to comment.