Skip to content

Commit

Permalink
doc: change examples to use the same data type as the result
Browse files Browse the repository at this point in the history
  • Loading branch information
khaosdoctor committed Aug 26, 2022
1 parent 0102e52 commit 0ee26a1
Showing 1 changed file with 25 additions and 67 deletions.
92 changes: 25 additions & 67 deletions doc/api/webstreams.md
Expand Up @@ -1458,33 +1458,26 @@ added: v16.7.0
contents of the stream.
```mjs
import { arrayBuffer } from 'node:stream/consumers';
import { buffer as arrayBuffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';
import { TextEncoder } from 'node:util';

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);
const encoder = new TextEncoder();
const dataArray = encoder.encode('hello world from consumers!');

const readable = Readable.from(items);
const readable = Readable.from(dataArray);
const data = await arrayBuffer(readable);
console.log(`from readable: ${data.byteLength}`);
```
```cjs
const { arrayBuffer } = require('node:stream/consumers');
const { Readable } = require('node:stream');
const { Readable } = require('stream');
const { TextEncoder } = require('util');

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);

const readable = Readable.from(items);
const encoder = new TextEncoder();
const dataArray = encoder.encode(['hello world from consumers!']);
const readable = Readable.from(dataArray);
arrayBuffer(readable).then((data) => {
console.log(`from readable: ${data.byteLength}`);
});
Expand All @@ -1502,42 +1495,29 @@ added: v16.7.0
```mjs
import { blob } from 'node:stream/consumers';
import { Readable } from 'node:stream';

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);
const dataBlob = new Blob(['hello world from consumers!']);

const readable = Readable.from(items);
const readable = dataBlob.stream();
const data = await blob(readable);
console.log(`from readable: ${data.size}`);
```
```cjs
const { blob } = require('node:stream/consumers');
const { Readable } = require('node:stream');

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);
const dataBlob = new Blob(['hello world from consumers!']);

const readable = Readable.from(items);
const readable = dataBlob.stream();
blob(readable).then((data) => {
console.log(`from readable: ${data.size}`);
});
```
#### `streamConsumers.buffer(stream)`
<!-- YAML
added: v16.7.0
-->
\<!-added: v16.7.0
\-->
* `stream` {ReadableStream|stream.Readable|AsyncIterator}
* Returns: {Promise} Fulfills with a {Buffer} containing the full
Expand All @@ -1546,31 +1526,23 @@ added: v16.7.0
```mjs
import { buffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';
import { Buffer } from 'node:buffer';

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);
const dataBuffer = Buffer.from('hello world from consumers!');

const readable = Readable.from(items);
const readable = Readable.from(dataBuffer);
const data = await buffer(readable);
console.log(`from readable: ${data.length}`);
```
```cjs
const { buffer } = require('node:stream/consumers');
const { Readable } = require('node:stream');
const { Buffer } = require('node:buffer');

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);
const dataBuffer = Buffer.from('hello world from consumers!');

const readable = Readable.from(items);
const readable = Readable.from(dataBuffer);
buffer(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
Expand Down Expand Up @@ -1617,7 +1589,7 @@ const items = Array.from(
})
);

const readable = Readable.from(items);
const readable = Readable.from(JSON.stringify(items));
json(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
Expand All @@ -1637,14 +1609,7 @@ added: v16.7.0
import { json, text, blob, buffer } from 'node:stream/consumers';
import { Readable } from 'node:stream';

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);

const readable = Readable.from(items);
const readable = Readable.from('Hello world from consumers!');
const data = await text(readable);
console.log(`from readable: ${data.length}`);
```
Expand All @@ -1653,14 +1618,7 @@ console.log(`from readable: ${data.length}`);
const { text } = require('node:stream/consumers');
const { Readable } = require('node:stream');

const items = Array.from(
{
length: 100
},
() => 'hello world from consumers!'
);

const readable = Readable.from(items);
const readable = Readable.from('Hello world from consumers!');
text(readable).then((data) => {
console.log(`from readable: ${data.length}`);
});
Expand Down

0 comments on commit 0ee26a1

Please sign in to comment.