jsonify-my-table

Convert a HTML table to JSON
git clone http://git.hanabi.in/repos/jsonify-my-table.git
Log | Files | Refs | LICENSE

commit b4492ce1f3b0f9b2328f1070cb5430b19731d48e
parent d6da115b4e72a3d95a7d8a9273acb9a13a917050
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Sat, 10 Oct 2020 13:58:33 +0530

Add support for nested elements

Diffstat:
MCHANGELOG.md | 4++++
Mjsonify-my-table.js | 45++++++++++++++++++++++++++++++++-------------
Mjsonify-my-table.min.js | 3+--
Mpackage.json | 2+-
4 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.2 + +- Support for extracting text in this format `<td><span class="bold">Foo</span></td>`. + # 2.0.0 ## API changes diff --git a/jsonify-my-table.js b/jsonify-my-table.js @@ -1,32 +1,51 @@ HTMLTableElement.prototype.jsonify = function() { + const result = {}; + result["error"] = false; // 1. Does table have rows? - if (this.rows < 1) - return { error: `There are no rows in the table.` }; + if (this.rows < 1) { + result["error"] = "There are no rows in the table."; + return result; + } // 2. Extract headers. - const headers = [...this.rows[0].cells].map( - cell => cell.innerText - ); + const headers = [...this.rows[0].cells].map(cell => { + if (cell.childElementCount == 0) return cell.innerText; + if (cell.childElementCount != 1) { + result["error"] = "Can't extract headers."; + return undefined; + } + while (cell.childElementCount == 1) cell = cell.children[0]; + return cell.innerText; + }); // 3. Are headers unique? - if (headers.length > new Set(headers).size) - return { error: `Headers repeat.` }; + if (headers.length > new Set(headers).size) { + result["error"] = "Headers repeat"; + return result; + } // 4. Prepare the result. const res = [...this.rows].map((row, index) => { if (index == 0) return; // This is header - const cells = [...row.cells].map( - cell => cell.innerText - ); + const cells = [...row.cells].map(cell => { + if (cell.childElementCount == 0) return cell.innerText; + if (cell.childElementCount != 1) { + result["error"] = "Can't extract information."; + return undefined; + } + while (cell.childElementCount == 1) cell = cell.children[0]; + return cell.innerText; + }); return headers.reduce( - (acc, cur, index) => - (acc = { ...acc, [cur.valueOf()]: cells[index] }), + (acc, cur, index) => (acc = { ...acc, [cur.valueOf()]: cells[index] }), {} ); }); res.shift(); // Remove header - return { error: false, res, headers }; + result["res"] = res; + result["headers"] = headers; + return result; }; if (!window) module.exports = jsonify; diff --git a/jsonify-my-table.min.js b/jsonify-my-table.min.js @@ -1 +1 @@ -HTMLTableElement.prototype.jsonify=function(){if(this.rows<1){return{error:'There are no rows in the table.'}}const H=[...this.rows[0].cells].map(c=>c.innerText);if(H.length > new Set(H).size){return{error:`Headers repeat.`}}const R=[...this.rows].map((r,i)=>{if(i==0){return;}const C=[...r.cells].map(c=>c.innerText);return H.reduce((A,P,I)=>(A={...A,[P.valueOf()]:C[I] }),{})});R.shift();return{error:!1,res:R,headers:H}};if(!window){module.exports=jsonify} -\ No newline at end of file +HTMLTableElement.prototype.jsonify=function(){var e={error:!1};if(this.rows<1)return e.error="There are no rows in the table.",e;var r=[...this.rows[0].cells].map(r=>{if(0==r.childElementCount)return r.innerText;if(1==r.childElementCount){while(1==r.childElementCount)r=r.children[0];return r.innerText}e.error="Can't extract headers."});if(r.length>new Set(r).size)return e.error="Headers repeat",e;var t=[...this.rows].map((t,n)=>{if(0==n)return;var o=[...t.cells].map(r=>{if(0==r.childElementCount)return r.innerText;if(1==r.childElementCount){while(1==r.childElementCount)r=r.children[0];return r.innerText}e.error="Can't extract information."});return r.reduce((e,r,t)=>e={...e,[r.valueOf()]:o[t]},{})});return t.shift(),e.res=t,e.headers=r,e},window||(module.exports=jsonify); diff --git a/package.json b/package.json @@ -1,6 +1,6 @@ { "name": "jsonify-my-table", - "version": "2.0.1", + "version": "2.0.2", "description": "Convert a HTML table to JSON", "main": "jsonify-my-table.js", "scripts": {