Browse Source

doc: make comment indentation consistent

Currently, some of the docs use different indentation for comments
in the code examples. This commit makes the indentation consistent
by putting the comments at the beginning of the line (really no
indentation that is).

PR-URL: https://github.com/nodejs/node/pull/9518
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
v6
Daniel Bevenius 8 years ago
parent
commit
367065be4b
  1. 42
      doc/api/addons.md
  2. 26
      doc/api/assert.md
  3. 16
      doc/api/buffer.md
  4. 14
      doc/api/crypto.md
  5. 2
      doc/api/events.md
  6. 8
      doc/api/fs.md
  7. 4
      doc/api/globals.md
  8. 64
      doc/api/path.md
  9. 118
      doc/api/util.md

42
doc/api/addons.md

@ -140,7 +140,8 @@ Once built, the binary Addon can be used from within Node.js by pointing
// hello.js // hello.js
const addon = require('./build/Release/addon'); const addon = require('./build/Release/addon');
console.log(addon.hello()); // 'world' console.log(addon.hello());
// Prints: 'world'
``` ```
Please see the examples below for further information or Please see the examples below for further information or
@ -372,7 +373,8 @@ To test it, run the following JavaScript:
const addon = require('./build/Release/addon'); const addon = require('./build/Release/addon');
addon((msg) => { addon((msg) => {
console.log(msg); // 'hello world' console.log(msg);
// Prints: 'hello world'
}); });
``` ```
@ -423,7 +425,8 @@ const addon = require('./build/Release/addon');
var obj1 = addon('hello'); var obj1 = addon('hello');
var obj2 = addon('world'); var obj2 = addon('world');
console.log(obj1.msg, obj2.msg); // 'hello world' console.log(obj1.msg, obj2.msg);
// Prints: 'hello world'
``` ```
@ -480,7 +483,8 @@ To test:
const addon = require('./build/Release/addon'); const addon = require('./build/Release/addon');
var fn = addon(); var fn = addon();
console.log(fn()); // 'hello world' console.log(fn());
// Prints: 'hello world'
``` ```
@ -642,9 +646,12 @@ Test it with:
const addon = require('./build/Release/addon'); const addon = require('./build/Release/addon');
var obj = new addon.MyObject(10); var obj = new addon.MyObject(10);
console.log(obj.plusOne()); // 11 console.log(obj.plusOne());
console.log(obj.plusOne()); // 12 // Prints: 11
console.log(obj.plusOne()); // 13 console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13
``` ```
### Factory of wrapped objects ### Factory of wrapped objects
@ -834,14 +841,20 @@ Test it with:
const createObject = require('./build/Release/addon'); const createObject = require('./build/Release/addon');
var obj = createObject(10); var obj = createObject(10);
console.log(obj.plusOne()); // 11 console.log(obj.plusOne());
console.log(obj.plusOne()); // 12 // Prints: 11
console.log(obj.plusOne()); // 13 console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13
var obj2 = createObject(20); var obj2 = createObject(20);
console.log(obj2.plusOne()); // 21 console.log(obj2.plusOne());
console.log(obj2.plusOne()); // 22 // Prints: 21
console.log(obj2.plusOne()); // 23 console.log(obj2.plusOne());
// Prints: 22
console.log(obj2.plusOne());
// Prints: 23
``` ```
@ -1013,7 +1026,8 @@ var obj1 = addon.createObject(10);
var obj2 = addon.createObject(20); var obj2 = addon.createObject(20);
var result = addon.add(obj1, obj2); var result = addon.add(obj1, obj2);
console.log(result); // 30 console.log(result);
// Prints: 30
``` ```
### AtExit hooks ### AtExit hooks

26
doc/api/assert.md

