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.π
- Host: GitHub
- URL: https://github.com/igorskyflyer/npm-encode-entities
- Owner: igorskyflyer
- License: mit
- Created: 2019-10-03T13:24:37.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-08-28T18:56:03.000Z (10 months ago)
- Last Synced: 2025-10-02T01:53:30.060Z (9 months ago)
- Topics: back-end, biome, characters, decoder, encode-entities, encoder, fast, html, html-entities, igorskyflyer, javascript, map, module, node, npm, package, regexp, typescript, vitest
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@igorskyflyer/encode-entities
- Size: 242 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Support: SUPPORT.md
Awesome Lists containing this project
README
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. β
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/)).