Skip to content

Commit da75ba6

Browse files
committed
make interface more robust
1 parent a4d1d7f commit da75ba6

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

lib/html-table-parser.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { parseTable, getElementPositionInTable } from "./html-table-parser"
1+
import {
2+
parseTable,
3+
getElementPositionInCellMatrix,
4+
createCellMatrix
5+
} from "./html-table-parser"
26

37
const cases = [
48
{
@@ -273,7 +277,8 @@ describe("getElementPositionInTable", () => {
273277
<tr><td>3</td><td>4</tr>
274278
`)
275279
const cell = table.querySelectorAll("td")[3]
276-
expect(getElementPositionInTable(table, cell)).toStrictEqual({
280+
const matrix = createCellMatrix(table)
281+
expect(getElementPositionInCellMatrix(matrix, cell)).toStrictEqual({
277282
x: 1,
278283
y: 1
279284
})

lib/html-table-parser.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ type RowElement = HTMLTableRowElement
22
type CellElement = HTMLTableHeaderCellElement | HTMLTableCellElement
33

44
export function parseTable(tableElement: HTMLTableElement) {
5+
return parseCellMatrix(createCellMatrix(tableElement))
6+
}
7+
8+
export function parseCellMatrix(cellMatrix: CellElement[][]) {
59
let elementList = []
610
let parsedInfoList = []
711

8-
return createCellMatrix(tableElement).map((row, rowIndex) =>
12+
return cellMatrix.map((row, rowIndex) =>
913
row.map((cell, colIndex) => {
1014
let index = elementList.indexOf(cell)
1115
if (index === -1) {
@@ -30,13 +34,12 @@ export function parseTable(tableElement: HTMLTableElement) {
3034
)
3135
}
3236

33-
export function getElementPositionInTable(
34-
tableElement: HTMLTableElement,
37+
export function getElementPositionInCellMatrix(
38+
cellMatrix: CellElement[][],
3539
cellElement: CellElement
3640
) {
37-
const matrix = createCellMatrix(tableElement)
38-
for (let y = 0; y < matrix.length; y++) {
39-
const row = matrix[y]
41+
for (let y = 0; y < cellMatrix.length; y++) {
42+
const row = cellMatrix[y]
4043
for (let x = 0; x < row.length; x++) {
4144
if (row[x] === cellElement) {
4245
return { x, y }
@@ -57,7 +60,9 @@ Normalize table structure considering colspan and rowspan.
5760
| | D |
5861
+-------+-------+
5962
*/
60-
function createCellMatrix(tableElement: HTMLTableElement) {
63+
export function createCellMatrix(
64+
tableElement: HTMLTableElement
65+
): CellElement[][] {
6166
const rows = [tableElement.tHead]
6267
.concat(Array.from(tableElement.tBodies))
6368
.concat(tableElement.tFoot)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@autifyhq/html-table-parser",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Parse html tables and convert to json",
55
"source": "lib/html-table-parser.ts",
66
"main": "dist/html-table-parser.js",

0 commit comments

Comments
 (0)