@ -22,8 +22,10 @@ An alias of [`assert.ok()`][] .
```js ```js
const assert = require('assert'); const assert = require('assert');
assert(true); // OK assert(true);
assert(1); // OK // OK
assert(1);
// OK
assert(false); assert(false);
// throws "AssertionError: false == true" // throws "AssertionError: false == true"
assert(0); assert(0);
@ -228,10 +230,14 @@ argument in callbacks.
```js ```js
const assert = require('assert'); const assert = require('assert');
assert.ifError(0); // OK assert.ifError(0);
assert.ifError(1); // Throws 1 // OK
assert.ifError('error'); // Throws 'error' assert.ifError(1);
assert.ifError(new Error()); // Throws Error // Throws 1
assert.ifError('error');
// Throws 'error'
assert.ifError(new Error());
// Throws Error
``` ```
## assert.notDeepEqual(actual, expected[, message]) ## assert.notDeepEqual(actual, expected[, message])
@ -271,7 +277,7 @@ assert.notDeepEqual(obj1, obj3);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } } // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
assert.notDeepEqual(obj1, obj4); assert.notDeepEqual(obj1, obj4);
// OK, obj1 and obj4 are not deeply equal // OK, obj1 and obj2 are not deeply equal
``` ```
If the values are deeply equal, an `AssertionError` is thrown with a `message` If the values are deeply equal, an `AssertionError` is thrown with a `message`
@ -364,8 +370,10 @@ parameter is `undefined`, a default error message is assigned.
```js ```js
const assert = require('assert'); const assert = require('assert');
assert.ok(true); // OK assert.ok(true);
assert.ok(1); // OK // OK
assert.ok(1);
// OK
assert.ok(false); assert.ok(false);
// throws "AssertionError: false == true" // throws "AssertionError: false == true"
assert.ok(0); assert.ok(0);

16
doc/api/buffer.md

@ -406,7 +406,7 @@ Example:
```js ```js
const buf = new Buffer(5); const buf = new Buffer(5);
// Prints (contents may vary): <Buffer 78 e0 82 02 01> // Prints: (contents may vary): <Buffer 78 e0 82 02 01>
console.log(buf); console.log(buf);
buf.fill(0); buf.fill(0);
@ -525,7 +525,7 @@ Example:
```js ```js
const buf = Buffer.allocUnsafe(5); const buf = Buffer.allocUnsafe(5);
// Prints (contents may vary): <Buffer 78 e0 82 02 01> // Prints: (contents may vary): <Buffer 78 e0 82 02 01>
console.log(buf); console.log(buf);
buf.fill(0); buf.fill(0);
@ -1755,12 +1755,12 @@ Examples:
```js ```js
const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
// Prints <Buffer 01 02 03 04 05 06 07 08> // Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf1); console.log(buf1);
buf1.swap32(); buf1.swap32();
// Prints <Buffer 04 03 02 01 08 07 06 05> // Prints: <Buffer 04 03 02 01 08 07 06 05>
console.log(buf1); console.log(buf1);
@ -1785,12 +1785,12 @@ Examples:
```js ```js
const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
// Prints <Buffer 01 02 03 04 05 06 07 08> // Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf1); console.log(buf1);
buf1.swap64(); buf1.swap64();
// Prints <Buffer 08 07 06 05 04 03 02 01> // Prints: <Buffer 08 07 06 05 04 03 02 01>
console.log(buf1); console.log(buf1);
@ -2327,7 +2327,7 @@ sequence cannot be adequately represented in the target encoding. For instance:
```js ```js
const newBuf = buffer.transcode(Buffer.from('€'), 'utf8', 'ascii'); const newBuf = buffer.transcode(Buffer.from('€'), 'utf8', 'ascii');
console.log(newBuf.toString('ascii')); console.log(newBuf.toString('ascii'));
// prints '?' // Prints: '?'
``` ```
Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced Because the Euro (`€`) sign is not representable in US-ASCII, it is replaced
@ -2397,7 +2397,7 @@ const SlowBuffer = require('buffer').SlowBuffer;
const buf = new SlowBuffer(5); const buf = new SlowBuffer(5);
// Prints (contents may vary): <Buffer 78 e0 82 02 01> // Prints: (contents may vary): <Buffer 78 e0 82 02 01>
console.log(buf); console.log(buf);
buf.fill(0); buf.fill(0);

