Skip to content

Commit 4e68760

Browse files
vsemozhetbytjasnell
authored andcommitted
doc: use destructuring in code examples
PR-URL: #13349 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c208f9d commit 4e68760

8 files changed

+49
-49
lines changed

doc/api/buffer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ sensitive data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` t
26092609
Example:
26102610

26112611
```js
2612-
const SlowBuffer = require('buffer').SlowBuffer;
2612+
const { SlowBuffer } = require('buffer');
26132613

26142614
const buf = new SlowBuffer(5);
26152615

doc/api/child_process.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ a manner that is similar, but not identical, to popen(3). This capability
77
is primarily provided by the [`child_process.spawn()`][] function:
88

99
```js
10-
const spawn = require('child_process').spawn;
10+
const { spawn } = require('child_process');
1111
const ls = spawn('ls', ['-lh', '/usr']);
1212

1313
ls.stdout.on('data', (data) => {
@@ -87,7 +87,7 @@ spaces it needs to be quoted.
8787

8888
```js
8989
// On Windows Only ...
90-
const spawn = require('child_process').spawn;
90+
const { spawn } = require('child_process');
9191
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
9292

9393
bat.stdout.on('data', (data) => {
@@ -105,7 +105,7 @@ bat.on('exit', (code) => {
105105

106106
```js
107107
// OR...
108-
const exec = require('child_process').exec;
108+
const { exec } = require('child_process');
109109
exec('my.bat', (err, stdout, stderr) => {
110110
if (err) {
111111
console.error(err);
@@ -168,7 +168,7 @@ containing shell metacharacters may be used to trigger arbitrary command
168168
execution.
169169

170170
```js
171-
const exec = require('child_process').exec;
171+
const { exec } = require('child_process');
172172
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
173173
if (error) {
174174
console.error(`exec error: ${error}`);
@@ -264,7 +264,7 @@ The same options as [`child_process.exec()`][] are supported. Since a shell is n
264264
spawned, behaviors such as I/O redirection and file globbing are not supported.
265265

266266
```js
267-
const execFile = require('child_process').execFile;
267+
const { execFile } = require('child_process');
268268
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
269269
if (error) {
270270
throw error;
@@ -410,7 +410,7 @@ Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the
410410
exit code:
411411

412412
```js
413-
const spawn = require('child_process').spawn;
413+
const { spawn } = require('child_process');
414414
const ls = spawn('ls', ['-lh', '/usr']);
415415

416416
ls.stdout.on('data', (data) => {
@@ -430,7 +430,7 @@ ls.on('close', (code) => {
430430
Example: A very elaborate way to run `ps ax | grep ssh`
431431

432432
```js
433-
const spawn = require('child_process').spawn;
433+
const { spawn } = require('child_process');
434434
const ps = spawn('ps', ['ax']);
435435
const grep = spawn('grep', ['ssh']);
436436

@@ -468,7 +468,7 @@ grep.on('close', (code) => {
468468
Example of checking for failed exec:
469469

470470
```js
471-
const spawn = require('child_process').spawn;
471+
const { spawn } = require('child_process');
472472
const child = spawn('bad_command');
473473

474474
child.on('error', (err) => {
@@ -515,7 +515,7 @@ Example of a long-running process, by detaching and also ignoring its parent
515515
`stdio` file descriptors, in order to ignore the parent's termination:
516516

517517
```js
518-
const spawn = require('child_process').spawn;
518+
const { spawn } = require('child_process');
519519

520520
const child = spawn(process.argv[0], ['child_program.js'], {
521521
detached: true,
@@ -529,7 +529,7 @@ Alternatively one can redirect the child process' output into files:
529529

530530
```js
531531
const fs = require('fs');
532-
const spawn = require('child_process').spawn;
532+
const { spawn } = require('child_process');
533533
const out = fs.openSync('./out.log', 'a');
534534
const err = fs.openSync('./out.log', 'a');
535535

@@ -601,7 +601,7 @@ pipes between the parent and child. The value is one of the following:
601601
Example:
602602

603603
```js
604-
const spawn = require('child_process').spawn;
604+
const { spawn } = require('child_process');
605605

606606
// Child will use parent's stdios
607607
spawn('prg', [], { stdio: 'inherit' });
@@ -933,7 +933,7 @@ is given, the process will be sent the `'SIGTERM'` signal. See signal(7) for
933933
a list of available signals.
934934

935935
```js
936-
const spawn = require('child_process').spawn;
936+
const { spawn } = require('child_process');
937937
const grep = spawn('grep', ['ssh']);
938938

939939
grep.on('close', (code, signal) => {
@@ -963,7 +963,7 @@ as in this example:
963963

964964
```js
965965
'use strict';
966-
const spawn = require('child_process').spawn;
966+
const { spawn } = require('child_process');
967967

968968
const child = spawn(
969969
'sh',
@@ -994,7 +994,7 @@ Returns the process identifier (PID) of the child process.
994994
Example:
995995

996996
```js
997-
const spawn = require('child_process').spawn;
997+
const { spawn } = require('child_process');
998998
const grep = spawn('grep', ['ssh']);
999999

10001000
console.log(`Spawned child pid: ${grep.pid}`);

doc/api/console.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ changes:
6565

6666
The `Console` class can be used to create a simple logger with configurable
6767
output streams and can be accessed using either `require('console').Console`
68-
or `console.Console`:
68+
or `console.Console` (or their destructured counterparts):
6969

7070
```js
71-
const Console = require('console').Console;
71+
const { Console } = require('console');
7272
```
7373

7474
```js
75-
const Console = console.Console;
75+
const { Console } = console;
7676
```
7777

7878
### new Console(stdout[, stderr])

doc/api/repl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ translation of text from one language to another:
172172

173173
```js
174174
const repl = require('repl');
175-
const Translator = require('translator').Translator;
175+
const { Translator } = require('translator');
176176

177177
const myTranslator = new Translator('en', 'fr');
178178

doc/api/stream.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ section for more information.
10231023
// Pull off a header delimited by \n\n
10241024
// use unshift() if we get too much
10251025
// Call the callback with (error, header, stream)
1026-
const StringDecoder = require('string_decoder').StringDecoder;
1026+
const { StringDecoder } = require('string_decoder');
10271027
function parseHeader(stream, callback) {
10281028
stream.on('error', callback);
10291029
stream.on('readable', onReadable);
@@ -1087,8 +1087,8 @@ libraries.
10871087
For example:
10881088

10891089
```js
1090-
const OldReader = require('./old-api-module.js').OldReader;
1091-
const Readable = require('stream').Readable;
1090+
const { OldReader } = require('./old-api-module.js');
1091+
const { Readable } = require('stream');
10921092
const oreader = new OldReader();
10931093
const myReader = new Readable().wrap(oreader);
10941094

@@ -1170,7 +1170,7 @@ of the four basic stream classes (`stream.Writable`, `stream.Readable`,
11701170
parent class constructor:
11711171

11721172
```js
1173-
const Writable = require('stream').Writable;
1173+
const { Writable } = require('stream');
11741174

11751175
class MyWritable extends Writable {
11761176
constructor(options) {
@@ -1264,7 +1264,7 @@ objects and passing appropriate methods as constructor options.
12641264
For example:
12651265

12661266
```js
1267-
const Writable = require('stream').Writable;
1267+
const { Writable } = require('stream');
12681268

12691269
const myWritable = new Writable({
12701270
write(chunk, encoding, callback) {
@@ -1307,7 +1307,7 @@ constructor and implement the `writable._write()` method. The
13071307
For example:
13081308

13091309
```js
1310-
const Writable = require('stream').Writable;
1310+
const { Writable } = require('stream');
13111311

13121312
class MyWritable extends Writable {
13131313
constructor(options) {
@@ -1321,7 +1321,7 @@ class MyWritable extends Writable {
13211321
Or, when using pre-ES6 style constructors:
13221322

13231323
```js
1324-
const Writable = require('stream').Writable;
1324+
const { Writable } = require('stream');
13251325
const util = require('util');
13261326

13271327
function MyWritable(options) {
@@ -1335,7 +1335,7 @@ util.inherits(MyWritable, Writable);
13351335
Or, using the Simplified Constructor approach:
13361336

13371337
```js
1338-
const Writable = require('stream').Writable;
1338+
const { Writable } = require('stream');
13391339

13401340
const myWritable = new Writable({
13411341
write(chunk, encoding, callback) {
@@ -1449,7 +1449,7 @@ on how the stream is being used. Using the callback ensures consistent and
14491449
predictable handling of errors.
14501450

14511451
```js
1452-
const Writable = require('stream').Writable;
1452+
const { Writable } = require('stream');
14531453

14541454
const myWritable = new Writable({
14551455
write(chunk, encoding, callback) {
@@ -1470,7 +1470,7 @@ is not of any real particular usefulness, the example illustrates each of the
14701470
required elements of a custom [Writable][] stream instance:
14711471

14721472
```js
1473-
const Writable = require('stream').Writable;
1473+
const { Writable } = require('stream');
14741474

14751475
class MyWritable extends Writable {
14761476
constructor(options) {
@@ -1514,7 +1514,7 @@ constructor and implement the `readable._read()` method.
15141514
For example:
15151515

15161516
```js
1517-
const Readable = require('stream').Readable;
1517+
const { Readable } = require('stream');
15181518

15191519
class MyReadable extends Readable {
15201520
constructor(options) {
@@ -1528,7 +1528,7 @@ class MyReadable extends Readable {
15281528
Or, when using pre-ES6 style constructors:
15291529

15301530
```js
1531-
const Readable = require('stream').Readable;
1531+
const { Readable } = require('stream');
15321532
const util = require('util');
15331533

15341534
function MyReadable(options) {
@@ -1542,7 +1542,7 @@ util.inherits(MyReadable, Readable);
15421542
Or, using the Simplified Constructor approach:
15431543

15441544
```js
1545-
const Readable = require('stream').Readable;
1545+
const { Readable } = require('stream');
15461546

15471547
const myReadable = new Readable({
15481548
read(size) {
@@ -1661,7 +1661,7 @@ consistent and predictable handling of errors.
16611661

16621662
<!-- eslint-disable no-useless-return -->
16631663
```js
1664-
const Readable = require('stream').Readable;
1664+
const { Readable } = require('stream');
16651665

16661666
const myReadable = new Readable({
16671667
read(size) {
@@ -1682,7 +1682,7 @@ The following is a basic example of a Readable stream that emits the numerals
16821682
from 1 to 1,000,000 in ascending order, and then ends.
16831683

16841684
```js
1685-
const Readable = require('stream').Readable;
1685+
const { Readable } = require('stream');
16861686

16871687
class Counter extends Readable {
16881688
constructor(opt) {
@@ -1739,7 +1739,7 @@ constructor and implement *both* the `readable._read()` and
17391739
For example:
17401740

17411741
```js
1742-
const Duplex = require('stream').Duplex;
1742+
const { Duplex } = require('stream');
17431743

17441744
class MyDuplex extends Duplex {
17451745
constructor(options) {
@@ -1752,7 +1752,7 @@ class MyDuplex extends Duplex {
17521752
Or, when using pre-ES6 style constructors:
17531753

17541754
```js
1755-
const Duplex = require('stream').Duplex;
1755+
const { Duplex } = require('stream');
17561756
const util = require('util');
17571757

17581758
function MyDuplex(options) {
@@ -1766,7 +1766,7 @@ util.inherits(MyDuplex, Duplex);
17661766
Or, using the Simplified Constructor approach:
17671767

17681768
```js
1769-
const Duplex = require('stream').Duplex;
1769+
const { Duplex } = require('stream');
17701770

17711771
const myDuplex = new Duplex({
17721772
read(size) {
@@ -1789,7 +1789,7 @@ incoming written data via the [Writable][] interface that is read back out
17891789
via the [Readable][] interface.
17901790

17911791
```js
1792-
const Duplex = require('stream').Duplex;
1792+
const { Duplex } = require('stream');
17931793
const kSource = Symbol('source');
17941794

17951795
class MyDuplex extends Duplex {
@@ -1830,7 +1830,7 @@ that accepts JavaScript numbers that are converted to hexadecimal strings on
18301830
the Readable side.
18311831

18321832
```js
1833-
const Transform = require('stream').Transform;
1833+
const { Transform } = require('stream');
18341834

18351835
// All Transform streams are also Duplex Streams
18361836
const myTransform = new Transform({
@@ -1895,7 +1895,7 @@ the output on the Readable side is not consumed.
18951895
For example:
18961896

18971897
```js
1898-
const Transform = require('stream').Transform;
1898+
const { Transform } = require('stream');
18991899

19001900
class MyTransform extends Transform {
19011901
constructor(options) {
@@ -1908,7 +1908,7 @@ class MyTransform extends Transform {
19081908
Or, when using pre-ES6 style constructors:
19091909

19101910
```js
1911-
const Transform = require('stream').Transform;
1911+
const { Transform } = require('stream');
19121912
const util = require('util');
19131913

19141914
function MyTransform(options) {
@@ -1922,7 +1922,7 @@ util.inherits(MyTransform, Transform);
19221922
Or, using the Simplified Constructor approach:
19231923

19241924
```js
1925-
const Transform = require('stream').Transform;
1925+
const { Transform } = require('stream');
19261926

19271927
const myTransform = new Transform({
19281928
transform(chunk, encoding, callback) {

doc/api/string_decoder.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
77
characters. It can be accessed using:
88

99
```js
10-
const StringDecoder = require('string_decoder').StringDecoder;
10+
const { StringDecoder } = require('string_decoder');
1111
```
1212

1313
The following example shows the basic use of the `StringDecoder` class.
1414

1515
```js
16-
const StringDecoder = require('string_decoder').StringDecoder;
16+
const { StringDecoder } = require('string_decoder');
1717
const decoder = new StringDecoder('utf8');
1818

1919
const cent = Buffer.from([0xC2, 0xA2]);
@@ -32,7 +32,7 @@ In the following example, the three UTF-8 encoded bytes of the European Euro
3232
symbol (``) are written over three separate operations:
3333

3434
```js
35-
const StringDecoder = require('string_decoder').StringDecoder;
35+
const { StringDecoder } = require('string_decoder');
3636
const decoder = new StringDecoder('utf8');
3737

3838
decoder.write(Buffer.from([0xE2]));

0 commit comments

Comments
 (0)