Skip to content

Commit 3518ab9

Browse files
thefourtheyervagg
authored andcommitted
doc: minor improvements to util.md
PR-URL: #6932 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Jefe Lindstaedt <robert.lindstaedt@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 8c289df commit 3518ab9

File tree

1 file changed

+79
-77
lines changed

1 file changed

+79
-77
lines changed

doc/api/util.md

Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ const util = require('util');
1919
The `util.debuglog()` method is used to create a function that conditionally
2020
writes debug messages to `stderr` based on the existence of the `NODE_DEBUG`
2121
environment variable. If the `section` name appears within the value of that
22-
environment variable, then the returned function operate similar to
22+
environment variable, then the returned function operates similar to
2323
`console.error()`. If not, then the returned function is a no-op.
2424

2525
For example:
2626

2727
```js
28+
const util = require('util');
2829
const debuglog = util.debuglog('foo');
2930

3031
debuglog('hello from foo [%d]', 123);
@@ -46,7 +47,7 @@ environment variable. For example: `NODE_DEBUG=fs,net,tls`.
4647
## util.deprecate(function, string)
4748

4849
The `util.deprecate()` method wraps the given `function` in such a way that
49-
marks it as being deprecated.
50+
it is marked as deprecated.
5051

5152
```js
5253
const util = require('util');
@@ -58,10 +59,10 @@ exports.puts = util.deprecate(() => {
5859
}, 'util.puts: Use console.log instead');
5960
```
6061

61-
When called, `util.deprecated()` will return a function that will emit a
62+
When called, `util.deprecate()` will return a function that will emit a
6263
`DeprecationWarning` using the `process.on('warning')` event. By default,
63-
this warning will be emitted and printed to `stderr` exactly once the first
64-
time that it is called. After the warning is emitted, the wrapped `function`
64+
this warning will be emitted and printed to `stderr` exactly once, the first
65+
time it is called. After the warning is emitted, the wrapped `function`
6566
is called.
6667

6768
If either the `--no-deprecation` or `--no-warnings` command line flags are
@@ -106,10 +107,10 @@ util.format('%s:%s', 'foo');
106107
// Returns 'foo:%s'
107108
```
108109

109-
If there are more arguments passed to the `util.format()` method than there are
110-
placeholders, the extra arguments are coerced into strings (for objects and
111-
symbols, `util.inspect()` is used) then concatenated to the returned string,
112-
delimited by a space.
110+
If there are more arguments passed to the `util.format()` method than the
111+
number of placeholders, the extra arguments are coerced into strings (for
112+
objects and symbols, `util.inspect()` is used) then concatenated to the
113+
returned string, each delimited by a space.
113114

114115
```js
115116
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
@@ -125,7 +126,7 @@ util.format(1, 2, 3); // '1 2 3'
125126

126127
## util.inherits(constructor, superConstructor)
127128

128-
_Note: usage of util.inherits() is discouraged. Please use the ES6 `class` and
129+
_Note: usage of `util.inherits()` is discouraged. Please use the ES6 `class` and
129130
`extends` keywords to get language level inheritance support. Also note that
130131
the two styles are [semantically incompatible][]._
131132

@@ -153,7 +154,7 @@ MyStream.prototype.write = function(data) {
153154
this.emit('data', data);
154155
}
155156

156-
var stream = new MyStream();
157+
const stream = new MyStream();
157158

158159
console.log(stream instanceof EventEmitter); // true
159160
console.log(MyStream.super_ === EventEmitter); // true
@@ -168,20 +169,19 @@ stream.write('It works!'); // Received data: "It works!"
168169

169170
* `object` {any} Any JavaScript primitive or Object.
170171
* `options` {Object}
171-
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable and
172-
symbol properties will be included in the formatted result. Defaults to
173-
`false`.
174-
* `depth` (number) Specifies how many times to recurse while formatting the
175-
`object`. This is useful for inspecting large complicated objects. Defaults
176-
to `2`. To make it recurse indefinitely pass `null`.
172+
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
173+
properties will be included in the formatted result. Defaults to `false`.
174+
* `depth` {number} Specifies the number of times to recurse while formatting
175+
the `object`. This is useful for inspecting large complicated objects.
176+
Defaults to `2`. To make it recurse indefinitely pass `null`.
177177
* `colors` {boolean} If `true`, the output will be styled with ANSI color
178178
codes. Defaults to `false`. Colors are customizable, see
179179
[Customizing `util.inspect` colors][].
180180
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)`
181181
functions exported on the `object` being inspected will not be called.
182182
Defaults to `true`.
183183
* `showProxy` {boolean} If `true`, then objects and functions that are
184-
`Proxy` objects will be introspected to show their `target` and `hander`
184+
`Proxy` objects will be introspected to show their `target` and `handler`
185185
objects. Defaults to `false`.
186186
* `maxArrayLength` {number} Specifies the maximum number of array and
187187
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
@@ -243,7 +243,7 @@ Objects may also define their own `inspect(depth, opts)` function that
243243
```js
244244
const util = require('util');
245245

246-
var obj = { name: 'nate' };
246+
const obj = { name: 'nate' };
247247
obj.inspect = function(depth) {
248248
return `{${this.name}}`;
249249
};
@@ -257,7 +257,9 @@ return a value of any type that will be formatted accordingly by
257257
`util.inspect()`.
258258

259259
```js
260-
var obj = { foo: 'this will not show up in the inspect() output' };
260+
const util = require('util');
261+
262+
const obj = { foo: 'this will not show up in the inspect() output' };
261263
obj.inspect = function(depth) {
262264
return { bar: 'baz' };
263265
};
@@ -300,11 +302,11 @@ Returns `true` if the given `object` is an `Array`. Otherwise, returns `false`.
300302
```js
301303
const util = require('util');
302304

303-
util.isArray([])
305+
util.isArray([]);
304306
// true
305-
util.isArray(new Array)
307+
util.isArray(new Array);
306308
// true
307-
util.isArray({})
309+
util.isArray({});
308310
// false
309311
```
310312

