Skip to content

Commit

Permalink
add options to add custom headers and created npmignore
Browse files Browse the repository at this point in the history
  • Loading branch information
coderosh committed Jun 19, 2021
1 parent 9565a3b commit d2fb464
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
src/
tests/
.github/
node_modules/
tsconfig.json
jest.config.ts
.eslintrc.json
.prettierrc
CODE_OF_CONDUCT.md
.npmignore
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down Expand Up @@ -171,6 +172,17 @@ const bSelectors = [
]
```

### headers

You can don't like the headers in table, you can add your own.

```js
jsonFromTable({
html: `<table>...</table>`
headers: ["SN", "Name", "Age"]
})
```

## License

MIT
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand All @@ -21,7 +22,10 @@
"html to json",
"table parser",
"html table",
"table scrapper"
"table scrapper",
"scrapper",
"json",
"table"
],
"author": "Roshan Acharya <acharyaroshan2357@gmail.com>",
"license": "MIT",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Options {
hSelector?: string
bSelector?: [string, string]
format?: Format
headers?: string[]
}

/**
Expand All @@ -32,6 +33,7 @@ function jsonFromTable<T extends Format>(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"];
Expand All @@ -57,14 +59,12 @@ function jsonFromTable<T extends Format>(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<T>
}
Expand Down

0 comments on commit d2fb464

Please sign in to comment.