commit 7f4c9bb91d5ceac258393d2808d1bcc3bd828469
parent fb265d33dbc1217fb6f0817df3c98fe9851437d4
Author: Agastya Chandrakant <me@hanabi.in>
Date: Mon, 16 Sep 2019 22:50:20 +0530
Move to 1.1.0
Diffstat:
5 files changed, 68 insertions(+), 46 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -0,0 +1,12 @@
+# 1.1.0
+
+- Eliminated `table` as the data member.
+- `error` is now a permanent data member: set to `false` if there was no error, or to the error message if there was an error.
+
+```javascript
+const data = new jsonifyMyTable('id');
+
+if (!data.error) {
+ // data.res...
+}
+```
diff --git a/README.MD b/README.MD
@@ -15,32 +15,39 @@ Consider a HTML table with `id=myTable`.
To convert it to an array of JSON objects:
```javascript
-const myTableObj = new jsonifyMyTable("myTable");
-myTableObj.res; // array of JSON objects.
+const tableData = new jsonifyMyTable('myTable');
+tableData.res; // array of JSON objects.
```
`jsonifyMyTable` class accepts one parameter; the table id.
## Data members
-In the above example, `myTableObj` contains following data members:
+In the above example, `tableData` contains following data members:
- **`res`** — the array containing JSON objects.
- `headers` — the keys in the objects (also, the table headers).
-- <small>`table`</small> — this one is the selection of table, in this case `table#myTable`.
+- `error` — a flag which indicates if an error was occurred. Set to `false` if no errors occurred, else contains the error message.
-- [OPTIONAL] `error` — a flag which indicates if an error was occurred.
+```javascript
+const tableData = new jsonifyMyTable('id');
+
+if (!tableData.error) {
+ // tableData.res...
+}
+```
## Errors
-1. If the `id` was not found in the `document`.
-2. If the specified `id` is not of a table.
-3. If the headers repeat.
+1. `id` does not exist in DOM.
+2. Element of `id` is not a table.
+3. There are no rows in `table#id`.
+4. Headers repeat.
## Note
-Use this for the client-side JavaScript. Though this module is published on `npm`, it can not run on server-side JavaScript. This is because it makes use of `document`, which is not available in node, as is.
+Use this for the client-side JavaScript. Though this module is published as a _Node Package Module_, it can not run on server-side JavaScript. This is because it makes use of `document`, which is not available in Node, as is.
-To add it in the `<script>` tag, make use of [Unpkg](https://unpkg.com "UNPKG").
+To add it in the `<script>` tag, make use of [Unpkg](https://unpkg.com 'UNPKG').
**Example:** `<script src="unpkg.com/jsonify-my-table@latest/jsonify-my-table.min.js"></script>`
diff --git a/jsonify-my-table.js b/jsonify-my-table.js
@@ -1,36 +1,38 @@
-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);
- }
+function jsonifyMyTable(id = undefined) {
+ this.error = false;
+ const table = document.getElementById(id);
+ // 1. Does id exist in DOM?
+ if (!table) {
+ this.error = `${id} does not exist in DOM.`;
+ return;
}
+ // 2. Does id correspond to <table>?
+ if (table.tagName.toLowerCase() != 'table') {
+ this.error = `Element of ${id} is not a table.`;
+ return;
+ }
+ // 3. Does table have rows?
+ if ([...table.rows].length < 1) {
+ this.error = `There are no rows in table#${id}.`;
+ return;
+ }
+ // 4. Extract header.
+ this.headers = [...table.rows[0].cells].map(cell => cell.innerText);
+ // 5. Are headers unique?
+ if (this.headers.length > new Set(this.headers).size) {
+ this.error = `Headers repeat.`;
+ return;
+ }
+ // 6. Prepare the result.
+ this.res = [...table.rows].map((row, index) => {
+ if (index == 0) return; // This is the header.
+ const cells = [...row.cells].map(cell => cell.innerText);
+ return this.headers.reduce(
+ (acc, cur, index) => (acc = { ...acc, [cur.valueOf()]: cells[index] }),
+ Object.assign({})
+ );
+ });
+ this.res.shift(); // Remove header.
}
-if(!window)
- module.exports=jsonifyMyTable;
+
+if (!window) module.exports = jsonifyMyTable;
diff --git a/jsonify-my-table.min.js b/jsonify-my-table.min.js
@@ -1 +1 @@
-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+=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)}}}if(!window)module.exports=jsonifyMyTable;
+function jsonifyMyTable(id=undefined){const s=this;s.error=false;const T=document.getElementById(id);if(!T){s.error=`${id} does not exist in DOM.`;return;}if(T.tagName.toLowerCase()!='table'){s.error=`Element of ${id} is not a table.`;return;}if([...T.rows].length<1){s.error=`There are no rows in table#${id}.`;return;}s.headers=[...T.rows[0].cells].map(c=>c.innerText);if(s.headers.length>new Set(s.headers).size){s.error=`Headers repeat.`;return;}s.res=[...T.rows].map((R,I)=>{if(I==0)return;const C=[...R.cells].map(c=>c.innerText);return s.headers.reduce((a,c,i)=>(a={...a,[c.valueOf()]:C[i]}),Object.assign({}));});s.res.shift();}if(!window)module.exports=jsonifyMyTable;
+\ No newline at end of file
diff --git a/package.json b/package.json
@@ -1,6 +1,6 @@
{
"name": "jsonify-my-table",
- "version": "1.0.3",
+ "version": "1.1.0",
"description": "Convert a HTML table to JSON",
"main": "jsonify-my-table.js",
"scripts": {