14
doc/api/crypto.md

@ -73,7 +73,7 @@ const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow(); const spkac = getSpkacSomehow();
const challenge = cert.exportChallenge(spkac); const challenge = cert.exportChallenge(spkac);
console.log(challenge.toString('utf8')); console.log(challenge.toString('utf8'));
// Prints the challenge as a UTF8 string // Prints: the challenge as a UTF8 string
``` ```
### certificate.exportPublicKey(spkac) ### certificate.exportPublicKey(spkac)
@ -91,7 +91,7 @@ const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow(); const spkac = getSpkacSomehow();
const publicKey = cert.exportPublicKey(spkac); const publicKey = cert.exportPublicKey(spkac);
console.log(publicKey); console.log(publicKey);
// Prints the public key as <Buffer ...> // Prints: the public key as <Buffer ...>
``` ```
### certificate.verifySpkac(spkac) ### certificate.verifySpkac(spkac)
@ -106,7 +106,7 @@ The `spkac` argument must be a Node.js [`Buffer`][].
const cert = require('crypto').Certificate(); const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow(); const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(Buffer.from(spkac))); console.log(cert.verifySpkac(Buffer.from(spkac)));
// Prints true or false // Prints: true or false
``` ```
## Class: Cipher ## Class: Cipher
@ -839,7 +839,7 @@ sign.end();
const private_key = getPrivateKeySomehow(); const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); console.log(sign.sign(private_key, 'hex'));
// Prints the calculated signature // Prints: the calculated signature
``` ```
Example: Using the [`sign.update()`][] and [`sign.sign()`][] methods: Example: Using the [`sign.update()`][] and [`sign.sign()`][] methods:
@ -852,7 +852,7 @@ sign.update('some data to sign');
const private_key = getPrivateKeySomehow(); const private_key = getPrivateKeySomehow();
console.log(sign.sign(private_key, 'hex')); console.log(sign.sign(private_key, 'hex'));
// Prints the calculated signature // Prints: the calculated signature
``` ```
A `Sign` instance can also be created by just passing in the digest A `Sign` instance can also be created by just passing in the digest
@ -940,7 +940,7 @@ verify.end();
const public_key = getPublicKeySomehow(); const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify(); const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); console.log(verify.verify(public_key, signature));
// Prints true or false // Prints: true or false
``` ```
Example: Using the [`verify.update()`][] and [`verify.verify()`][] methods: Example: Using the [`verify.update()`][] and [`verify.verify()`][] methods:
@ -954,7 +954,7 @@ verify.update('some data to sign');
const public_key = getPublicKeySomehow(); const public_key = getPublicKeySomehow();
const signature = getSignatureToVerify(); const signature = getSignatureToVerify();
console.log(verify.verify(public_key, signature)); console.log(verify.verify(public_key, signature));
// Prints true or false // Prints: true or false
``` ```
### verifier.update(data[, input_encoding]) ### verifier.update(data[, input_encoding])

2
doc/api/events.md

@ -323,7 +323,7 @@ const sym = Symbol('symbol');
myEE.on(sym, () => {}); myEE.on(sym, () => {});
console.log(myEE.eventNames()); console.log(myEE.eventNames());
// Prints [ 'foo', 'bar', Symbol(symbol) ] // Prints: [ 'foo', 'bar', Symbol(symbol) ]
``` ```
### emitter.getMaxListeners() ### emitter.getMaxListeners()

8
doc/api/fs.md

