Skip to content

Commit

Permalink
fixed some edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
maugenst committed Jan 8, 2024
1 parent ca04e7d commit 7be53cc
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 24 deletions.
10 changes: 6 additions & 4 deletions dist/lib/cjs/Tabletojson.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/cjs/Tabletojson.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/cjs/tsconfig-cjs.tsbuildinfo

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions dist/lib/mjs/Tabletojson.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/mjs/Tabletojson.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/mjs/tsconfig-mjs.tsbuildinfo

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
{
"transform": {
"^.+\\.(ts|tsx)$": ["ts-jest", {
"diagnostics": false,
"useESM": true
"diagnostics": false
}]
},
"extensionsToTreatAsEsm": [".ts", ".tsx"],
Expand Down
12 changes: 6 additions & 6 deletions lib/Tabletojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as cheerio from 'cheerio';
* The values will be concatenated with the given concatWith value
* Rowspans and colspans will be taken into account
* @property {number} from (Optional) Start row [default=0]
* @property {number} to End row
* @property {number} to End row, must be a value greater than 0
* @property {string} concatWith Concatenate the values with this string
*/
export type HeaderRows = {
Expand Down Expand Up @@ -119,6 +119,7 @@ export class Tabletojson {
// To have the correct names of keys in the json result we need to first analyze the header rows
// flatten them, and concatenate the values
if (options.headers) {
if (options.headers.to === 0) return;
const rows: number[] = [];
for (let i = options.headers.from || 0; i <= options.headers.to; i++) {
rows.push(i);
Expand All @@ -137,7 +138,7 @@ export class Tabletojson {
const createNew2DArray = (columns: number, ca_rows: number, defaultValue: any) => {
return Array.from(Array(ca_rows), (_row) => Array.from(Array(columns), (_cell) => defaultValue));
};
const headings: any[] = createNew2DArray(columnLength, rows.length || 0, undefined);
const headings: any[] = createNew2DArray(columnLength, rows.length, undefined);

// Fill the 2D array with the values from the table while taking care of the colspan and rowspan
rows.forEach((rowIndex: number, index: number) => {
Expand Down Expand Up @@ -169,9 +170,6 @@ export class Tabletojson {

// Flatten the 2D array by columns and concatenate the values with the given concatWith value
const flatten2DArrayByColumns = (arr) => {
// Ensure the array is not empty
if (arr.length === 0) return [];

const numRows = arr.length;
const numCols = arr[0].length;
const flattened: any[] = new Array(numCols).fill('');
Expand All @@ -193,7 +191,9 @@ export class Tabletojson {
$(`table${additionalSelectors} tr`).eq(rowToBeRemoved).remove();
});
// Add the new header row to the table
$(table).prepend(`<thead><tr><th>${flatHeadings.join('</th><th>')}</th></tr></thead>`);
if (flatHeadings.length > 0) {
$(table).prepend(`<thead><tr><th>${flatHeadings.join('</th><th>')}</th></tr></thead>`);
}
}

// Regular table work starts now
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@
"lint": "eslint . -c .eslintrc --ext .ts",
"prepare": "npm run clean && npm run build && npm run lint",
"prebuild": "npm run lint",
"test": "jest --coverage"
"test": "jest --coverage --"
}
}
2 changes: 0 additions & 2 deletions test/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -1448,8 +1448,6 @@ <h2>Table #13: Table with no headers</h2>
<h2>Table #14: Table with content</h2>
<table id="table14" class="table" border="1">
<tbody>
<tr>
</tr>
</tbody>
</table>

Expand Down
14 changes: 13 additions & 1 deletion test/tabletojsonLocal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,18 @@ describe('TableToJSON Local', function () {
expect(_.has(table[0], 'Industry')).toBeTruthy();
expect(table[0].Industry).toEqual('total');
expect(converted.length).toBe(1);
fs.writeFileSync('test/table15.json', JSON.stringify(table, null, 2));
});

it('Converting a multi-level header table with no content', async function () {
const converted = tabletojson.convert(html, {
id: ['table14'],
headers: {
to: 0,
concatWith: ' ',
},
});
expect(converted).toBeDefined();
expect(Array.isArray(converted)).toBeTruthy();
expect(converted.length).toBe(0);
});
});

0 comments on commit 7be53cc

Please sign in to comment.