An open API service indexing awesome lists of open source software.

https://github.com/chrismichaelps/parse-html-table

A lightweight library for parsing HTML tables into structured data using TypeScript.
https://github.com/chrismichaelps/parse-html-table

cheerio-table-parser parse-html-table table-parser

Last synced: 2 months ago
JSON representation

A lightweight library for parsing HTML tables into structured data using TypeScript.

Awesome Lists containing this project

README

          

# ✓ parse-html-table

> A lightweight library for parsing HTML tables into structured data using TypeScript.

## 📦 Installation

### npm

```shell
npm install parse-html-table
```

### yarn

```shell
yarn add parse-html-table
```

# 📚 Documentation

## Usage

```typescript
import {
createTableParser,
RowData,
TableParserFactory,
} from "parse-html-table";

// Define the specific row data interface
interface PersonData extends RowData {
name: string;
age: string;
country: string;
}

// Create a specific table parser factory
export const personTableParserFactory: TableParserFactory =
createTableParser();

// Example HTML table 1
const html1 = `



name
age
country




John Doe
30
USA


Jane Smith
25
Canada



`;

// Example HTML table 2
// Parse the name of the columns if they have spaces as cameoCase correctly
const html2 = `



full name
full age
full country




John Doe
30
USA


Jane Smith
25
Canada



`;

// Example HTML table 3 (empty table)
const html3 = "";

// Parse table 1
const tableParser1 = personTableParserFactory(html1);
const tableData1 = tableParser1();
console.log("Table 1 Data:", tableData1);

// Parse table 2
const tableParser2 = personTableParserFactory(html2);
const tableData2 = tableParser2();
console.log("Table 2 Data:", tableData2);

// Parse table 3
const tableParser3 = personTableParserFactory(html3);
const tableData3 = tableParser3();
console.log("Table 3 Data:", tableData3);
```

```typescript
// table 1
{
headers: [ 'name', 'age', 'country' ],
rows: [
{ name: 'John Doe', age: '30', country: 'USA' },
{ name: 'Jane Smith', age: '25', country: 'Canada' }
]
}

// table 2
{
headers: [ 'fullName', 'fullAge', 'fullCountry' ],
rows: [
{ fullName: 'John Doe', fullAge: '30', fullCountry: 'USA' },
{ fullName: 'Jane Smith', fullAge: '25', fullCountry: 'Canada' }
]
}

// table 3
{
headers: [],
rows: []
}
```

## **:handshake: Contributing**

- Fork it!
- Create your feature branch: `git checkout -b my-new-feature`
- Commit your changes: `git commit -am 'Add some feature'`
- Push to the branch: `git push origin my-new-feature`
- Submit a pull request

---

### **:busts_in_silhouette: Credits**

- [Chris Michael](https://github.com/ChrisMichaelPerezSantiago) (Project Leader, and Developer)

---

### **:anger: Troubleshootings**

This is just a personal project created for study / demonstration purpose and to simplify my working life, it may or may
not be a good fit for your project(s).

---

### **:heart: Show your support**

Please :star: this repository if you like it or this project helped you!\
Feel free to open issues or submit pull-requests to help me improving my work.

---

### **:robot: Author**

_*Chris M. Perez*_

> You can follow me on
> [github](https://github.com/ChrisMichaelPerezSantiago) · [twitter](https://twitter.com/Chris5855M)

---

Copyright ©2023 [parse-html-table](https://github.com/ChrisMichaelPerezSantiago/parse-html-table).