@ -918,7 +918,7 @@ For example, the following program retains only the first four bytes of the file
```js ```js
console.log(fs.readFileSync('temp.txt', 'utf8')); console.log(fs.readFileSync('temp.txt', 'utf8'));
// prints Node.js // Prints: Node.js
// get the file descriptor of the file to be truncated // get the file descriptor of the file to be truncated
const fd = fs.openSync('temp.txt', 'r+'); const fd = fs.openSync('temp.txt', 'r+');
@ -928,7 +928,7 @@ fs.ftruncate(fd, 4, (err) => {
assert.ifError(err); assert.ifError(err);
console.log(fs.readFileSync('temp.txt', 'utf8')); console.log(fs.readFileSync('temp.txt', 'utf8'));
}); });
// prints Node // Prints: Node
``` ```
If the file previously was shorter than `len` bytes, it is extended, and the If the file previously was shorter than `len` bytes, it is extended, and the
@ -936,7 +936,7 @@ extended part is filled with null bytes ('\0'). For example,
```js ```js
console.log(fs.readFileSync('temp.txt', 'utf-8')); console.log(fs.readFileSync('temp.txt', 'utf-8'));
// prints Node.js // Prints: Node.js
// get the file descriptor of the file to be truncated // get the file descriptor of the file to be truncated
const fd = fs.openSync('temp.txt', 'r+'); const fd = fs.openSync('temp.txt', 'r+');
@ -946,7 +946,7 @@ fs.ftruncate(fd, 10, (err) => {
assert.ifError(!err); assert.ifError(!err);
console.log(fs.readFileSync('temp.txt')); console.log(fs.readFileSync('temp.txt'));
}); });
// prints <Buffer 4e 6f 64 65 2e 6a 73 00 00 00> // Prints: <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
// ('Node.js\0\0\0' in UTF8) // ('Node.js\0\0\0' in UTF8)
``` ```

4
doc/api/globals.md

@ -35,7 +35,7 @@ Example: running `node example.js` from `/Users/mjr`
```js ```js
console.log(__dirname); console.log(__dirname);
// /Users/mjr // Prints: /Users/mjr
``` ```
`__dirname` isn't actually a global but rather local to each module. `__dirname` isn't actually a global but rather local to each module.
@ -68,7 +68,7 @@ Example: running `node example.js` from `/Users/mjr`
```js ```js
console.log(__filename); console.log(__filename);
// /Users/mjr/example.js // Prints: /Users/mjr/example.js
``` ```
`__filename` isn't actually a global but rather local to each module. `__filename` isn't actually a global but rather local to each module.

64
doc/api/path.md

