Skip to content

Commit d2fb464

Browse files
committed
add options to add custom headers and created npmignore
1 parent 9565a3b commit d2fb464

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

.npmignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
src/
2+
tests/
3+
.github/
4+
node_modules/
5+
tsconfig.json
6+
jest.config.ts
7+
.eslintrc.json
8+
.prettierrc
9+
CODE_OF_CONDUCT.md
10+
.npmignore

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ interface Options {
5858
hSelector?: string // head selector
5959
bSelector?: [string, string] // body selector [row, td]
6060
format?: 'json' | 'array' | 'raw' | 'object' // output format
61+
headers?: string[] // custom headers
6162
}
6263
```
6364

@@ -171,6 +172,17 @@ const bSelectors = [
171172
]
172173
```
173174

175+
### headers
176+
177+
You can don't like the headers in table, you can add your own.
178+
179+
```js
180+
jsonFromTable({
181+
html: `<table>...</table>`
182+
headers: ["SN", "Name", "Age"]
183+
})
184+
```
185+
174186
## License
175187

176188
MIT

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonfromtable",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Convert html tables to javascript objects, array or json",
55
"main": "dist/index.js",
66
"scripts": {
@@ -9,7 +9,8 @@
99
"lint": "eslint src/**.ts",
1010
"test": "jest",
1111
"clean": "rimraf dist",
12-
"cb": "yarn clean && yarn build"
12+
"cb": "yarn clean && yarn build",
13+
"prepublishOnly": "yarn lint && yarn test && yarn build"
1314
},
1415
"repository": {
1516
"type": "git",
@@ -21,7 +22,10 @@
2122
"html to json",
2223
"table parser",
2324
"html table",
24-
"table scrapper"
25+
"table scrapper",
26+
"scrapper",
27+
"json",
28+
"table"
2529
],
2630
"author": "Roshan Acharya <[email protected]>",
2731
"license": "MIT",

src/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Options {
99
hSelector?: string
1010
bSelector?: [string, string]
1111
format?: Format
12+
headers?: string[]
1213
}
1314

1415
/**
@@ -32,6 +33,7 @@ function jsonFromTable<T extends Format>(options: Options = {}) {
3233
hSelector = 'tr:first-child th',
3334
bSelector = ['tr:not(:first-child)', 'td'],
3435
format = 'object',
36+
headers: customHeaders = [],
3537
} = options
3638
// prettier-ignore
3739
const hSelectors = [hSelector, "thead tr:first-child th", "tr:first-child th", "tr:first-child td"];
@@ -57,14 +59,12 @@ function jsonFromTable<T extends Format>(options: Options = {}) {
5759
if (table.html() === null)
5860
throw new Error(`Couldn't find table with selector "${selector}"`)
5961

60-
const headers = getHeaders($, table, hSelectors)
61-
const body = getBody($, table, bSelectors)
62+
const headers =
63+
customHeaders.length > 0
64+
? customHeaders
65+
: getHeaders($, table, hSelectors)
6266

63-
if (headers.values.length !== body.values.length) {
64-
console.warn(
65-
`Length of body and head is not same:\nHeader: ${headers.values.length}\nBody: ${body.values.length}`
66-
)
67-
}
67+
const body = getBody($, table, bSelectors)
6868

6969
return output(headers, body, format) as Result<T>
7070
}

0 commit comments

Comments
 (0)