diff options
author | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2022-04-20 11:23:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-20 11:23:41 +0300 |
commit | 6afd3fcf653ed92063bafefa83661a076d241585 (patch) | |
tree | 9e1a22ab2f3d3cf1f14ec2348ca0a318fef16898 /doc/api/util.md | |
parent | 1fe5d56403a3725eac5e67c7a08830ce5ee0e2f5 (diff) |
doc: add `node:` prefix for all core modules
Some core modules can be loaded with or without the `node:` prefix.
Using the prefix disambiguates which specifiers refer to core modules.
This commit updates the docs to use the prefix everywhere a core module
is referenced.
PR-URL: https://github.com/nodejs/node/pull/42752
Fixes: https://github.com/nodejs/node/issues/38343
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Diffstat (limited to 'doc/api/util.md')
-rw-r--r-- | doc/api/util.md | 94 |
1 files changed, 47 insertions, 47 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index e235b8f338e..1686c08e0fb 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -6,12 +6,12 @@ <!-- source_link=lib/util.js --> -The `util` module supports the needs of Node.js internal APIs. Many of the +The `node:util` module supports the needs of Node.js internal APIs. Many of the utilities are useful for application and module developers as well. To access it: ```js -const util = require('util'); +const util = require('node:util'); ``` ## `util.callbackify(original)` @@ -30,7 +30,7 @@ first argument will be the rejection reason (or `null` if the `Promise` resolved), and the second argument will be the resolved value. ```js -const util = require('util'); +const util = require('node:util'); async function fn() { return 'hello world'; @@ -90,7 +90,7 @@ environment variable, then the returned function operates similar to [`console.error()`][]. If not, then the returned function is a no-op. ```js -const util = require('util'); +const util = require('node:util'); const debuglog = util.debuglog('foo'); debuglog('hello from foo [%d]', 123); @@ -109,7 +109,7 @@ environment variable set, then it will not print anything. The `section` supports wildcard also: ```js -const util = require('util'); +const util = require('node:util'); const debuglog = util.debuglog('foo-bar'); debuglog('hi there, it\'s foo-bar [%d]', 2333); @@ -130,7 +130,7 @@ with a different function that doesn't have any initialization or unnecessary wrapping. ```js -const util = require('util'); +const util = require('node:util'); let debuglog = util.debuglog('internals', (debug) => { // Replace with a logging function that optimizes out // testing if the section is enabled @@ -153,7 +153,7 @@ then the returned value will be `true`. If not, then the returned value will be `false`. ```js -const util = require('util'); +const util = require('node:util'); const enabled = util.debuglog('foo').enabled; if (enabled) { console.log('hello from foo [%d]', 123); @@ -197,7 +197,7 @@ The `util.deprecate()` method wraps `fn` (which may be a function or class) in such a way that it is marked as deprecated. ```js -const util = require('util'); +const util = require('node:util'); exports.obsoleteFunction = util.deprecate(() => { // Do something here. @@ -214,7 +214,7 @@ If the same optional `code` is supplied in multiple calls to `util.deprecate()`, the warning will be emitted only once for that `code`. ```js -const util = require('util'); +const util = require('node:util'); const fn1 = util.deprecate(someFunction, someMessage, 'DEP0001'); const fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001'); @@ -435,8 +435,8 @@ As an additional convenience, `superConstructor` will be accessible through the `constructor.super_` property. ```js -const util = require('util'); -const EventEmitter = require('events'); +const util = require('node:util'); +const EventEmitter = require('node:events'); function MyStream() { EventEmitter.call(this); @@ -462,7 +462,7 @@ stream.write('It works!'); // Received data: "It works!" ES6 example using `class` and `extends`: ```js -const EventEmitter = require('events'); +const EventEmitter = require('node:events'); class MyStream extends EventEmitter { write(data) { @@ -642,7 +642,7 @@ util.inspect(baz); // '[foo] {}' Circular references point to their anchor by using a reference index: ```js -const { inspect } = require('util'); +const { inspect } = require('node:util'); const obj = {}; obj.a = [obj]; @@ -660,7 +660,7 @@ console.log(inspect(obj)); The following example inspects all properties of the `util` object: ```js -const util = require('util'); +const util = require('node:util'); console.log(util.inspect(util, { showHidden: true, depth: null })); ``` @@ -668,7 +668,7 @@ console.log(util.inspect(util, { showHidden: true, depth: null })); The following example highlights the effect of the `compact` option: ```js -const util = require('util'); +const util = require('node:util'); const o = { a: [1, 2, [[ @@ -724,7 +724,7 @@ guarantee which entries are displayed. That means retrieving the same with no remaining strong references may be garbage collected at any time. ```js -const { inspect } = require('util'); +const { inspect } = require('node:util'); const obj = { a: 1 }; const obj2 = { b: 2 }; @@ -738,8 +738,8 @@ The `sorted` option ensures that an object's property insertion order does not impact the result of `util.inspect()`. ```js -const { inspect } = require('util'); -const assert = require('assert'); +const { inspect } = require('node:util'); +const assert = require('node:assert'); const o1 = { b: [2, 3, 1], @@ -766,7 +766,7 @@ The `numericSeparator` option adds an underscore every three digits to all numbers. ```js -const { inspect } = require('util'); +const { inspect } = require('node:util'); const thousand = 1_000; const million = 1_000_000; @@ -892,7 +892,7 @@ which `util.inspect()` will invoke and use the result of when inspecting the object. ```js -const util = require('util'); +const util = require('node:util'); class Box { constructor(value) { @@ -927,7 +927,7 @@ a string but may return a value of any type that will be formatted accordingly by `util.inspect()`. ```js -const util = require('util'); +const util = require('node:util'); const obj = { foo: 'this will not show up in the inspect() output' }; obj[util.inspect.custom] = (depth) => { @@ -996,7 +996,7 @@ object containing one or more valid [`util.inspect()`][] options. Setting option properties directly is also supported. ```js -const util = require('util'); +const util = require('node:util'); const arr = Array(101).fill(0); console.log(arr); // Logs the truncated array @@ -1034,8 +1034,8 @@ an `(err, value) => ...` callback as the last argument, and returns a version that returns promises. ```js -const util = require('util'); -const fs = require('fs'); +const util = require('node:util'); +const fs = require('node:fs'); const stat = util.promisify(fs.stat); stat('.').then((stats) => { @@ -1048,8 +1048,8 @@ stat('.').then((stats) => { Or, equivalently using `async function`s: ```js -const util = require('util'); -const fs = require('fs'); +const util = require('node:util'); +const fs = require('node:fs'); const stat = util.promisify(fs.stat); @@ -1072,7 +1072,7 @@ Using `promisify()` on class methods or other methods that use `this` may not work as expected unless handled specially: ```js -const util = require('util'); +const util = require('node:util'); class Foo { constructor() { @@ -1102,7 +1102,7 @@ Using the `util.promisify.custom` symbol one can override the return value of [`util.promisify()`][]: ```js -const util = require('util'); +const util = require('node:util'); function doSomething(foo, callback) { // ... @@ -1397,7 +1397,7 @@ added: v10.0.0 changes: - version: v15.3.0 pr-url: https://github.com/nodejs/node/pull/34055 - description: Exposed as `require('util/types')`. + description: Exposed as `require('node:util/types')`. --> `util.types` provides type checks for different kinds of built-in objects. @@ -1409,7 +1409,7 @@ The result generally does not make any guarantees about what kinds of properties or behavior a value exposes in JavaScript. They are primarily useful for addon developers who prefer to do type checking in JavaScript. -The API is accessible via `require('util').types` or `require('util/types')`. +The API is accessible via `require('node:util').types` or `require('node:util/types')`. ### `util.types.isAnyArrayBuffer(value)` @@ -2210,7 +2210,7 @@ Alias for [`Array.isArray()`][]. Returns `true` if the given `object` is an `Array`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isArray([]); // Returns: true @@ -2235,7 +2235,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isBoolean(1); // Returns: false @@ -2260,7 +2260,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `Buffer`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isBuffer({ length: 0 }); // Returns: false @@ -2285,7 +2285,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `Date`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isDate(new Date()); // Returns: true @@ -2311,7 +2311,7 @@ Returns `true` if the given `object` is an [`Error`][]. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isError(new Error()); // Returns: true @@ -2326,7 +2326,7 @@ possible to obtain an incorrect result when the `object` argument manipulates `@@toStringTag`. ```js -const util = require('util'); +const util = require('node:util'); const obj = { name: 'Error', message: 'an error occurred' }; util.isError(obj); @@ -2352,7 +2352,7 @@ Returns `true` if the given `object` is a `Function`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); function Foo() {} const Bar = () => {}; @@ -2381,7 +2381,7 @@ Returns `true` if the given `object` is strictly `null`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isNull(0); // Returns: false @@ -2408,7 +2408,7 @@ Returns `true` if the given `object` is `null` or `undefined`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isNullOrUndefined(0); // Returns: false @@ -2433,7 +2433,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `Number`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isNumber(false); // Returns: false @@ -2463,7 +2463,7 @@ Returns `true` if the given `object` is strictly an `Object` **and** not a Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isObject(5); // Returns: false @@ -2493,7 +2493,7 @@ Returns `true` if the given `object` is a primitive type. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isPrimitive(5); // Returns: true @@ -2530,7 +2530,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `RegExp`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isRegExp(/some regexp/); // Returns: true @@ -2555,7 +2555,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `string`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isString(''); // Returns: true @@ -2582,7 +2582,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is a `Symbol`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); util.isSymbol(5); // Returns: false @@ -2607,7 +2607,7 @@ deprecated: v4.0.0 Returns `true` if the given `object` is `undefined`. Otherwise, returns `false`. ```js -const util = require('util'); +const util = require('node:util'); const foo = undefined; util.isUndefined(5); @@ -2633,7 +2633,7 @@ The `util.log()` method prints the given `string` to `stdout` with an included timestamp. ```js -const util = require('util'); +const util = require('node:util'); util.log('Timestamped message.'); ``` |