-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (114 loc) · 3.04 KB
/
index.js
File metadata and controls
145 lines (114 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";
if(!global.document){
var jsdom = require("jsdom");
var { JSDOM } = jsdom;
var html = '<html></html>';
global.document = new JSDOM(html).window.document;
}
function t2j(element){
if(!element) return {};
let head = [];
let data = [];
if(element.length > 100 && typeof element === 'string' && String(element).toLowerCase().indexOf('table') > -1) {
let htmlObject = global.document.createElement('html');
htmlObject.innerHTML = element;
// let element = htmlObject.firstChild;
element = htmlObject.getElementsByTagName('table')[0]; // Live NodeList of your anchor elements
// console.log('element', element);
}
const table = element;
const rows = table.rows.length;
let cells = 0;
let text = '';
for (var r = 0; r < rows; r++){
cells || (cells = table.rows[0].cells.length);
let simpleRow = [];
for (var c = 0; c < cells; c++){
const d = table.rows[r].cells[c];
if(d){
text = table.rows[r].cells[c].textContent;
if( r === 0 ) {
head.push(text);
}else {
simpleRow.push(text);
}
}
}
simpleRow.length && data.push(simpleRow);
}
if(!head.length) return null; // void return;
return {
headers: head,
datas: [],
rows: data,
};
// console.log(head, data);
// console.log(table.rows);
// console.log(table.rows.length);
// console.log(table.rows[0].cells);
// console.log(table.rows[0].cells[0].textContent);
// console.log(table.rows[0].cells[0].cellIndex);
// console.log(table.rows[0].innerHTML);
}
function getElById(idElement) {
if(idElement.indexOf('#') > -1) {
idElement = idElement.replace('#', '');
}
try {
return global.document.getElementById(idElement) || false;
} catch (error) {
console.log(error);
return false;
}
}
function getElByTagName(idElement) {
try {
return global.document.getElementsByTagName(idElement) || false;
} catch (error) {
console.log(error);
return false;
}
}
function one(idElement){
// is a table
if(typeof idElement === 'string' && idElement.length > 100) {
return t2j(idElement);
}
// is a id
try {
return t2j(getElById(idElement) ||null );
} catch (error) {
return '';
}
}
function all(){
let all = [];
const table = getElByTagName('table');
const rows = table.length;
for(var r = 0; r < rows; r++){
const content = t2j(table[r]);
// console.log(table[r]);
// const dom = new JSDOM(`<!DOCTYPE html>${tableSample}`);
// const words = dom.window.document.querySelector("table").textContent;
// if (words.indexOf('june') && words.indexOf('july')) {
// // calendary
// } else {
// // push
// }
content && all.push( content );
}
return all;
}
const tables2json = {
t2j, one, all,
id: one,
oneTable2json: one,
allTables2json: all,
};
module.exports = tables2json;
/*
var s = '<table...>...</table>';
var htmlObject = document.createElement('html');
htmlObject.innerHTML = s;
var table = htmlObject.firstChild;
*/