@@ -10,13 +10,29 @@ The `node:string_decoder` module provides an API for decoding `Buffer` objects
1010into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
1111characters. It can be accessed using:
1212
13- ``` js
13+ ``` mjs
14+ import { StringDecoder } from ' node:string_decoder' ;
15+ ```
16+
17+ ``` cjs
1418const { StringDecoder } = require (' node:string_decoder' );
1519```
1620
1721The 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
2036const { StringDecoder } = require (' node:string_decoder' );
2137const decoder = new StringDecoder (' utf8' );
2238
@@ -35,7 +51,17 @@ next call to `stringDecoder.write()` or until `stringDecoder.end()` is called.
3551In the following example, the three UTF-8 encoded bytes of the European Euro
3652symbol (` € ` ) 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
3965const { StringDecoder } = require (' node:string_decoder' );
4066const decoder = new StringDecoder (' utf8' );
4167
0 commit comments