You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. If url parameter is passed then the function will return a promise.
33
+
const { headers, body } =JSONFromTable.arrayFromString(`<table>...</table>`)
const { headers, body } =awaitJSONFromTable.arrayFromUrl(`https://...`)
76
39
})()
77
40
```
78
41
79
-
### html
80
-
81
-
If you want the output from a html then you need to pass `html` option. The html should contain `table` tag.
82
-
83
-
```js
84
-
constobj=jsonFromTable({
85
-
html:`<table>...</table>`,
86
-
})
87
-
88
-
console.log(obj)
89
-
```
90
-
91
-
### format
92
-
93
-
If you want the json or array or raw output then you can pass `format` option. Default value is `object`.
94
-
95
-
```js
96
-
constjson=jsonFromTable({
97
-
html:`<table>...</table>`,
98
-
format:'json',
99
-
})
100
-
console.log(json)
101
-
102
-
jsonFromTable({
103
-
url:`https://example.com`,
104
-
format:'array',
105
-
}).then((arr) =>console.log(arr))
106
-
107
-
const [headers, body] =jsonFromTable({
108
-
html:`<table>...</table>`,
109
-
format:'raw',
110
-
})
111
-
console.log({ headers, body })
112
-
```
113
-
114
-
### selector
115
-
116
-
If the page has more than one table, then you can pass css selector of the table as `selector`.
117
-
118
-
```js
119
-
consthtml=`
120
-
<html>
121
-
<table>...</table>
122
-
<table class="table">...</table>
123
-
</html>
124
-
`
125
-
126
-
constobj=jsonFromTable({
127
-
html: html,
128
-
selector:'.table',
129
-
})
130
-
131
-
console.log(obj)
132
-
```
133
-
134
-
### hSelector
42
+
Each function in `JSONFromTable` accepts two arguments. First is source (string or url) and second is `options`.
135
43
136
-
By default `tr:first-child th` is used to get the headings from table. Sometimes that selecter may not give you the best result. In such case you can provide css selector which will select all headings.
firstRowIsHeading?:boolean// use first row for titles ?
48
+
includeFirstRowInBody?:boolean// add first row in body ?
49
+
tableSelector?:string// css selector for table (eg: table.wikitable)
50
+
rowColSelector?: [string, string] // css selectors for row and col (eg: ["tr", "th,td"])
51
+
shouldBeText?:boolean// if false value is html else true
52
+
trim?:boolean// should trim the value ?
53
+
}
145
54
```
146
55
147
-
### bSelector
148
-
149
-
By default `['tr:not(:first-child)', 'td']` is used to get body from table. Sometimes that selecter may not give you the best result. In such case you can provide css selector.
56
+
## Example
150
57
151
58
```js
152
-
constobj=jsonFromTable({
153
-
html:`<table>...</table>`,
154
-
bSelector: ['tbody tr:not(:first-child)', 'td'],
155
-
})
156
-
59
+
conststr=`<table>
60
+
<tr>
61
+
<th>name</th>
62
+
<th>alias</th>
63
+
<th>class</th>
64
+
<th>info</th>
65
+
</tr>
66
+
<tr>
67
+
<td colspan="2">Roshan</td>
68
+
<td>Eng</td>
69
+
<td rowspan="2">na</td>
70
+
</tr>
71
+
<tr>
72
+
<td rowspan="2">John</td>
73
+
<td colspan="2">Cook</td>
74
+
</tr>
75
+
<tr>
76
+
<td rowspan="2">Danger</td>
77
+
<td colspan="2">Ninja</td>
78
+
</tr>
79
+
<tr>
80
+
<td>AGuy</td>
81
+
<td>Eng</td>
82
+
<td rowspan="2">Eats a lot</td>
83
+
</tr>
84
+
<tr>
85
+
<td colspan="2">Dante</td>
86
+
<td rowspan="2">Art</td>
87
+
</tr>
88
+
<tr>
89
+
<td>Jake</td>
90
+
<td>ake</td>
91
+
<td>Actor</td>
92
+
</tr>
93
+
</table>`
94
+
95
+
constobj=JSONFromTable.fromString(str)
157
96
console.log(obj)
158
97
```
159
98
160
-
> Note that if provided `hSelector` and `bSelector` failes to select headers/body than following selectors will be used to select and get headers and body.
161
-
162
-
```js
163
-
consthSelectors= [
164
-
'thead tr:first-child th',
165
-
'tr:first-child th',
166
-
'tr:first-child td',
167
-
]
168
-
constbSelectors= [
169
-
['tbody tr', 'td'],
170
-
['tr:not(:first-child)', 'td'],
171
-
['tr', 'td'],
172
-
]
173
-
```
174
-
175
-
### headers
176
-
177
-
You can don't like the headers in table, you can add your own.
0 commit comments