Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f00ad27

Browse filesBrowse files
mfdebianRafaelGSS
authored andcommitted
doc: add esm examples to node:string_decoder
PR-URL: #55507 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 71cc20a commit f00ad27
Copy full SHA for f00ad27

File tree

Expand file treeCollapse file tree

1 file changed

+29
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+29
-3
lines changed
Open diff view settings
Collapse file

‎doc/api/string_decoder.md‎

Copy file name to clipboardExpand all lines: doc/api/string_decoder.md
+29-3Lines changed: 29 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ The `node:string_decoder` module provides an API for decoding `Buffer` objects
1010
into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
1111
characters. It can be accessed using:
1212

13-
```js
13+
```mjs
14+
import { StringDecoder } from 'node:string_decoder';
15+
```
16+
17+
```cjs
1418
const { StringDecoder } = require('node:string_decoder');
1519
```
1620

1721
The following example shows the basic use of the `StringDecoder` class.
1822

19-
```js
23+
```mjs
24+
import { StringDecoder } from 'node:string_decoder';
25+
import { Buffer } from 'node:buffer';
26+
const decoder = new StringDecoder('utf8');
27+
28+
const cent = Buffer.from([0xC2, 0xA2]);
29+
console.log(decoder.write(cent)); // Prints: ¢
30+
31+
const euro = Buffer.from([0xE2, 0x82, 0xAC]);
32+
console.log(decoder.write(euro)); // Prints: €
33+
```
34+
35+
```cjs
2036
const { StringDecoder } = require('node:string_decoder');
2137
const decoder = new StringDecoder('utf8');
2238

@@ -35,7 +51,17 @@ next call to `stringDecoder.write()` or until `stringDecoder.end()` is called.
3551
In the following example, the three UTF-8 encoded bytes of the European Euro
3652
symbol (``) are written over three separate operations:
3753

38-
```js
54+
```mjs
55+
import { StringDecoder } from 'node:string_decoder';
56+
import { Buffer } from 'node:buffer';
57+
const decoder = new StringDecoder('utf8');
58+
59+
decoder.write(Buffer.from([0xE2]));
60+
decoder.write(Buffer.from([0x82]));
61+
console.log(decoder.end(Buffer.from([0xAC]))); // Prints: €
62+
```
63+
64+
```cjs
3965
const { StringDecoder } = require('node:string_decoder');
4066
const decoder = new StringDecoder('utf8');
4167

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.