Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ESLint plugin.
- [`buffer-equals`](./buffer-equals.md)
- [`builtin-modules`](./builtin-modules.md)
- [`chalk`](./chalk.md)
| [`core-util-is`](./core-util-is.md)
- [`core-util-is`](./core-util-is.md)
- [`cpx`](./cpx.md)
- [`crypto-js`](./crypto-js.md)
- [`deep-equal`](./deep-equal.md)
Expand Down Expand Up @@ -77,4 +77,5 @@ ESLint plugin.
- [`tempy`](./tempy.md)
- [`traverse`](./traverse.md)
- [`uri-js`](./uri-js.md)
- [`xmldom`](./xmldom.md)
- [`utf8`](./utf8.md)
- [`xmldom`](./xmldom.md)
31 changes: 31 additions & 0 deletions docs/modules/utf8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
description: Modern alternatives to the utf8 package for UTF-8 encoding and decoding
---

# Replacements for `utf8`

Modern Node and browsers provide native UTF-8 APIs, so this dependency is rarely needed.

## TextEncoder/TextDecoder (built-in)

The built-in [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) and [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) APIs provide a native way to handle UTF-8 encoding and decoding.

```ts
const text = "€";
const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(text); // Uint8Array of UTF-8 bytes

// and to decode:

const decoder = new TextDecoder('utf-8', { fatal: true });
const decodedText = decoder.decode(utf8Bytes); // "€"
```

## Buffer (Node.js)

Node's built-in [`Buffer`](https://nodejs.org/api/buffer.html) provides both `Buffer.from(str, 'utf8')` and `buf.toString('utf8')` methods for UTF-8 encoding and decoding.

```ts
const text = "€";
const utf8Buffer = Buffer.from(text, 'utf8'); // Buffer of UTF-8 bytes
```
6 changes: 6 additions & 0 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,12 @@
"docPath": "uri-js",
"category": "preferred"
},
{
"type": "documented",
"moduleName": "utf8",
"docPath": "utf8",
"category": "preferred"
},
{
"type": "documented",
"moduleName": "xmldom",
Expand Down