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 635cc1d6df59ceda18e835811a4aa878ad3dcc5d
parent cf996e5efcea287b47da51e286cc61e7e6380ca9
Author: Agastya <acagastya@outlook.com>
Date:   Thu, 13 Jun 2019 20:02:16 +0530

rename

Diffstat:
M.prettierignore | 4++--
MREADME.MD | 8++++----
Ajsonify-my-table.js | 35+++++++++++++++++++++++++++++++++++
Ajsonify-my-table.min.js | 21+++++++++++++++++++++
Mpackage.json | 4++--
Dtable2json.js | 35-----------------------------------
6 files changed, 64 insertions(+), 43 deletions(-)

diff --git a/.prettierignore b/.prettierignore @@ -1 +1 @@ -table2json.min.js -\ No newline at end of file +jsonify-my-table.min.js +\ No newline at end of file diff --git a/README.MD b/README.MD @@ -1,4 +1,4 @@ -# table2json +# jsonify-my-table Convert a HTML table to JSON. Actually, to an array of JSON objects. @@ -15,11 +15,11 @@ Consider a HTML table with `id=myTable`. To convert it to an array of JSON objects: ```javascript -const myTableObj = new table2json("myTable"); +const myTableObj = new jsonifyMyTable("myTable"); myTableObj.res; // array of JSON objects. ``` -`table2json` class accepts one parameter; the table id. +`jsonifyMyTable` class accepts one parameter; the table id. ## Data members @@ -43,4 +43,4 @@ Use this for the client-side JavaScript. Though this module is published on `npm To add it in the `<script>` tag, make use of [Unpkg](https://unpkg.com "UNPKG"). -**Example:** `<script src="unpkg.com/table2json@latest/table2json.min.js"></script>` +**Example:** `<script src="unpkg.com/jsonify-the-table@latest/jsonify-my-table.min.js"></script>` diff --git a/jsonify-my-table.js b/jsonify-my-table.js @@ -0,0 +1,35 @@ +class jsonifyMyTable { + constructor(tableID) { + this.table = document.getElementById(tableID); + if (!this.table) { + this.error = true; + console.error(`Table of id "${tableID}" not found.`); + return; + } + if (this.table.tagName != "TABLE") { + this.error = true; + console.error(`Element of id "${tableID}" is not a table.`); + return; + } + this.headers = []; + for (let i = 0; i < this.table.rows[0].cells.length; i++) + 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++) { + let values = []; + for (let j = 0; j < this.table.rows[i].cells.length; j++) + 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); + } + } +} +exports.jsonifyMyTable = jsonifyMyTable; diff --git a/jsonify-my-table.min.js b/jsonify-my-table.min.js @@ -0,0 +1,21 @@ +class jsonifyMyTable { + 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.jsonifyMyTable = jsonifyMyTable; diff --git a/package.json b/package.json @@ -1,8 +1,8 @@ { - "name": "table2json", + "name": "jsonify-my-table", "version": "1.0.0", "description": "Convert a HTML table to JSON", - "main": "table2json.js", + "main": "jsonify-my-table.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/table2json.js b/table2json.js @@ -1,35 +0,0 @@ -class table2json { - constructor(tableID) { - this.table = document.getElementById(tableID); - if (!this.table) { - this.error = true; - console.error(`Table of id "${tableID}" not found.`); - return; - } - if (this.table.tagName != "TABLE") { - this.error = true; - console.error(`Element of id "${tableID}" is not a table.`); - return; - } - this.headers = []; - for (let i = 0; i < this.table.rows[0].cells.length; i++) - 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++) { - let values = []; - for (let j = 0; j < this.table.rows[i].cells.length; j++) - 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); - } - } -} -exports.table2json = table2json;