Skip to content

Commit 1a39e0c

Browse files
authored
fix: Generate larger tables more quickly (#224)
1 parent 28e8e6e commit 1a39e0c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/spanningCellManager.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
} from './types/internal';
2222
import {
2323
areCellEqual,
24-
findOriginalRowIndex,
24+
flatten,
2525
isCellInRange, sequence, sumArray,
2626
} from './utils';
2727

@@ -30,6 +30,8 @@ export type SpanningCellManager = {
3030
inSameRange: (cell1: CellCoordinates, cell2: CellCoordinates) => boolean,
3131
rowHeights: number[],
3232
setRowHeights: (rowHeights: number[]) => void,
33+
rowIndexMapping: number[],
34+
setRowIndexMapping: (mappedRowHeights: number[]) => void,
3335
};
3436

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

116118
let rowHeights: number[] = [];
119+
let rowIndexMapping: number[] = [];
117120

118121
return {getContainingRange: (cell, options) => {
119-
const originalRow = options?.mapped ? findOriginalRowIndex(rowHeights, cell.row) : cell.row;
122+
const originalRow = options?.mapped ? rowIndexMapping[cell.row] : cell.row;
120123

121124
const range = findRangeConfig({...cell,
122125
row: originalRow}, ranges);
@@ -139,7 +142,15 @@ export const createSpanningCellManager = (parameters: SpanningCellParameters): S
139142
return inSameRange(cell1, cell2, ranges);
140143
},
141144
rowHeights,
145+
rowIndexMapping,
142146
setRowHeights: (_rowHeights: number[]) => {
143147
rowHeights = _rowHeights;
148+
},
149+
setRowIndexMapping: (mappedRowHeights: number[]) => {
150+
rowIndexMapping = flatten(mappedRowHeights.map((height, index) => {
151+
return Array.from({length: height}, () => {
152+
return index;
153+
});
154+
}));
144155
}};
145156
};

src/table.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const table = (data: unknown[][], userConfig: TableUserConfig = {}): stri
5252
const rowHeights = calculateRowHeights(rows, config);
5353

5454
config.spanningCellManager.setRowHeights(rowHeights);
55+
config.spanningCellManager.setRowIndexMapping(rowHeights);
5556

5657
rows = mapDataUsingRowHeights(rows, rowHeights, config);
5758
rows = alignTableData(rows, config);

src/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,6 @@ export const flatten = <T>(array: T[][]): T[] => {
127127
return ([] as T[]).concat(...array);
128128
};
129129

130-
export const findOriginalRowIndex = (mappedRowHeights: number[], mappedRowIndex: number): number => {
131-
const rowIndexMapping = flatten(mappedRowHeights.map((height, index) => {
132-
return Array.from({length: height}, () => {
133-
return index;
134-
});
135-
}));
136-
137-
return rowIndexMapping[mappedRowIndex];
138-
};
139-
140130
export const calculateRangeCoordinate = (spanningCellConfig: SpanningCellConfig): RangeCoordinate => {
141131
const {row, col, colSpan = 1, rowSpan = 1} = spanningCellConfig;
142132

0 commit comments

Comments
 (0)