Skip to content

Commit 5a37447

Browse files
committed
trycatch
1 parent fa7fbf7 commit 5a37447

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jsonfromtable(options, headers).then(data => {
2828
})
2929
```
3030

31-
`jsonfromtable` function takes two arguments `options` and `headers`.
31+
`jsonfromtable` function takes two arguments `options` and `headers` and returns promise.
3232

3333
## Options
3434

@@ -42,6 +42,16 @@ options = {
4242
}
4343
```
4444

45+
### html
46+
47+
If you want the output from a html then you need to pass `html` option. The html should contain `table` tag.
48+
49+
```js
50+
options = {
51+
html: '<table>....</table>'
52+
}
53+
```
54+
4555
### format
4656

4757
If you want the json output then you can pass `format` option.

index.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const cheerio = require('cheerio')
22
const fetch = require('node-fetch')
3-
43
const toJson = require('./utils/tojson')
54

65
const defaultOptions = {
@@ -11,33 +10,36 @@ const defaultOptions = {
1110
}
1211

1312
const htmlTableToJson = async (options = defaultOptions, headers) => {
14-
const { url, html, selector, format } = { ...defaultOptions, ...options }
15-
16-
let data
17-
18-
if (url) {
19-
const response = await fetch(url)
20-
data = await response.text()
21-
} else if (html) {
22-
data = html
23-
} else {
24-
return []
25-
}
26-
27-
const $ = cheerio.load(data)
28-
29-
if ($('table').html() === null) {
30-
throw new Error(
31-
`Please provide ${url ? 'url' : 'html'} which contains table`
32-
)
33-
}
34-
35-
let body = toJson($, selector, headers)
36-
37-
if (format === 'json') {
38-
return JSON.stringify(body)
39-
} else {
40-
return body
13+
try {
14+
const { url, html, selector, format } = { ...defaultOptions, ...options }
15+
16+
let data
17+
18+
if (url) {
19+
const response = await fetch(url)
20+
data = await response.text()
21+
} else if (html) {
22+
data = html
23+
} else {
24+
return []
25+
}
26+
27+
const $ = cheerio.load(data)
28+
29+
if ($('table').html() === null) {
30+
throw new Error(
31+
`Please provide ${url ? 'url' : 'html'} which contains table`
32+
)
33+
}
34+
35+
let body = toJson($, selector, headers)
36+
if (format === 'json') {
37+
return JSON.stringify(body)
38+
} else {
39+
return body
40+
}
41+
} catch (e) {
42+
throw new Error(e)
4143
}
4244
}
4345

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonfromtable",
3-
"version": "1.0.1",
3+
"version": "1.0.3",
44
"description": "Generate json output from html tables",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)