commit 0b847b3e789c72c96593a703c88884bee69caf78
parent ddb48e7a62b131732266a4140a3645640d0cd62d
Author: Agastya Chandrakant <me@hanabi.in>
Date: Sat, 2 Nov 2019 12:15:31 +0530
Update to v2.0.0
Diffstat:
4 files changed, 50 insertions(+), 38 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -1,3 +1,21 @@
+# 2.0.0
+
+## API changes
+
+- `jsonifyMyTable` is no longer a class.
+- New function is called `jsonify`, and works on tables (`HTMLTableElement`).
+
+```javascript
+const myTable = document.getElementById('tableID');
+const data = myTable.jsonify();
+```
+
+There is no change in the result object, it contains:
+
+1. `error` if any, else set to `false`.
+2. `res` array of objects.
+3. `headers` array of headers.
+
# 1.1.0
- Eliminated `table` as the data member.
diff --git a/jsonify-my-table.js b/jsonify-my-table.js
@@ -1,38 +1,32 @@
-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({})
+HTMLTableElement.prototype.jsonify = function() {
+ // 1. Does table have rows?
+ if (this.rows < 1)
+ return { error: `There are no rows in the table.` };
+
+ // 2. Extract headers.
+ const headers = [...this.rows[0].cells].map(
+ cell => cell.innerText
+ );
+
+ // 3. Are headers unique?
+ if (headers.length > new Set(headers).size)
+ return { error: `Headers repeat.` };
+
+ // 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
+ );
+ return headers.reduce(
+ (acc, cur, index) =>
+ (acc = { ...acc, [cur.valueOf()]: cells[index] }),
+ {}
);
});
- this.res.shift(); // Remove header.
-}
-if (!window) module.exports = jsonifyMyTable;
+ res.shift(); // Remove header
+ return { error: false, res, headers };
+};
+
+if (!window) module.exports = jsonify;
diff --git a/jsonify-my-table.min.js b/jsonify-my-table.min.js
@@ -1 +1 @@
-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
+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
diff --git a/package.json b/package.json
@@ -1,6 +1,6 @@
{
"name": "jsonify-my-table",
- "version": "1.1.1",
+ "version": "2.0.0",
"description": "Convert a HTML table to JSON",
"main": "jsonify-my-table.js",
"scripts": {