Skip to content

Commit 25ca585

Browse files
committedDec 17, 2021
v3
1 parent d2fb464 commit 25ca585

14 files changed

+5142
-1955
lines changed
 

‎.gitignore

+126-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,127 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
142
node_modules/
2-
dist/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Serverless directories
108+
.serverless/
109+
110+
# FuseBox cache
111+
.fusebox/
112+
113+
# DynamoDB Local files
114+
.dynamodb/
115+
116+
# TernJS port file
117+
.tern-port
118+
119+
# Stores VSCode versions used for testing VSCode extensions
120+
.vscode-test
121+
122+
# yarn v2
123+
.yarn/cache
124+
.yarn/unplugged
125+
.yarn/build-state.yml
126+
.yarn/install-state.gz
127+
.pnp.*

‎.npmignore

-10
This file was deleted.

‎README.md

+58-143
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# jsonFromTable
22

3-
Convert html tables to javascript objects, array or json.
3+
Convert html tables to object (or array). Supports complex rowspan and colspan.
44

55
<a href="https://www.npmjs.com/package/sjsonfromtable"><img alt="NPM" src="https://img.shields.io/npm/v/jsonfromtable" /></a>
66
<a href="https://github.com/coderosh/jsonfromtable"><img alt="MIT" src="https://img.shields.io/badge/license-MIT-blue.svg" /></a>
@@ -25,163 +25,78 @@ yarn add jsonfromtable
2525
## Usage
2626

2727
```js
28-
const { jsonFromtable } = require('jsonfromtable')
29-
// OR import { jsonFromTable } from "jsonfromtable"
30-
31-
const obj = jsonFromTable({
32-
html: `<table>...</table>`,
33-
})
34-
35-
const json = jsonFromTable({
36-
html: `<table>...</table>`,
37-
format: 'json',
38-
})
39-
40-
const arr = jsonFromTable({
41-
html: `<table>...</table>`,
42-
format: 'array',
43-
})
44-
45-
const [headers, body] = jsonFromTable({
46-
html: `<table>...</table>`,
47-
format: 'raw',
48-
})
49-
```
50-
51-
`jsonFromTable` function accepts only one argument `options`;
52-
53-
```ts
54-
interface Options {
55-
url?: string // utl to page which contains table
56-
html?: string // html which contains table
57-
selector?: string // table selector
58-
hSelector?: string // head selector
59-
bSelector?: [string, string] // body selector [row, td]
60-
format?: 'json' | 'array' | 'raw' | 'object' // output format
61-
headers?: string[] // custom headers
62-
}
63-
```
64-
65-
## Options
28+
const { JSONFromTable } = require('jsonfromtable')
6629

67-
### url
30+
const obj = JSONFromTable.fromString(`<table>...</table>`)
31+
// [ { title1: value1, title2: value2, ... }, ... ]
6832

69-
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>`)
34+
// { headers: [title1, titel2, ...], body: [[val2, val2, ...],...] }
7035

71-
```js
7236
;(async () => {
73-
const obj = await jsonFromTable({ url: 'https://example.com' })
74-
75-
console.log(obj)
37+
const obj = await JSONFromTable.fromUrl(`https://...`)
38+
const { headers, body } = await JSONFromTable.arrayFromUrl(`https://...`)
7639
})()
7740
```
7841

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-
const obj = 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-
const json = 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-
const html = `
120-
<html>
121-
<table>...</table>
122-
<table class="table">...</table>
123-
</html>
124-
`
125-
126-
const obj = 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`.
13543

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.
137-
138-
```js
139-
const obj = jsonFromTable({
140-
html: `<table>...</table>`,
141-
hSelector: `thead tr:first-child th`,
142-
})
143-
144-
console.log(obj)
44+
```ts
45+
interface Options {
46+
titles?: string[] // custom titles (eg: ["sn", "name", "title"])
47+
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+
}
14554
```
14655

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
15057

15158
```js
152-
const obj = jsonFromTable({
153-
html: `<table>...</table>`,
154-
bSelector: ['tbody tr:not(:first-child)', 'td'],
155-
})
156-
59+
const str = `<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+
const obj = JSONFromTable.fromString(str)
15796
console.log(obj)
15897
```
15998

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-
const hSelectors = [
164-
'thead tr:first-child th',
165-
'tr:first-child th',
166-
'tr:first-child td',
167-
]
168-
const bSelectors = [
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.
178-
179-
```js
180-
jsonFromTable({
181-
html: `<table>...</table>`
182-
headers: ["SN", "Name", "Age"]
183-
})
184-
```
99+
<img src="./example.png" height="350" />
185100

186101
## License
187102

‎example.png

56.8 KB
Loading

‎jest.config.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { Config } from "@jest/types";
1+
import type { Config } from '@jest/types'
22

33
const config: Config.InitialOptions = {
4-
preset: "ts-jest",
5-
testMatch: ["**/*.test.ts"],
6-
};
4+
preset: 'ts-jest',
5+
testMatch: ['**/*.test.ts'],
6+
collectCoverage: true,
7+
}
78

8-
export default config;
9+
export default config

0 commit comments

Comments
 (0)
Please sign in to comment.