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

https://github.com/igorskyflyer/npm-encode-entities

πŸƒβ€β™‚οΈ Fast and simple Map and RegExp based HTML entities encoder.🍁
https://github.com/igorskyflyer/npm-encode-entities

back-end biome characters decoder encode-entities encoder fast html html-entities igorskyflyer javascript map module node npm package regexp typescript vitest

Last synced: 4 months ago
JSON representation

πŸƒβ€β™‚οΈ Fast and simple Map and RegExp based HTML entities encoder.🍁

Awesome Lists containing this project

README

          


Icon of Encode Entities

Encode Entities




πŸƒβ€β™‚οΈ Fast and simple Map and RegExp based HTML entities encoder. 🍁




## πŸ“ƒ Table of Contents

- [Features](#-features)
- [Usage](#-usage)
- [API](#-api)
- [Changelog](#-changelog)
- [Support](#-support)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)




## πŸ€– Features

- ⚑ Instant HTML encoding for special characters
- πŸ›  Add your own custom encoding rules
- β™» Reset back to default rules anytime
- ✏️ Update individual rules on the fly
- ❌ Remove unwanted rules easily
- πŸ“Š See exactly how many rules are active
- πŸš€ Single‑pass, high‑performance replacement engine (powered by [`@igorskyflyer/mapped-replacer`](https://www.npmjs.com/package/@igorskyflyer/mapped-replacer))


> πŸ›‘ **SECURITY**
>
> Encoding of special characters into HTML entities helps mitigate XSS risks in the textual layer by ensuring user‑supplied content is treated as text, not executable code.
>
> **Note: not a full XSS solution, usage of other XSS-prevention techniques is still required.**
>




## πŸ•΅πŸΌ Usage

Install it by executing any of the following, depending on your preferred package manager:

```bash
pnpm add @igorskyflyer/encode-entities
```

```bash
yarn add @igorskyflyer/encode-entities
```

```bash
npm i @igorskyflyer/encode-entities
```




## 🀹🏼 API

> πŸ’‘ **TIP**
>
> *Encoded by default:*
> **<**, **>**, **"**, **'**, **&**, **=**, `**,** **!**, **@**, **\$**, **%**, **(**, **)**, **+**, **{**, **}**, **[**, **]**.
>
> You can however remove any of these rules and/or add your own.
>


### resetRules(): void

_Resets the rules to the default ones._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()

encoder.updateRule('πŸ˜€', '<')
encoder.updateRule('πŸ˜‚', '>')
encoder.resetRules()

console.log(encoder.encode('')) // outputs '<strong>'
```


### addRule(key: string, value: string): boolean

_Adds a new rule for entities encoding. Returns true if the rule was added successfully or false if not._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()
encoder.addRule('β†’', 'β†’')

console.log(encoder.encode('β†’')) // outputs '<a href="#">β†’</a>'
```


### updateRule(replaceWith: string, searchFor: string): boolean

_Updates an existing rule for entity encoding. Returns true if the rule was updated successfully or false if not._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()
encoder.addRule('←', 'β†’')
encoder.updateRule('β†’', 'β†’')

console.log(encoder.encode('β†’')) // outputs '<a href="#">β†’</a>'
```


### addRules(rules: Object): boolean

_Adds rules for entity encoding._

_Passed object is a simple key-value object, i.e. **{ '<': '\<', '>': '\>' }**_

_Returns true if the rules were added successfully or false if not._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()

encoder.addRules({
'𝕋':'𝕋'
'β‰ˆ':'β‰ˆ'
'𝔱':'𝔱'
})

console.log(encoder.encode('𝕋 β‰ˆ 𝔱')) // outputs '<span>𝕋 β‰ˆ 𝔱</span>'
```


### removeRule(key: string): boolean

_Removes the rule that matches the provided key._
_Returns true if the rule was removed successfully or false if not._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()

encoder.addRules({
'𝕋': '𝕋',
'β‰ˆ': 'β‰ˆ',
'𝔱': '𝔱'
})
encoder.removeRule('β‰ˆ')

console.log(encoder.rulesCount()) // outputs 20
```


### rulesCount(): number

_Gets the number of rules for entity encoding._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()

encoder.addRules({
'𝕋': '𝕋',
'β‰ˆ': 'β‰ˆ',
'𝔱': '𝔱',
})

console.log(encoder.rulesCount()) // outputs 21
```


### encode()

_Encodes special characters in the given string to HTML entities._

```ts
import { Encoder } from '@igorskyflyer/encode-entities'

const encoder: Encoder = new Encoder()

console.log(encoder.encode('')) // outputs '<strong>'
```




## πŸ“ Changelog

πŸ“‘ The changelog is available here, [CHANGELOG.md](https://github.com/igorskyflyer/npm-encode-entities/blob/main/CHANGELOG.md).




## πŸͺͺ License

Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-encode-entities/blob/main/LICENSE).




## πŸ’– Support


I work hard for every project, including this one and your support means a lot to me!


Consider buying me a coffee. β˜•




Donate to igorskyflyer




Thank you for supporting my efforts! πŸ™πŸ˜Š




## 🧬 Related

[@igorskyflyer/str-is-in](https://www.npmjs.com/package/@igorskyflyer/str-is-in)

> _🧡 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. πŸ”_

[@igorskyflyer/aria](https://www.npmjs.com/package/@igorskyflyer/aria)

> _🧬 Meet Aria, an efficient Adblock filter list compiler, with many features that make your maintenance of Adblock filter lists a breeze! πŸ—‘_

[@igorskyflyer/pathexists](https://www.npmjs.com/package/@igorskyflyer/pathexists)

> _🧲 Provides ways of properly checking if a path exists inside a given array of files/directories both on Windows and UNIX-like operating systems. πŸ—Ί_

[@igorskyflyer/chars-in-string](https://www.npmjs.com/package/@igorskyflyer/chars-in-string)

> _πŸͺ Provides ways of testing whether an array of chars is present inside a given String. β˜„_

[@igorskyflyer/valid-path](https://www.npmjs.com/package/@igorskyflyer/valid-path)

> _🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜_






## πŸ‘¨πŸ»β€πŸ’» Author
Created by **Igor Dimitrijević** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).