html-space-cleaner
Version:
A TypeScript library that provides functions to normalize HTML strings by removing extra spaces and ensuring consistent formatting.
178 lines (124 loc) โข 4.76 kB
Markdown
//img.shields.io/npm/v/html-space-cleaner.svg)](https://www.npmjs.com/package/html-space-cleaner)
[](https://www.npmjs.com/package/html-space-cleaner)
[](https://github.com/nati-grossman/html-space-cleaner/blob/main/LICENSE)
[](https://www.typescriptlang.org/)
[](https://nodejs.org/)
A TypeScript library that provides functions to normalize HTML strings by removing extra spaces and ensuring consistent formatting. This library is perfect for cleaning up HTML output, ensuring consistent attribute formatting, and preparing HTML for comparison or processing.
- ๐งน Remove extra spaces from HTML attributes
- ๐ Normalize attribute formatting
- ๐ฏ Remove extra spaces within HTML tags
- ๐ฆ Written in TypeScript with full type support
- โ
Comprehensive test coverage
- ๐ Zero dependencies
```bash
npm install html-space-cleaner
```
```typescript
import {
normalizeAttributes,
removeExtraSpacesInTags,
normalizeAttributesAndTags,
} from 'html-space-cleaner';
// Normalize specific attributes
const html = '<div class=" test class " id=" my-id ">';
const normalized = normalizeAttributes(html, ['class', 'id']);
// Result: '<div class="test class" id="my-id">'
// Remove extra spaces in tags
const htmlWithSpaces = '<div class="test" >';
const cleaned = removeExtraSpacesInTags(htmlWithSpaces);
// Result: '<div class="test">'
// Combine both operations
const htmlToClean = '<div class=" test " id=" my-id ">';
const fullyNormalized = normalizeAttributesAndTags(htmlToClean, ['class', 'id']);
// Result: '<div class="test" id="my-id">'
```
Normalizes specified attributes in an HTML string by removing extra spaces and ensuring consistent formatting.
```typescript
const html = '<div class=" test " id=" my-id ">';
const result = normalizeAttributes(html, ['class', 'id']);
// Result: '<div class="test" id="my-id">'
```
- `html`: The HTML string to normalize
- `attributes`: Array of attribute names to normalize
- The normalized HTML string
Normalizes an HTML string by removing extra spaces inside tags.
```typescript
const html = '<div class="test" >';
const result = removeExtraSpacesInTags(html);
// Result: '<div class="test">'
```
- `html`: The HTML string to normalize
- The normalized HTML string
Combines both functions to normalize specified attributes and remove extra spaces within HTML tags.
```typescript
const html = '<div class=" test " id=" my-id ">';
const result = normalizeAttributesAndTags(html, ['class', 'id']);
// Result: '<div class="test" id="my-id">'
```
- `html`: The HTML string to normalize
- `attributes`: Array of attribute names to normalize
- The normalized HTML string
```typescript
import { normalizeAttributes } from 'html-space-cleaner';
const html = `
<div class=" container " id=" main ">
<p style=" color: red ;">Hello, world!</p>
</div>
`;
const result = normalizeAttributes(html, ['class', 'style']);
console.log(result);
// Output:
// <div class="container" id=" main ">
// <p style="color: red;">Hello, world!</p>
// </div>
```
```typescript
import { normalizeAttributesAndTags } from 'html-space-cleaner';
const html = `
<div class=" container " id=" main ">
<p style=" color: red ;" >Hello, world!</p>
</div>
`;
const result = normalizeAttributesAndTags(html, ['class', 'style']);
console.log(result);
// Output:
// <div class="container" id="main">
// <p style="color: red;">Hello, world!</p>
// </div>
```
This project is written in TypeScript and uses Jest for testing.
```bash
npm install
npm test
npm run test:watch
npm run build
npm run format
npm run lint
```
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
[![npm version](https: