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: 8 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# πŸƒβ€β™‚οΈ Encode Entities 🍁


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






πŸ’– Support further development


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


Consider buying me a coffee. β˜•


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





Donate to igorskyflyer




@igorskyflyer






Fast and simple Map and RegExp based HTML entities encoder. In order to overcome different methods of possible XSS attacks, it by default encodes the following characters: **<**, **>**, **"**, **'**, **&**, **=**, `**,** **!**, **@**, **\$**, **%**, **(**, **)**, **+**, **{**, **}**, **[**, **]**.
_You can however remove any of these rules and/or add your own._


> Uses the [@igor.dvlpr/mapped-replacer](https://www.npmjs.com/package/@igor.dvlpr/mapped-replacer) package.


## πŸ•΅πŸΌ Usage

Install it by executing:

```shell
npm i "@igor.dvlpr/encode-entities"
```


## 🀹🏼 API

### resetRules(): void

_Resets the rules to the default ones._

```ts
import { Encoder } from '@igor.dvlpr/encode-entities'

const encoder: Encoder = new Encoder()

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

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


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

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

```ts
import { Encoder } from '@igor.dvlpr/encode-entities'

const encoder: Encoder = new Encoder()

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


### addRules(rules: Object): boolean

_Adds rules or updates the existing 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 '@igor.dvlpr/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 '@igor.dvlpr/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 '@igor.dvlpr/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 '@igor.dvlpr/encode-entities'

const encoder: Encoder = new Encoder()

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

---

## πŸͺͺ License

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

---

## 🧬 Related

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

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

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

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

[@igor.dvlpr/pathexists](https://www.npmjs.com/package/@igor.dvlpr/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. πŸ—Ί_

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

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

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

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




>
> Provided by **Igor Dimitrijević** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).
>