diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..ac28a52
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,10 @@
+src/
+tests/
+.github/
+node_modules/
+tsconfig.json
+jest.config.ts
+.eslintrc.json
+.prettierrc
+CODE_OF_CONDUCT.md
+.npmignore
\ No newline at end of file
diff --git a/README.md b/README.md
index 6988159..e63e5a2 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,7 @@ interface Options {
hSelector?: string // head selector
bSelector?: [string, string] // body selector [row, td]
format?: 'json' | 'array' | 'raw' | 'object' // output format
+ headers?: string[] // custom headers
}
```
@@ -171,6 +172,17 @@ const bSelectors = [
]
```
+### headers
+
+You can don't like the headers in table, you can add your own.
+
+```js
+jsonFromTable({
+ html: `
`
+ headers: ["SN", "Name", "Age"]
+})
+```
+
## License
MIT
diff --git a/package.json b/package.json
index fa03266..867fe0f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "jsonfromtable",
- "version": "2.0.0",
+ "version": "2.1.0",
"description": "Convert html tables to javascript objects, array or json",
"main": "dist/index.js",
"scripts": {
@@ -9,7 +9,8 @@
"lint": "eslint src/**.ts",
"test": "jest",
"clean": "rimraf dist",
- "cb": "yarn clean && yarn build"
+ "cb": "yarn clean && yarn build",
+ "prepublishOnly": "yarn lint && yarn test && yarn build"
},
"repository": {
"type": "git",
@@ -21,7 +22,10 @@
"html to json",
"table parser",
"html table",
- "table scrapper"
+ "table scrapper",
+ "scrapper",
+ "json",
+ "table"
],
"author": "Roshan Acharya ",
"license": "MIT",
diff --git a/src/index.ts b/src/index.ts
index de2ac6c..e3425ec 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -9,6 +9,7 @@ interface Options {
hSelector?: string
bSelector?: [string, string]
format?: Format
+ headers?: string[]
}
/**
@@ -32,6 +33,7 @@ function jsonFromTable(options: Options = {}) {
hSelector = 'tr:first-child th',
bSelector = ['tr:not(:first-child)', 'td'],
format = 'object',
+ headers: customHeaders = [],
} = options
// prettier-ignore
const hSelectors = [hSelector, "thead tr:first-child th", "tr:first-child th", "tr:first-child td"];
@@ -57,14 +59,12 @@ function jsonFromTable(options: Options = {}) {
if (table.html() === null)
throw new Error(`Couldn't find table with selector "${selector}"`)
- const headers = getHeaders($, table, hSelectors)
- const body = getBody($, table, bSelectors)
+ const headers =
+ customHeaders.length > 0
+ ? customHeaders
+ : getHeaders($, table, hSelectors)
- if (headers.values.length !== body.values.length) {
- console.warn(
- `Length of body and head is not same:\nHeader: ${headers.values.length}\nBody: ${body.values.length}`
- )
- }
+ const body = getBody($, table, bSelectors)
return output(headers, body, format) as Result
}