Skip to content

Commit dff6ec9

Browse files
committed
header bug fixed
1 parent e3a347f commit dff6ec9

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install jsonfromtable
1717
```js
1818
const jsonfromtable = require('jsonfromtable')
1919

20-
jsonfromtable(options, headers).then(data => {
20+
jsonfromtable(options, headers).then((data) => {
2121
console.log(data)
2222
})
2323
```
@@ -34,7 +34,7 @@ If you want the output from a url then you need to pass `url` option. The url sh
3434

3535
```js
3636
options = {
37-
url: 'https://www.example.com'
37+
url: 'https://www.example.com',
3838
}
3939
```
4040

@@ -44,7 +44,7 @@ If you want the output from a html then you need to pass `html` option. The html
4444

4545
```js
4646
options = {
47-
html: '<table>....</table>'
47+
html: '<table>....</table>',
4848
}
4949
```
5050

@@ -55,7 +55,7 @@ If you want the json output then you can pass `format` option.
5555
```js
5656
options = {
5757
url: 'https://www.example.com',
58-
format: 'json' // default => jsobject
58+
format: 'json', // default => jsobject
5959
}
6060
```
6161

@@ -65,23 +65,25 @@ If the page has more than one table, then you can pass id of the table as `selec
6565

6666
```js
6767
options = {
68-
url: 'https:///www.example.com',
69-
selector: '#table_example' // default => table
68+
url: 'https://www.example.com',
69+
selector: '#table_example', // default => table
7070
}
7171
```
7272

7373
<br />
7474

7575
## Headers
7676

77-
If the table doesn't have `<th>` tags 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.
77+
The first row from table is taken a key. You can also pass your own headers
7878

7979
```js
80-
headers = ['header1', 'header2', 'header3']
80+
options = {
81+
url: 'https://www.example.com',
82+
selector: '#table_example', // default => table
83+
headers: ['header1', 'header2', 'header3'],
84+
}
8185
```
8286

83-
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.
84-
8587
## License
8688

8789
MIT

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const defaultOptions = {
66
url: undefined,
77
html: undefined,
88
selector: 'table',
9-
format: 'jsobject'
9+
format: 'jsobject',
1010
}
1111

1212
const jsonfromtable = async (options = defaultOptions, headers) => {

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.6",
3+
"version": "1.1.0",
44
"description": "Generate json output from html tables",
55
"main": "index.js",
66
"repository": {

utils/tojson.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ const toJson = ($, tableSelector, _header) => {
77
throw new Error(`${tableSelector} is not a valid selector for table`)
88
}
99

10-
$(`${tableSelector} th`).each((_, el) => {
10+
const firstRow = $($(`${tableSelector} tr`)[0]).children()
11+
$(firstRow).each((_, el) => {
1112
header.push($(el).text().trim())
1213
})
1314

1415
if (_header) {
1516
header = map(header, _header)
1617
}
1718

18-
if (header.length === 0) {
19-
throw new Error(
20-
"The table don't have any headers (th), please provide headers as a second arguement"
21-
)
22-
}
23-
2419
let d = {},
2520
j = 0
2621

0 commit comments

Comments
 (0)