doc: simplify util.TextDecoder example

This simplifies the example and makes it runnable. (The current example
has a magic function.) (This also removes an assignment in a condition
which will be flagged if we enable ESLint's no-cond-assign rule.)

PR-URL: https://github.com/nodejs/node/pull/41574
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Rich Trott authored and Danielle Adams committed 2022-03-14 09:31:10 -04:00
1 parent 2c813d825f
commit 9f8e442dc7
1 file changed
+3 -7
+3 -7
View File
@@ -1185,13 +1185,9 @@ added: v8.3.0
An implementation of the [WHATWG Encoding Standard][] `TextDecoder` API.
```js
const decoder = new TextDecoder('shift_jis');
let string = '';
let buffer;
while (buffer = getNextChunkSomehow()) {
string += decoder.decode(buffer, { stream: true });
}
string += decoder.decode(); // end-of-stream
const decoder = new TextDecoder();
const u8arr = new Uint8Array([72, 101, 108, 108, 111]);
console.log(decoder.decode(u8arr)); // Hello
```
### WHATWG supported encodings