tabletojson
Advanced tools
Comparing version 0.9.2 to 0.9.3
@@ -16,2 +16,7 @@ 'use strict'; | ||
* @param options.forceIndexAsNumber Force the index to be used as number [default=false] | ||
* @param options.countDuplicateHeadings If given a _<NUMBER> will be added to the duplicate key [default=false] | ||
* @param options.ignoreColumns <Array> Array of column indices to ignored [default=null] | ||
* @param options.onlyColumns <Array> Array of column indices to be used. Overrides ignoreColumn [default=null] | ||
* @param options.ignoreHiddenRows Ignoring hidden rows [default=true] | ||
* @param options.headings <Array> Array of Strings to be used as headings [default=null] | ||
* @return {Object} Converted Object as an object literal | ||
@@ -27,3 +32,7 @@ */ | ||
forceIndexAsNumber: false, | ||
countDuplicateHeadings: true | ||
countDuplicateHeadings: true, | ||
ignoreColumns: null, | ||
onlyColumns: null, | ||
ignoreHiddenRows: true, | ||
headings: null | ||
}, | ||
@@ -41,10 +50,9 @@ options | ||
let jsonResponse = [], | ||
alreadySeen = [], | ||
suffix = undefined; | ||
const jsonResponse = []; | ||
let suffix = undefined; | ||
let $ = cheerio.load(html); | ||
const $ = cheerio.load(html); | ||
$('table').each(function(i, table) { | ||
let tableAsJson = []; | ||
const tableAsJson = []; | ||
const alreadySeen = {}; | ||
@@ -54,3 +62,3 @@ // Get column headings | ||
// @todo Try to support badly formated tables. | ||
let columnHeadings = []; | ||
const columnHeadings = []; | ||
@@ -60,3 +68,4 @@ let trs = $(table).find('tr'); | ||
if (options.useFirstRowForHeadings) trs = $(trs[0]); | ||
let headingsCounter = 0; | ||
// Use headings for objects key evaluation | ||
trs.each(function(i, row) { | ||
@@ -66,14 +75,22 @@ $(row) | ||
.each(function(j, cell) { | ||
let value = options.stripHtmlFromHeadings | ||
? $(cell) | ||
.text() | ||
.trim() | ||
: $(cell) | ||
.html() | ||
.trim(); | ||
if (options.onlyColumns && !options.onlyColumns.includes(j)) return; | ||
if (options.ignoreColumns && !options.onlyColumns && options.ignoreColumns.includes(j)) return; | ||
let value = ''; | ||
let seen = alreadySeen[value]; | ||
if (options.headings) { | ||
value = options.headings[headingsCounter++]; | ||
} else { | ||
value = options.stripHtmlFromHeadings | ||
? $(cell) | ||
.text() | ||
.trim() | ||
: $(cell) | ||
.html() | ||
.trim(); | ||
} | ||
const seen = alreadySeen[value]; | ||
if (seen && options.countDuplicateHeadings) { | ||
suffix = ++alreadySeen[value]; | ||
columnHeadings[j] = (value !== '') ? value + '_' + suffix : j; | ||
columnHeadings[j] = value !== '' ? value + '_' + suffix : j; | ||
} else { | ||
@@ -90,7 +107,19 @@ alreadySeen[value] = 1; | ||
.each(function(i, row) { | ||
let rowAsJson = {}; | ||
const rowAsJson = {}; | ||
let rows = options.useFirstRowForHeadings ? $(row).find('td, th') : $(row).find('td'); | ||
rows.each(function(j, cell) { | ||
let content = options.stripHtmlFromCells | ||
const cells = options.useFirstRowForHeadings ? $(row).find('td, th') : $(row).find('td'); | ||
cells.each(function(j, cell) { | ||
// ignoreHiddenRows | ||
if (options.ignoreHiddenRows) { | ||
const style = $(row).attr('style'); | ||
if (style) { | ||
const m = style.match(/.*display.*:.*none.*/g); | ||
if (m && m.length > 0) return; | ||
} | ||
} | ||
if (options.onlyColumns && !options.onlyColumns.includes(j)) return; | ||
if (options.ignoreColumns && !options.onlyColumns && options.ignoreColumns.includes(j)) return; | ||
const content = options.stripHtmlFromCells | ||
? $(cell) | ||
@@ -111,7 +140,7 @@ .text() | ||
// Skip blank rows | ||
if (JSON.stringify(rowAsJson) != '{}') tableAsJson.push(rowAsJson); | ||
if (JSON.stringify(rowAsJson) !== '{}') tableAsJson.push(rowAsJson); | ||
}); | ||
// Add the table to the response | ||
if (tableAsJson.length != 0) jsonResponse.push(tableAsJson); | ||
if (tableAsJson.length !== 0) jsonResponse.push(tableAsJson); | ||
}); | ||
@@ -118,0 +147,0 @@ |
@@ -9,3 +9,3 @@ { | ||
"description": "Converts HTML tables to JSON objects", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"main": "./lib/tabletojson.js", | ||
@@ -12,0 +12,0 @@ "keywords": [ |
357
README.md
@@ -12,11 +12,70 @@ [](https://nodei.co/npm/tabletojson/) | ||
Can be passed the markup for a single table as a string, a fragment of HTML or an entire page or just a URL (with an optional callback function; promises also supported). | ||
Can be passed the markup for a single table as a string, a fragment of HTML or an entire page or just | ||
a URL (with an optional callback function; promises also supported). | ||
The response is always an array. Every array entry in the response represents a table found on the page (in same the order they were found in the HTML). | ||
The response is always an array. Every array entry in the response represents a table found on the page | ||
(in same the order they were found in the HTML). | ||
## Options | ||
## Basic usage | ||
Install via npm | ||
``` | ||
npm install tabletojson | ||
``` | ||
### Remote (`convertUrl`) | ||
```javascript | ||
'use strict'; | ||
const tabletojson = require('tabletojson'); | ||
tabletojson.convertUrl( | ||
'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes', | ||
function(tablesAsJson) { | ||
console.log(tablesAsJson[1]); | ||
} | ||
); | ||
``` | ||
### Local (`convert`) | ||
Have a look in the examples. | ||
```javascript | ||
// example-6.js | ||
'use strict'; | ||
const tabletojson = require('../lib/tabletojson'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const html = fs.readFileSync(path.resolve(__dirname, '../test/tables.html'), {encoding: 'UTF-8'}); | ||
const converted = tabletojson.convert(html); | ||
console.log(converted); | ||
``` | ||
### Duplicate column headings | ||
If there are duplicate column headings, subsequent headings are suffixed with a count: | ||
``` | ||
// Table | ||
| PLACE | VALUE | PLACE | VALUE | | ||
| abc | 1 | def | 2 | | ||
// Example output | ||
[{ | ||
PLACE: 'abc', VALUE: '1', | ||
PLACE_2: 'def', VALUE_2: '2', | ||
}] | ||
``` | ||
### Tables with headings in the first column | ||
If a table contains headings in the first column you might get an unexpected result, but you can pass a second argument with options with `{ useFirstRowForHeadings: true }` to have it treat the first column as it would any other cell. | ||
If a table contains headings in the first column you might get an unexpected result, but you can pass a | ||
second argument with options with `{ useFirstRowForHeadings: true }` to have it treat the first column | ||
as it would any other cell. | ||
@@ -35,3 +94,4 @@ ``` javascript | ||
The following options are true by default, which converts all values to plain text to give you an easier more readable object to work with: | ||
The following options are true by default, which converts all values to plain text to give you an easier | ||
more readable object to work with: | ||
@@ -41,3 +101,4 @@ * stripHtmlFromHeadings | ||
If your table contains HTML you want to parse (for example for links) you can set `stripHtmlFromCells` to `false` to treat it as raw text. | ||
If your table contains HTML you want to parse (for example for links) you can set `stripHtmlFromCells` | ||
to `false` to treat it as raw text. | ||
@@ -57,20 +118,85 @@ ``` javascript | ||
You probably don't need to set `stripHtmlFromHeadings` to false (and setting it to false can make the results hard to parse), but if you do you can also set both at the same time by setting `stripHtml` to false. | ||
You probably don't need to set `stripHtmlFromHeadings` to false (and setting it to false can make the | ||
results hard to parse), but if you do you can also set both at the same time by setting `stripHtml` to | ||
false. | ||
### Duplicate column headings | ||
If there are duplicate column headings, subsequent headings are suffixed with a count: | ||
## Options | ||
### request (only `convertUrl`) | ||
If you need to get data from a remote server to pass it to the parser you can call `tabletojson.convertUrl`. | ||
When working behind a proxy you can pass any request-options (proxy, headers,...) by adding a request | ||
object to the options passed to `convertUrl`. | ||
for more information on how to configure request please have a look at: [https://github.com/request/request](https://github.com/request/request) | ||
``` javascript | ||
tabletojson.convertUrl('https://www.timeanddate.com/holidays/ireland/2017', { | ||
useFirstRowForHeadings: true, | ||
request: { | ||
proxy: 'http://proxy:8080' | ||
} | ||
}); | ||
``` | ||
### stripHtmlFromHeadings | ||
Strip any HTML from heading cells. Default is true. | ||
``` | ||
// Table | ||
| PLACE | VALUE | PLACE | VALUE | | ||
| abc | 1 | def | 2 | | ||
| KEY | <b>VALUE</b> | | ||
| abc | 1 | | ||
| dev | 2 | | ||
// Example output | ||
[{ | ||
PLACE: 'abc', VALUE: '1', | ||
PLACE_2: 'def', VALUE_2: '2', | ||
}] | ||
// Example output with stripHtmlFromHeadings:true | ||
[ | ||
{ | ||
KEY: 'abc', VALUE: '1' | ||
}, | ||
{ | ||
KEY: 'dev', VALUE: '2' | ||
} | ||
] | ||
// Example output with stripHtmlFromHeadings:false | ||
[ | ||
{ | ||
KEY: 'abc', '<b>VALUE</b>': '1' | ||
}, | ||
{ | ||
KEY: 'dev', '<b>VALUE</b>': '2' | ||
} | ||
] | ||
``` | ||
### Options forceIndexAsNumber | ||
### stripHtmlFromCells | ||
Strip any HTML from tableBody cells. Default is true. | ||
``` | ||
// Table | ||
| KEY | VALUE | | ||
| abc | <i>1</i> | | ||
| dev | <i>2</i> | | ||
// Example output with stripHtmlFromHeadings:true | ||
[ | ||
{ | ||
KEY: 'abc', VALUE: '1' | ||
}, | ||
{ | ||
KEY: 'dev', VALUE: '2' | ||
} | ||
] | ||
// Example output with stripHtmlFromHeadings:false | ||
[ | ||
{ | ||
KEY: 'abc', 'VALUE': '<i>1</i>' | ||
}, | ||
{ | ||
KEY: 'dev', 'VALUE': '<i>2</i>' | ||
} | ||
] | ||
``` | ||
### forceIndexAsNumber | ||
Instead of using column text (that sometime re-order the data), force an index as a number (string number). | ||
@@ -92,12 +218,193 @@ | ||
## Options, known issues and limitations | ||
### countDuplicateHeadings | ||
Default is 'true'. If set to 'false' duplicate headings will not get a trailing _<NUMBER>. The value of | ||
the field will be the last value found in the table row: | ||
This module only supports parsing basic tables with a simple horizontal set of <th></th> headings and corresponding <td></td> cells. | ||
``` | ||
// Table | ||
| PLACE | VALUE | PLACE | VALUE | | ||
| abc | 1 | def | 2 | | ||
| ghi | 3 | jkl | 4 | | ||
It can give useless or weird results on tables that have complex structures (such as nested tables) or multiple headers (such as on both X and Y axis). | ||
// Example output with countDuplicateHeadings:false | ||
[ | ||
{ | ||
PLACE: 'def', VALUE: '2' | ||
}, | ||
{ | ||
PLACE: 'jkl', VALUE: '4' | ||
} | ||
] | ||
``` | ||
You'll need to handle things like work out which tables to parse and (in most cases) clean up the data. You might want to combine it it with modules like json2csv or CsvToMarkdownTable. | ||
### ignoreColumns | ||
Array of indexes to be ignored, starting with 0. Default is 'null/undefined'. | ||
You might want to use it with a module like 'cheerio' if you want to parse specific tables identified by id or class (i.e. select them with cheerio and pass the HTML of them as a string). | ||
``` | ||
// Table | ||
| NAME | PLACE | WEIGHT | SEX | AGE | | ||
| Mel | 1 | 58 | W | 23 | | ||
| Tom | 2 | 78 | M | 54 | | ||
| Bill | 3 | 92 | M | 31 | | ||
// Example output with ignoreColumns: [2, 3] | ||
[ | ||
{ | ||
NAME: 'Mel', PLACE: '1', AGE: '23' | ||
}, | ||
{ | ||
NAME: 'Tom', PLACE: '2', AGE: '54' | ||
}, | ||
{ | ||
NAME: 'Bill', PLACE: '3', AGE: '31' | ||
} | ||
] | ||
``` | ||
### onlyColumns | ||
Array of indexes that are taken, starting with 0. Default is 'null/undefined'. | ||
If given, this option overrides ignoreColumns. | ||
``` | ||
// Table | ||
| NAME | PLACE | WEIGHT | SEX | AGE | | ||
| Mel | 1 | 58 | W | 23 | | ||
| Tom | 2 | 78 | M | 54 | | ||
| Bill | 3 | 92 | M | 31 | | ||
// Example output with onlyColumns: [0, 4] | ||
[ | ||
{ | ||
NAME: 'Mel', AGE: '23' | ||
}, | ||
{ | ||
NAME: 'Tom', AGE: '54' | ||
}, | ||
{ | ||
NAME: 'Bill', AGE: '31' | ||
} | ||
] | ||
``` | ||
### ignoreHiddenRows | ||
Indicates if hidden rows (display:none) are ignored. Default is true: | ||
``` | ||
// Table | ||
| NAME | PLACE | WEIGHT | SEX | AGE | | ||
| Mel | 1 | 58 | W | 23 | | ||
| Tom | 2 | 78 | M | 54 | | ||
| Bill | 3 | 92 | M | 31 | | ||
*| Cat | 4 | 4 | W | 2 |* | ||
// Example output with ignoreHiddenRows:true | ||
[ | ||
{ | ||
NAME: 'Mel', PLACE: '1', WEIGHT: '58', SEX: 'W', AGE: '23' | ||
}, | ||
{ | ||
NAME: 'Tom', PLACE: '2', WEIGHT: '78', SEX: 'M', AGE: '54' | ||
}, | ||
{ | ||
NAME: 'Bill', PLACE: '3', WEIGHT: '92', SEX: 'M', AGE: '31' | ||
} | ||
] | ||
// Example output with ignoreHiddenRows:false | ||
[ | ||
{ | ||
NAME: 'Mel', PLACE: '1', WEIGHT: '58', SEX: 'W', AGE: '23' | ||
}, | ||
{ | ||
NAME: 'Tom', PLACE: '2', WEIGHT: '78', SEX: 'M', AGE: '54' | ||
}, | ||
{ | ||
NAME: 'Bill', PLACE: '3', WEIGHT: '92', SEX: 'M', AGE: '31' | ||
} | ||
}, | ||
{ | ||
NAME: 'Cat', PLACE: '4', WEIGHT: '4', SEX: 'W', AGE: '2' | ||
} | ||
] | ||
``` | ||
### headings | ||
Array of Strings to be used as headings. Default is 'null/undefined'. | ||
If more headings are given than columns exist the overcounting ones will be ignored. If less headings | ||
are given than existing values the overcounting values are ignored. | ||
``` | ||
// Table | ||
| NAME | PLACE | WEIGHT | SEX | AGE | | ||
| Mel | 1 | 58 | W | 23 | | ||
| Tom | 2 | 78 | M | 54 | | ||
| Bill | 3 | 92 | M | 31 | | ||
*| Cat | 4 | 4 | W | 2 |* | ||
// Example output with headings: ['A','B','C','D','E'] | ||
[ | ||
{ | ||
A: 'Mel', B: '1', C: '58', D: 'W', E: '23' | ||
}, | ||
{ | ||
A: 'Tom', B: '2', C: '78', D: 'M', E: '54' | ||
}, | ||
{ | ||
A: 'Bill', B: '3', C: '92', D: 'M', E: '31' | ||
} | ||
] | ||
// Example output with headings: ['A','B','C'] | ||
[ | ||
{ | ||
A: 'Mel', B: '1', C: '58' | ||
}, | ||
{ | ||
A: 'Tom', B: '2', C: '78' | ||
}, | ||
{ | ||
A: 'Bill', B: '3', C: '92' | ||
} | ||
] | ||
// Example output with headings: ['A','B','C','D','E','F','G','H'] | ||
[ | ||
{ | ||
A: 'Mel', B: '1', C: '58', D: 'W', E: '23' | ||
}, | ||
{ | ||
A: 'Tom', B: '2', C: '78', D: 'M', E: '54' | ||
}, | ||
{ | ||
A: 'Bill', B: '3', C: '92', D: 'M', E: '31' | ||
} | ||
] | ||
// Example output with headings: ['A','B','C'] && ignoreColumns: [2, 3] | ||
[ | ||
{ | ||
A: 'Mel', B: 'W', C: '23' | ||
}, | ||
{ | ||
A: 'Tom', B: 'M', C: '54' | ||
}, | ||
{ | ||
A: 'Bill', B: 'M', C: '31' | ||
} | ||
] | ||
``` | ||
## Known issues and limitations | ||
This module only supports parsing basic tables with a simple horizontal set of <th></th> headings and | ||
corresponding <td></td> cells. | ||
It can give useless or weird results on tables that have complex structures (such as nested tables) or | ||
multiple headers (such as on both X and Y axis). | ||
You'll need to handle things like work out which tables to parse and (in most cases) clean up the data. | ||
You might want to combine it it with modules like json2csv or CsvToMarkdownTable. | ||
You might want to use it with a module like 'cheerio' if you want to parse specific tables identified | ||
by id or class (i.e. select them with cheerio and pass the HTML of them as a string). | ||
## Example usage | ||
@@ -165,3 +472,4 @@ | ||
Right now the table needs to be "well formatted" to be convertable. Tables in Html pages with not be processed. | ||
Right now the table needs to be "well formatted" to be convertable. Tables in Html pages with not be | ||
processed. | ||
@@ -197,6 +505,3 @@ ```html | ||
Special thanks to Marius Augenstein (@maugenst) for the latest major update, which includes ES6 syntax, uses native | ||
promises and has much improved code and inline documentation. | ||
Additional thanks to @roryok, Max Thyen (@maxthyen), Thor Jacobsen (@twjacobsen) and Michael Keller (@mhkeller) for | ||
improvements and bug fixes. |
@@ -20,4 +20,4 @@ 'use strict'; | ||
const converted = await tabletojson.convert(html, { | ||
stripHtmlFromHeadings: true, | ||
stripHtmlFromCells: true | ||
stripHtmlFromHeadings: true, | ||
stripHtmlFromCells: true | ||
}); | ||
@@ -34,3 +34,3 @@ converted.should.be.ok(); | ||
const converted = await tabletojson.convert(html, { | ||
stripHtml: true | ||
stripHtml: true | ||
}); | ||
@@ -47,4 +47,4 @@ converted.should.be.ok(); | ||
const converted = await tabletojson.convert(html, { | ||
stripHtmlFromHeadings: true, | ||
stripHtmlFromCells: false | ||
stripHtmlFromHeadings: true, | ||
stripHtmlFromCells: false | ||
}); | ||
@@ -61,4 +61,4 @@ converted.should.be.ok(); | ||
const converted = await tabletojson.convert(html, { | ||
stripHtmlFromHeadings: false, | ||
stripHtmlFromCells: true | ||
stripHtmlFromHeadings: false, | ||
stripHtmlFromCells: true | ||
}); | ||
@@ -95,14 +95,2 @@ converted.should.be.ok(); | ||
it('Double Header Entry: do not count duplicate headings', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
countDuplicateHeadings: false | ||
}); | ||
converted.should.be.ok(); | ||
const firstTable = converted[0]; | ||
_.has(firstTable[0], 'isDumb').should.be.true(); | ||
_.has(firstTable[0], 'isDumb_2').should.be.false(); | ||
}); | ||
it('Directly local html content: Table with header', async function() { | ||
@@ -125,7 +113,7 @@ const converted = await tabletojson.convert(html); | ||
const firstTable = converted[0]; | ||
const firstTable = converted[0]; | ||
_.has(firstTable[0], 'Dog').should.be.true(); | ||
_.has(firstTable[0], 'Race').should.be.true(); | ||
_.has(firstTable[0], '<b>Age</b>').should.be.true(); | ||
_.has(firstTable[0], 'Dog').should.be.true(); | ||
_.has(firstTable[0], 'Race').should.be.true(); | ||
_.has(firstTable[0], '<b>Age</b>').should.be.true(); | ||
}); | ||
@@ -158,4 +146,344 @@ | ||
_.has(forthTable[0], '4').should.be.true(); | ||
}); | ||
// ADDED TO FIX: https://github.com/maugenst/tabletojson/pull/18 | ||
it('Double Header Entry: countDuplicateHeadings:false', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
countDuplicateHeadings: false | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[4]; | ||
_.has(table[0], 'PLACE').should.be.true(); | ||
_.has(table[0], 'VALUE').should.be.true(); | ||
_.has(table[0], 'PLACE_2').should.be.false(); | ||
_.has(table[0], 'VALUE_2').should.be.false(); | ||
_.has(table[1], 'PLACE').should.be.true(); | ||
_.has(table[1], 'VALUE').should.be.true(); | ||
_.has(table[1], 'PLACE_2').should.be.false(); | ||
_.has(table[1], 'VALUE_2').should.be.false(); | ||
table[0].PLACE.should.be.equal('def'); | ||
table[0].VALUE.should.be.equal('2'); | ||
table[1].PLACE.should.be.equal('jkl'); | ||
table[1].VALUE.should.be.equal('4'); | ||
}); | ||
// ADDED TO FIX: https://github.com/maugenst/tabletojson/pull/18 | ||
it('Double Header Entry: countDuplicateHeadings:true', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
countDuplicateHeadings: true | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[4]; | ||
_.has(table[0], 'PLACE').should.be.true(); | ||
_.has(table[0], 'VALUE').should.be.true(); | ||
_.has(table[0], 'PLACE_2').should.be.true(); | ||
_.has(table[0], 'VALUE_2').should.be.true(); | ||
_.has(table[1], 'PLACE').should.be.true(); | ||
_.has(table[1], 'VALUE').should.be.true(); | ||
_.has(table[1], 'PLACE_2').should.be.true(); | ||
_.has(table[1], 'VALUE_2').should.be.true(); | ||
table[0].PLACE.should.be.equal('abc'); | ||
table[0].VALUE.should.be.equal('1'); | ||
table[0].PLACE_2.should.be.equal('def'); | ||
table[0].VALUE_2.should.be.equal('2'); | ||
table[1].PLACE.should.be.equal('ghi'); | ||
table[1].VALUE.should.be.equal('3'); | ||
table[1].PLACE_2.should.be.equal('jkl'); | ||
table[1].VALUE_2.should.be.equal('4'); | ||
}); | ||
// FEATURE 'ignoreColumns' | ||
it('Option: ignoreColumns: [2, 3]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
ignoreColumns: [2, 3] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'NAME').should.be.true(); | ||
_.has(table[0], 'PLACE').should.be.true(); | ||
_.has(table[0], 'WEIGHT').should.be.false(); | ||
_.has(table[0], 'SEX').should.be.false(); | ||
_.has(table[0], 'AGE').should.be.true(); | ||
table[0].NAME.should.be.equal('Mel'); | ||
table[0].PLACE.should.be.equal('1'); | ||
table[0].AGE.should.be.equal('23'); | ||
_.has(table[1], 'NAME').should.be.true(); | ||
_.has(table[1], 'PLACE').should.be.true(); | ||
_.has(table[1], 'WEIGHT').should.be.false(); | ||
_.has(table[1], 'SEX').should.be.false(); | ||
_.has(table[1], 'AGE').should.be.true(); | ||
table[1].NAME.should.be.equal('Tom'); | ||
table[1].PLACE.should.be.equal('2'); | ||
table[1].AGE.should.be.equal('54'); | ||
_.has(table[2], 'NAME').should.be.true(); | ||
_.has(table[2], 'PLACE').should.be.true(); | ||
_.has(table[2], 'WEIGHT').should.be.false(); | ||
_.has(table[2], 'SEX').should.be.false(); | ||
_.has(table[2], 'AGE').should.be.true(); | ||
table[2].NAME.should.be.equal('Bill'); | ||
table[2].PLACE.should.be.equal('3'); | ||
table[2].AGE.should.be.equal('31'); | ||
}); | ||
// FEATURE 'onlyColumns' | ||
it('Option: onlyColumns: [0, 4]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
onlyColumns: [0, 4], | ||
ignoreColumns: [2, 4] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'NAME').should.be.true(); | ||
_.has(table[0], 'PLACE').should.be.false(); | ||
_.has(table[0], 'WEIGHT').should.be.false(); | ||
_.has(table[0], 'SEX').should.be.false(); | ||
_.has(table[0], 'AGE').should.be.true(); | ||
table[0].NAME.should.be.equal('Mel'); | ||
table[0].AGE.should.be.equal('23'); | ||
_.has(table[1], 'NAME').should.be.true(); | ||
_.has(table[1], 'PLACE').should.be.false(); | ||
_.has(table[1], 'WEIGHT').should.be.false(); | ||
_.has(table[1], 'SEX').should.be.false(); | ||
_.has(table[1], 'AGE').should.be.true(); | ||
table[1].NAME.should.be.equal('Tom'); | ||
table[1].AGE.should.be.equal('54'); | ||
_.has(table[2], 'NAME').should.be.true(); | ||
_.has(table[2], 'PLACE').should.be.false(); | ||
_.has(table[2], 'WEIGHT').should.be.false(); | ||
_.has(table[2], 'SEX').should.be.false(); | ||
_.has(table[2], 'AGE').should.be.true(); | ||
table[2].NAME.should.be.equal('Bill'); | ||
table[2].AGE.should.be.equal('31'); | ||
}); | ||
// FEATURE 'ignoreHiddenRows:true' | ||
it('Option: ignoreHiddenRows:true', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
ignoreHiddenRows: true | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'NAME').should.be.true(); | ||
_.has(table[0], 'PLACE').should.be.true(); | ||
_.has(table[0], 'WEIGHT').should.be.true(); | ||
_.has(table[0], 'SEX').should.be.true(); | ||
_.has(table[0], 'AGE').should.be.true(); | ||
table.length.should.be.equal(3); | ||
}); | ||
// FEATURE 'ignoreHiddenRows:false' | ||
it('Option: ignoreHiddenRows:false', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
ignoreHiddenRows: false | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'NAME').should.be.true(); | ||
_.has(table[0], 'PLACE').should.be.true(); | ||
_.has(table[0], 'WEIGHT').should.be.true(); | ||
_.has(table[0], 'SEX').should.be.true(); | ||
_.has(table[0], 'AGE').should.be.true(); | ||
table.length.should.be.equal(4); | ||
}); | ||
// FEATURE 'headings: ['A', 'B', 'C', 'D', 'E']' | ||
it('Option: headings: ["A","B","C","D","E"]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A', 'B', 'C', 'D', 'E'] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.true(); | ||
_.has(table[0], 'C').should.be.true(); | ||
_.has(table[0], 'D').should.be.true(); | ||
_.has(table[0], 'E').should.be.true(); | ||
table.length.should.be.equal(3); | ||
}); | ||
// FEATURE 'headings: ['A', 'B', 'C']' | ||
it('Option: headings: ["A","B","C"]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A', 'B', 'C'] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.true(); | ||
_.has(table[0], 'C').should.be.true(); | ||
_.has(table[0], 'D').should.be.false(); | ||
_.has(table[0], 'E').should.be.false(); | ||
table.length.should.be.equal(3); | ||
}); | ||
/** | ||
* | NAME | PLACE | WEIGHT | SEX | AGE | | ||
* | Mel | 1 | 58 | W | 23 | | ||
* | Tom | 2 | 78 | M | 54 | | ||
* | Bill | 3 | 92 | M | 31 | | ||
*/ | ||
// FEATURE 'headings: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']' | ||
it('Option: headings: ["A","B","C","E","E","F","G","H","I"]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.true(); | ||
_.has(table[0], 'C').should.be.true(); | ||
_.has(table[0], 'D').should.be.true(); | ||
_.has(table[0], 'E').should.be.true(); | ||
table.length.should.be.equal(3); | ||
table[0].A.should.equal('Mel'); | ||
table[0].B.should.equal('1'); | ||
table[0].C.should.equal('58'); | ||
table[0].D.should.equal('W'); | ||
table[0].E.should.equal('23'); | ||
table[1].A.should.equal('Tom'); | ||
table[1].B.should.equal('2'); | ||
table[1].C.should.equal('78'); | ||
table[1].D.should.equal('M'); | ||
table[1].E.should.equal('54'); | ||
table[2].A.should.equal('Bill'); | ||
table[2].B.should.equal('3'); | ||
table[2].C.should.equal('92'); | ||
table[2].D.should.equal('M'); | ||
table[2].E.should.equal('31'); | ||
}); | ||
/** | ||
* | NAME | PLACE | WEIGHT | SEX | AGE | | ||
* | Mel | 1 | 58 | W | 23 | | ||
* | Tom | 2 | 78 | M | 54 | | ||
* | Bill | 3 | 92 | M | 31 | | ||
*/ | ||
// FEATURE 'headings: ['A', 'B', 'C'] && ignoreColumns: [1, 2]' | ||
it('Option: headings: ["A","B","C"] && ignoreColumns: [1, 2]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A', 'B', 'C'], | ||
ignoreColumns: [1, 2] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.true(); | ||
_.has(table[0], 'C').should.be.true(); | ||
_.has(table[0], 'D').should.be.false(); | ||
_.has(table[0], 'E').should.be.false(); | ||
table.length.should.be.equal(3); | ||
table[0].A.should.equal('Mel'); | ||
table[0].B.should.equal('W'); | ||
table[0].C.should.equal('23'); | ||
table[1].A.should.equal('Tom'); | ||
table[1].B.should.equal('M'); | ||
table[1].C.should.equal('54'); | ||
table[2].A.should.equal('Bill'); | ||
table[2].B.should.equal('M'); | ||
table[2].C.should.equal('31'); | ||
}); | ||
/** | ||
* | NAME | PLACE | WEIGHT | SEX | AGE | | ||
* | Mel | 1 | 58 | W | 23 | | ||
* | Tom | 2 | 78 | M | 54 | | ||
* | Bill | 3 | 92 | M | 31 | | ||
*/ | ||
// FEATURE 'headings: ['A', 'B', 'C'] && ignoreColumns: [1, 2] && onlyColumns: [0, 4]' | ||
it('Option: headings: ["A","B","C"] && ignoreColumns: [1, 2] && onlyColumns: [0, 4]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A', 'B', 'C'], | ||
ignoreColumns: [1, 2], | ||
onlyColumns: [0, 4] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.true(); | ||
_.has(table[0], 'C').should.be.false(); | ||
_.has(table[0], 'D').should.be.false(); | ||
_.has(table[0], 'E').should.be.false(); | ||
table.length.should.be.equal(3); | ||
table[0].A.should.equal('Mel'); | ||
table[0].B.should.equal('23'); | ||
table[1].A.should.equal('Tom'); | ||
table[1].B.should.equal('54'); | ||
table[2].A.should.equal('Bill'); | ||
table[2].B.should.equal('31'); | ||
}); | ||
/** | ||
* | NAME | PLACE | WEIGHT | SEX | AGE | | ||
* | Mel | 1 | 58 | W | 23 | | ||
* | Tom | 2 | 78 | M | 54 | | ||
* | Bill | 3 | 92 | M | 31 | | ||
*/ | ||
// FEATURE 'headings: ['A'] && ignoreColumns: [1, 2] && onlyColumns: [0, 4]' | ||
it('Option: headings: ["A"] && ignoreColumns: [1, 2] && onlyColumns: [0, 4]', async function() { | ||
const converted = await tabletojson.convert(html, { | ||
headings: ['A'], | ||
ignoreColumns: [1, 2], | ||
onlyColumns: [0, 4] | ||
}); | ||
converted.should.be.ok(); | ||
const table = converted[5]; | ||
_.has(table[0], 'A').should.be.true(); | ||
_.has(table[0], 'B').should.be.false(); | ||
_.has(table[0], 'C').should.be.false(); | ||
_.has(table[0], 'D').should.be.false(); | ||
_.has(table[0], 'E').should.be.false(); | ||
table.length.should.be.equal(3); | ||
table[0].A.should.equal('Mel'); | ||
table[1].A.should.equal('Tom'); | ||
table[2].A.should.equal('Bill'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
57105
18
810
500
2