npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-table-to-dataframe

v1.0.37

Published

Convert HTML tables to data-frames

Downloads

748

Readme

html-table-to-dataframe

Convert an HTML Table to a DataFrame

CI Status

Overview

This project provides utilities to convert HTML tables into structured data formats and pretty-print them as tables. It uses jsdom for parsing HTML and cli-table3 for displaying data in a formatted table.

Usage:

| Person | Like | Age | |--------|-----------------------|-----| | Chris | HTML tables | 22 | | Dennis | Web accessibility | 45 | | Sarah | JavaScript frameworks | 29 | | Karen | Web performance | 36 |

import { toDataFrame } from 'html-table-to-dataframe';

const dataFrame = toDataFrame(htmlString, ['Person', 'Likes', 'Age']);

console.log(dataFrame)
// data => [
//  { Person: "Chris", Likes: "HTML tables", Age: "22" },
//];

await toPrettyPrint(data);

In Print Form

┌─────────┬──────────────────────┬─────┐
│ Person  │ Likes                │ Age │
├─────────┼──────────────────────┼─────┤
│ Chris   │ HTML tables          │ 22  │
│ Dennis  │ Web accessibility    │ 45  │
│ Sarah   │ JavaScript frameworks│ 29  │
│ Karen   │ Web performance      │ 36  │
└─────────┴──────────────────────┴─────┘

Accessible Expectations

toDataFrame

The toDataFrame function converts an HTML string into a data frame structure, which is an array of objects where each object represents a row in the table. This function is essential for transforming raw HTML tables into a format that can be easily manipulated and tested.

toHaveColumnToBeValue

Checks if a single row in the table has the specified value in a given column.

toHaveColumnToBeValue(tableData, 'col_2', '3');

const tableData = [
  { col_1: '1', col_2: '3' }
];

toHaveColumnToBeValue(tableData, 'col_2', '3'); // Passes, as the value in 'col_2' is '3'

toHaveTableRowCountGreaterThan

Checks if the table contains more rows than the specified count.

toHaveTableRowCountGreaterThan(tableData, 3);

const tableData = [
  { one: '1', two: '3' },
  { one: '2', two: '100' }
];

toHaveTableRowCountGreaterThan(tableData, 3); // Fails, as row count is 2

toHaveColumnValuesToMatchRegex & toHaveColumnsValuesToMatchRegex

Checks if the values in the specified column match the given regular expression pattern.

toHaveColumnValuesToMatchRegex(tableData, "two", "\d\d");

const tableData = [
  { one: '1', two: '3' },
  { one: '2', two: '100' }
];

toHaveColumnValuesToMatchRegex(tableData, "two", "\\d\\d"); // Fails, as {"two":"100"} has 3 digits
toHaveColumnsValuesToMatchRegex(tableData, ["one", "two"], "\\d\\d"); // Fails, as {"two":"100"} has 3 digits

toHaveColumnValuesToBeInRange

Checks if the values in the specified column fall within the given range.

toHaveColumnValuesToBeInRange(tableData, "two", 0, 4);

const tableData = [
  { one: '1', two: '3' },
  { one: '2', two: '100' }
];

toHaveColumnValuesToBeInRange(tableData, "two", 0, 4); // Fails, as {"two":"100"} is greater than 4

toHaveColumnValuesToBeNumbers

Checks if the values in the specified column are numbers.

toHaveColumnValuesToBeNumbers(tableData, "two");

const tableData = [
  { one: '1', two: '3' },
  { one: '2', two: '1e' }
];

toHaveColumnValuesToBeNumbers(tableData, "two"); // Fails, as {"two":"1e"} is not a number

toHaveColumnToMatchWhenFilteredBy

Checks if a target column/value pair exists when filtered by a specified column/value.

toHaveColumnToMatchWhenFilteredBy(tableData, "col_1", "2", "col_2", "xyz");

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '1e' }
];

toHaveColumnToMatchWhenFilteredBy(tableData, "col_1", "2", "col_2", "xyz"); 
// Fails, as {"col_1":"2"} is found, but {"col_2":"xyz"} is not

toHaveColumnToMatchGroupWhenFilteredBy

