All files / src/geometry TableData.ts

95% Statements 57/60
92.59% Branches 25/27
100% Functions 7/7
95% Lines 57/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                            1x 17x 17x 17x 17x 17x   17x 17x 17x 17x 17x 17x 17x   17x 673x 353x 2021x 7431x 353x 353x 7431x 1668x     320x 673x   17x 3x 3x   17x 3x 3x   17x                                 3x 3x   3x 8x 23x 23x 23x 8x   3x       3x 3x   17x 11x 11x 78x 78x 312x 312x 312x 312x 312x 78x 78x 11x 11x 17x  
import type { Point } from './Point.js';
 
export type TableCell = {
	minXY: Point;
	maxXY: Point;
	width: number;
	height: number;
	colspan?: number;
	rowspan?: number;
	text: Array<string>;
};
 
export type TableRow = Array<TableCell>;
 
export class TableData {
	public minXY: Point;
	public maxXY: Point;
	public rows: Array<TableRow>;
	private rowPivots: Array<number>;
	private colPivots: Array<number>;
 
	constructor(minXY: Point, maxXY: Point, rowPivots: Array<number>, colPivots: Array<number>) {
		this.minXY = minXY;
		this.maxXY = maxXY;
		this.rows = [];
		this.rowPivots = rowPivots;
		this.colPivots = colPivots;
	}
 
	public findCell(x: number, y: number): TableCell | undefined {
		if (x >= this.minXY.x && y >= this.minXY.y && x <= this.maxXY.x && y <= this.maxXY.y) {
			for (const row of this.rows) {
				for (const cell of row) {
					if (cell.minXY.x <= x && cell.minXY.y <= y && cell.maxXY.x >= x && cell.maxXY.y >= y) {
						return cell;
					}
				}
			}
		}
 
		return undefined;
	}
 
	public get cellCount() {
		return this.rows.reduce((acc, row) => acc + row.length, 0);
	}
 
	public get rowCount() {
		return this.rows.length;
	}
 
	public check(): boolean {
		// const cellCounts:Array<number> = []
		//
		// for (const row of this.rows) {
		//     let cellNum = 0
		//     for (const cell of row) {
		//         cellNum += cell.colspan || 1
		//     }
		//     cellCounts.push(cellNum)
		// }
		//
		// for (let i = 1; i < cellCounts.length; i++) {
		//     if (cellCounts[i] !== cellCounts[i - 1]) {
		//         return false
		//     }
		// }
 
		const virtualCellCount = (this.colPivots.length - 1) * (this.rowPivots.length - 1);
		let allCellCount = 0;
 
		for (const row of this.rows) {
			for (const cell of row) {
				const count = (cell.colspan || 1) * (cell.rowspan || 1);
				allCellCount += count;
			}
		}
 
		if (virtualCellCount !== allCellCount) {
			return false;
		}
 
		return true;
	}
 
	public toArray(): string[][] {
		const tableArr: string[][] = [];
		for (const row of this.rows) {
			const rowArr: string[] = [];
			for (const cell of row) {
				let text = cell.text.join('');
				text = text.replace(/^[\s]+|[\s]+$/g, '');
				text = text.trim();
				rowArr.push(text);
			}
			tableArr.push(rowArr);
		}
		return tableArr;
	}
}