Skip to content

Commit

Permalink
doc: use destructuring in code examples
Browse files Browse the repository at this point in the history
PR-URL: #13349
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
vsemozhetbyt authored and jasnell committed Jun 5, 2017
1 parent c208f9d commit 4e68760
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion doc/api/buffer.md
Expand Up @@ -2609,7 +2609,7 @@ sensitive data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` t
Example:

```js
const SlowBuffer = require('buffer').SlowBuffer;
const { SlowBuffer } = require('buffer');

const buf = new SlowBuffer(5);

Expand Down
28 changes: 14 additions & 14 deletions doc/api/child_process.md
Expand Up @@ -7,7 +7,7 @@ a manner that is similar, but not identical, to popen(3). This capability
is primarily provided by the [`child_process.spawn()`][] function:

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

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

```js
// On Windows Only ...
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c', 'my.bat']);

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

```js
// OR...
const exec = require('child_process').exec;
const { exec } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
Expand Down Expand Up @@ -168,7 +168,7 @@ containing shell metacharacters may be used to trigger arbitrary command
execution.

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

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

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

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

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const ps = spawn('ps', ['ax']);
const grep = spawn('grep', ['ssh']);

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

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const child = spawn('bad_command');

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

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');

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

```js
const fs = require('fs');
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const out = fs.openSync('./out.log', 'a');
const err = fs.openSync('./out.log', 'a');

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

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');

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

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const grep = spawn('grep', ['ssh']);

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

```js
'use strict';
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');

const child = spawn(
'sh',
Expand Down Expand Up @@ -994,7 +994,7 @@ Returns the process identifier (PID) of the child process.
Example:

```js
const spawn = require('child_process').spawn;
const { spawn } = require('child_process');
const grep = spawn('grep', ['ssh']);

console.log(`Spawned child pid: ${grep.pid}`);
Expand Down
6 changes: 3 additions & 3 deletions doc/api/console.md
Expand Up @@ -65,14 +65,14 @@ changes:

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

```js
const Console = require('console').Console;
const { Console } = require('console');
```

```js
const Console = console.Console;
const { Console } = console;
```

### new Console(stdout[, stderr])
Expand Down
2 changes: 1 addition & 1 deletion doc/api/repl.md
Expand Up @@ -172,7 +172,7 @@ translation of text from one language to another:

```js
const repl = require('repl');
const Translator = require('translator').Translator;
const { Translator } = require('translator');

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

Expand Down
46 changes: 23 additions & 23 deletions doc/api/stream.md
Expand Up @@ -1023,7 +1023,7 @@ section for more information.
// Pull off a header delimited by \n\n
// use unshift() if we get too much
// Call the callback with (error, header, stream)
const StringDecoder = require('string_decoder').StringDecoder;
const { StringDecoder } = require('string_decoder');
function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('readable', onReadable);
Expand Down Expand Up @@ -1087,8 +1087,8 @@ libraries.
For example:

```js
const OldReader = require('./old-api-module.js').OldReader;
const Readable = require('stream').Readable;
const { OldReader } = require('./old-api-module.js');
const { Readable } = require('stream');
const oreader = new OldReader();
const myReader = new Readable().wrap(oreader);

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

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

class MyWritable extends Writable {
constructor(options) {
Expand Down Expand Up @@ -1264,7 +1264,7 @@ objects and passing appropriate methods as constructor options.
For example:

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

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

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

class MyWritable extends Writable {
constructor(options) {
Expand All @@ -1321,7 +1321,7 @@ class MyWritable extends Writable {
Or, when using pre-ES6 style constructors:

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');
const util = require('util');

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

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

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

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

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

```js
const Writable = require('stream').Writable;
const { Writable } = require('stream');

class MyWritable extends Writable {
constructor(options) {
Expand Down Expand Up @@ -1514,7 +1514,7 @@ constructor and implement the `readable._read()` method.
For example:

```js
const Readable = require('stream').Readable;
const { Readable } = require('stream');

class MyReadable extends Readable {
constructor(options) {
Expand All @@ -1528,7 +1528,7 @@ class MyReadable extends Readable {
Or, when using pre-ES6 style constructors:

```js
const Readable = require('stream').Readable;
const { Readable } = require('stream');
const util = require('util');

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

```js
const Readable = require('stream').Readable;
const { Readable } = require('stream');

const myReadable = new Readable({
read(size) {
Expand Down Expand Up @@ -1661,7 +1661,7 @@ consistent and predictable handling of errors.

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

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

```js
const Readable = require('stream').Readable;
const { Readable } = require('stream');

class Counter extends Readable {
constructor(opt) {
Expand Down Expand Up @@ -1739,7 +1739,7 @@ constructor and implement *both* the `readable._read()` and
For example:

```js
const Duplex = require('stream').Duplex;
const { Duplex } = require('stream');

class MyDuplex extends Duplex {
constructor(options) {
Expand All @@ -1752,7 +1752,7 @@ class MyDuplex extends Duplex {
Or, when using pre-ES6 style constructors:

```js
const Duplex = require('stream').Duplex;
const { Duplex } = require('stream');
const util = require('util');

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

```js
const Duplex = require('stream').Duplex;
const { Duplex } = require('stream');

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

```js
const Duplex = require('stream').Duplex;
const { Duplex } = require('stream');
const kSource = Symbol('source');

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

```js
const Transform = require('stream').Transform;
const { Transform } = require('stream');

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

```js
const Transform = require('stream').Transform;
const { Transform } = require('stream');

class MyTransform extends Transform {
constructor(options) {
Expand All @@ -1908,7 +1908,7 @@ class MyTransform extends Transform {
Or, when using pre-ES6 style constructors:

```js
const Transform = require('stream').Transform;
const { Transform } = require('stream');
const util = require('util');

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

```js
const Transform = require('stream').Transform;
const { Transform } = require('stream');

const myTransform = new Transform({
transform(chunk, encoding, callback) {
Expand Down
6 changes: 3 additions & 3 deletions doc/api/string_decoder.md
Expand Up @@ -7,13 +7,13 @@ strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
characters. It can be accessed using:

```js
const StringDecoder = require('string_decoder').StringDecoder;
const { StringDecoder } = require('string_decoder');
```

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

```js
const StringDecoder = require('string_decoder').StringDecoder;
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');

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

```js
const StringDecoder = require('string_decoder').StringDecoder;
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');

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

0 comments on commit 4e68760

Please sign in to comment.