@ -24,14 +24,14 @@ On POSIX:
```js ```js
path.basename('C:\\temp\\myfile.html'); path.basename('C:\\temp\\myfile.html');
// returns 'C:\temp\myfile.html' // Returns: 'C:\temp\myfile.html'
``` ```
On Windows: On Windows:
```js ```js
path.basename('C:\\temp\\myfile.html'); path.basename('C:\\temp\\myfile.html');
// returns 'myfile.html' // Returns: 'myfile.html'
``` ```
To achieve consistent results when working with Windows file paths on any To achieve consistent results when working with Windows file paths on any
@ -41,7 +41,7 @@ On POSIX and Windows:
```js ```js
path.win32.basename('C:\\temp\\myfile.html'); path.win32.basename('C:\\temp\\myfile.html');
// returns 'myfile.html' // Returns: 'myfile.html'
``` ```
To achieve consistent results when working with POSIX file paths on any To achieve consistent results when working with POSIX file paths on any
@ -51,7 +51,7 @@ On POSIX and Windows:
```js ```js
path.posix.basename('/tmp/myfile.html'); path.posix.basename('/tmp/myfile.html');
// returns 'myfile.html' // Returns: 'myfile.html'
``` ```
## path.basename(path[, ext]) ## path.basename(path[, ext])
@ -70,10 +70,10 @@ For example:
```js ```js
path.basename('/foo/bar/baz/asdf/quux.html') path.basename('/foo/bar/baz/asdf/quux.html')
// returns 'quux.html' // Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html') path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns 'quux' // Returns: 'quux'
``` ```
A [`TypeError`][] is thrown if `path` is not a string or if `ext` is given A [`TypeError`][] is thrown if `path` is not a string or if `ext` is given
@ -95,20 +95,20 @@ For example, on POSIX:
```js ```js
console.log(process.env.PATH) console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin' // Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter) process.env.PATH.split(path.delimiter)
// returns ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin'] // Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
``` ```
On Windows: On Windows:
```js ```js
console.log(process.env.PATH) console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\node\' // Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
process.env.PATH.split(path.delimiter) process.env.PATH.split(path.delimiter)
// returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\'] // Returns: ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
``` ```
## path.dirname(path) ## path.dirname(path)
@ -126,7 +126,7 @@ For example:
```js ```js
path.dirname('/foo/bar/baz/asdf/quux') path.dirname('/foo/bar/baz/asdf/quux')
// returns '/foo/bar/baz/asdf' // Returns: '/foo/bar/baz/asdf'
``` ```
A [`TypeError`][] is thrown if `path` is not a string. A [`TypeError`][] is thrown if `path` is not a string.
@ -149,19 +149,19 @@ For example:
```js ```js
path.extname('index.html') path.extname('index.html')
// returns '.html' // Returns: '.html'
path.extname('index.coffee.md') path.extname('index.coffee.md')
// returns '.md' // Returns: '.md'
path.extname('index.') path.extname('index.')
// returns '.' // Returns: '.'
path.extname('index') path.extname('index')
// returns '' // Returns: ''
path.extname('.index') path.extname('.index')
// returns '' // Returns: ''
``` ```
A [`TypeError`][] is thrown if `path` is not a string. A [`TypeError`][] is thrown if `path` is not a string.
@ -205,7 +205,7 @@ path.format({
dir: '/home/user/dir', dir: '/home/user/dir',
base: 'file.txt' base: 'file.txt'
}); });
// returns '/home/user/dir/file.txt' // Returns: '/home/user/dir/file.txt'
// `root` will be used if `dir` is not specified. // `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the // If only `root` is provided or `dir` is equal to `root` then the
@ -214,7 +214,7 @@ path.format({
root: '/', root: '/',
base: 'file.txt' base: 'file.txt'
}); });
// returns '/file.txt' // Returns: '/file.txt'
// `name` + `ext` will be used if `base` is not specified. // `name` + `ext` will be used if `base` is not specified.
path.format({ path.format({
@ -222,13 +222,13 @@ path.format({
name: 'file', name: 'file',
ext: '.txt' ext: '.txt'
}); });
// returns '/file.txt' // Returns: '/file.txt'
// `base` will be returned if `dir` or `root` are not provided. // `base` will be returned if `dir` or `root` are not provided.
path.format({ path.format({
base: 'file.txt' base: 'file.txt'
}); });
// returns 'file.txt' // Returns: 'file.txt'
``` ```
On Windows: On Windows:
@ -241,7 +241,7 @@ path.format({
ext : ".txt", ext : ".txt",
name : "file" name : "file"
}); });
// returns 'C:\\path\\dir\\file.txt' // Returns: 'C:\\path\\dir\\file.txt'
``` ```
## path.isAbsolute(path) ## path.isAbsolute(path)
@ -298,7 +298,7 @@ For example:
```js ```js
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..') path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns '/foo/bar/baz/asdf' // Returns: '/foo/bar/baz/asdf'
path.join('foo', {}, 'bar') path.join('foo', {}, 'bar')
// throws TypeError: Arguments to path.join must be strings // throws TypeError: Arguments to path.join must be strings
@ -328,14 +328,14 @@ For example on POSIX:
```js ```js
path.normalize('/foo/bar//baz/asdf/quux/..') path.normalize('/foo/bar//baz/asdf/quux/..')
// returns '/foo/bar/baz/asdf' // Returns: '/foo/bar/baz/asdf'
``` ```
On Windows: On Windows:
```js ```js
path.normalize('C:\\temp\\\\foo\\bar\\..\\'); path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// returns 'C:\\temp\\foo\\' // Returns: 'C:\\temp\\foo\\'
``` ```
A [`TypeError`][] is thrown if `path` is not a string. A [`TypeError`][] is thrown if `path` is not a string.
@ -363,7 +363,7 @@ For example on POSIX:
```js ```js
path.parse('/home/user/dir/file.txt') path.parse('/home/user/dir/file.txt')
// returns // Returns:
// { // {
// root : "/", // root : "/",
// dir : "/home/user/dir", // dir : "/home/user/dir",
@ -387,7 +387,7 @@ On Windows:
```js ```js
path.parse('C:\\path\\dir\\file.txt') path.parse('C:\\path\\dir\\file.txt')
// returns // Returns:
// { // {
// root : "C:\\", // root : "C:\\",
// dir : "C:\\path\\dir", // dir : "C:\\path\\dir",
@ -439,14 +439,14 @@ For example on POSIX:
```js ```js
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb') path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns '../../impl/bbb' // Returns: '../../impl/bbb'
``` ```
On Windows: On Windows:
```js ```js
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb') path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns '..\\..\\impl\\bbb' // Returns: '..\\..\\impl\\bbb'
``` ```
A [`TypeError`][] is thrown if neither `from` nor `to` is a string. A [`TypeError`][] is thrown if neither `from` nor `to` is a string.
@ -482,10 +482,10 @@ For example:
```js ```js
path.resolve('/foo/bar', './baz') path.resolve('/foo/bar', './baz')
// returns '/foo/bar/baz' // Returns: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/') path.resolve('/foo/bar', '/tmp/file/')
// returns '/tmp/file' // Returns: '/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif') path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if the current working directory is /home/myself/node, // if the current working directory is /home/myself/node,
@ -510,14 +510,14 @@ For example on POSIX:
```js ```js
'foo/bar/baz'.split(path.sep) 'foo/bar/baz'.split(path.sep)
// returns ['foo', 'bar', 'baz'] // Returns: ['foo', 'bar', 'baz']
``` ```
On Windows: On Windows:
```js ```js
'foo\\bar\\baz'.split(path.sep) 'foo\\bar\\baz'.split(path.sep)
// returns ['foo', 'bar', 'baz'] // Returns: ['foo', 'bar', 'baz']
``` ```
## path.win32 ## path.win32

