Skip to content

Commit 5d0f327

Browse files
committed
readme.md added
1 parent b7c46d0 commit 5d0f327

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

README.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,81 @@
11
# JSON FROM TABLE
22

3-
Converts html tables into JSON
3+
Converts html tables to JSON and JS objects
44

5-
## Usage
5+
## Installation
66

77
Install via npm
88

9-
```npm
10-
npm i jsonfromtable
9+
```batch
10+
npm install jsonfromtable
11+
```
12+
13+
Install via yarn
14+
15+
```batch
16+
yarn add jsonfromtable
17+
```
18+
19+
## Usage
20+
21+
```js
22+
const jsonfromtable = require('jsonfromtable')
23+
24+
jsonfromtable(options, headers).then(data => {
25+
console.log(data)
26+
})
27+
```
28+
29+
`jsonfromtable` function takes two arguments `options` and `headers.
30+
31+
## Options
32+
33+
### url
34+
35+
If you want the output from a url then you need to pass `url` option. The url should be of a webpage which has a table.
36+
37+
```js
38+
options = {
39+
url: 'https://www.example.com'
40+
}
1141
```
1242

13-
You can convert
43+
### format
44+
45+
If you want the json output then you can pass `format` option.
46+
47+
```js
48+
options = {
49+
url: 'https://www.example.com',
50+
format: 'json' // default => jsobject
51+
}
52+
```
53+
54+
### selector
55+
56+
If the page has more than one table, then you can pass id of the table as `selector`.
57+
58+
```js
59+
options = {
60+
url: 'https:///www.example.com',
61+
selector: '#table_example' // default => table
62+
}
63+
```
64+
65+
<br />
66+
67+
## Headers
68+
69+
If the table already has `<th>` tag then you don't need to worry about headers.
70+
71+
If the table doesn't have `<th>` tag then you need to pass headers on your own. Make sure no of items in headers in equal to the no of columns of table.
72+
73+
```js
74+
headers = ['header1', 'header2', 'header3']
75+
```
76+
77+
If your table has `<th>` tags as headers but you want to pass your own header then also it will work and it is not necessary to pass all the headers equal to no of columns.
78+
79+
## License
80+
81+
MIT

index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const defaultOptions = {
1010
format: 'jsobject'
1111
}
1212

13-
const htmlTableToJson = async (options, headers) => {
13+
const htmlTableToJson = async (options = defaultOptions, headers) => {
1414
const { url, html, selector, format } = { ...defaultOptions, ...options }
1515

1616
let data
@@ -20,6 +20,8 @@ const htmlTableToJson = async (options, headers) => {
2020
data = await response.text()
2121
} else if (html) {
2222
data = html
23+
} else {
24+
return []
2325
}
2426

2527
const $ = cheerio.load(data)
@@ -39,11 +41,4 @@ const htmlTableToJson = async (options, headers) => {
3941
}
4042
}
4143

42-
htmlTableToJson({
43-
url: 'https://www.worldometers.info/coronavirus/',
44-
format: 'json'
45-
}).then(data => {
46-
console.log(data)
47-
})
48-
4944
module.exports = htmlTableToJson

utils/tojson.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const toJson = ($, tableSelector, _header) => {
2525

2626
if (header.length === 0) {
2727
throw new Error(
28-
'The table do not have any headers (<th></th>), please provide header tag as a second arguement'
28+
'The table do not have any headers (<th></th>), please provide array of headers as a second arguement'
2929
)
3030
}
3131

0 commit comments

Comments
 (0)