Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x backport] assert & util PRs #19230

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
314 changes: 248 additions & 66 deletions doc/api/assert.md

Large diffs are not rendered by default.

16 changes: 6 additions & 10 deletions doc/api/deprecations.md
Expand Up @@ -774,19 +774,15 @@ Type: Runtime
cause a lot of issues. See https://github.com/nodejs/node/issues/14328 for more
details.

<a id="DEP0098"></a>
### DEP0098: AsyncHooks Embedder AsyncResource.emit{Before,After} APIs
<a id="DEP0089"></a>
### DEP0089: require('assert')

Type: Runtime

The embedded API provided by AsyncHooks exposes emit{Before,After} methods
which are very easy to use incorrectly which can lead to unrecoverable errors.
Type: Documentation-only

Use [`asyncResource.runInAsyncScope()`][] API instead which provides a much
safer, and more convenient, alternative. See
https://github.com/nodejs/node/pull/18513 for more details.
Importing assert directly is not recommended as the exposed functions will use
loose equality checks. Use `require('assert').strict` instead. The API is the
same as the legacy assert but it will always use strict equality checks.

[`--pending-deprecation`]: cli.html#cli_pending_deprecation
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
Expand Down
26 changes: 26 additions & 0 deletions doc/api/tty.md
Expand Up @@ -121,6 +121,32 @@ added: v0.7.7
A `number` specifying the number of rows the TTY currently has. This property
is updated whenever the `'resize'` event is emitted.

### writeStream.getColorDepth([env])
<!-- YAML
added: REPLACEME
-->

* `env` {object} A object containing the environment variables to check.
Defaults to `process.env`.
* Returns: {number}

Returns:
* 1 for 2,
* 4 for 16,
* 8 for 256,
* 24 for 16,777,216
colors supported.

Use this to determine what colors the terminal supports. Due to the nature of
colors in terminals it is possible to either have false positives or false
negatives. It depends on process information and the environment variables that
may lie about what terminal is used.
To enforce a specific behavior without relying on `process.env` it is possible
to pass in an object with different settings.

Use the `NODE_DISABLE_COLORS` environment variable to enforce this function to
always return 1.

## tty.isatty(fd)
<!-- YAML
added: v0.5.8
Expand Down
70 changes: 68 additions & 2 deletions doc/api/util.md
Expand Up @@ -316,6 +316,9 @@ stream.write('With ES6');
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/REPLACEME
description: The `compact` option is supported now.
- version: v6.6.0
pr-url: https://github.com/nodejs/node/pull/8174
description: Custom inspection functions can now return `this`.
Expand All @@ -342,8 +345,7 @@ changes:
codes. Defaults to `false`. Colors are customizable, see
[Customizing `util.inspect` colors][].
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)`
functions exported on the `object` being inspected will not be called.
Defaults to `true`.
functions will not be called. Defaults to `true`.
* `showProxy` {boolean} If `true`, then objects and functions that are
`Proxy` objects will be introspected to show their `target` and `handler`
objects. Defaults to `false`.
Expand All @@ -354,6 +356,13 @@ changes:
* `breakLength` {number} The length at which an object's keys are split
across multiple lines. Set to `Infinity` to format an object as a single
line. Defaults to 60 for legacy compatibility.
* `compact` {boolean} Setting this to `false` changes the default indentation
to use a line break for each object key instead of lining up multiple
properties in one line. It will also break text that is above the
`breakLength` size into smaller and better readable chunks and indents
objects the same as arrays. Note that no text will be reduced below 16
characters, no matter the `breakLength` size. For more information, see the
example below. Defaults to `true`.

The `util.inspect()` method returns a string representation of `object` that is
primarily useful for debugging. Additional `options` may be passed that alter
Expand All @@ -371,6 +380,63 @@ Values may supply their own custom `inspect(depth, opts)` functions, when
called these receive the current `depth` in the recursive inspection, as well as
the options object passed to `util.inspect()`.

The following example highlights the difference with the `compact` option:

```js
const util = require('util');

const o = {
a: [1, 2, [[
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +
'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'test',
'foo']], 4],
b: new Map([['za', 1], ['zb', 'test']])
};
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));

// This will print

// { a:
// [ 1,
// 2,
// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line
// 'test',
// 'foo' ] ],
// 4 ],
// b: Map { 'za' => 1, 'zb' => 'test' } }

// Setting `compact` to false changes the output to be more reader friendly.
console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));

// {
// a: [
// 1,
// 2,
// [
// [
// 'Lorem ipsum dolor sit amet, consectetur ' +
// 'adipiscing elit, sed do eiusmod tempor ' +
// 'incididunt ut labore et dolore magna ' +
// 'aliqua.,
// 'test',
// 'foo'
// ]
// ],
// 4
// ],
// b: Map {
// 'za' => 1,
// 'zb' => 'test'
// }
// }

// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a
// single line.
// Reducing the `breakLength` will split the "Lorem ipsum" text in smaller
// chunks.
```

### Customizing `util.inspect` colors

<!-- type=misc -->
Expand Down