118
doc/api/util.md

@ -113,7 +113,7 @@ not replaced.
```js ```js
util.format('%s:%s', 'foo'); util.format('%s:%s', 'foo');
// Returns 'foo:%s' // Returns: 'foo:%s'
``` ```
If there are more arguments passed to the `util.format()` method than the If there are more arguments passed to the `util.format()` method than the
@ -310,7 +310,7 @@ class Box {
const box = new Box(true); const box = new Box(true);
util.inspect(box); util.inspect(box);
// "Box< true >" // Returns: "Box< true >"
``` ```
Custom `[util.inspect.custom](depth, opts)` functions typically return a string Custom `[util.inspect.custom](depth, opts)` functions typically return a string
@ -326,7 +326,7 @@ obj[util.inspect.custom] = function(depth) {
}; };
util.inspect(obj); util.inspect(obj);
// "{ bar: 'baz' }" // Returns: "{ bar: 'baz' }"
``` ```
A custom inspection method can alternatively be provided by exposing A custom inspection method can alternatively be provided by exposing
@ -341,7 +341,7 @@ obj.inspect = function(depth) {
}; };
util.inspect(obj); util.inspect(obj);
// "{ bar: 'baz' }" // Returns: "{ bar: 'baz' }"
``` ```
### util.inspect.defaultOptions ### util.inspect.defaultOptions
@ -419,11 +419,11 @@ Returns `true` if the given `object` is an `Array`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isArray([]); util.isArray([]);
// true // Returns: true
util.isArray(new Array); util.isArray(new Array);
// true // Returns: true
util.isArray({}); util.isArray({});
// false // Returns: false
``` ```
### util.isBoolean(object) ### util.isBoolean(object)
@ -442,11 +442,11 @@ Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isBoolean(1); util.isBoolean(1);
// false // Returns: false
util.isBoolean(0); util.isBoolean(0);
// false // Returns: false
util.isBoolean(false); util.isBoolean(false);
// true // Returns: true
``` ```
### util.isBuffer(object) ### util.isBuffer(object)
@ -465,11 +465,11 @@ Returns `true` if the given `object` is a `Buffer`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isBuffer({ length: 0 }); util.isBuffer({ length: 0 });
// false // Returns: false
util.isBuffer([]); util.isBuffer([]);
// false // Returns: false
util.isBuffer(Buffer.from('hello world')); util.isBuffer(Buffer.from('hello world'));
// true // Returns: true
``` ```
### util.isDate(object) ### util.isDate(object)
@ -488,11 +488,11 @@ Returns `true` if the given `object` is a `Date`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isDate(new Date()); util.isDate(new Date());
// true // Returns: true
util.isDate(Date()); util.isDate(Date());
// false (without 'new' returns a String) // false (without 'new' returns a String)
util.isDate({}); util.isDate({});
// false // Returns: false
``` ```
### util.isError(object) ### util.isError(object)
@ -512,11 +512,11 @@ Returns `true` if the given `object` is an [`Error`][]. Otherwise, returns
const util = require('util'); const util = require('util');
util.isError(new Error()); util.isError(new Error());
// true // Returns: true
util.isError(new TypeError()); util.isError(new TypeError());
// true // Returns: true
util.isError({ name: 'Error', message: 'an error occurred' }); util.isError({ name: 'Error', message: 'an error occurred' });
// false // Returns: false
``` ```
Note that this method relies on `Object.prototype.toString()` behavior. It is Note that this method relies on `Object.prototype.toString()` behavior. It is
@ -528,10 +528,10 @@ const util = require('util');
const obj = { name: 'Error', message: 'an error occurred' }; const obj = { name: 'Error', message: 'an error occurred' };
util.isError(obj); util.isError(obj);
// false // Returns: false
obj[Symbol.toStringTag] = 'Error'; obj[Symbol.toStringTag] = 'Error';
util.isError(obj); util.isError(obj);
// true // Returns: true
``` ```
### util.isFunction(object) ### util.isFunction(object)
@ -554,11 +554,11 @@ function Foo() {}
const Bar = function() {}; const Bar = function() {};
util.isFunction({}); util.isFunction({});
// false // Returns: false
util.isFunction(Foo); util.isFunction(Foo);
// true // Returns: true
util.isFunction(Bar); util.isFunction(Bar);
// true // Returns: true
``` ```
### util.isNull(object) ### util.isNull(object)
@ -578,11 +578,11 @@ Returns `true` if the given `object` is strictly `null`. Otherwise, returns
const util = require('util'); const util = require('util');
util.isNull(0); util.isNull(0);
// false // Returns: false
util.isNull(undefined); util.isNull(undefined);
// false // Returns: false
util.isNull(null); util.isNull(null);
// true // Returns: true
``` ```
### util.isNullOrUndefined(object) ### util.isNullOrUndefined(object)
@ -602,11 +602,11 @@ returns `false`.
const util = require('util'); const util = require('util');
util.isNullOrUndefined(0); util.isNullOrUndefined(0);
// false // Returns: false
util.isNullOrUndefined(undefined); util.isNullOrUndefined(undefined);
// true // Returns: true
util.isNullOrUndefined(null); util.isNullOrUndefined(null);
// true // Returns: true
``` ```
### util.isNumber(object) ### util.isNumber(object)
@ -625,13 +625,13 @@ Returns `true` if the given `object` is a `Number`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isNumber(false); util.isNumber(false);
// false // Returns: false
util.isNumber(Infinity); util.isNumber(Infinity);
// true // Returns: true
util.isNumber(0); util.isNumber(0);
// true // Returns: true
util.isNumber(NaN); util.isNumber(NaN);
// true // Returns: true
``` ```
### util.isObject(object) ### util.isObject(object)
@ -651,13 +651,13 @@ Returns `true` if the given `object` is strictly an `Object` **and** not a
const util = require('util'); const util = require('util');
util.isObject(5); util.isObject(5);
// false // Returns: false
util.isObject(null); util.isObject(null);
// false // Returns: false
util.isObject({}); util.isObject({});
// true // Returns: true
util.isObject(function(){}); util.isObject(function(){});
// false // Returns: false
``` ```
### util.isPrimitive(object) ### util.isPrimitive(object)
@ -677,23 +677,23 @@ Returns `true` if the given `object` is a primitive type. Otherwise, returns
const util = require('util'); const util = require('util');
util.isPrimitive(5); util.isPrimitive(5);
// true // Returns: true
util.isPrimitive('foo'); util.isPrimitive('foo');
// true // Returns: true
util.isPrimitive(false); util.isPrimitive(false);
// true // Returns: true
util.isPrimitive(null); util.isPrimitive(null);
// true // Returns: true
util.isPrimitive(undefined); util.isPrimitive(undefined);
// true // Returns: true
util.isPrimitive({}); util.isPrimitive({});
// false // Returns: false
util.isPrimitive(function() {}); util.isPrimitive(function() {});
// false // Returns: false
util.isPrimitive(/^$/); util.isPrimitive(/^$/);
// false // Returns: false
util.isPrimitive(new Date()); util.isPrimitive(new Date());
// false // Returns: false
``` ```
### util.isRegExp(object) ### util.isRegExp(object)
@ -712,11 +712,11 @@ Returns `true` if the given `object` is a `RegExp`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isRegExp(/some regexp/); util.isRegExp(/some regexp/);
// true // Returns: true
util.isRegExp(new RegExp('another regexp')); util.isRegExp(new RegExp('another regexp'));
// true // Returns: true
util.isRegExp({}); util.isRegExp({});
// false // Returns: false
``` ```
### util.isString(object) ### util.isString(object)
@ -735,13 +735,13 @@ Returns `true` if the given `object` is a `string`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isString(''); util.isString('');
// true // Returns: true
util.isString('foo'); util.isString('foo');
// true // Returns: true
util.isString(String('foo')); util.isString(String('foo'));
// true // Returns: true
util.isString(5); util.isString(5);
// false // Returns: false
``` ```
### util.isSymbol(object) ### util.isSymbol(object)
@ -760,11 +760,11 @@ Returns `true` if the given `object` is a `Symbol`. Otherwise, returns `false`.
const util = require('util'); const util = require('util');
util.isSymbol(5); util.isSymbol(5);
// false // Returns: false
util.isSymbol('foo'); util.isSymbol('foo');
// false // Returns: false
util.isSymbol(Symbol('foo')); util.isSymbol(Symbol('foo'));
// true // Returns: true
``` ```
### util.isUndefined(object) ### util.isUndefined(object)
@ -784,11 +784,11 @@ const util = require('util');
const foo = undefined; const foo = undefined;
util.isUndefined(5); util.isUndefined(5);
// false // Returns: false
util.isUndefined(foo); util.isUndefined(foo);
// true // Returns: true
util.isUndefined(null); util.isUndefined(null);
// false // Returns: false
``` ```
### util.log(string) ### util.log(string)

Loading…
Cancel
Save