Uses an array of GroupType to check if a target column/value pair exists when filtered by each specified column/value.

toHaveColumnToMatchGroupWhenFilteredBy(tableData, "col_1", "1", group);

type GroupType = {
  filterColumn: string;
  filterValue: string;
};

const tableData = [
  { col_1: '1', col_2: 'a', col_3: 'b' }
];

const group: GroupType[] = [
  { filterColumn: "col_2", filterValue: "a" },
  { filterColumn: "col_3", filterValue: "a" }
];

toHaveColumnToMatchGroupWhenFilteredBy(tableData, "col_1", "1", group);
// Fails, as {"col_2": "a"} is OK; however, {"col_3": "a"} should be "b"

toHaveColumnToNotMatch

Confirms that a specified column no longer contains a certain value. This is useful for checking if a row has been deleted or archived.

toHaveColumnToNotMatch(tableData, "col_1", "2");

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '1e' }
];

toHaveColumnToNotMatch(tableData, "col_1", "2");
// Fails, as {"col_1":"2"} is found and should not be.

toHaveTableRowCount

Matches the row count of the table data against the expected value.

toHaveTableRowCount(tableData, 3);

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '1e' }
];

toHaveTableRowCount(tableData, 3);
// Fails, as row count should be 2.

toHaveColumnToBeValue

Expects only 1 table row and checks if the column value matches the provided value.

toHaveColumnToBeValue(tableData, "col_2", "3");

const tableData = [
  { col_1: '1', col_2: '3' }
];

toHaveColumnToBeValue(tableData, "col_2", "3");
// Passes, as the column value is "3".

toHaveColumnGroupToBeValue

Expects only 1 table row and checks if the column values in a group match the provided values. Exceptions are made when the filter value is null or undefined.

toHaveColumnGroupToBeValue(tableData, [{ filterColumn: "col_2", filterValue: "3" }]);

const tableData = [
  { col_1: '1', col_2: '3' }
];

const filterGroup = [
  { filterColumn: "col_2", filterValue: "3" }
];

toHaveColumnGroupToBeValue(tableData, filterGroup);
// Passes, as the column value for "col_2" is "3".

toHaveColumnGroupToBeValues

Performs multiple grouped checks using toHaveColumnGroupToBeValue for each row. The tableData must be the same length as the filterGroups, and each entry in filterGroups is applied to the corresponding row in tableData.

toHaveColumnGroupToBeValues(tableData, filterGroups);

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '4' }
];

const filterGroups = [
  [{ filterColumn: "col_2", filterValue: "3" }],
  [{ filterColumn: "col_2", filterValue: "4" }]
];
toHaveColumnGroupToBeValues(tableData, filterGroups);
// Passes, as the column values match the expected values for each row.

toHaveTableToNotMatch

Converts two tables into strings and compares them for equality. This assertion checks that the two tables do not match exactly.

toHaveTableToNotMatch(tableData1, tableData2);

const tableData1 = [
  { col_1: '1', col_2: '3' }
];

const tableData2 = [
  { col_1: '1', col_2: '3' }
];

toHaveTableToNotMatch(tableData1, tableData2);
// Fails, as the two tables are identical

toHaveTableToMatch

Converts two tables into strings and compares them for equality. This assertion checks that the two tables match exactly.

toHaveTableToMatch(tableData1, tableData2);

const tableData1 = [
  { col_1: '1', col_2: '3' }
];

const tableData2 = [
  { col_1: '1', col_2: '4' }
];
toHaveTableToMatch(tableData1, tableData2);
// Fails, as the two tables are different

toHaveTableRowCountEqualTo

Check the length of the table is equal to

toHaveTableRowCountEqualTo(tableData, expectedLength);

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '4' }
];

toHaveTableRowCountEqualTo(tableData, 3);
// Fails, as the tables are length of 2

toHaveTableRowCountLessThan

Check the length of the table is equal to

toHaveTableRowCountLessThan(tableData, expectedLength);

const tableData = [
  { col_1: '1', col_2: '3' },
  { col_1: '2', col_2: '4' }
];

toHaveTableRowCountLessThan(tableData, 1);
// Fails, as the tables are length of 2