@@ -10,13 +10,29 @@ The `node:string_decoder` module provides an API for decoding `Buffer` objects
10
10
into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
11
11
characters. It can be accessed using:
12
12
13
- ``` js
13
+ ``` mjs
14
+ import { StringDecoder } from ' node:string_decoder' ;
15
+ ```
16
+
17
+ ``` cjs
14
18
const { StringDecoder } = require (' node:string_decoder' );
15
19
```
16
20
17
21
The following example shows the basic use of the ` StringDecoder ` class.
18
22
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
20
36
const { StringDecoder } = require (' node:string_decoder' );
21
37
const decoder = new StringDecoder (' utf8' );
22
38
@@ -35,7 +51,17 @@ next call to `stringDecoder.write()` or until `stringDecoder.end()` is called.
35
51
In the following example, the three UTF-8 encoded bytes of the European Euro
36
52
symbol (` € ` ) are written over three separate operations:
37
53
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
39
65
const { StringDecoder } = require (' node:string_decoder' );
40
66
const decoder = new StringDecoder (' utf8' );
41
67
0 commit comments