@@ -319,11 +321,11 @@ Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
319321
```js
320322
const util = require('util');
321323

322-
util.isBoolean(1)
324+
util.isBoolean(1);
323325
// false
324-
util.isBoolean(0)
326+
util.isBoolean(0);
325327
// false
326-
util.isBoolean(false)
328+
util.isBoolean(false);
327329
// true
328330
```
329331

@@ -338,11 +340,11 @@ Returns `true` if the given `object` is a `Buffer`. Otherwise, returns `false`.
338340
```js
339341
const util = require('util');
340342

341-
util.isBuffer({ length: 0 })
343+
util.isBuffer({ length: 0 });
342344
// false
343-
util.isBuffer([])
345+
util.isBuffer([]);
344346
// false
345-
util.isBuffer(Buffer.from('hello world'))
347+
util.isBuffer(Buffer.from('hello world'));
346348
// true
347349
```
348350

@@ -357,11 +359,11 @@ Returns `true` if the given `object` is a `Date`. Otherwise, returns `false`.
357359
```js
358360
const util = require('util');
359361

360-
util.isDate(new Date())
362+
util.isDate(new Date());
361363
// true
362-
util.isDate(Date())
364+
util.isDate(Date());
363365
// false (without 'new' returns a String)
364-
util.isDate({})
366+
util.isDate({});
365367
// false
366368
```
367369

@@ -377,11 +379,11 @@ Returns `true` if the given `object` is an [`Error`][]. Otherwise, returns
377379
```js
378380
const util = require('util');
379381

380-
util.isError(new Error())
382+
util.isError(new Error());
381383
// true
382-
util.isError(new TypeError())
384+
util.isError(new TypeError());
383385
// true
384-
util.isError({ name: 'Error', message: 'an error occurred' })
386+
util.isError({ name: 'Error', message: 'an error occurred' });
385387
// false
386388
```
387389

@@ -413,13 +415,13 @@ Returns `true` if the given `object` is a `Function`. Otherwise, returns
413415
const util = require('util');
414416

415417
function Foo() {}
416-
var Bar = function() {};
418+
const Bar = function() {};
417419

418-
util.isFunction({})
420+
util.isFunction({});
419421
// false
420-
util.isFunction(Foo)
422+
util.isFunction(Foo);
421423
// true
422-
util.isFunction(Bar)
424+
util.isFunction(Bar);
423425
// true
424426
```
425427

@@ -435,11 +437,11 @@ Returns `true` if the given `object` is strictly `null`. Otherwise, returns
435437
```js
436438
const util = require('util');
437439

438-
util.isNull(0)
440+
util.isNull(0);
439441
// false
440-
util.isNull(undefined)
442+
util.isNull(undefined);
441443
// false
442-
util.isNull(null)
444+
util.isNull(null);
443445
// true
444446
```
445447

@@ -455,11 +457,11 @@ returns `false`.
455457
```js
456458
const util = require('util');
457459

