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 3ee76754d90a11540d5e74b78f138e77b129cabe
parent 15d51f46a84fe6f8aa8683df96636a9e5fe08491
Author: Agastya <acagastya@outlook.com>
Date:   Thu, 13 Jun 2019 15:53:25 +0530

export

Diffstat:
A.prettierignore | 2++
Dtabl2.json.min.js | 2--
Mtable2json.js | 1+
Atable2json.min.js | 21+++++++++++++++++++++
4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/.prettierignore b/.prettierignore @@ -0,0 +1 @@ +table2json.min.js +\ No newline at end of file diff --git a/tabl2.json.min.js b/tabl2.json.min.js @@ -1 +0,0 @@ -class table2json{constructor(tableID){this.table=document.getElementById(tableID);if(!this.table){this.error=true;console.error(`Table not found.`);return}this.headers=[];for(let i=0;i<this.table.rows[0].cells.length;i+=1){this.headers.push(this.table.rows[0].cells[i].innerText)}if(this.headers.length>new Set(this.headers).size){this.error=true;console.error(`Headers repeat.`);return}this.res=[];for(let i=1;i<this.table.rows.length;i+=1){let values=[];for(let j=0;j<this.table.rows[i].cells.length;j+=1){values.push(this.table.rows[i].cells[j].innerText)}let obj={};this.headers.forEach((el,index)=>(obj={...obj,[el.valueOf()]:values[index]}));this.res.push(obj)}}} -\ No newline at end of file diff --git a/table2json.js b/table2json.js @@ -32,3 +32,4 @@ class table2json { } } } +exports.table2json = table2json; diff --git a/table2json.min.js b/table2json.min.js @@ -0,0 +1,21 @@ +class table2json { + constructor(e) { + if (((this.table = document.getElementById(e)), !this.table)) + return (this.error = !0), void console.error("Table not found."); + this.headers = []; + for (let e = 0; e < this.table.rows[0].cells.length; e += 1) + this.headers.push(this.table.rows[0].cells[e].innerText); + if (this.headers.length > new Set(this.headers).size) + return (this.error = !0), void console.error("Headers repeat."); + this.res = []; + for (let e = 1; e < this.table.rows.length; e += 1) { + let t = []; + for (let s = 0; s < this.table.rows[e].cells.length; s += 1) + t.push(this.table.rows[e].cells[s].innerText); + let s = {}; + this.headers.forEach((e, r) => (s = { ...s, [e.valueOf()]: t[r] })), + this.res.push(s); + } + } +} +exports.table2json = table2json;