Skip to content

Commit 110c4a4

Browse files
committed
Updates for next version
1 parent 09a3134 commit 110c4a4

File tree

8 files changed

+1338
-2624
lines changed

8 files changed

+1338
-2624
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
node_modules
2-
.vscode/

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Debug AVA test file",
11+
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ava",
12+
"runtimeArgs": [
13+
"debug",
14+
"--break",
15+
"--serial",
16+
"${file}"
17+
],
18+
"port": 9229,
19+
"outputCapture": "std",
20+
"skipFiles": [
21+
"<node_internals>/**/*.js"
22+
]
23+
}
24+
]
25+
}

index.d.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const cheerio = require('cheerio')
44

55
class HtmlTableToJson {
6-
constructor (html, opts) {
6+
constructor (html, opts = {}) {
77
if (typeof html !== 'string') { throw new TypeError('html input must be a string') }
88

99
this.html = html
@@ -14,10 +14,12 @@ class HtmlTableToJson {
1414
this._headers = []
1515
this._count = null
1616

17+
this._firstRowUsedAsHeaders = []
18+
1719
this._process()
1820
}
1921

20-
static factory (html, opts) {
22+
static parse (html, opts) {
2123
return new HtmlTableToJson(html, opts)
2224
}
2325

@@ -26,7 +28,9 @@ class HtmlTableToJson {
2628
}
2729

2830
get results () {
29-
return this._results
31+
return this.opts.values === true
32+
? this._results.map(result => result.map(Object.values))
33+
: this._results
3034
}
3135

3236
get headers () {
@@ -50,6 +54,8 @@ class HtmlTableToJson {
5054
}
5155

5256
_processRow (tableIndex, index, row) {
57+
if (index === 0 && this._firstRowUsedAsHeaders[tableIndex] === true) return
58+
5359
this._results[tableIndex][index] = {}
5460

5561
this._$(row).find('td').each((i, cell) => {
@@ -65,6 +71,13 @@ class HtmlTableToJson {
6571
this._headers[index][j] = this._$(cell).text().trim()
6672
})
6773
})
74+
75+
if (this._headers[index].length) return
76+
77+
this._firstRowUsedAsHeaders[index] = true
78+
this._$(table).find('tr').first().find('td').each((j, cell) => {
79+
this._headers[index][j] = this._$(cell).text().trim()
80+
})
6881
}
6982

7083
_pruneEmptyRows (tableIndex) {

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
"version": "0.4.1",
44
"description": "Extracts all tables within a provided html snippet and converts them to JSON objects.",
55
"license": "MIT",
6-
"types": "index.d.ts",
76
"repository": "brandon93s/html-table-to-json",
87
"author": {
98
"name": "Brandon Smith",
10-
"email": "[email protected]",
119
"url": "github.com/brandon93s"
1210
},
1311
"engines": {
@@ -17,20 +15,18 @@
1715
"test": "ava --verbose"
1816
},
1917
"files": [
20-
"index.js",
21-
"index.d.ts"
18+
"index.js"
2219
],
2320
"keywords": [
2421
"html",
2522
"table",
2623
"json"
2724
],
2825
"dependencies": {
29-
"@types/cheerio": "^0.22.9",
30-
"cheerio": "^1.0.0-rc.2"
26+
"cheerio": "^1.0.0-rc.3"
3127
},
3228
"devDependencies": {
33-
"ava": "^0.25.0",
29+
"ava": "^3.0.0",
3430
"eslint": "^4.19.1",
3531
"eslint-config-standard": "^11.0.0",
3632
"eslint-plugin-import": "^2.10.0",

readme.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
> Extracts tables from a provided html snippet and converts them to JSON objects
44
5-
*Note: Currently always returns an array of results regardless of table count*
6-
75
## Install
86

97
```
10-
$ npm install --save html-table-to-json
8+
$ npm install html-table-to-json
119
```
1210

1311

@@ -16,7 +14,7 @@ $ npm install --save html-table-to-json
1614
```js
1715
const HtmlTableToJson = require('html-table-to-json');
1816

19-
const jsonTables = new HtmlTableToJson(`
17+
const jsonTables = HtmlTableToJson.parse(`
2018
<table>
2119
<tr>
2220
<th>Animal</th>
@@ -51,8 +49,7 @@ console.log(jsonTables.count);
5149

5250
## API
5351

54-
### new HtmlTableToJson(input [,options])
55-
### HtmlTableToJson.factory(input [,options])
52+
### HtmlTableToJson.parse(input [,options])
5653

5754
#### input
5855

@@ -62,8 +59,25 @@ Any html snippet.
6259

6360
#### options
6461

65-
##### // todo
62+
Type: `object`
63+
64+
##### values
65+
66+
Type: `bool`
67+
68+
Return table rows as value arrays:
69+
70+
```js
71+
/* => [[
72+
* ['Unicorn', 'Pink', 'Billy'],
73+
* ['Walrus', 'Orange', 'Sue']
74+
* ]]
75+
*/
76+
```
77+
78+
## Headers
6679

80+
HtmlTableToJson extracts table headers ( `th` ) to be used as JSON object keys. The first row is used when no `th` elements are present.
6781

6882
## License
6983

test.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
import test from 'ava'
4-
import HtmlTableToJson from './'
3+
const test = require('ava')
4+
const HtmlTableToJson = require('./')
55

66
const singleEmpty = `<table></table>`
77
const doubleEmpty = `${singleEmpty}${singleEmpty}`
@@ -21,7 +21,7 @@ test('double table count is 2', t => {
2121

2222
const singleWithHeaderRow = `<table><tr><th>${h1}</th><th>${h2}</th><th>${h3}</th><th>${h4}</th></tr></table>`
2323

24-
test('extracts headers correctly', t => {
24+
test('extracts th headers correctly', t => {
2525
const toString = value => value + ''
2626

2727
const headers = new HtmlTableToJson(singleWithHeaderRow).headers[0]
@@ -32,10 +32,70 @@ test('extracts headers correctly', t => {
3232
t.is(headers[3], toString(h4))
3333
})
3434

35+
const singleWithoutHeaderRow = `<table>
36+
<tr><td>${h1}</td><td>${h2}</td><td>${h3}</td><td>${h4}</td></tr>
37+
<tr><td>A</td><td>B</td><td>C</td><td>D</td></tr>
38+
</table>`
39+
40+
test('falls back to first row for headers', t => {
41+
const toString = value => value + ''
42+
43+
const _sut = new HtmlTableToJson(singleWithoutHeaderRow)
44+
45+
const headers = _sut.headers[0]
46+
t.is(headers[0], toString(h1))
47+
t.is(headers[1], toString(h2))
48+
t.is(headers[2], toString(h3))
49+
t.is(headers[3], toString(h4))
50+
51+
const rows = _sut.results[0]
52+
t.is(rows[0][headers[0]], 'A')
53+
t.is(rows[0][headers[1]], 'B')
54+
t.is(rows[0][headers[2]], 'C')
55+
t.is(rows[0][headers[3]], 'D')
56+
})
57+
3558
test('throws when html is not a string', t => {
3659
t.throws(() => new HtmlTableToJson({}))
3760
t.throws(() => new HtmlTableToJson([]))
3861
t.throws(() => new HtmlTableToJson(1))
3962
t.throws(() => new HtmlTableToJson(NaN))
4063
t.throws(() => new HtmlTableToJson(true))
4164
})
65+
66+
test('parse method', t => {
67+
const _sut = HtmlTableToJson.parse(doubleEmpty)
68+
69+
t.is(_sut.count, 2)
70+
})
71+
72+
const sampleTable = `<table>
73+
<tr>
74+
<th>Animal</th>
75+
<th>Color</th>
76+
<th>Name</th>
77+
</tr>
78+
<tr>
79+
<td>Unicorn</td>
80+
<td>Pink</td>
81+
<td>Billy</td>
82+
</tr>
83+
<tr>
84+
<td>Walrus</td>
85+
<td>Orange</td>
86+
<td>Sue</td>
87+
</tr>
88+
</table>`
89+
90+
test('values option', t => {
91+
const _sut = new HtmlTableToJson(sampleTable, { values: true })
92+
93+
t.is(_sut.count, 1)
94+
95+
const results = _sut.results[0]
96+
t.is(results.length, 2)
97+
t.true(Array.isArray(results[0]))
98+
t.true(Array.isArray(results[1]))
99+
t.is(results[0][0], 'Unicorn')
100+
t.is(results[1][2], 'Sue')
101+
})

0 commit comments

Comments
 (0)