458-
util.isNullOrUndefined(0)
460+
util.isNullOrUndefined(0);
459461
// false
460-
util.isNullOrUndefined(undefined)
462+
util.isNullOrUndefined(undefined);
461463
// true
462-
util.isNullOrUndefined(null)
464+
util.isNullOrUndefined(null);
463465
// true
464466
```
465467

@@ -474,13 +476,13 @@ Returns `true` if the given `object` is a `Number`. Otherwise, returns `false`.
474476
```js
475477
const util = require('util');
476478

477-
util.isNumber(false)
479+
util.isNumber(false);
478480
// false
479-
util.isNumber(Infinity)
481+
util.isNumber(Infinity);
480482
// true
481-
util.isNumber(0)
483+
util.isNumber(0);
482484
// true
483-
util.isNumber(NaN)
485+
util.isNumber(NaN);
484486
// true
485487
```
486488

@@ -496,13 +498,13 @@ Returns `true` if the given `object` is strictly an `Object` __and__ not a
496498
```js
497499
const util = require('util');
498500

499-
util.isObject(5)
501+
util.isObject(5);
500502
// false
501-
util.isObject(null)
503+
util.isObject(null);
502504
// false
503-
util.isObject({})
505+
util.isObject({});
504506
// true
505-
util.isObject(function(){})
507+
util.isObject(function(){});
506508
// false
507509
```
508510

@@ -518,23 +520,23 @@ Returns `true` if the given `object` is a primitive type. Otherwise, returns
518520
```js
519521
const util = require('util');
520522

521-
util.isPrimitive(5)
523+
util.isPrimitive(5);
522524
// true
523-
util.isPrimitive('foo')
525+
util.isPrimitive('foo');
524526
// true
525-
util.isPrimitive(false)
527+
util.isPrimitive(false);
526528
// true
527-
util.isPrimitive(null)
529+
util.isPrimitive(null);
528530
// true
529-
util.isPrimitive(undefined)
531+
util.isPrimitive(undefined);
530532
// true
531-
util.isPrimitive({})
533+
util.isPrimitive({});
532534
// false
533-
util.isPrimitive(function() {})
535+
util.isPrimitive(function() {});
534536
// false
535-
util.isPrimitive(/^$/)
537+
util.isPrimitive(/^$/);
536538
// false
537-
util.isPrimitive(new Date())
539+
util.isPrimitive(new Date());
538540
// false
539541
```
540542

@@ -549,11 +551,11 @@ Returns `true` if the given `object` is a `RegExp`. Otherwise, returns `false`.
549551
```js
550552
const util = require('util');
551553

552-
util.isRegExp(/some regexp/)
554+
util.isRegExp(/some regexp/);
553555
// true
554-
util.isRegExp(new RegExp('another regexp'))
556+
util.isRegExp(new RegExp('another regexp'));
555557
// true
556-
util.isRegExp({})
558+
util.isRegExp({});
557559
// false
558560
```
559561

@@ -568,13 +570,13 @@ Returns `true` if the given `object` is a `string`. Otherwise, returns `false`.
568570
```js
569571
const util = require('util');
570572

571-
util.isString('')
573+
util.isString('');
572574
// true
573-
util.isString('foo')
575+
util.isString('foo');
574576
// true
575-
util.isString(String('foo'))
577+
util.isString(String('foo'));
576578
// true
577-
util.isString(5)
579+
util.isString(5);
578580
// false
579581
```
580582

@@ -589,11 +591,11 @@ Returns `true` if the given `object` is a `Symbol`. Otherwise, returns `false`.
589591
```js
590592
const util = require('util');
591593

592-
util.isSymbol(5)
594+
util.isSymbol(5);
593595
// false
594-
util.isSymbol('foo')
596+
util.isSymbol('foo');
595597
// false
596-
util.isSymbol(Symbol('foo'))
598+
util.isSymbol(Symbol('foo'));
597599
// true
598600
```
599601

@@ -608,12 +610,12 @@ Returns `true` if the given `object` is `undefined`. Otherwise, returns `false`.
608610
```js
609611
const util = require('util');
610612

611-
var foo;
612-
util.isUndefined(5)
613+
const foo = undefined;
614+
util.isUndefined(5);
613615
// false
614-
util.isUndefined(foo)
616+
util.isUndefined(foo);
615617
// true
616-
util.isUndefined(null)
618+
util.isUndefined(null);
617619
// false
618620
```
619621

@@ -627,7 +629,7 @@ The `util.log()` method prints the given `string` to `stdout` with an included
627629
timestamp.
628630

629631
```js
630-
const util = require('util')
632+
const util = require('util');
631633

632634
util.log('Timestamped message.');
633635
```

0 commit comments

Comments
 (0)