Skip to content

Commit 4794ef6

Browse files
committed
tojson added
1 parent 007c037 commit 4794ef6

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const cheerio = require('cheerio')
2+
const fetch = require('node-fetch')
3+
4+
const toJson = require('./utils/tojson')
5+
6+
const defaultOptions = {
7+
url: undefined,
8+
html: undefined,
9+
selector: 'table'
10+
}
11+
12+
const html = async (options, headers) => {
13+
const { url, html, selector } = { ...defaultOptions, ...options }
14+
15+
let data
16+
17+
if (url) {
18+
const response = await fetch(url)
19+
data = await response.text()
20+
} else if (html) {
21+
data = html
22+
}
23+
24+
const $ = cheerio.load(data)
25+
26+
let body = toJson($, 'table', headers)
27+
28+
console.log(body)
29+
}
30+
31+
html(
32+
{
33+
html: `
34+
<table style="width:100%">
35+
<tr>
36+
<td>Jill</td>
37+
<td>Smith</td>
38+
<td>50</td>
39+
</tr>
40+
<tr>
41+
<td>Eve</td>
42+
<td>Jackson</td>
43+
<td>94</td>
44+
</tr>
45+
</table>
46+
`
47+
},
48+
['Name', 'LastName', 'Age']
49+
)

utils/map.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const map = (arr1, arr2) => {
2+
if (arr1.length === 0) {
3+
return arr2
4+
}
5+
6+
return [...arr1].map((_, i) => {
7+
if (!arr2[i]) {
8+
return arr1[i]
9+
}
10+
11+
return arr2[i]
12+
})
13+
}
14+
15+
module.exports = map

utils/tojson.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const map = require('./map')
2+
const toJson = ($, tableSelector, _header) => {
3+
const body = []
4+
let header = []
5+
6+
// Add headers from table to header array
7+
$(`${tableSelector} th`).each((_, el) => {
8+
header.push(
9+
$(el)
10+
.text()
11+
.trim()
12+
)
13+
})
14+
15+
// Compare table header to header given
16+
if (_header) {
17+
header = map(header, _header)
18+
}
19+
20+
if (header.length === 0) {
21+
throw new Error(
22+
'The table do not have any headers (<th></th>), please provide header tag as a second arguement'
23+
)
24+
}
25+
26+
let d = {},
27+
j = 0
28+
29+
// For tds in table
30+
$(`${tableSelector} td`).each((_, el) => {
31+
let val = $(el)
32+
.text()
33+
.trim()
34+
35+
if (!isNaN(parseFloat(val))) {
36+
val = parseFloat(val)
37+
}
38+
39+
if (val == '') {
40+
val = 0
41+
}
42+
d[`${header[j]}`] = val
43+
44+
if (j == header.length - 1) {
45+
body.push(d)
46+
j = 0
47+
d = {}
48+
} else {
49+
j++
50+
}
51+
})
52+
53+
return body
54+
}
55+
56+
module.exports = toJson

0 commit comments

Comments
 (0)