11cb0ef41Sopenharmony_ci# Util
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/util.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:util` module supports the needs of Node.js internal APIs. Many of the
101cb0ef41Sopenharmony_ciutilities are useful for application and module developers as well. To access
111cb0ef41Sopenharmony_ciit:
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```js
141cb0ef41Sopenharmony_ciconst util = require('node:util');
151cb0ef41Sopenharmony_ci```
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci## `util.callbackify(original)`
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci<!-- YAML
201cb0ef41Sopenharmony_ciadded: v8.2.0
211cb0ef41Sopenharmony_ci-->
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci* `original` {Function} An `async` function
241cb0ef41Sopenharmony_ci* Returns: {Function} a callback style function
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciTakes an `async` function (or a function that returns a `Promise`) and returns a
271cb0ef41Sopenharmony_cifunction following the error-first callback style, i.e. taking
281cb0ef41Sopenharmony_cian `(err, value) => ...` callback as the last argument. In the callback, the
291cb0ef41Sopenharmony_cifirst argument will be the rejection reason (or `null` if the `Promise`
301cb0ef41Sopenharmony_ciresolved), and the second argument will be the resolved value.
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci```js
331cb0ef41Sopenharmony_ciconst util = require('node:util');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciasync function fn() {
361cb0ef41Sopenharmony_ci  return 'hello world';
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ciconst callbackFunction = util.callbackify(fn);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cicallbackFunction((err, ret) => {
411cb0ef41Sopenharmony_ci  if (err) throw err;
421cb0ef41Sopenharmony_ci  console.log(ret);
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ci```
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciWill print:
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```text
491cb0ef41Sopenharmony_cihello world
501cb0ef41Sopenharmony_ci```
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciThe callback is executed asynchronously, and will have a limited stack trace.
531cb0ef41Sopenharmony_ciIf the callback throws, the process will emit an [`'uncaughtException'`][]
541cb0ef41Sopenharmony_cievent, and if not handled will exit.
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciSince `null` has a special meaning as the first argument to a callback, if a
571cb0ef41Sopenharmony_ciwrapped function rejects a `Promise` with a falsy value as a reason, the value
581cb0ef41Sopenharmony_ciis wrapped in an `Error` with the original value stored in a field named
591cb0ef41Sopenharmony_ci`reason`.
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci```js
621cb0ef41Sopenharmony_cifunction fn() {
631cb0ef41Sopenharmony_ci  return Promise.reject(null);
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ciconst callbackFunction = util.callbackify(fn);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cicallbackFunction((err, ret) => {
681cb0ef41Sopenharmony_ci  // When the Promise was rejected with `null` it is wrapped with an Error and
691cb0ef41Sopenharmony_ci  // the original value is stored in `reason`.
701cb0ef41Sopenharmony_ci  err && Object.hasOwn(err, 'reason') && err.reason === null;  // true
711cb0ef41Sopenharmony_ci});
721cb0ef41Sopenharmony_ci```
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci## `util.debuglog(section[, callback])`
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci<!-- YAML
771cb0ef41Sopenharmony_ciadded: v0.11.3
781cb0ef41Sopenharmony_ci-->
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci* `section` {string} A string identifying the portion of the application for
811cb0ef41Sopenharmony_ci  which the `debuglog` function is being created.
821cb0ef41Sopenharmony_ci* `callback` {Function} A callback invoked the first time the logging function
831cb0ef41Sopenharmony_ci  is called with a function argument that is a more optimized logging function.
841cb0ef41Sopenharmony_ci* Returns: {Function} The logging function
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciThe `util.debuglog()` method is used to create a function that conditionally
871cb0ef41Sopenharmony_ciwrites debug messages to `stderr` based on the existence of the `NODE_DEBUG`
881cb0ef41Sopenharmony_cienvironment variable. If the `section` name appears within the value of that
891cb0ef41Sopenharmony_cienvironment variable, then the returned function operates similar to
901cb0ef41Sopenharmony_ci[`console.error()`][]. If not, then the returned function is a no-op.
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci```js
931cb0ef41Sopenharmony_ciconst util = require('node:util');
941cb0ef41Sopenharmony_ciconst debuglog = util.debuglog('foo');
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_cidebuglog('hello from foo [%d]', 123);
971cb0ef41Sopenharmony_ci```
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciIf this program is run with `NODE_DEBUG=foo` in the environment, then
1001cb0ef41Sopenharmony_ciit will output something like:
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci```console
1031cb0ef41Sopenharmony_ciFOO 3245: hello from foo [123]
1041cb0ef41Sopenharmony_ci```
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciwhere `3245` is the process id. If it is not run with that
1071cb0ef41Sopenharmony_cienvironment variable set, then it will not print anything.
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciThe `section` supports wildcard also:
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci```js
1121cb0ef41Sopenharmony_ciconst util = require('node:util');
1131cb0ef41Sopenharmony_ciconst debuglog = util.debuglog('foo-bar');
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_cidebuglog('hi there, it\'s foo-bar [%d]', 2333);
1161cb0ef41Sopenharmony_ci```
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ciif it is run with `NODE_DEBUG=foo*` in the environment, then it will output
1191cb0ef41Sopenharmony_cisomething like:
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci```console
1221cb0ef41Sopenharmony_ciFOO-BAR 3257: hi there, it's foo-bar [2333]
1231cb0ef41Sopenharmony_ci```
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciMultiple comma-separated `section` names may be specified in the `NODE_DEBUG`
1261cb0ef41Sopenharmony_cienvironment variable: `NODE_DEBUG=fs,net,tls`.
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciThe optional `callback` argument can be used to replace the logging function
1291cb0ef41Sopenharmony_ciwith a different function that doesn't have any initialization or
1301cb0ef41Sopenharmony_ciunnecessary wrapping.
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci```js
1331cb0ef41Sopenharmony_ciconst util = require('node:util');
1341cb0ef41Sopenharmony_cilet debuglog = util.debuglog('internals', (debug) => {
1351cb0ef41Sopenharmony_ci  // Replace with a logging function that optimizes out
1361cb0ef41Sopenharmony_ci  // testing if the section is enabled
1371cb0ef41Sopenharmony_ci  debuglog = debug;
1381cb0ef41Sopenharmony_ci});
1391cb0ef41Sopenharmony_ci```
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci### `debuglog().enabled`
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci<!-- YAML
1441cb0ef41Sopenharmony_ciadded: v14.9.0
1451cb0ef41Sopenharmony_ci-->
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci* {boolean}
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciThe `util.debuglog().enabled` getter is used to create a test that can be used
1501cb0ef41Sopenharmony_ciin conditionals based on the existence of the `NODE_DEBUG` environment variable.
1511cb0ef41Sopenharmony_ciIf the `section` name appears within the value of that environment variable,
1521cb0ef41Sopenharmony_cithen the returned value will be `true`. If not, then the returned value will be
1531cb0ef41Sopenharmony_ci`false`.
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci```js
1561cb0ef41Sopenharmony_ciconst util = require('node:util');
1571cb0ef41Sopenharmony_ciconst enabled = util.debuglog('foo').enabled;
1581cb0ef41Sopenharmony_ciif (enabled) {
1591cb0ef41Sopenharmony_ci  console.log('hello from foo [%d]', 123);
1601cb0ef41Sopenharmony_ci}
1611cb0ef41Sopenharmony_ci```
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ciIf this program is run with `NODE_DEBUG=foo` in the environment, then it will
1641cb0ef41Sopenharmony_cioutput something like:
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci```console
1671cb0ef41Sopenharmony_cihello from foo [123]
1681cb0ef41Sopenharmony_ci```
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci## `util.debug(section)`
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci<!-- YAML
1731cb0ef41Sopenharmony_ciadded: v14.9.0
1741cb0ef41Sopenharmony_ci-->
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ciAlias for `util.debuglog`. Usage allows for readability of that doesn't imply
1771cb0ef41Sopenharmony_cilogging when only using `util.debuglog().enabled`.
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci## `util.deprecate(fn, msg[, code])`
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci<!-- YAML
1821cb0ef41Sopenharmony_ciadded: v0.8.0
1831cb0ef41Sopenharmony_cichanges:
1841cb0ef41Sopenharmony_ci  - version: v10.0.0
1851cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/16393
1861cb0ef41Sopenharmony_ci    description: Deprecation warnings are only emitted once for each code.
1871cb0ef41Sopenharmony_ci-->
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci* `fn` {Function} The function that is being deprecated.
1901cb0ef41Sopenharmony_ci* `msg` {string} A warning message to display when the deprecated function is
1911cb0ef41Sopenharmony_ci  invoked.
1921cb0ef41Sopenharmony_ci* `code` {string} A deprecation code. See the [list of deprecated APIs][] for a
1931cb0ef41Sopenharmony_ci  list of codes.
1941cb0ef41Sopenharmony_ci* Returns: {Function} The deprecated function wrapped to emit a warning.
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ciThe `util.deprecate()` method wraps `fn` (which may be a function or class) in
1971cb0ef41Sopenharmony_cisuch a way that it is marked as deprecated.
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci```js
2001cb0ef41Sopenharmony_ciconst util = require('node:util');
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ciexports.obsoleteFunction = util.deprecate(() => {
2031cb0ef41Sopenharmony_ci  // Do something here.
2041cb0ef41Sopenharmony_ci}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
2051cb0ef41Sopenharmony_ci```
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ciWhen called, `util.deprecate()` will return a function that will emit a
2081cb0ef41Sopenharmony_ci`DeprecationWarning` using the [`'warning'`][] event. The warning will
2091cb0ef41Sopenharmony_cibe emitted and printed to `stderr` the first time the returned function is
2101cb0ef41Sopenharmony_cicalled. After the warning is emitted, the wrapped function is called without
2111cb0ef41Sopenharmony_ciemitting a warning.
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciIf the same optional `code` is supplied in multiple calls to `util.deprecate()`,
2141cb0ef41Sopenharmony_cithe warning will be emitted only once for that `code`.
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci```js
2171cb0ef41Sopenharmony_ciconst util = require('node:util');
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ciconst fn1 = util.deprecate(someFunction, someMessage, 'DEP0001');
2201cb0ef41Sopenharmony_ciconst fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
2211cb0ef41Sopenharmony_cifn1(); // Emits a deprecation warning with code DEP0001
2221cb0ef41Sopenharmony_cifn2(); // Does not emit a deprecation warning because it has the same code
2231cb0ef41Sopenharmony_ci```
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ciIf either the `--no-deprecation` or `--no-warnings` command-line flags are
2261cb0ef41Sopenharmony_ciused, or if the `process.noDeprecation` property is set to `true` _prior_ to
2271cb0ef41Sopenharmony_cithe first deprecation warning, the `util.deprecate()` method does nothing.
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ciIf the `--trace-deprecation` or `--trace-warnings` command-line flags are set,
2301cb0ef41Sopenharmony_cior the `process.traceDeprecation` property is set to `true`, a warning and a
2311cb0ef41Sopenharmony_cistack trace are printed to `stderr` the first time the deprecated function is
2321cb0ef41Sopenharmony_cicalled.
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ciIf the `--throw-deprecation` command-line flag is set, or the
2351cb0ef41Sopenharmony_ci`process.throwDeprecation` property is set to `true`, then an exception will be
2361cb0ef41Sopenharmony_cithrown when the deprecated function is called.
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ciThe `--throw-deprecation` command-line flag and `process.throwDeprecation`
2391cb0ef41Sopenharmony_ciproperty take precedence over `--trace-deprecation` and
2401cb0ef41Sopenharmony_ci`process.traceDeprecation`.
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ci## `util.format(format[, ...args])`
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci<!-- YAML
2451cb0ef41Sopenharmony_ciadded: v0.5.3
2461cb0ef41Sopenharmony_cichanges:
2471cb0ef41Sopenharmony_ci  - version: v12.11.0
2481cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/29606
2491cb0ef41Sopenharmony_ci    description: The `%c` specifier is ignored now.
2501cb0ef41Sopenharmony_ci  - version: v12.0.0
2511cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/23162
2521cb0ef41Sopenharmony_ci    description: The `format` argument is now only taken as such if it actually
2531cb0ef41Sopenharmony_ci                 contains format specifiers.
2541cb0ef41Sopenharmony_ci  - version: v12.0.0
2551cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/23162
2561cb0ef41Sopenharmony_ci    description: If the `format` argument is not a format string, the output
2571cb0ef41Sopenharmony_ci                 string's formatting is no longer dependent on the type of the
2581cb0ef41Sopenharmony_ci                 first argument. This change removes previously present quotes
2591cb0ef41Sopenharmony_ci                 from strings that were being output when the first argument
2601cb0ef41Sopenharmony_ci                 was not a string.
2611cb0ef41Sopenharmony_ci  - version: v11.4.0
2621cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/23708
2631cb0ef41Sopenharmony_ci    description: The `%d`, `%f`, and `%i` specifiers now support Symbols
2641cb0ef41Sopenharmony_ci                 properly.
2651cb0ef41Sopenharmony_ci  - version: v11.4.0
2661cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/24806
2671cb0ef41Sopenharmony_ci    description: The `%o` specifier's `depth` has default depth of 4 again.
2681cb0ef41Sopenharmony_ci  - version: v11.0.0
2691cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17907
2701cb0ef41Sopenharmony_ci    description: The `%o` specifier's `depth` option will now fall back to the
2711cb0ef41Sopenharmony_ci                 default depth.
2721cb0ef41Sopenharmony_ci  - version: v10.12.0
2731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22097
2741cb0ef41Sopenharmony_ci    description: The `%d` and `%i` specifiers now support BigInt.
2751cb0ef41Sopenharmony_ci  - version: v8.4.0
2761cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/14558
2771cb0ef41Sopenharmony_ci    description: The `%o` and `%O` specifiers are supported now.
2781cb0ef41Sopenharmony_ci-->
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci* `format` {string} A `printf`-like format string.
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ciThe `util.format()` method returns a formatted string using the first argument
2831cb0ef41Sopenharmony_cias a `printf`-like format string which can contain zero or more format
2841cb0ef41Sopenharmony_cispecifiers. Each specifier is replaced with the converted value from the
2851cb0ef41Sopenharmony_cicorresponding argument. Supported specifiers are:
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci* `%s`: `String` will be used to convert all values except `BigInt`, `Object`
2881cb0ef41Sopenharmony_ci  and `-0`. `BigInt` values will be represented with an `n` and Objects that
2891cb0ef41Sopenharmony_ci  have no user defined `toString` function are inspected using `util.inspect()`
2901cb0ef41Sopenharmony_ci  with options `{ depth: 0, colors: false, compact: 3 }`.
2911cb0ef41Sopenharmony_ci* `%d`: `Number` will be used to convert all values except `BigInt` and
2921cb0ef41Sopenharmony_ci  `Symbol`.
2931cb0ef41Sopenharmony_ci* `%i`: `parseInt(value, 10)` is used for all values except `BigInt` and
2941cb0ef41Sopenharmony_ci  `Symbol`.
2951cb0ef41Sopenharmony_ci* `%f`: `parseFloat(value)` is used for all values expect `Symbol`.
2961cb0ef41Sopenharmony_ci* `%j`: JSON. Replaced with the string `'[Circular]'` if the argument contains
2971cb0ef41Sopenharmony_ci  circular references.
2981cb0ef41Sopenharmony_ci* `%o`: `Object`. A string representation of an object with generic JavaScript
2991cb0ef41Sopenharmony_ci  object formatting. Similar to `util.inspect()` with options
3001cb0ef41Sopenharmony_ci  `{ showHidden: true, showProxy: true }`. This will show the full object
3011cb0ef41Sopenharmony_ci  including non-enumerable properties and proxies.
3021cb0ef41Sopenharmony_ci* `%O`: `Object`. A string representation of an object with generic JavaScript
3031cb0ef41Sopenharmony_ci  object formatting. Similar to `util.inspect()` without options. This will show
3041cb0ef41Sopenharmony_ci  the full object not including non-enumerable properties and proxies.
3051cb0ef41Sopenharmony_ci* `%c`: `CSS`. This specifier is ignored and will skip any CSS passed in.
3061cb0ef41Sopenharmony_ci* `%%`: single percent sign (`'%'`). This does not consume an argument.
3071cb0ef41Sopenharmony_ci* Returns: {string} The formatted string
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ciIf a specifier does not have a corresponding argument, it is not replaced:
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci```js
3121cb0ef41Sopenharmony_ciutil.format('%s:%s', 'foo');
3131cb0ef41Sopenharmony_ci// Returns: 'foo:%s'
3141cb0ef41Sopenharmony_ci```
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ciValues that are not part of the format string are formatted using
3171cb0ef41Sopenharmony_ci`util.inspect()` if their type is not `string`.
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ciIf there are more arguments passed to the `util.format()` method than the
3201cb0ef41Sopenharmony_cinumber of specifiers, the extra arguments are concatenated to the returned
3211cb0ef41Sopenharmony_cistring, separated by spaces:
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci```js
3241cb0ef41Sopenharmony_ciutil.format('%s:%s', 'foo', 'bar', 'baz');
3251cb0ef41Sopenharmony_ci// Returns: 'foo:bar baz'
3261cb0ef41Sopenharmony_ci```
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ciIf the first argument does not contain a valid format specifier, `util.format()`
3291cb0ef41Sopenharmony_cireturns a string that is the concatenation of all arguments separated by spaces:
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci```js
3321cb0ef41Sopenharmony_ciutil.format(1, 2, 3);
3331cb0ef41Sopenharmony_ci// Returns: '1 2 3'
3341cb0ef41Sopenharmony_ci```
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ciIf only one argument is passed to `util.format()`, it is returned as it is
3371cb0ef41Sopenharmony_ciwithout any formatting:
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci```js
3401cb0ef41Sopenharmony_ciutil.format('%% %s');
3411cb0ef41Sopenharmony_ci// Returns: '%% %s'
3421cb0ef41Sopenharmony_ci```
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci`util.format()` is a synchronous method that is intended as a debugging tool.
3451cb0ef41Sopenharmony_ciSome input values can have a significant performance overhead that can block the
3461cb0ef41Sopenharmony_cievent loop. Use this function with care and never in a hot code path.
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci## `util.formatWithOptions(inspectOptions, format[, ...args])`
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci<!-- YAML
3511cb0ef41Sopenharmony_ciadded: v10.0.0
3521cb0ef41Sopenharmony_ci-->
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci* `inspectOptions` {Object}
3551cb0ef41Sopenharmony_ci* `format` {string}
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ciThis function is identical to [`util.format()`][], except in that it takes
3581cb0ef41Sopenharmony_cian `inspectOptions` argument which specifies options that are passed along to
3591cb0ef41Sopenharmony_ci[`util.inspect()`][].
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_ci```js
3621cb0ef41Sopenharmony_ciutil.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
3631cb0ef41Sopenharmony_ci// Returns 'See object { foo: 42 }', where `42` is colored as a number
3641cb0ef41Sopenharmony_ci// when printed to a terminal.
3651cb0ef41Sopenharmony_ci```
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci## `util.getSystemErrorName(err)`
3681cb0ef41Sopenharmony_ci
3691cb0ef41Sopenharmony_ci<!-- YAML
3701cb0ef41Sopenharmony_ciadded: v9.7.0
3711cb0ef41Sopenharmony_ci-->
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci* `err` {number}
3741cb0ef41Sopenharmony_ci* Returns: {string}
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ciReturns the string name for a numeric error code that comes from a Node.js API.
3771cb0ef41Sopenharmony_ciThe mapping between error codes and error names is platform-dependent.
3781cb0ef41Sopenharmony_ciSee [Common System Errors][] for the names of common errors.
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci```js
3811cb0ef41Sopenharmony_cifs.access('file/that/does/not/exist', (err) => {
3821cb0ef41Sopenharmony_ci  const name = util.getSystemErrorName(err.errno);
3831cb0ef41Sopenharmony_ci  console.error(name);  // ENOENT
3841cb0ef41Sopenharmony_ci});
3851cb0ef41Sopenharmony_ci```
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci## `util.getSystemErrorMap()`
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ci<!-- YAML
3901cb0ef41Sopenharmony_ciadded:
3911cb0ef41Sopenharmony_ci  - v16.0.0
3921cb0ef41Sopenharmony_ci  - v14.17.0
3931cb0ef41Sopenharmony_ci-->
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci* Returns: {Map}
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ciReturns a Map of all system error codes available from the Node.js API.
3981cb0ef41Sopenharmony_ciThe mapping between error codes and error names is platform-dependent.
3991cb0ef41Sopenharmony_ciSee [Common System Errors][] for the names of common errors.
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci```js
4021cb0ef41Sopenharmony_cifs.access('file/that/does/not/exist', (err) => {
4031cb0ef41Sopenharmony_ci  const errorMap = util.getSystemErrorMap();
4041cb0ef41Sopenharmony_ci  const name = errorMap.get(err.errno);
4051cb0ef41Sopenharmony_ci  console.error(name);  // ENOENT
4061cb0ef41Sopenharmony_ci});
4071cb0ef41Sopenharmony_ci```
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci## `util.inherits(constructor, superConstructor)`
4101cb0ef41Sopenharmony_ci
4111cb0ef41Sopenharmony_ci<!-- YAML
4121cb0ef41Sopenharmony_ciadded: v0.3.0
4131cb0ef41Sopenharmony_cichanges:
4141cb0ef41Sopenharmony_ci  - version: v5.0.0
4151cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/3455
4161cb0ef41Sopenharmony_ci    description: The `constructor` parameter can refer to an ES6 class now.
4171cb0ef41Sopenharmony_ci-->
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use ES2015 class syntax and `extends` keyword instead.
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci* `constructor` {Function}
4221cb0ef41Sopenharmony_ci* `superConstructor` {Function}
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ciUsage of `util.inherits()` is discouraged. Please use the ES6 `class` and
4251cb0ef41Sopenharmony_ci`extends` keywords to get language level inheritance support. Also note
4261cb0ef41Sopenharmony_cithat the two styles are [semantically incompatible][].
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_ciInherit the prototype methods from one [constructor][] into another. The
4291cb0ef41Sopenharmony_ciprototype of `constructor` will be set to a new object created from
4301cb0ef41Sopenharmony_ci`superConstructor`.
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ciThis mainly adds some input validation on top of
4331cb0ef41Sopenharmony_ci`Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)`.
4341cb0ef41Sopenharmony_ciAs an additional convenience, `superConstructor` will be accessible
4351cb0ef41Sopenharmony_cithrough the `constructor.super_` property.
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci```js
4381cb0ef41Sopenharmony_ciconst util = require('node:util');
4391cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_cifunction MyStream() {
4421cb0ef41Sopenharmony_ci  EventEmitter.call(this);
4431cb0ef41Sopenharmony_ci}
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ciutil.inherits(MyStream, EventEmitter);
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ciMyStream.prototype.write = function(data) {
4481cb0ef41Sopenharmony_ci  this.emit('data', data);
4491cb0ef41Sopenharmony_ci};
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ciconst stream = new MyStream();
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ciconsole.log(stream instanceof EventEmitter); // true
4541cb0ef41Sopenharmony_ciconsole.log(MyStream.super_ === EventEmitter); // true
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_cistream.on('data', (data) => {
4571cb0ef41Sopenharmony_ci  console.log(`Received data: "${data}"`);
4581cb0ef41Sopenharmony_ci});
4591cb0ef41Sopenharmony_cistream.write('It works!'); // Received data: "It works!"
4601cb0ef41Sopenharmony_ci```
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_ciES6 example using `class` and `extends`:
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci```js
4651cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ciclass MyStream extends EventEmitter {
4681cb0ef41Sopenharmony_ci  write(data) {
4691cb0ef41Sopenharmony_ci    this.emit('data', data);
4701cb0ef41Sopenharmony_ci  }
4711cb0ef41Sopenharmony_ci}
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ciconst stream = new MyStream();
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_cistream.on('data', (data) => {
4761cb0ef41Sopenharmony_ci  console.log(`Received data: "${data}"`);
4771cb0ef41Sopenharmony_ci});
4781cb0ef41Sopenharmony_cistream.write('With ES6');
4791cb0ef41Sopenharmony_ci```
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci## `util.inspect(object[, options])`
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci## `util.inspect(object[, showHidden[, depth[, colors]]])`
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ci<!-- YAML
4861cb0ef41Sopenharmony_ciadded: v0.3.0
4871cb0ef41Sopenharmony_cichanges:
4881cb0ef41Sopenharmony_ci  - version:
4891cb0ef41Sopenharmony_ci    - v17.3.0
4901cb0ef41Sopenharmony_ci    - v16.14.0
4911cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41003
4921cb0ef41Sopenharmony_ci    description: The `numericSeparator` option is supported now.
4931cb0ef41Sopenharmony_ci  - version:
4941cb0ef41Sopenharmony_ci    - v14.6.0
4951cb0ef41Sopenharmony_ci    - v12.19.0
4961cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/33690
4971cb0ef41Sopenharmony_ci    description: If `object` is from a different `vm.Context` now, a custom
4981cb0ef41Sopenharmony_ci                 inspection function on it will not receive context-specific
4991cb0ef41Sopenharmony_ci                 arguments anymore.
5001cb0ef41Sopenharmony_ci  - version:
5011cb0ef41Sopenharmony_ci     - v13.13.0
5021cb0ef41Sopenharmony_ci     - v12.17.0
5031cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32392
5041cb0ef41Sopenharmony_ci    description: The `maxStringLength` option is supported now.
5051cb0ef41Sopenharmony_ci  - version:
5061cb0ef41Sopenharmony_ci     - v13.5.0
5071cb0ef41Sopenharmony_ci     - v12.16.0
5081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30768
5091cb0ef41Sopenharmony_ci    description: User defined prototype properties are inspected in case
5101cb0ef41Sopenharmony_ci                 `showHidden` is `true`.
5111cb0ef41Sopenharmony_ci  - version: v13.0.0
5121cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27685
5131cb0ef41Sopenharmony_ci    description: Circular references now include a marker to the reference.
5141cb0ef41Sopenharmony_ci  - version: v12.0.0
5151cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27109
5161cb0ef41Sopenharmony_ci    description: The `compact` options default is changed to `3` and the
5171cb0ef41Sopenharmony_ci                 `breakLength` options default is changed to `80`.
5181cb0ef41Sopenharmony_ci  - version: v12.0.0
5191cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/24971
5201cb0ef41Sopenharmony_ci    description: Internal properties no longer appear in the context argument
5211cb0ef41Sopenharmony_ci                 of a custom inspection function.
5221cb0ef41Sopenharmony_ci  - version: v11.11.0
5231cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/26269
5241cb0ef41Sopenharmony_ci    description: The `compact` option accepts numbers for a new output mode.
5251cb0ef41Sopenharmony_ci  - version: v11.7.0
5261cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/25006
5271cb0ef41Sopenharmony_ci    description: ArrayBuffers now also show their binary contents.
5281cb0ef41Sopenharmony_ci  - version: v11.5.0
5291cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/24852
5301cb0ef41Sopenharmony_ci    description: The `getters` option is supported now.
5311cb0ef41Sopenharmony_ci  - version: v11.4.0
5321cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/24326
5331cb0ef41Sopenharmony_ci    description: The `depth` default changed back to `2`.
5341cb0ef41Sopenharmony_ci  - version: v11.0.0
5351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22846
5361cb0ef41Sopenharmony_ci    description: The `depth` default changed to `20`.
5371cb0ef41Sopenharmony_ci  - version: v11.0.0
5381cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22756
5391cb0ef41Sopenharmony_ci    description: The inspection output is now limited to about 128 MiB. Data
5401cb0ef41Sopenharmony_ci                 above that size will not be fully inspected.
5411cb0ef41Sopenharmony_ci  - version: v10.12.0
5421cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22788
5431cb0ef41Sopenharmony_ci    description: The `sorted` option is supported now.
5441cb0ef41Sopenharmony_ci  - version: v10.6.0
5451cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/20725
5461cb0ef41Sopenharmony_ci    description: Inspecting linked lists and similar objects is now possible
5471cb0ef41Sopenharmony_ci                 up to the maximum call stack size.
5481cb0ef41Sopenharmony_ci  - version: v10.0.0
5491cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19259
5501cb0ef41Sopenharmony_ci    description: The `WeakMap` and `WeakSet` entries can now be inspected
5511cb0ef41Sopenharmony_ci                 as well.
5521cb0ef41Sopenharmony_ci  - version: v9.9.0
5531cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17576
5541cb0ef41Sopenharmony_ci    description: The `compact` option is supported now.
5551cb0ef41Sopenharmony_ci  - version: v6.6.0
5561cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8174
5571cb0ef41Sopenharmony_ci    description: Custom inspection functions can now return `this`.
5581cb0ef41Sopenharmony_ci  - version: v6.3.0
5591cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/7499
5601cb0ef41Sopenharmony_ci    description: The `breakLength` option is supported now.
5611cb0ef41Sopenharmony_ci  - version: v6.1.0
5621cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6334
5631cb0ef41Sopenharmony_ci    description: The `maxArrayLength` option is supported now; in particular,
5641cb0ef41Sopenharmony_ci                 long arrays are truncated by default.
5651cb0ef41Sopenharmony_ci  - version: v6.1.0
5661cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6465
5671cb0ef41Sopenharmony_ci    description: The `showProxy` option is supported now.
5681cb0ef41Sopenharmony_ci-->
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ci* `object` {any} Any JavaScript primitive or `Object`.
5711cb0ef41Sopenharmony_ci* `options` {Object}
5721cb0ef41Sopenharmony_ci  * `showHidden` {boolean} If `true`, `object`'s non-enumerable symbols and
5731cb0ef41Sopenharmony_ci    properties are included in the formatted result. [`WeakMap`][] and
5741cb0ef41Sopenharmony_ci    [`WeakSet`][] entries are also included as well as user defined prototype
5751cb0ef41Sopenharmony_ci    properties (excluding method properties). **Default:** `false`.
5761cb0ef41Sopenharmony_ci  * `depth` {number} Specifies the number of times to recurse while formatting
5771cb0ef41Sopenharmony_ci    `object`. This is useful for inspecting large objects. To recurse up to
5781cb0ef41Sopenharmony_ci    the maximum call stack size pass `Infinity` or `null`.
5791cb0ef41Sopenharmony_ci    **Default:** `2`.
5801cb0ef41Sopenharmony_ci  * `colors` {boolean} If `true`, the output is styled with ANSI color
5811cb0ef41Sopenharmony_ci    codes. Colors are customizable. See [Customizing `util.inspect` colors][].
5821cb0ef41Sopenharmony_ci    **Default:** `false`.
5831cb0ef41Sopenharmony_ci  * `customInspect` {boolean} If `false`,
5841cb0ef41Sopenharmony_ci    `[util.inspect.custom](depth, opts, inspect)` functions are not invoked.
5851cb0ef41Sopenharmony_ci    **Default:** `true`.
5861cb0ef41Sopenharmony_ci  * `showProxy` {boolean} If `true`, `Proxy` inspection includes
5871cb0ef41Sopenharmony_ci    the [`target` and `handler`][] objects. **Default:** `false`.
5881cb0ef41Sopenharmony_ci  * `maxArrayLength` {integer} Specifies the maximum number of `Array`,
5891cb0ef41Sopenharmony_ci    [`TypedArray`][], [`WeakMap`][], and [`WeakSet`][] elements to include when
5901cb0ef41Sopenharmony_ci    formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
5911cb0ef41Sopenharmony_ci    negative to show no elements. **Default:** `100`.
5921cb0ef41Sopenharmony_ci  * `maxStringLength` {integer} Specifies the maximum number of characters to
5931cb0ef41Sopenharmony_ci    include when formatting. Set to `null` or `Infinity` to show all elements.
5941cb0ef41Sopenharmony_ci    Set to `0` or negative to show no characters. **Default:** `10000`.
5951cb0ef41Sopenharmony_ci  * `breakLength` {integer} The length at which input values are split across
5961cb0ef41Sopenharmony_ci    multiple lines. Set to `Infinity` to format the input as a single line
5971cb0ef41Sopenharmony_ci    (in combination with `compact` set to `true` or any number >= `1`).
5981cb0ef41Sopenharmony_ci    **Default:** `80`.
5991cb0ef41Sopenharmony_ci  * `compact` {boolean|integer} Setting this to `false` causes each object key
6001cb0ef41Sopenharmony_ci    to be displayed on a new line. It will break on new lines in text that is
6011cb0ef41Sopenharmony_ci    longer than `breakLength`. If set to a number, the most `n` inner elements
6021cb0ef41Sopenharmony_ci    are united on a single line as long as all properties fit into
6031cb0ef41Sopenharmony_ci    `breakLength`. Short array elements are also grouped together. For more
6041cb0ef41Sopenharmony_ci    information, see the example below. **Default:** `3`.
6051cb0ef41Sopenharmony_ci  * `sorted` {boolean|Function} If set to `true` or a function, all properties
6061cb0ef41Sopenharmony_ci    of an object, and `Set` and `Map` entries are sorted in the resulting
6071cb0ef41Sopenharmony_ci    string. If set to `true` the [default sort][] is used. If set to a function,
6081cb0ef41Sopenharmony_ci    it is used as a [compare function][].
6091cb0ef41Sopenharmony_ci  * `getters` {boolean|string} If set to `true`, getters are inspected. If set
6101cb0ef41Sopenharmony_ci    to `'get'`, only getters without a corresponding setter are inspected. If
6111cb0ef41Sopenharmony_ci    set to `'set'`, only getters with a corresponding setter are inspected.
6121cb0ef41Sopenharmony_ci    This might cause side effects depending on the getter function.
6131cb0ef41Sopenharmony_ci    **Default:** `false`.
6141cb0ef41Sopenharmony_ci  * `numericSeparator` {boolean} If set to `true`, an underscore is used to
6151cb0ef41Sopenharmony_ci    separate every three digits in all bigints and numbers.
6161cb0ef41Sopenharmony_ci    **Default:** `false`.
6171cb0ef41Sopenharmony_ci* Returns: {string} The representation of `object`.
6181cb0ef41Sopenharmony_ci
6191cb0ef41Sopenharmony_ciThe `util.inspect()` method returns a string representation of `object` that is
6201cb0ef41Sopenharmony_ciintended for debugging. The output of `util.inspect` may change at any time
6211cb0ef41Sopenharmony_ciand should not be depended upon programmatically. Additional `options` may be
6221cb0ef41Sopenharmony_cipassed that alter the result.
6231cb0ef41Sopenharmony_ci`util.inspect()` will use the constructor's name and/or `@@toStringTag` to make
6241cb0ef41Sopenharmony_cian identifiable tag for an inspected value.
6251cb0ef41Sopenharmony_ci
6261cb0ef41Sopenharmony_ci```js
6271cb0ef41Sopenharmony_ciclass Foo {
6281cb0ef41Sopenharmony_ci  get [Symbol.toStringTag]() {
6291cb0ef41Sopenharmony_ci    return 'bar';
6301cb0ef41Sopenharmony_ci  }
6311cb0ef41Sopenharmony_ci}
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ciclass Bar {}
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ciconst baz = Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } });
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ciutil.inspect(new Foo()); // 'Foo [bar] {}'
6381cb0ef41Sopenharmony_ciutil.inspect(new Bar()); // 'Bar {}'
6391cb0ef41Sopenharmony_ciutil.inspect(baz);       // '[foo] {}'
6401cb0ef41Sopenharmony_ci```
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ciCircular references point to their anchor by using a reference index:
6431cb0ef41Sopenharmony_ci
6441cb0ef41Sopenharmony_ci```js
6451cb0ef41Sopenharmony_ciconst { inspect } = require('node:util');
6461cb0ef41Sopenharmony_ci
6471cb0ef41Sopenharmony_ciconst obj = {};
6481cb0ef41Sopenharmony_ciobj.a = [obj];
6491cb0ef41Sopenharmony_ciobj.b = {};
6501cb0ef41Sopenharmony_ciobj.b.inner = obj.b;
6511cb0ef41Sopenharmony_ciobj.b.obj = obj;
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ciconsole.log(inspect(obj));
6541cb0ef41Sopenharmony_ci// <ref *1> {
6551cb0ef41Sopenharmony_ci//   a: [ [Circular *1] ],
6561cb0ef41Sopenharmony_ci//   b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
6571cb0ef41Sopenharmony_ci// }
6581cb0ef41Sopenharmony_ci```
6591cb0ef41Sopenharmony_ci
6601cb0ef41Sopenharmony_ciThe following example inspects all properties of the `util` object:
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci```js
6631cb0ef41Sopenharmony_ciconst util = require('node:util');
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ciconsole.log(util.inspect(util, { showHidden: true, depth: null }));
6661cb0ef41Sopenharmony_ci```
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ciThe following example highlights the effect of the `compact` option:
6691cb0ef41Sopenharmony_ci
6701cb0ef41Sopenharmony_ci```js
6711cb0ef41Sopenharmony_ciconst util = require('node:util');
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_ciconst o = {
6741cb0ef41Sopenharmony_ci  a: [1, 2, [[
6751cb0ef41Sopenharmony_ci    'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit, sed do ' +
6761cb0ef41Sopenharmony_ci      'eiusmod \ntempor incididunt ut labore et dolore magna aliqua.',
6771cb0ef41Sopenharmony_ci    'test',
6781cb0ef41Sopenharmony_ci    'foo']], 4],
6791cb0ef41Sopenharmony_ci  b: new Map([['za', 1], ['zb', 'test']]),
6801cb0ef41Sopenharmony_ci};
6811cb0ef41Sopenharmony_ciconsole.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci// { a:
6841cb0ef41Sopenharmony_ci//   [ 1,
6851cb0ef41Sopenharmony_ci//     2,
6861cb0ef41Sopenharmony_ci//     [ [ 'Lorem ipsum dolor sit amet,\nconsectetur [...]', // A long line
6871cb0ef41Sopenharmony_ci//           'test',
6881cb0ef41Sopenharmony_ci//           'foo' ] ],
6891cb0ef41Sopenharmony_ci//     4 ],
6901cb0ef41Sopenharmony_ci//   b: Map(2) { 'za' => 1, 'zb' => 'test' } }
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci// Setting `compact` to false or an integer creates more reader friendly output.
6931cb0ef41Sopenharmony_ciconsole.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci// {
6961cb0ef41Sopenharmony_ci//   a: [
6971cb0ef41Sopenharmony_ci//     1,
6981cb0ef41Sopenharmony_ci//     2,
6991cb0ef41Sopenharmony_ci//     [
7001cb0ef41Sopenharmony_ci//       [
7011cb0ef41Sopenharmony_ci//         'Lorem ipsum dolor sit amet,\n' +
7021cb0ef41Sopenharmony_ci//           'consectetur adipiscing elit, sed do eiusmod \n' +
7031cb0ef41Sopenharmony_ci//           'tempor incididunt ut labore et dolore magna aliqua.',
7041cb0ef41Sopenharmony_ci//         'test',
7051cb0ef41Sopenharmony_ci//         'foo'
7061cb0ef41Sopenharmony_ci//       ]
7071cb0ef41Sopenharmony_ci//     ],
7081cb0ef41Sopenharmony_ci//     4
7091cb0ef41Sopenharmony_ci//   ],
7101cb0ef41Sopenharmony_ci//   b: Map(2) {
7111cb0ef41Sopenharmony_ci//     'za' => 1,
7121cb0ef41Sopenharmony_ci//     'zb' => 'test'
7131cb0ef41Sopenharmony_ci//   }
7141cb0ef41Sopenharmony_ci// }
7151cb0ef41Sopenharmony_ci
7161cb0ef41Sopenharmony_ci// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a
7171cb0ef41Sopenharmony_ci// single line.
7181cb0ef41Sopenharmony_ci```
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ciThe `showHidden` option allows [`WeakMap`][] and [`WeakSet`][] entries to be
7211cb0ef41Sopenharmony_ciinspected. If there are more entries than `maxArrayLength`, there is no
7221cb0ef41Sopenharmony_ciguarantee which entries are displayed. That means retrieving the same
7231cb0ef41Sopenharmony_ci[`WeakSet`][] entries twice may result in different output. Furthermore, entries
7241cb0ef41Sopenharmony_ciwith no remaining strong references may be garbage collected at any time.
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ci```js
7271cb0ef41Sopenharmony_ciconst { inspect } = require('node:util');
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ciconst obj = { a: 1 };
7301cb0ef41Sopenharmony_ciconst obj2 = { b: 2 };
7311cb0ef41Sopenharmony_ciconst weakSet = new WeakSet([obj, obj2]);
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_ciconsole.log(inspect(weakSet, { showHidden: true }));
7341cb0ef41Sopenharmony_ci// WeakSet { { a: 1 }, { b: 2 } }
7351cb0ef41Sopenharmony_ci```
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ciThe `sorted` option ensures that an object's property insertion order does not
7381cb0ef41Sopenharmony_ciimpact the result of `util.inspect()`.
7391cb0ef41Sopenharmony_ci
7401cb0ef41Sopenharmony_ci```js
7411cb0ef41Sopenharmony_ciconst { inspect } = require('node:util');
7421cb0ef41Sopenharmony_ciconst assert = require('node:assert');
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ciconst o1 = {
7451cb0ef41Sopenharmony_ci  b: [2, 3, 1],
7461cb0ef41Sopenharmony_ci  a: '`a` comes before `b`',
7471cb0ef41Sopenharmony_ci  c: new Set([2, 3, 1]),
7481cb0ef41Sopenharmony_ci};
7491cb0ef41Sopenharmony_ciconsole.log(inspect(o1, { sorted: true }));
7501cb0ef41Sopenharmony_ci// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }
7511cb0ef41Sopenharmony_ciconsole.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
7521cb0ef41Sopenharmony_ci// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
7531cb0ef41Sopenharmony_ci
7541cb0ef41Sopenharmony_ciconst o2 = {
7551cb0ef41Sopenharmony_ci  c: new Set([2, 1, 3]),
7561cb0ef41Sopenharmony_ci  a: '`a` comes before `b`',
7571cb0ef41Sopenharmony_ci  b: [2, 3, 1],
7581cb0ef41Sopenharmony_ci};
7591cb0ef41Sopenharmony_ciassert.strict.equal(
7601cb0ef41Sopenharmony_ci  inspect(o1, { sorted: true }),
7611cb0ef41Sopenharmony_ci  inspect(o2, { sorted: true }),
7621cb0ef41Sopenharmony_ci);
7631cb0ef41Sopenharmony_ci```
7641cb0ef41Sopenharmony_ci
7651cb0ef41Sopenharmony_ciThe `numericSeparator` option adds an underscore every three digits to all
7661cb0ef41Sopenharmony_cinumbers.
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci```js
7691cb0ef41Sopenharmony_ciconst { inspect } = require('node:util');
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ciconst thousand = 1_000;
7721cb0ef41Sopenharmony_ciconst million = 1_000_000;
7731cb0ef41Sopenharmony_ciconst bigNumber = 123_456_789n;
7741cb0ef41Sopenharmony_ciconst bigDecimal = 1_234.123_45;
7751cb0ef41Sopenharmony_ci
7761cb0ef41Sopenharmony_ciconsole.log(thousand, million, bigNumber, bigDecimal);
7771cb0ef41Sopenharmony_ci// 1_000 1_000_000 123_456_789n 1_234.123_45
7781cb0ef41Sopenharmony_ci```
7791cb0ef41Sopenharmony_ci
7801cb0ef41Sopenharmony_ci`util.inspect()` is a synchronous method intended for debugging. Its maximum
7811cb0ef41Sopenharmony_cioutput length is approximately 128 MiB. Inputs that result in longer output will
7821cb0ef41Sopenharmony_cibe truncated.
7831cb0ef41Sopenharmony_ci
7841cb0ef41Sopenharmony_ci### Customizing `util.inspect` colors
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ci<!-- type=misc -->
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ciColor output (if enabled) of `util.inspect` is customizable globally
7891cb0ef41Sopenharmony_civia the `util.inspect.styles` and `util.inspect.colors` properties.
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci`util.inspect.styles` is a map associating a style name to a color from
7921cb0ef41Sopenharmony_ci`util.inspect.colors`.
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ciThe default styles and associated colors are:
7951cb0ef41Sopenharmony_ci
7961cb0ef41Sopenharmony_ci* `bigint`: `yellow`
7971cb0ef41Sopenharmony_ci* `boolean`: `yellow`
7981cb0ef41Sopenharmony_ci* `date`: `magenta`
7991cb0ef41Sopenharmony_ci* `module`: `underline`
8001cb0ef41Sopenharmony_ci* `name`: (no styling)
8011cb0ef41Sopenharmony_ci* `null`: `bold`
8021cb0ef41Sopenharmony_ci* `number`: `yellow`
8031cb0ef41Sopenharmony_ci* `regexp`: `red`
8041cb0ef41Sopenharmony_ci* `special`: `cyan` (e.g., `Proxies`)
8051cb0ef41Sopenharmony_ci* `string`: `green`
8061cb0ef41Sopenharmony_ci* `symbol`: `green`
8071cb0ef41Sopenharmony_ci* `undefined`: `grey`
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ciColor styling uses ANSI control codes that may not be supported on all
8101cb0ef41Sopenharmony_citerminals. To verify color support use [`tty.hasColors()`][].
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ciPredefined control codes are listed below (grouped as "Modifiers", "Foreground
8131cb0ef41Sopenharmony_cicolors", and "Background colors").
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci#### Modifiers
8161cb0ef41Sopenharmony_ci
8171cb0ef41Sopenharmony_ciModifier support varies throughout different terminals. They will mostly be
8181cb0ef41Sopenharmony_ciignored, if not supported.
8191cb0ef41Sopenharmony_ci
8201cb0ef41Sopenharmony_ci* `reset` - Resets all (color) modifiers to their defaults
8211cb0ef41Sopenharmony_ci* **bold** - Make text bold
8221cb0ef41Sopenharmony_ci* _italic_ - Make text italic
8231cb0ef41Sopenharmony_ci* <span style="border-bottom: 1px;">underline</span> - Make text underlined
8241cb0ef41Sopenharmony_ci* ~~strikethrough~~ - Puts a horizontal line through the center of the text
8251cb0ef41Sopenharmony_ci  (Alias: `strikeThrough`, `crossedout`, `crossedOut`)
8261cb0ef41Sopenharmony_ci* `hidden` - Prints the text, but makes it invisible (Alias: conceal)
8271cb0ef41Sopenharmony_ci* <span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
8281cb0ef41Sopenharmony_ci  `faint`)
8291cb0ef41Sopenharmony_ci* <span style="border-top: 1px">overlined</span> - Make text overlined
8301cb0ef41Sopenharmony_ci* blink - Hides and shows the text in an interval
8311cb0ef41Sopenharmony_ci* <span style="filter: invert(100%)">inverse</span> - Swap foreground and
8321cb0ef41Sopenharmony_ci  background colors (Alias: `swapcolors`, `swapColors`)
8331cb0ef41Sopenharmony_ci* <span style="border-bottom: 1px double;">doubleunderline</span> - Make text
8341cb0ef41Sopenharmony_ci  double underlined (Alias: `doubleUnderline`)
8351cb0ef41Sopenharmony_ci* <span style="border: 1px">framed</span> - Draw a frame around the text
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ci#### Foreground colors
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci* `black`
8401cb0ef41Sopenharmony_ci* `red`
8411cb0ef41Sopenharmony_ci* `green`
8421cb0ef41Sopenharmony_ci* `yellow`
8431cb0ef41Sopenharmony_ci* `blue`
8441cb0ef41Sopenharmony_ci* `magenta`
8451cb0ef41Sopenharmony_ci* `cyan`
8461cb0ef41Sopenharmony_ci* `white`
8471cb0ef41Sopenharmony_ci* `gray` (alias: `grey`, `blackBright`)
8481cb0ef41Sopenharmony_ci* `redBright`
8491cb0ef41Sopenharmony_ci* `greenBright`
8501cb0ef41Sopenharmony_ci* `yellowBright`
8511cb0ef41Sopenharmony_ci* `blueBright`
8521cb0ef41Sopenharmony_ci* `magentaBright`
8531cb0ef41Sopenharmony_ci* `cyanBright`
8541cb0ef41Sopenharmony_ci* `whiteBright`
8551cb0ef41Sopenharmony_ci
8561cb0ef41Sopenharmony_ci#### Background colors
8571cb0ef41Sopenharmony_ci
8581cb0ef41Sopenharmony_ci* `bgBlack`
8591cb0ef41Sopenharmony_ci* `bgRed`
8601cb0ef41Sopenharmony_ci* `bgGreen`
8611cb0ef41Sopenharmony_ci* `bgYellow`
8621cb0ef41Sopenharmony_ci* `bgBlue`
8631cb0ef41Sopenharmony_ci* `bgMagenta`
8641cb0ef41Sopenharmony_ci* `bgCyan`
8651cb0ef41Sopenharmony_ci* `bgWhite`
8661cb0ef41Sopenharmony_ci* `bgGray` (alias: `bgGrey`, `bgBlackBright`)
8671cb0ef41Sopenharmony_ci* `bgRedBright`
8681cb0ef41Sopenharmony_ci* `bgGreenBright`
8691cb0ef41Sopenharmony_ci* `bgYellowBright`
8701cb0ef41Sopenharmony_ci* `bgBlueBright`
8711cb0ef41Sopenharmony_ci* `bgMagentaBright`
8721cb0ef41Sopenharmony_ci* `bgCyanBright`
8731cb0ef41Sopenharmony_ci* `bgWhiteBright`
8741cb0ef41Sopenharmony_ci
8751cb0ef41Sopenharmony_ci### Custom inspection functions on objects
8761cb0ef41Sopenharmony_ci
8771cb0ef41Sopenharmony_ci<!-- type=misc -->
8781cb0ef41Sopenharmony_ci
8791cb0ef41Sopenharmony_ci<!-- YAML
8801cb0ef41Sopenharmony_ciadded: v0.1.97
8811cb0ef41Sopenharmony_cichanges:
8821cb0ef41Sopenharmony_ci  - version:
8831cb0ef41Sopenharmony_ci      - v17.3.0
8841cb0ef41Sopenharmony_ci      - v16.14.0
8851cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41019
8861cb0ef41Sopenharmony_ci    description: The inspect argument is added for more interoperability.
8871cb0ef41Sopenharmony_ci-->
8881cb0ef41Sopenharmony_ci
8891cb0ef41Sopenharmony_ciObjects may also define their own
8901cb0ef41Sopenharmony_ci[`[util.inspect.custom](depth, opts, inspect)`][util.inspect.custom] function,
8911cb0ef41Sopenharmony_ciwhich `util.inspect()` will invoke and use the result of when inspecting
8921cb0ef41Sopenharmony_cithe object.
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_ci```js
8951cb0ef41Sopenharmony_ciconst util = require('node:util');
8961cb0ef41Sopenharmony_ci
8971cb0ef41Sopenharmony_ciclass Box {
8981cb0ef41Sopenharmony_ci  constructor(value) {
8991cb0ef41Sopenharmony_ci    this.value = value;
9001cb0ef41Sopenharmony_ci  }
9011cb0ef41Sopenharmony_ci
9021cb0ef41Sopenharmony_ci  [util.inspect.custom](depth, options, inspect) {
9031cb0ef41Sopenharmony_ci    if (depth < 0) {
9041cb0ef41Sopenharmony_ci      return options.stylize('[Box]', 'special');
9051cb0ef41Sopenharmony_ci    }
9061cb0ef41Sopenharmony_ci
9071cb0ef41Sopenharmony_ci    const newOptions = Object.assign({}, options, {
9081cb0ef41Sopenharmony_ci      depth: options.depth === null ? null : options.depth - 1,
9091cb0ef41Sopenharmony_ci    });
9101cb0ef41Sopenharmony_ci
9111cb0ef41Sopenharmony_ci    // Five space padding because that's the size of "Box< ".
9121cb0ef41Sopenharmony_ci    const padding = ' '.repeat(5);
9131cb0ef41Sopenharmony_ci    const inner = inspect(this.value, newOptions)
9141cb0ef41Sopenharmony_ci                  .replace(/\n/g, `\n${padding}`);
9151cb0ef41Sopenharmony_ci    return `${options.stylize('Box', 'special')}< ${inner} >`;
9161cb0ef41Sopenharmony_ci  }
9171cb0ef41Sopenharmony_ci}
9181cb0ef41Sopenharmony_ci
9191cb0ef41Sopenharmony_ciconst box = new Box(true);
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ciutil.inspect(box);
9221cb0ef41Sopenharmony_ci// Returns: "Box< true >"
9231cb0ef41Sopenharmony_ci```
9241cb0ef41Sopenharmony_ci
9251cb0ef41Sopenharmony_ciCustom `[util.inspect.custom](depth, opts, inspect)` functions typically return
9261cb0ef41Sopenharmony_cia string but may return a value of any type that will be formatted accordingly
9271cb0ef41Sopenharmony_ciby `util.inspect()`.
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ci```js
9301cb0ef41Sopenharmony_ciconst util = require('node:util');
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ciconst obj = { foo: 'this will not show up in the inspect() output' };
9331cb0ef41Sopenharmony_ciobj[util.inspect.custom] = (depth) => {
9341cb0ef41Sopenharmony_ci  return { bar: 'baz' };
9351cb0ef41Sopenharmony_ci};
9361cb0ef41Sopenharmony_ci
9371cb0ef41Sopenharmony_ciutil.inspect(obj);
9381cb0ef41Sopenharmony_ci// Returns: "{ bar: 'baz' }"
9391cb0ef41Sopenharmony_ci```
9401cb0ef41Sopenharmony_ci
9411cb0ef41Sopenharmony_ci### `util.inspect.custom`
9421cb0ef41Sopenharmony_ci
9431cb0ef41Sopenharmony_ci<!-- YAML
9441cb0ef41Sopenharmony_ciadded: v6.6.0
9451cb0ef41Sopenharmony_cichanges:
9461cb0ef41Sopenharmony_ci  - version: v10.12.0
9471cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/20857
9481cb0ef41Sopenharmony_ci    description: This is now defined as a shared symbol.
9491cb0ef41Sopenharmony_ci-->
9501cb0ef41Sopenharmony_ci
9511cb0ef41Sopenharmony_ci* {symbol} that can be used to declare custom inspect functions.
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ciIn addition to being accessible through `util.inspect.custom`, this
9541cb0ef41Sopenharmony_cisymbol is [registered globally][global symbol registry] and can be
9551cb0ef41Sopenharmony_ciaccessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`.
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ciUsing this allows code to be written in a portable fashion, so that the custom
9581cb0ef41Sopenharmony_ciinspect function is used in an Node.js environment and ignored in the browser.
9591cb0ef41Sopenharmony_ciThe `util.inspect()` function itself is passed as third argument to the custom
9601cb0ef41Sopenharmony_ciinspect function to allow further portability.
9611cb0ef41Sopenharmony_ci
9621cb0ef41Sopenharmony_ci```js
9631cb0ef41Sopenharmony_ciconst customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ciclass Password {
9661cb0ef41Sopenharmony_ci  constructor(value) {
9671cb0ef41Sopenharmony_ci    this.value = value;
9681cb0ef41Sopenharmony_ci  }
9691cb0ef41Sopenharmony_ci
9701cb0ef41Sopenharmony_ci  toString() {
9711cb0ef41Sopenharmony_ci    return 'xxxxxxxx';
9721cb0ef41Sopenharmony_ci  }
9731cb0ef41Sopenharmony_ci
9741cb0ef41Sopenharmony_ci  [customInspectSymbol](depth, inspectOptions, inspect) {
9751cb0ef41Sopenharmony_ci    return `Password <${this.toString()}>`;
9761cb0ef41Sopenharmony_ci  }
9771cb0ef41Sopenharmony_ci}
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ciconst password = new Password('r0sebud');
9801cb0ef41Sopenharmony_ciconsole.log(password);
9811cb0ef41Sopenharmony_ci// Prints Password <xxxxxxxx>
9821cb0ef41Sopenharmony_ci```
9831cb0ef41Sopenharmony_ci
9841cb0ef41Sopenharmony_ciSee [Custom inspection functions on Objects][] for more details.
9851cb0ef41Sopenharmony_ci
9861cb0ef41Sopenharmony_ci### `util.inspect.defaultOptions`
9871cb0ef41Sopenharmony_ci
9881cb0ef41Sopenharmony_ci<!-- YAML
9891cb0ef41Sopenharmony_ciadded: v6.4.0
9901cb0ef41Sopenharmony_ci-->
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_ciThe `defaultOptions` value allows customization of the default options used by
9931cb0ef41Sopenharmony_ci`util.inspect`. This is useful for functions like `console.log` or
9941cb0ef41Sopenharmony_ci`util.format` which implicitly call into `util.inspect`. It shall be set to an
9951cb0ef41Sopenharmony_ciobject containing one or more valid [`util.inspect()`][] options. Setting
9961cb0ef41Sopenharmony_cioption properties directly is also supported.
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ci```js
9991cb0ef41Sopenharmony_ciconst util = require('node:util');
10001cb0ef41Sopenharmony_ciconst arr = Array(101).fill(0);
10011cb0ef41Sopenharmony_ci
10021cb0ef41Sopenharmony_ciconsole.log(arr); // Logs the truncated array
10031cb0ef41Sopenharmony_ciutil.inspect.defaultOptions.maxArrayLength = null;
10041cb0ef41Sopenharmony_ciconsole.log(arr); // logs the full array
10051cb0ef41Sopenharmony_ci```
10061cb0ef41Sopenharmony_ci
10071cb0ef41Sopenharmony_ci## `util.isDeepStrictEqual(val1, val2)`
10081cb0ef41Sopenharmony_ci
10091cb0ef41Sopenharmony_ci<!-- YAML
10101cb0ef41Sopenharmony_ciadded: v9.0.0
10111cb0ef41Sopenharmony_ci-->
10121cb0ef41Sopenharmony_ci
10131cb0ef41Sopenharmony_ci* `val1` {any}
10141cb0ef41Sopenharmony_ci* `val2` {any}
10151cb0ef41Sopenharmony_ci* Returns: {boolean}
10161cb0ef41Sopenharmony_ci
10171cb0ef41Sopenharmony_ciReturns `true` if there is deep strict equality between `val1` and `val2`.
10181cb0ef41Sopenharmony_ciOtherwise, returns `false`.
10191cb0ef41Sopenharmony_ci
10201cb0ef41Sopenharmony_ciSee [`assert.deepStrictEqual()`][] for more information about deep strict
10211cb0ef41Sopenharmony_ciequality.
10221cb0ef41Sopenharmony_ci
10231cb0ef41Sopenharmony_ci## Class: `util.MIMEType`
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci<!-- YAML
10261cb0ef41Sopenharmony_ciadded: v18.13.0
10271cb0ef41Sopenharmony_ci-->
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
10301cb0ef41Sopenharmony_ci
10311cb0ef41Sopenharmony_ciAn implementation of [the MIMEType class](https://bmeck.github.io/node-proposal-mime-api/).
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_ciIn accordance with browser conventions, all properties of `MIMEType` objects
10341cb0ef41Sopenharmony_ciare implemented as getters and setters on the class prototype, rather than as
10351cb0ef41Sopenharmony_cidata properties on the object itself.
10361cb0ef41Sopenharmony_ci
10371cb0ef41Sopenharmony_ciA MIME string is a structured string containing multiple meaningful
10381cb0ef41Sopenharmony_cicomponents. When parsed, a `MIMEType` object is returned containing
10391cb0ef41Sopenharmony_ciproperties for each of these components.
10401cb0ef41Sopenharmony_ci
10411cb0ef41Sopenharmony_ci### Constructor: `new MIMEType(input)`
10421cb0ef41Sopenharmony_ci
10431cb0ef41Sopenharmony_ci* `input` {string} The input MIME to parse
10441cb0ef41Sopenharmony_ci
10451cb0ef41Sopenharmony_ciCreates a new `MIMEType` object by parsing the `input`.
10461cb0ef41Sopenharmony_ci
10471cb0ef41Sopenharmony_ci```mjs
10481cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/plain');
10511cb0ef41Sopenharmony_ci```
10521cb0ef41Sopenharmony_ci
10531cb0ef41Sopenharmony_ci```cjs
10541cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
10551cb0ef41Sopenharmony_ci
10561cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/plain');
10571cb0ef41Sopenharmony_ci```
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ciA `TypeError` will be thrown if the `input` is not a valid MIME. Note
10601cb0ef41Sopenharmony_cithat an effort will be made to coerce the given values into strings. For
10611cb0ef41Sopenharmony_ciinstance:
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ci```mjs
10641cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
10651cb0ef41Sopenharmony_ciconst myMIME = new MIMEType({ toString: () => 'text/plain' });
10661cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
10671cb0ef41Sopenharmony_ci// Prints: text/plain
10681cb0ef41Sopenharmony_ci```
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ci```cjs
10711cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
10721cb0ef41Sopenharmony_ciconst myMIME = new MIMEType({ toString: () => 'text/plain' });
10731cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
10741cb0ef41Sopenharmony_ci// Prints: text/plain
10751cb0ef41Sopenharmony_ci```
10761cb0ef41Sopenharmony_ci
10771cb0ef41Sopenharmony_ci### `mime.type`
10781cb0ef41Sopenharmony_ci
10791cb0ef41Sopenharmony_ci* {string}
10801cb0ef41Sopenharmony_ci
10811cb0ef41Sopenharmony_ciGets and sets the type portion of the MIME.
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ci```mjs
10841cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
10851cb0ef41Sopenharmony_ci
10861cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/javascript');
10871cb0ef41Sopenharmony_ciconsole.log(myMIME.type);
10881cb0ef41Sopenharmony_ci// Prints: text
10891cb0ef41Sopenharmony_cimyMIME.type = 'application';
10901cb0ef41Sopenharmony_ciconsole.log(myMIME.type);
10911cb0ef41Sopenharmony_ci// Prints: application
10921cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
10931cb0ef41Sopenharmony_ci// Prints: application/javascript
10941cb0ef41Sopenharmony_ci```
10951cb0ef41Sopenharmony_ci
10961cb0ef41Sopenharmony_ci```cjs
10971cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
10981cb0ef41Sopenharmony_ci
10991cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/javascript');
11001cb0ef41Sopenharmony_ciconsole.log(myMIME.type);
11011cb0ef41Sopenharmony_ci// Prints: text
11021cb0ef41Sopenharmony_cimyMIME.type = 'application';
11031cb0ef41Sopenharmony_ciconsole.log(myMIME.type);
11041cb0ef41Sopenharmony_ci// Prints: application
11051cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
11061cb0ef41Sopenharmony_ci// Prints: application/javascript
11071cb0ef41Sopenharmony_ci```
11081cb0ef41Sopenharmony_ci
11091cb0ef41Sopenharmony_ci### `mime.subtype`
11101cb0ef41Sopenharmony_ci
11111cb0ef41Sopenharmony_ci* {string}
11121cb0ef41Sopenharmony_ci
11131cb0ef41Sopenharmony_ciGets and sets the subtype portion of the MIME.
11141cb0ef41Sopenharmony_ci
11151cb0ef41Sopenharmony_ci```mjs
11161cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
11171cb0ef41Sopenharmony_ci
11181cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/ecmascript');
11191cb0ef41Sopenharmony_ciconsole.log(myMIME.subtype);
11201cb0ef41Sopenharmony_ci// Prints: ecmascript
11211cb0ef41Sopenharmony_cimyMIME.subtype = 'javascript';
11221cb0ef41Sopenharmony_ciconsole.log(myMIME.subtype);
11231cb0ef41Sopenharmony_ci// Prints: javascript
11241cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
11251cb0ef41Sopenharmony_ci// Prints: text/javascript
11261cb0ef41Sopenharmony_ci```
11271cb0ef41Sopenharmony_ci
11281cb0ef41Sopenharmony_ci```cjs
11291cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
11301cb0ef41Sopenharmony_ci
11311cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/ecmascript');
11321cb0ef41Sopenharmony_ciconsole.log(myMIME.subtype);
11331cb0ef41Sopenharmony_ci// Prints: ecmascript
11341cb0ef41Sopenharmony_cimyMIME.subtype = 'javascript';
11351cb0ef41Sopenharmony_ciconsole.log(myMIME.subtype);
11361cb0ef41Sopenharmony_ci// Prints: javascript
11371cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
11381cb0ef41Sopenharmony_ci// Prints: text/javascript
11391cb0ef41Sopenharmony_ci```
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ci### `mime.essence`
11421cb0ef41Sopenharmony_ci
11431cb0ef41Sopenharmony_ci* {string}
11441cb0ef41Sopenharmony_ci
11451cb0ef41Sopenharmony_ciGets the essence of the MIME. This property is read only.
11461cb0ef41Sopenharmony_ciUse `mime.type` or `mime.subtype` to alter the MIME.
11471cb0ef41Sopenharmony_ci
11481cb0ef41Sopenharmony_ci```mjs
11491cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
11501cb0ef41Sopenharmony_ci
11511cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/javascript;key=value');
11521cb0ef41Sopenharmony_ciconsole.log(myMIME.essence);
11531cb0ef41Sopenharmony_ci// Prints: text/javascript
11541cb0ef41Sopenharmony_cimyMIME.type = 'application';
11551cb0ef41Sopenharmony_ciconsole.log(myMIME.essence);
11561cb0ef41Sopenharmony_ci// Prints: application/javascript
11571cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
11581cb0ef41Sopenharmony_ci// Prints: application/javascript;key=value
11591cb0ef41Sopenharmony_ci```
11601cb0ef41Sopenharmony_ci
11611cb0ef41Sopenharmony_ci```cjs
11621cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ciconst myMIME = new MIMEType('text/javascript;key=value');
11651cb0ef41Sopenharmony_ciconsole.log(myMIME.essence);
11661cb0ef41Sopenharmony_ci// Prints: text/javascript
11671cb0ef41Sopenharmony_cimyMIME.type = 'application';
11681cb0ef41Sopenharmony_ciconsole.log(myMIME.essence);
11691cb0ef41Sopenharmony_ci// Prints: application/javascript
11701cb0ef41Sopenharmony_ciconsole.log(String(myMIME));
11711cb0ef41Sopenharmony_ci// Prints: application/javascript;key=value
11721cb0ef41Sopenharmony_ci```
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci### `mime.params`
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci* {MIMEParams}
11771cb0ef41Sopenharmony_ci
11781cb0ef41Sopenharmony_ciGets the [`MIMEParams`][] object representing the
11791cb0ef41Sopenharmony_ciparameters of the MIME. This property is read-only. See
11801cb0ef41Sopenharmony_ci[`MIMEParams`][] documentation for details.
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_ci### `mime.toString()`
11831cb0ef41Sopenharmony_ci
11841cb0ef41Sopenharmony_ci* Returns: {string}
11851cb0ef41Sopenharmony_ci
11861cb0ef41Sopenharmony_ciThe `toString()` method on the `MIMEType` object returns the serialized MIME.
11871cb0ef41Sopenharmony_ci
11881cb0ef41Sopenharmony_ciBecause of the need for standard compliance, this method does not allow users
11891cb0ef41Sopenharmony_cito customize the serialization process of the MIME.
11901cb0ef41Sopenharmony_ci
11911cb0ef41Sopenharmony_ci### `mime.toJSON()`
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci* Returns: {string}
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ciAlias for [`mime.toString()`][].
11961cb0ef41Sopenharmony_ci
11971cb0ef41Sopenharmony_ciThis method is automatically called when an `MIMEType` object is serialized
11981cb0ef41Sopenharmony_ciwith [`JSON.stringify()`][].
11991cb0ef41Sopenharmony_ci
12001cb0ef41Sopenharmony_ci```mjs
12011cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
12021cb0ef41Sopenharmony_ci
12031cb0ef41Sopenharmony_ciconst myMIMES = [
12041cb0ef41Sopenharmony_ci  new MIMEType('image/png'),
12051cb0ef41Sopenharmony_ci  new MIMEType('image/gif'),
12061cb0ef41Sopenharmony_ci];
12071cb0ef41Sopenharmony_ciconsole.log(JSON.stringify(myMIMES));
12081cb0ef41Sopenharmony_ci// Prints: ["image/png", "image/gif"]
12091cb0ef41Sopenharmony_ci```
12101cb0ef41Sopenharmony_ci
12111cb0ef41Sopenharmony_ci```cjs
12121cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
12131cb0ef41Sopenharmony_ci
12141cb0ef41Sopenharmony_ciconst myMIMES = [
12151cb0ef41Sopenharmony_ci  new MIMEType('image/png'),
12161cb0ef41Sopenharmony_ci  new MIMEType('image/gif'),
12171cb0ef41Sopenharmony_ci];
12181cb0ef41Sopenharmony_ciconsole.log(JSON.stringify(myMIMES));
12191cb0ef41Sopenharmony_ci// Prints: ["image/png", "image/gif"]
12201cb0ef41Sopenharmony_ci```
12211cb0ef41Sopenharmony_ci
12221cb0ef41Sopenharmony_ci## Class: `util.MIMEParams`
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_ci<!-- YAML
12251cb0ef41Sopenharmony_ciadded: v18.13.0
12261cb0ef41Sopenharmony_ci-->
12271cb0ef41Sopenharmony_ci
12281cb0ef41Sopenharmony_ciThe `MIMEParams` API provides read and write access to the parameters of a
12291cb0ef41Sopenharmony_ci`MIMEType`.
12301cb0ef41Sopenharmony_ci
12311cb0ef41Sopenharmony_ci### Constructor: `new MIMEParams()`
12321cb0ef41Sopenharmony_ci
12331cb0ef41Sopenharmony_ciCreates a new `MIMEParams` object by with empty parameters
12341cb0ef41Sopenharmony_ci
12351cb0ef41Sopenharmony_ci```mjs
12361cb0ef41Sopenharmony_ciimport { MIMEParams } from 'node:util';
12371cb0ef41Sopenharmony_ci
12381cb0ef41Sopenharmony_ciconst myParams = new MIMEParams();
12391cb0ef41Sopenharmony_ci```
12401cb0ef41Sopenharmony_ci
12411cb0ef41Sopenharmony_ci```cjs
12421cb0ef41Sopenharmony_ciconst { MIMEParams } = require('node:util');
12431cb0ef41Sopenharmony_ci
12441cb0ef41Sopenharmony_ciconst myParams = new MIMEParams();
12451cb0ef41Sopenharmony_ci```
12461cb0ef41Sopenharmony_ci
12471cb0ef41Sopenharmony_ci### `mimeParams.delete(name)`
12481cb0ef41Sopenharmony_ci
12491cb0ef41Sopenharmony_ci* `name` {string}
12501cb0ef41Sopenharmony_ci
12511cb0ef41Sopenharmony_ciRemove all name-value pairs whose name is `name`.
12521cb0ef41Sopenharmony_ci
12531cb0ef41Sopenharmony_ci### `mimeParams.entries()`
12541cb0ef41Sopenharmony_ci
12551cb0ef41Sopenharmony_ci* Returns: {Iterator}
12561cb0ef41Sopenharmony_ci
12571cb0ef41Sopenharmony_ciReturns an iterator over each of the name-value pairs in the parameters.
12581cb0ef41Sopenharmony_ciEach item of the iterator is a JavaScript `Array`. The first item of the array
12591cb0ef41Sopenharmony_ciis the `name`, the second item of the array is the `value`.
12601cb0ef41Sopenharmony_ci
12611cb0ef41Sopenharmony_ci### `mimeParams.get(name)`
12621cb0ef41Sopenharmony_ci
12631cb0ef41Sopenharmony_ci* `name` {string}
12641cb0ef41Sopenharmony_ci* Returns: {string} or `null` if there is no name-value pair with the given
12651cb0ef41Sopenharmony_ci  `name`.
12661cb0ef41Sopenharmony_ci
12671cb0ef41Sopenharmony_ciReturns the value of the first name-value pair whose name is `name`. If there
12681cb0ef41Sopenharmony_ciare no such pairs, `null` is returned.
12691cb0ef41Sopenharmony_ci
12701cb0ef41Sopenharmony_ci### `mimeParams.has(name)`
12711cb0ef41Sopenharmony_ci
12721cb0ef41Sopenharmony_ci* `name` {string}
12731cb0ef41Sopenharmony_ci* Returns: {boolean}
12741cb0ef41Sopenharmony_ci
12751cb0ef41Sopenharmony_ciReturns `true` if there is at least one name-value pair whose name is `name`.
12761cb0ef41Sopenharmony_ci
12771cb0ef41Sopenharmony_ci### `mimeParams.keys()`
12781cb0ef41Sopenharmony_ci
12791cb0ef41Sopenharmony_ci* Returns: {Iterator}
12801cb0ef41Sopenharmony_ci
12811cb0ef41Sopenharmony_ciReturns an iterator over the names of each name-value pair.
12821cb0ef41Sopenharmony_ci
12831cb0ef41Sopenharmony_ci```mjs
12841cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=0;bar=1');
12871cb0ef41Sopenharmony_cifor (const name of params.keys()) {
12881cb0ef41Sopenharmony_ci  console.log(name);
12891cb0ef41Sopenharmony_ci}
12901cb0ef41Sopenharmony_ci// Prints:
12911cb0ef41Sopenharmony_ci//   foo
12921cb0ef41Sopenharmony_ci//   bar
12931cb0ef41Sopenharmony_ci```
12941cb0ef41Sopenharmony_ci
12951cb0ef41Sopenharmony_ci```cjs
12961cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
12971cb0ef41Sopenharmony_ci
12981cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=0;bar=1');
12991cb0ef41Sopenharmony_cifor (const name of params.keys()) {
13001cb0ef41Sopenharmony_ci  console.log(name);
13011cb0ef41Sopenharmony_ci}
13021cb0ef41Sopenharmony_ci// Prints:
13031cb0ef41Sopenharmony_ci//   foo
13041cb0ef41Sopenharmony_ci//   bar
13051cb0ef41Sopenharmony_ci```
13061cb0ef41Sopenharmony_ci
13071cb0ef41Sopenharmony_ci### `mimeParams.set(name, value)`
13081cb0ef41Sopenharmony_ci
13091cb0ef41Sopenharmony_ci* `name` {string}
13101cb0ef41Sopenharmony_ci* `value` {string}
13111cb0ef41Sopenharmony_ci
13121cb0ef41Sopenharmony_ciSets the value in the `MIMEParams` object associated with `name` to
13131cb0ef41Sopenharmony_ci`value`. If there are any pre-existing name-value pairs whose names are `name`,
13141cb0ef41Sopenharmony_ciset the first such pair's value to `value`.
13151cb0ef41Sopenharmony_ci
13161cb0ef41Sopenharmony_ci```mjs
13171cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
13181cb0ef41Sopenharmony_ci
13191cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=0;bar=1');
13201cb0ef41Sopenharmony_ciparams.set('foo', 'def');
13211cb0ef41Sopenharmony_ciparams.set('baz', 'xyz');
13221cb0ef41Sopenharmony_ciconsole.log(params.toString());
13231cb0ef41Sopenharmony_ci// Prints: foo=def&bar=1&baz=xyz
13241cb0ef41Sopenharmony_ci```
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ci```cjs
13271cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
13281cb0ef41Sopenharmony_ci
13291cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=0;bar=1');
13301cb0ef41Sopenharmony_ciparams.set('foo', 'def');
13311cb0ef41Sopenharmony_ciparams.set('baz', 'xyz');
13321cb0ef41Sopenharmony_ciconsole.log(params.toString());
13331cb0ef41Sopenharmony_ci// Prints: foo=def&bar=1&baz=xyz
13341cb0ef41Sopenharmony_ci```
13351cb0ef41Sopenharmony_ci
13361cb0ef41Sopenharmony_ci### `mimeParams.values()`
13371cb0ef41Sopenharmony_ci
13381cb0ef41Sopenharmony_ci* Returns: {Iterator}
13391cb0ef41Sopenharmony_ci
13401cb0ef41Sopenharmony_ciReturns an iterator over the values of each name-value pair.
13411cb0ef41Sopenharmony_ci
13421cb0ef41Sopenharmony_ci### `mimeParams[@@iterator]()`
13431cb0ef41Sopenharmony_ci
13441cb0ef41Sopenharmony_ci* Returns: {Iterator}
13451cb0ef41Sopenharmony_ci
13461cb0ef41Sopenharmony_ciAlias for [`mimeParams.entries()`][].
13471cb0ef41Sopenharmony_ci
13481cb0ef41Sopenharmony_ci```mjs
13491cb0ef41Sopenharmony_ciimport { MIMEType } from 'node:util';
13501cb0ef41Sopenharmony_ci
13511cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=bar;xyz=baz');
13521cb0ef41Sopenharmony_cifor (const [name, value] of params) {
13531cb0ef41Sopenharmony_ci  console.log(name, value);
13541cb0ef41Sopenharmony_ci}
13551cb0ef41Sopenharmony_ci// Prints:
13561cb0ef41Sopenharmony_ci//   foo bar
13571cb0ef41Sopenharmony_ci//   xyz baz
13581cb0ef41Sopenharmony_ci```
13591cb0ef41Sopenharmony_ci
13601cb0ef41Sopenharmony_ci```cjs
13611cb0ef41Sopenharmony_ciconst { MIMEType } = require('node:util');
13621cb0ef41Sopenharmony_ci
13631cb0ef41Sopenharmony_ciconst { params } = new MIMEType('text/plain;foo=bar;xyz=baz');
13641cb0ef41Sopenharmony_cifor (const [name, value] of params) {
13651cb0ef41Sopenharmony_ci  console.log(name, value);
13661cb0ef41Sopenharmony_ci}
13671cb0ef41Sopenharmony_ci// Prints:
13681cb0ef41Sopenharmony_ci//   foo bar
13691cb0ef41Sopenharmony_ci//   xyz baz
13701cb0ef41Sopenharmony_ci```
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci## `util.parseArgs([config])`
13731cb0ef41Sopenharmony_ci
13741cb0ef41Sopenharmony_ci<!-- YAML
13751cb0ef41Sopenharmony_ciadded: v18.3.0
13761cb0ef41Sopenharmony_cichanges:
13771cb0ef41Sopenharmony_ci  - version: v18.11.0
13781cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/44631
13791cb0ef41Sopenharmony_ci    description: Add support for default values in input `config`.
13801cb0ef41Sopenharmony_ci  - version:
13811cb0ef41Sopenharmony_ci    - v18.7.0
13821cb0ef41Sopenharmony_ci    - v16.17.0
13831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/43459
13841cb0ef41Sopenharmony_ci    description: add support for returning detailed parse information
13851cb0ef41Sopenharmony_ci                 using `tokens` in input `config` and returned properties.
13861cb0ef41Sopenharmony_ci-->
13871cb0ef41Sopenharmony_ci
13881cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
13891cb0ef41Sopenharmony_ci
13901cb0ef41Sopenharmony_ci* `config` {Object} Used to provide arguments for parsing and to configure
13911cb0ef41Sopenharmony_ci  the parser. `config` supports the following properties:
13921cb0ef41Sopenharmony_ci  * `args` {string\[]} array of argument strings. **Default:** `process.argv`
13931cb0ef41Sopenharmony_ci    with `execPath` and `filename` removed.
13941cb0ef41Sopenharmony_ci  * `options` {Object} Used to describe arguments known to the parser.
13951cb0ef41Sopenharmony_ci    Keys of `options` are the long names of options and values are an
13961cb0ef41Sopenharmony_ci    {Object} accepting the following properties:
13971cb0ef41Sopenharmony_ci    * `type` {string} Type of argument, which must be either `boolean` or `string`.
13981cb0ef41Sopenharmony_ci    * `multiple` {boolean} Whether this option can be provided multiple
13991cb0ef41Sopenharmony_ci      times. If `true`, all values will be collected in an array. If
14001cb0ef41Sopenharmony_ci      `false`, values for the option are last-wins. **Default:** `false`.
14011cb0ef41Sopenharmony_ci    * `short` {string} A single character alias for the option.
14021cb0ef41Sopenharmony_ci    * `default` {string | boolean | string\[] | boolean\[]} The default option
14031cb0ef41Sopenharmony_ci      value when it is not set by args. It must be of the same type as the
14041cb0ef41Sopenharmony_ci      `type` property. When `multiple` is `true`, it must be an array.
14051cb0ef41Sopenharmony_ci  * `strict` {boolean} Should an error be thrown when unknown arguments
14061cb0ef41Sopenharmony_ci    are encountered, or when arguments are passed that do not match the
14071cb0ef41Sopenharmony_ci    `type` configured in `options`.
14081cb0ef41Sopenharmony_ci    **Default:** `true`.
14091cb0ef41Sopenharmony_ci  * `allowPositionals` {boolean} Whether this command accepts positional
14101cb0ef41Sopenharmony_ci    arguments.
14111cb0ef41Sopenharmony_ci    **Default:** `false` if `strict` is `true`, otherwise `true`.
14121cb0ef41Sopenharmony_ci  * `tokens` {boolean} Return the parsed tokens. This is useful for extending
14131cb0ef41Sopenharmony_ci    the built-in behavior, from adding additional checks through to reprocessing
14141cb0ef41Sopenharmony_ci    the tokens in different ways.
14151cb0ef41Sopenharmony_ci    **Default:** `false`.
14161cb0ef41Sopenharmony_ci
14171cb0ef41Sopenharmony_ci* Returns: {Object} The parsed command line arguments:
14181cb0ef41Sopenharmony_ci  * `values` {Object} A mapping of parsed option names with their {string}
14191cb0ef41Sopenharmony_ci    or {boolean} values.
14201cb0ef41Sopenharmony_ci  * `positionals` {string\[]} Positional arguments.
14211cb0ef41Sopenharmony_ci  * `tokens` {Object\[] | undefined} See [parseArgs tokens](#parseargs-tokens)
14221cb0ef41Sopenharmony_ci    section. Only returned if `config` includes `tokens: true`.
14231cb0ef41Sopenharmony_ci
14241cb0ef41Sopenharmony_ciProvides a higher level API for command-line argument parsing than interacting
14251cb0ef41Sopenharmony_ciwith `process.argv` directly. Takes a specification for the expected arguments
14261cb0ef41Sopenharmony_ciand returns a structured object with the parsed options and positionals.
14271cb0ef41Sopenharmony_ci
14281cb0ef41Sopenharmony_ci```mjs
14291cb0ef41Sopenharmony_ciimport { parseArgs } from 'node:util';
14301cb0ef41Sopenharmony_ciconst args = ['-f', '--bar', 'b'];
14311cb0ef41Sopenharmony_ciconst options = {
14321cb0ef41Sopenharmony_ci  foo: {
14331cb0ef41Sopenharmony_ci    type: 'boolean',
14341cb0ef41Sopenharmony_ci    short: 'f',
14351cb0ef41Sopenharmony_ci  },
14361cb0ef41Sopenharmony_ci  bar: {
14371cb0ef41Sopenharmony_ci    type: 'string',
14381cb0ef41Sopenharmony_ci  },
14391cb0ef41Sopenharmony_ci};
14401cb0ef41Sopenharmony_ciconst {
14411cb0ef41Sopenharmony_ci  values,
14421cb0ef41Sopenharmony_ci  positionals,
14431cb0ef41Sopenharmony_ci} = parseArgs({ args, options });
14441cb0ef41Sopenharmony_ciconsole.log(values, positionals);
14451cb0ef41Sopenharmony_ci// Prints: [Object: null prototype] { foo: true, bar: 'b' } []
14461cb0ef41Sopenharmony_ci```
14471cb0ef41Sopenharmony_ci
14481cb0ef41Sopenharmony_ci```cjs
14491cb0ef41Sopenharmony_ciconst { parseArgs } = require('node:util');
14501cb0ef41Sopenharmony_ciconst args = ['-f', '--bar', 'b'];
14511cb0ef41Sopenharmony_ciconst options = {
14521cb0ef41Sopenharmony_ci  foo: {
14531cb0ef41Sopenharmony_ci    type: 'boolean',
14541cb0ef41Sopenharmony_ci    short: 'f',
14551cb0ef41Sopenharmony_ci  },
14561cb0ef41Sopenharmony_ci  bar: {
14571cb0ef41Sopenharmony_ci    type: 'string',
14581cb0ef41Sopenharmony_ci  },
14591cb0ef41Sopenharmony_ci};
14601cb0ef41Sopenharmony_ciconst {
14611cb0ef41Sopenharmony_ci  values,
14621cb0ef41Sopenharmony_ci  positionals,
14631cb0ef41Sopenharmony_ci} = parseArgs({ args, options });
14641cb0ef41Sopenharmony_ciconsole.log(values, positionals);
14651cb0ef41Sopenharmony_ci// Prints: [Object: null prototype] { foo: true, bar: 'b' } []
14661cb0ef41Sopenharmony_ci```
14671cb0ef41Sopenharmony_ci
14681cb0ef41Sopenharmony_ci`util.parseArgs` is experimental and behavior may change. Join the
14691cb0ef41Sopenharmony_ciconversation in [pkgjs/parseargs][] to contribute to the design.
14701cb0ef41Sopenharmony_ci
14711cb0ef41Sopenharmony_ci### `parseArgs` `tokens`
14721cb0ef41Sopenharmony_ci
14731cb0ef41Sopenharmony_ciDetailed parse information is available for adding custom behaviors by
14741cb0ef41Sopenharmony_cispecifying `tokens: true` in the configuration.
14751cb0ef41Sopenharmony_ciThe returned tokens have properties describing:
14761cb0ef41Sopenharmony_ci
14771cb0ef41Sopenharmony_ci* all tokens
14781cb0ef41Sopenharmony_ci  * `kind` {string} One of 'option', 'positional', or 'option-terminator'.
14791cb0ef41Sopenharmony_ci  * `index` {number} Index of element in `args` containing token. So the
14801cb0ef41Sopenharmony_ci    source argument for a token is `args[token.index]`.
14811cb0ef41Sopenharmony_ci* option tokens
14821cb0ef41Sopenharmony_ci  * `name` {string} Long name of option.
14831cb0ef41Sopenharmony_ci  * `rawName` {string} How option used in args, like `-f` of `--foo`.
14841cb0ef41Sopenharmony_ci  * `value` {string | undefined} Option value specified in args.
14851cb0ef41Sopenharmony_ci    Undefined for boolean options.
14861cb0ef41Sopenharmony_ci  * `inlineValue` {boolean | undefined} Whether option value specified inline,
14871cb0ef41Sopenharmony_ci    like `--foo=bar`.
14881cb0ef41Sopenharmony_ci* positional tokens
14891cb0ef41Sopenharmony_ci  * `value` {string} The value of the positional argument in args (i.e. `args[index]`).
14901cb0ef41Sopenharmony_ci* option-terminator token
14911cb0ef41Sopenharmony_ci
14921cb0ef41Sopenharmony_ciThe returned tokens are in the order encountered in the input args. Options
14931cb0ef41Sopenharmony_cithat appear more than once in args produce a token for each use. Short option
14941cb0ef41Sopenharmony_cigroups like `-xy` expand to a token for each option. So `-xxx` produces
14951cb0ef41Sopenharmony_cithree tokens.
14961cb0ef41Sopenharmony_ci
14971cb0ef41Sopenharmony_ciFor example to use the returned tokens to add support for a negated option
14981cb0ef41Sopenharmony_cilike `--no-color`, the tokens can be reprocessed to change the value stored
14991cb0ef41Sopenharmony_cifor the negated option.
15001cb0ef41Sopenharmony_ci
15011cb0ef41Sopenharmony_ci```mjs
15021cb0ef41Sopenharmony_ciimport { parseArgs } from 'node:util';
15031cb0ef41Sopenharmony_ci
15041cb0ef41Sopenharmony_ciconst options = {
15051cb0ef41Sopenharmony_ci  'color': { type: 'boolean' },
15061cb0ef41Sopenharmony_ci  'no-color': { type: 'boolean' },
15071cb0ef41Sopenharmony_ci  'logfile': { type: 'string' },
15081cb0ef41Sopenharmony_ci  'no-logfile': { type: 'boolean' },
15091cb0ef41Sopenharmony_ci};
15101cb0ef41Sopenharmony_ciconst { values, tokens } = parseArgs({ options, tokens: true });
15111cb0ef41Sopenharmony_ci
15121cb0ef41Sopenharmony_ci// Reprocess the option tokens and overwrite the returned values.
15131cb0ef41Sopenharmony_citokens
15141cb0ef41Sopenharmony_ci  .filter((token) => token.kind === 'option')
15151cb0ef41Sopenharmony_ci  .forEach((token) => {
15161cb0ef41Sopenharmony_ci    if (token.name.startsWith('no-')) {
15171cb0ef41Sopenharmony_ci      // Store foo:false for --no-foo
15181cb0ef41Sopenharmony_ci      const positiveName = token.name.slice(3);
15191cb0ef41Sopenharmony_ci      values[positiveName] = false;
15201cb0ef41Sopenharmony_ci      delete values[token.name];
15211cb0ef41Sopenharmony_ci    } else {
15221cb0ef41Sopenharmony_ci      // Resave value so last one wins if both --foo and --no-foo.
15231cb0ef41Sopenharmony_ci      values[token.name] = token.value ?? true;
15241cb0ef41Sopenharmony_ci    }
15251cb0ef41Sopenharmony_ci  });
15261cb0ef41Sopenharmony_ci
15271cb0ef41Sopenharmony_ciconst color = values.color;
15281cb0ef41Sopenharmony_ciconst logfile = values.logfile ?? 'default.log';
15291cb0ef41Sopenharmony_ci
15301cb0ef41Sopenharmony_ciconsole.log({ logfile, color });
15311cb0ef41Sopenharmony_ci```
15321cb0ef41Sopenharmony_ci
15331cb0ef41Sopenharmony_ci```cjs
15341cb0ef41Sopenharmony_ciconst { parseArgs } = require('node:util');
15351cb0ef41Sopenharmony_ci
15361cb0ef41Sopenharmony_ciconst options = {
15371cb0ef41Sopenharmony_ci  'color': { type: 'boolean' },
15381cb0ef41Sopenharmony_ci  'no-color': { type: 'boolean' },
15391cb0ef41Sopenharmony_ci  'logfile': { type: 'string' },
15401cb0ef41Sopenharmony_ci  'no-logfile': { type: 'boolean' },
15411cb0ef41Sopenharmony_ci};
15421cb0ef41Sopenharmony_ciconst { values, tokens } = parseArgs({ options, tokens: true });
15431cb0ef41Sopenharmony_ci
15441cb0ef41Sopenharmony_ci// Reprocess the option tokens and overwrite the returned values.
15451cb0ef41Sopenharmony_citokens
15461cb0ef41Sopenharmony_ci  .filter((token) => token.kind === 'option')
15471cb0ef41Sopenharmony_ci  .forEach((token) => {
15481cb0ef41Sopenharmony_ci    if (token.name.startsWith('no-')) {
15491cb0ef41Sopenharmony_ci      // Store foo:false for --no-foo
15501cb0ef41Sopenharmony_ci      const positiveName = token.name.slice(3);
15511cb0ef41Sopenharmony_ci      values[positiveName] = false;
15521cb0ef41Sopenharmony_ci      delete values[token.name];
15531cb0ef41Sopenharmony_ci    } else {
15541cb0ef41Sopenharmony_ci      // Resave value so last one wins if both --foo and --no-foo.
15551cb0ef41Sopenharmony_ci      values[token.name] = token.value ?? true;
15561cb0ef41Sopenharmony_ci    }
15571cb0ef41Sopenharmony_ci  });
15581cb0ef41Sopenharmony_ci
15591cb0ef41Sopenharmony_ciconst color = values.color;
15601cb0ef41Sopenharmony_ciconst logfile = values.logfile ?? 'default.log';
15611cb0ef41Sopenharmony_ci
15621cb0ef41Sopenharmony_ciconsole.log({ logfile, color });
15631cb0ef41Sopenharmony_ci```
15641cb0ef41Sopenharmony_ci
15651cb0ef41Sopenharmony_ciExample usage showing negated options, and when an option is used
15661cb0ef41Sopenharmony_cimultiple ways then last one wins.
15671cb0ef41Sopenharmony_ci
15681cb0ef41Sopenharmony_ci```console
15691cb0ef41Sopenharmony_ci$ node negate.js
15701cb0ef41Sopenharmony_ci{ logfile: 'default.log', color: undefined }
15711cb0ef41Sopenharmony_ci$ node negate.js --no-logfile --no-color
15721cb0ef41Sopenharmony_ci{ logfile: false, color: false }
15731cb0ef41Sopenharmony_ci$ node negate.js --logfile=test.log --color
15741cb0ef41Sopenharmony_ci{ logfile: 'test.log', color: true }
15751cb0ef41Sopenharmony_ci$ node negate.js --no-logfile --logfile=test.log --color --no-color
15761cb0ef41Sopenharmony_ci{ logfile: 'test.log', color: false }
15771cb0ef41Sopenharmony_ci```
15781cb0ef41Sopenharmony_ci
15791cb0ef41Sopenharmony_ci## `util.promisify(original)`
15801cb0ef41Sopenharmony_ci
15811cb0ef41Sopenharmony_ci<!-- YAML
15821cb0ef41Sopenharmony_ciadded: v8.0.0
15831cb0ef41Sopenharmony_ci-->
15841cb0ef41Sopenharmony_ci
15851cb0ef41Sopenharmony_ci* `original` {Function}
15861cb0ef41Sopenharmony_ci* Returns: {Function}
15871cb0ef41Sopenharmony_ci
15881cb0ef41Sopenharmony_ciTakes a function following the common error-first callback style, i.e. taking
15891cb0ef41Sopenharmony_cian `(err, value) => ...` callback as the last argument, and returns a version
15901cb0ef41Sopenharmony_cithat returns promises.
15911cb0ef41Sopenharmony_ci
15921cb0ef41Sopenharmony_ci```js
15931cb0ef41Sopenharmony_ciconst util = require('node:util');
15941cb0ef41Sopenharmony_ciconst fs = require('node:fs');
15951cb0ef41Sopenharmony_ci
15961cb0ef41Sopenharmony_ciconst stat = util.promisify(fs.stat);
15971cb0ef41Sopenharmony_cistat('.').then((stats) => {
15981cb0ef41Sopenharmony_ci  // Do something with `stats`
15991cb0ef41Sopenharmony_ci}).catch((error) => {
16001cb0ef41Sopenharmony_ci  // Handle the error.
16011cb0ef41Sopenharmony_ci});
16021cb0ef41Sopenharmony_ci```
16031cb0ef41Sopenharmony_ci
16041cb0ef41Sopenharmony_ciOr, equivalently using `async function`s:
16051cb0ef41Sopenharmony_ci
16061cb0ef41Sopenharmony_ci```js
16071cb0ef41Sopenharmony_ciconst util = require('node:util');
16081cb0ef41Sopenharmony_ciconst fs = require('node:fs');
16091cb0ef41Sopenharmony_ci
16101cb0ef41Sopenharmony_ciconst stat = util.promisify(fs.stat);
16111cb0ef41Sopenharmony_ci
16121cb0ef41Sopenharmony_ciasync function callStat() {
16131cb0ef41Sopenharmony_ci  const stats = await stat('.');
16141cb0ef41Sopenharmony_ci  console.log(`This directory is owned by ${stats.uid}`);
16151cb0ef41Sopenharmony_ci}
16161cb0ef41Sopenharmony_ci```
16171cb0ef41Sopenharmony_ci
16181cb0ef41Sopenharmony_ciIf there is an `original[util.promisify.custom]` property present, `promisify`
16191cb0ef41Sopenharmony_ciwill return its value, see [Custom promisified functions][].
16201cb0ef41Sopenharmony_ci
16211cb0ef41Sopenharmony_ci`promisify()` assumes that `original` is a function taking a callback as its
16221cb0ef41Sopenharmony_cifinal argument in all cases. If `original` is not a function, `promisify()`
16231cb0ef41Sopenharmony_ciwill throw an error. If `original` is a function but its last argument is not
16241cb0ef41Sopenharmony_cian error-first callback, it will still be passed an error-first
16251cb0ef41Sopenharmony_cicallback as its last argument.
16261cb0ef41Sopenharmony_ci
16271cb0ef41Sopenharmony_ciUsing `promisify()` on class methods or other methods that use `this` may not
16281cb0ef41Sopenharmony_ciwork as expected unless handled specially:
16291cb0ef41Sopenharmony_ci
16301cb0ef41Sopenharmony_ci```js
16311cb0ef41Sopenharmony_ciconst util = require('node:util');
16321cb0ef41Sopenharmony_ci
16331cb0ef41Sopenharmony_ciclass Foo {
16341cb0ef41Sopenharmony_ci  constructor() {
16351cb0ef41Sopenharmony_ci    this.a = 42;
16361cb0ef41Sopenharmony_ci  }
16371cb0ef41Sopenharmony_ci
16381cb0ef41Sopenharmony_ci  bar(callback) {
16391cb0ef41Sopenharmony_ci    callback(null, this.a);
16401cb0ef41Sopenharmony_ci  }
16411cb0ef41Sopenharmony_ci}
16421cb0ef41Sopenharmony_ci
16431cb0ef41Sopenharmony_ciconst foo = new Foo();
16441cb0ef41Sopenharmony_ci
16451cb0ef41Sopenharmony_ciconst naiveBar = util.promisify(foo.bar);
16461cb0ef41Sopenharmony_ci// TypeError: Cannot read property 'a' of undefined
16471cb0ef41Sopenharmony_ci// naiveBar().then(a => console.log(a));
16481cb0ef41Sopenharmony_ci
16491cb0ef41Sopenharmony_cinaiveBar.call(foo).then((a) => console.log(a)); // '42'
16501cb0ef41Sopenharmony_ci
16511cb0ef41Sopenharmony_ciconst bindBar = naiveBar.bind(foo);
16521cb0ef41Sopenharmony_cibindBar().then((a) => console.log(a)); // '42'
16531cb0ef41Sopenharmony_ci```
16541cb0ef41Sopenharmony_ci
16551cb0ef41Sopenharmony_ci### Custom promisified functions
16561cb0ef41Sopenharmony_ci
16571cb0ef41Sopenharmony_ciUsing the `util.promisify.custom` symbol one can override the return value of
16581cb0ef41Sopenharmony_ci[`util.promisify()`][]:
16591cb0ef41Sopenharmony_ci
16601cb0ef41Sopenharmony_ci```js
16611cb0ef41Sopenharmony_ciconst util = require('node:util');
16621cb0ef41Sopenharmony_ci
16631cb0ef41Sopenharmony_cifunction doSomething(foo, callback) {
16641cb0ef41Sopenharmony_ci  // ...
16651cb0ef41Sopenharmony_ci}
16661cb0ef41Sopenharmony_ci
16671cb0ef41Sopenharmony_cidoSomething[util.promisify.custom] = (foo) => {
16681cb0ef41Sopenharmony_ci  return getPromiseSomehow();
16691cb0ef41Sopenharmony_ci};
16701cb0ef41Sopenharmony_ci
16711cb0ef41Sopenharmony_ciconst promisified = util.promisify(doSomething);
16721cb0ef41Sopenharmony_ciconsole.log(promisified === doSomething[util.promisify.custom]);
16731cb0ef41Sopenharmony_ci// prints 'true'
16741cb0ef41Sopenharmony_ci```
16751cb0ef41Sopenharmony_ci
16761cb0ef41Sopenharmony_ciThis can be useful for cases where the original function does not follow the
16771cb0ef41Sopenharmony_cistandard format of taking an error-first callback as the last argument.
16781cb0ef41Sopenharmony_ci
16791cb0ef41Sopenharmony_ciFor example, with a function that takes in
16801cb0ef41Sopenharmony_ci`(foo, onSuccessCallback, onErrorCallback)`:
16811cb0ef41Sopenharmony_ci
16821cb0ef41Sopenharmony_ci```js
16831cb0ef41Sopenharmony_cidoSomething[util.promisify.custom] = (foo) => {
16841cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
16851cb0ef41Sopenharmony_ci    doSomething(foo, resolve, reject);
16861cb0ef41Sopenharmony_ci  });
16871cb0ef41Sopenharmony_ci};
16881cb0ef41Sopenharmony_ci```
16891cb0ef41Sopenharmony_ci
16901cb0ef41Sopenharmony_ciIf `promisify.custom` is defined but is not a function, `promisify()` will
16911cb0ef41Sopenharmony_cithrow an error.
16921cb0ef41Sopenharmony_ci
16931cb0ef41Sopenharmony_ci### `util.promisify.custom`
16941cb0ef41Sopenharmony_ci
16951cb0ef41Sopenharmony_ci<!-- YAML
16961cb0ef41Sopenharmony_ciadded: v8.0.0
16971cb0ef41Sopenharmony_cichanges:
16981cb0ef41Sopenharmony_ci  - version:
16991cb0ef41Sopenharmony_ci      - v13.12.0
17001cb0ef41Sopenharmony_ci      - v12.16.2
17011cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/31672
17021cb0ef41Sopenharmony_ci    description: This is now defined as a shared symbol.
17031cb0ef41Sopenharmony_ci-->
17041cb0ef41Sopenharmony_ci
17051cb0ef41Sopenharmony_ci* {symbol} that can be used to declare custom promisified variants of functions,
17061cb0ef41Sopenharmony_ci  see [Custom promisified functions][].
17071cb0ef41Sopenharmony_ci
17081cb0ef41Sopenharmony_ciIn addition to being accessible through `util.promisify.custom`, this
17091cb0ef41Sopenharmony_cisymbol is [registered globally][global symbol registry] and can be
17101cb0ef41Sopenharmony_ciaccessed in any environment as `Symbol.for('nodejs.util.promisify.custom')`.
17111cb0ef41Sopenharmony_ci
17121cb0ef41Sopenharmony_ciFor example, with a function that takes in
17131cb0ef41Sopenharmony_ci`(foo, onSuccessCallback, onErrorCallback)`:
17141cb0ef41Sopenharmony_ci
17151cb0ef41Sopenharmony_ci```js
17161cb0ef41Sopenharmony_ciconst kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');
17171cb0ef41Sopenharmony_ci
17181cb0ef41Sopenharmony_cidoSomething[kCustomPromisifiedSymbol] = (foo) => {
17191cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
17201cb0ef41Sopenharmony_ci    doSomething(foo, resolve, reject);
17211cb0ef41Sopenharmony_ci  });
17221cb0ef41Sopenharmony_ci};
17231cb0ef41Sopenharmony_ci```
17241cb0ef41Sopenharmony_ci
17251cb0ef41Sopenharmony_ci## `util.stripVTControlCharacters(str)`
17261cb0ef41Sopenharmony_ci
17271cb0ef41Sopenharmony_ci<!-- YAML
17281cb0ef41Sopenharmony_ciadded: v16.11.0
17291cb0ef41Sopenharmony_ci-->
17301cb0ef41Sopenharmony_ci
17311cb0ef41Sopenharmony_ci* `str` {string}
17321cb0ef41Sopenharmony_ci* Returns: {string}
17331cb0ef41Sopenharmony_ci
17341cb0ef41Sopenharmony_ciReturns `str` with any ANSI escape codes removed.
17351cb0ef41Sopenharmony_ci
17361cb0ef41Sopenharmony_ci```js
17371cb0ef41Sopenharmony_ciconsole.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'));
17381cb0ef41Sopenharmony_ci// Prints "value"
17391cb0ef41Sopenharmony_ci```
17401cb0ef41Sopenharmony_ci
17411cb0ef41Sopenharmony_ci## Class: `util.TextDecoder`
17421cb0ef41Sopenharmony_ci
17431cb0ef41Sopenharmony_ci<!-- YAML
17441cb0ef41Sopenharmony_ciadded: v8.3.0
17451cb0ef41Sopenharmony_ci-->
17461cb0ef41Sopenharmony_ci
17471cb0ef41Sopenharmony_ciAn implementation of the [WHATWG Encoding Standard][] `TextDecoder` API.
17481cb0ef41Sopenharmony_ci
17491cb0ef41Sopenharmony_ci```js
17501cb0ef41Sopenharmony_ciconst decoder = new TextDecoder();
17511cb0ef41Sopenharmony_ciconst u8arr = new Uint8Array([72, 101, 108, 108, 111]);
17521cb0ef41Sopenharmony_ciconsole.log(decoder.decode(u8arr)); // Hello
17531cb0ef41Sopenharmony_ci```
17541cb0ef41Sopenharmony_ci
17551cb0ef41Sopenharmony_ci### WHATWG supported encodings
17561cb0ef41Sopenharmony_ci
17571cb0ef41Sopenharmony_ciPer the [WHATWG Encoding Standard][], the encodings supported by the
17581cb0ef41Sopenharmony_ci`TextDecoder` API are outlined in the tables below. For each encoding,
17591cb0ef41Sopenharmony_cione or more aliases may be used.
17601cb0ef41Sopenharmony_ci
17611cb0ef41Sopenharmony_ciDifferent Node.js build configurations support different sets of encodings.
17621cb0ef41Sopenharmony_ci(see [Internationalization][])
17631cb0ef41Sopenharmony_ci
17641cb0ef41Sopenharmony_ci#### Encodings supported by default (with full ICU data)
17651cb0ef41Sopenharmony_ci
17661cb0ef41Sopenharmony_ci| Encoding           | Aliases                                                                                                                                                                                                                             |
17671cb0ef41Sopenharmony_ci| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17681cb0ef41Sopenharmony_ci| `'ibm866'`         | `'866'`, `'cp866'`, `'csibm866'`                                                                                                                                                                                                    |
17691cb0ef41Sopenharmony_ci| `'iso-8859-2'`     | `'csisolatin2'`, `'iso-ir-101'`, `'iso8859-2'`, `'iso88592'`, `'iso_8859-2'`, `'iso_8859-2:1987'`, `'l2'`, `'latin2'`                                                                                                               |
17701cb0ef41Sopenharmony_ci| `'iso-8859-3'`     | `'csisolatin3'`, `'iso-ir-109'`, `'iso8859-3'`, `'iso88593'`, `'iso_8859-3'`, `'iso_8859-3:1988'`, `'l3'`, `'latin3'`                                                                                                               |
17711cb0ef41Sopenharmony_ci| `'iso-8859-4'`     | `'csisolatin4'`, `'iso-ir-110'`, `'iso8859-4'`, `'iso88594'`, `'iso_8859-4'`, `'iso_8859-4:1988'`, `'l4'`, `'latin4'`                                                                                                               |
17721cb0ef41Sopenharmony_ci| `'iso-8859-5'`     | `'csisolatincyrillic'`, `'cyrillic'`, `'iso-ir-144'`, `'iso8859-5'`, `'iso88595'`, `'iso_8859-5'`, `'iso_8859-5:1988'`                                                                                                              |
17731cb0ef41Sopenharmony_ci| `'iso-8859-6'`     | `'arabic'`, `'asmo-708'`, `'csiso88596e'`, `'csiso88596i'`, `'csisolatinarabic'`, `'ecma-114'`, `'iso-8859-6-e'`, `'iso-8859-6-i'`, `'iso-ir-127'`, `'iso8859-6'`, `'iso88596'`, `'iso_8859-6'`, `'iso_8859-6:1987'`                |
17741cb0ef41Sopenharmony_ci| `'iso-8859-7'`     | `'csisolatingreek'`, `'ecma-118'`, `'elot_928'`, `'greek'`, `'greek8'`, `'iso-ir-126'`, `'iso8859-7'`, `'iso88597'`, `'iso_8859-7'`, `'iso_8859-7:1987'`, `'sun_eu_greek'`                                                          |
17751cb0ef41Sopenharmony_ci| `'iso-8859-8'`     | `'csiso88598e'`, `'csisolatinhebrew'`, `'hebrew'`, `'iso-8859-8-e'`, `'iso-ir-138'`, `'iso8859-8'`, `'iso88598'`, `'iso_8859-8'`, `'iso_8859-8:1988'`, `'visual'`                                                                   |
17761cb0ef41Sopenharmony_ci| `'iso-8859-8-i'`   | `'csiso88598i'`, `'logical'`                                                                                                                                                                                                        |
17771cb0ef41Sopenharmony_ci| `'iso-8859-10'`    | `'csisolatin6'`, `'iso-ir-157'`, `'iso8859-10'`, `'iso885910'`, `'l6'`, `'latin6'`                                                                                                                                                  |
17781cb0ef41Sopenharmony_ci| `'iso-8859-13'`    | `'iso8859-13'`, `'iso885913'`                                                                                                                                                                                                       |
17791cb0ef41Sopenharmony_ci| `'iso-8859-14'`    | `'iso8859-14'`, `'iso885914'`                                                                                                                                                                                                       |
17801cb0ef41Sopenharmony_ci| `'iso-8859-15'`    | `'csisolatin9'`, `'iso8859-15'`, `'iso885915'`, `'iso_8859-15'`, `'l9'`                                                                                                                                                             |
17811cb0ef41Sopenharmony_ci| `'koi8-r'`         | `'cskoi8r'`, `'koi'`, `'koi8'`, `'koi8_r'`                                                                                                                                                                                          |
17821cb0ef41Sopenharmony_ci| `'koi8-u'`         | `'koi8-ru'`                                                                                                                                                                                                                         |
17831cb0ef41Sopenharmony_ci| `'macintosh'`      | `'csmacintosh'`, `'mac'`, `'x-mac-roman'`                                                                                                                                                                                           |
17841cb0ef41Sopenharmony_ci| `'windows-874'`    | `'dos-874'`, `'iso-8859-11'`, `'iso8859-11'`, `'iso885911'`, `'tis-620'`                                                                                                                                                            |
17851cb0ef41Sopenharmony_ci| `'windows-1250'`   | `'cp1250'`, `'x-cp1250'`                                                                                                                                                                                                            |
17861cb0ef41Sopenharmony_ci| `'windows-1251'`   | `'cp1251'`, `'x-cp1251'`                                                                                                                                                                                                            |
17871cb0ef41Sopenharmony_ci| `'windows-1252'`   | `'ansi_x3.4-1968'`, `'ascii'`, `'cp1252'`, `'cp819'`, `'csisolatin1'`, `'ibm819'`, `'iso-8859-1'`, `'iso-ir-100'`, `'iso8859-1'`, `'iso88591'`, `'iso_8859-1'`, `'iso_8859-1:1987'`, `'l1'`, `'latin1'`, `'us-ascii'`, `'x-cp1252'` |
17881cb0ef41Sopenharmony_ci| `'windows-1253'`   | `'cp1253'`, `'x-cp1253'`                                                                                                                                                                                                            |
17891cb0ef41Sopenharmony_ci| `'windows-1254'`   | `'cp1254'`, `'csisolatin5'`, `'iso-8859-9'`, `'iso-ir-148'`, `'iso8859-9'`, `'iso88599'`, `'iso_8859-9'`, `'iso_8859-9:1989'`, `'l5'`, `'latin5'`, `'x-cp1254'`                                                                     |
17901cb0ef41Sopenharmony_ci| `'windows-1255'`   | `'cp1255'`, `'x-cp1255'`                                                                                                                                                                                                            |
17911cb0ef41Sopenharmony_ci| `'windows-1256'`   | `'cp1256'`, `'x-cp1256'`                                                                                                                                                                                                            |
17921cb0ef41Sopenharmony_ci| `'windows-1257'`   | `'cp1257'`, `'x-cp1257'`                                                                                                                                                                                                            |
17931cb0ef41Sopenharmony_ci| `'windows-1258'`   | `'cp1258'`, `'x-cp1258'`                                                                                                                                                                                                            |
17941cb0ef41Sopenharmony_ci| `'x-mac-cyrillic'` | `'x-mac-ukrainian'`                                                                                                                                                                                                                 |
17951cb0ef41Sopenharmony_ci| `'gbk'`            | `'chinese'`, `'csgb2312'`, `'csiso58gb231280'`, `'gb2312'`, `'gb_2312'`, `'gb_2312-80'`, `'iso-ir-58'`, `'x-gbk'`                                                                                                                   |
17961cb0ef41Sopenharmony_ci| `'gb18030'`        |                                                                                                                                                                                                                                     |
17971cb0ef41Sopenharmony_ci| `'big5'`           | `'big5-hkscs'`, `'cn-big5'`, `'csbig5'`, `'x-x-big5'`                                                                                                                                                                               |
17981cb0ef41Sopenharmony_ci| `'euc-jp'`         | `'cseucpkdfmtjapanese'`, `'x-euc-jp'`                                                                                                                                                                                               |
17991cb0ef41Sopenharmony_ci| `'iso-2022-jp'`    | `'csiso2022jp'`                                                                                                                                                                                                                     |
18001cb0ef41Sopenharmony_ci| `'shift_jis'`      | `'csshiftjis'`, `'ms932'`, `'ms_kanji'`, `'shift-jis'`, `'sjis'`, `'windows-31j'`, `'x-sjis'`                                                                                                                                       |
18011cb0ef41Sopenharmony_ci| `'euc-kr'`         | `'cseuckr'`, `'csksc56011987'`, `'iso-ir-149'`, `'korean'`, `'ks_c_5601-1987'`, `'ks_c_5601-1989'`, `'ksc5601'`, `'ksc_5601'`, `'windows-949'`                                                                                      |
18021cb0ef41Sopenharmony_ci
18031cb0ef41Sopenharmony_ci#### Encodings supported when Node.js is built with the `small-icu` option
18041cb0ef41Sopenharmony_ci
18051cb0ef41Sopenharmony_ci| Encoding     | Aliases                         |
18061cb0ef41Sopenharmony_ci| ------------ | ------------------------------- |
18071cb0ef41Sopenharmony_ci| `'utf-8'`    | `'unicode-1-1-utf-8'`, `'utf8'` |
18081cb0ef41Sopenharmony_ci| `'utf-16le'` | `'utf-16'`                      |
18091cb0ef41Sopenharmony_ci| `'utf-16be'` |                                 |
18101cb0ef41Sopenharmony_ci
18111cb0ef41Sopenharmony_ci#### Encodings supported when ICU is disabled
18121cb0ef41Sopenharmony_ci
18131cb0ef41Sopenharmony_ci| Encoding     | Aliases                         |
18141cb0ef41Sopenharmony_ci| ------------ | ------------------------------- |
18151cb0ef41Sopenharmony_ci| `'utf-8'`    | `'unicode-1-1-utf-8'`, `'utf8'` |
18161cb0ef41Sopenharmony_ci| `'utf-16le'` | `'utf-16'`                      |
18171cb0ef41Sopenharmony_ci
18181cb0ef41Sopenharmony_ciThe `'iso-8859-16'` encoding listed in the [WHATWG Encoding Standard][]
18191cb0ef41Sopenharmony_ciis not supported.
18201cb0ef41Sopenharmony_ci
18211cb0ef41Sopenharmony_ci### `new TextDecoder([encoding[, options]])`
18221cb0ef41Sopenharmony_ci
18231cb0ef41Sopenharmony_ci<!-- YAML
18241cb0ef41Sopenharmony_ciadded: v8.3.0
18251cb0ef41Sopenharmony_cichanges:
18261cb0ef41Sopenharmony_ci  - version: v11.0.0
18271cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22281
18281cb0ef41Sopenharmony_ci    description: The class is now available on the global object.
18291cb0ef41Sopenharmony_ci-->
18301cb0ef41Sopenharmony_ci
18311cb0ef41Sopenharmony_ci* `encoding` {string} Identifies the `encoding` that this `TextDecoder` instance
18321cb0ef41Sopenharmony_ci  supports. **Default:** `'utf-8'`.
18331cb0ef41Sopenharmony_ci* `options` {Object}
18341cb0ef41Sopenharmony_ci  * `fatal` {boolean} `true` if decoding failures are fatal.
18351cb0ef41Sopenharmony_ci    This option is not supported when ICU is disabled
18361cb0ef41Sopenharmony_ci    (see [Internationalization][]). **Default:** `false`.
18371cb0ef41Sopenharmony_ci  * `ignoreBOM` {boolean} When `true`, the `TextDecoder` will include the byte
18381cb0ef41Sopenharmony_ci    order mark in the decoded result. When `false`, the byte order mark will
18391cb0ef41Sopenharmony_ci    be removed from the output. This option is only used when `encoding` is
18401cb0ef41Sopenharmony_ci    `'utf-8'`, `'utf-16be'`, or `'utf-16le'`. **Default:** `false`.
18411cb0ef41Sopenharmony_ci
18421cb0ef41Sopenharmony_ciCreates a new `TextDecoder` instance. The `encoding` may specify one of the
18431cb0ef41Sopenharmony_cisupported encodings or an alias.
18441cb0ef41Sopenharmony_ci
18451cb0ef41Sopenharmony_ciThe `TextDecoder` class is also available on the global object.
18461cb0ef41Sopenharmony_ci
18471cb0ef41Sopenharmony_ci### `textDecoder.decode([input[, options]])`
18481cb0ef41Sopenharmony_ci
18491cb0ef41Sopenharmony_ci* `input` {ArrayBuffer|DataView|TypedArray} An `ArrayBuffer`, `DataView`, or
18501cb0ef41Sopenharmony_ci  `TypedArray` instance containing the encoded data.
18511cb0ef41Sopenharmony_ci* `options` {Object}
18521cb0ef41Sopenharmony_ci  * `stream` {boolean} `true` if additional chunks of data are expected.
18531cb0ef41Sopenharmony_ci    **Default:** `false`.
18541cb0ef41Sopenharmony_ci* Returns: {string}
18551cb0ef41Sopenharmony_ci
18561cb0ef41Sopenharmony_ciDecodes the `input` and returns a string. If `options.stream` is `true`, any
18571cb0ef41Sopenharmony_ciincomplete byte sequences occurring at the end of the `input` are buffered
18581cb0ef41Sopenharmony_ciinternally and emitted after the next call to `textDecoder.decode()`.
18591cb0ef41Sopenharmony_ci
18601cb0ef41Sopenharmony_ciIf `textDecoder.fatal` is `true`, decoding errors that occur will result in a
18611cb0ef41Sopenharmony_ci`TypeError` being thrown.
18621cb0ef41Sopenharmony_ci
18631cb0ef41Sopenharmony_ci### `textDecoder.encoding`
18641cb0ef41Sopenharmony_ci
18651cb0ef41Sopenharmony_ci* {string}
18661cb0ef41Sopenharmony_ci
18671cb0ef41Sopenharmony_ciThe encoding supported by the `TextDecoder` instance.
18681cb0ef41Sopenharmony_ci
18691cb0ef41Sopenharmony_ci### `textDecoder.fatal`
18701cb0ef41Sopenharmony_ci
18711cb0ef41Sopenharmony_ci* {boolean}
18721cb0ef41Sopenharmony_ci
18731cb0ef41Sopenharmony_ciThe value will be `true` if decoding errors result in a `TypeError` being
18741cb0ef41Sopenharmony_cithrown.
18751cb0ef41Sopenharmony_ci
18761cb0ef41Sopenharmony_ci### `textDecoder.ignoreBOM`
18771cb0ef41Sopenharmony_ci
18781cb0ef41Sopenharmony_ci* {boolean}
18791cb0ef41Sopenharmony_ci
18801cb0ef41Sopenharmony_ciThe value will be `true` if the decoding result will include the byte order
18811cb0ef41Sopenharmony_cimark.
18821cb0ef41Sopenharmony_ci
18831cb0ef41Sopenharmony_ci## Class: `util.TextEncoder`
18841cb0ef41Sopenharmony_ci
18851cb0ef41Sopenharmony_ci<!-- YAML
18861cb0ef41Sopenharmony_ciadded: v8.3.0
18871cb0ef41Sopenharmony_cichanges:
18881cb0ef41Sopenharmony_ci  - version: v11.0.0
18891cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22281
18901cb0ef41Sopenharmony_ci    description: The class is now available on the global object.
18911cb0ef41Sopenharmony_ci-->
18921cb0ef41Sopenharmony_ci
18931cb0ef41Sopenharmony_ciAn implementation of the [WHATWG Encoding Standard][] `TextEncoder` API. All
18941cb0ef41Sopenharmony_ciinstances of `TextEncoder` only support UTF-8 encoding.
18951cb0ef41Sopenharmony_ci
18961cb0ef41Sopenharmony_ci```js
18971cb0ef41Sopenharmony_ciconst encoder = new TextEncoder();
18981cb0ef41Sopenharmony_ciconst uint8array = encoder.encode('this is some data');
18991cb0ef41Sopenharmony_ci```
19001cb0ef41Sopenharmony_ci
19011cb0ef41Sopenharmony_ciThe `TextEncoder` class is also available on the global object.
19021cb0ef41Sopenharmony_ci
19031cb0ef41Sopenharmony_ci### `textEncoder.encode([input])`
19041cb0ef41Sopenharmony_ci
19051cb0ef41Sopenharmony_ci* `input` {string} The text to encode. **Default:** an empty string.
19061cb0ef41Sopenharmony_ci* Returns: {Uint8Array}
19071cb0ef41Sopenharmony_ci
19081cb0ef41Sopenharmony_ciUTF-8 encodes the `input` string and returns a `Uint8Array` containing the
19091cb0ef41Sopenharmony_ciencoded bytes.
19101cb0ef41Sopenharmony_ci
19111cb0ef41Sopenharmony_ci### `textEncoder.encodeInto(src, dest)`
19121cb0ef41Sopenharmony_ci
19131cb0ef41Sopenharmony_ci* `src` {string} The text to encode.
19141cb0ef41Sopenharmony_ci* `dest` {Uint8Array} The array to hold the encode result.
19151cb0ef41Sopenharmony_ci* Returns: {Object}
19161cb0ef41Sopenharmony_ci  * `read` {number} The read Unicode code units of src.
19171cb0ef41Sopenharmony_ci  * `written` {number} The written UTF-8 bytes of dest.
19181cb0ef41Sopenharmony_ci
19191cb0ef41Sopenharmony_ciUTF-8 encodes the `src` string to the `dest` Uint8Array and returns an object
19201cb0ef41Sopenharmony_cicontaining the read Unicode code units and written UTF-8 bytes.
19211cb0ef41Sopenharmony_ci
19221cb0ef41Sopenharmony_ci```js
19231cb0ef41Sopenharmony_ciconst encoder = new TextEncoder();
19241cb0ef41Sopenharmony_ciconst src = 'this is some data';
19251cb0ef41Sopenharmony_ciconst dest = new Uint8Array(10);
19261cb0ef41Sopenharmony_ciconst { read, written } = encoder.encodeInto(src, dest);
19271cb0ef41Sopenharmony_ci```
19281cb0ef41Sopenharmony_ci
19291cb0ef41Sopenharmony_ci### `textEncoder.encoding`
19301cb0ef41Sopenharmony_ci
19311cb0ef41Sopenharmony_ci* {string}
19321cb0ef41Sopenharmony_ci
19331cb0ef41Sopenharmony_ciThe encoding supported by the `TextEncoder` instance. Always set to `'utf-8'`.
19341cb0ef41Sopenharmony_ci
19351cb0ef41Sopenharmony_ci## `util.toUSVString(string)`
19361cb0ef41Sopenharmony_ci
19371cb0ef41Sopenharmony_ci<!-- YAML
19381cb0ef41Sopenharmony_ciadded:
19391cb0ef41Sopenharmony_ci  - v16.8.0
19401cb0ef41Sopenharmony_ci  - v14.18.0
19411cb0ef41Sopenharmony_ci-->
19421cb0ef41Sopenharmony_ci
19431cb0ef41Sopenharmony_ci* `string` {string}
19441cb0ef41Sopenharmony_ci
19451cb0ef41Sopenharmony_ciReturns the `string` after replacing any surrogate code points
19461cb0ef41Sopenharmony_ci(or equivalently, any unpaired surrogate code units) with the
19471cb0ef41Sopenharmony_ciUnicode "replacement character" U+FFFD.
19481cb0ef41Sopenharmony_ci
19491cb0ef41Sopenharmony_ci## `util.transferableAbortController()`
19501cb0ef41Sopenharmony_ci
19511cb0ef41Sopenharmony_ci<!-- YAML
19521cb0ef41Sopenharmony_ciadded: v18.11.0
19531cb0ef41Sopenharmony_ci-->
19541cb0ef41Sopenharmony_ci
19551cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
19561cb0ef41Sopenharmony_ci
19571cb0ef41Sopenharmony_ciCreates and returns an {AbortController} instance whose {AbortSignal} is marked
19581cb0ef41Sopenharmony_cias transferable and can be used with `structuredClone()` or `postMessage()`.
19591cb0ef41Sopenharmony_ci
19601cb0ef41Sopenharmony_ci## `util.transferableAbortSignal(signal)`
19611cb0ef41Sopenharmony_ci
19621cb0ef41Sopenharmony_ci<!-- YAML
19631cb0ef41Sopenharmony_ciadded: v18.11.0
19641cb0ef41Sopenharmony_ci-->
19651cb0ef41Sopenharmony_ci
19661cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
19671cb0ef41Sopenharmony_ci
19681cb0ef41Sopenharmony_ci* `signal` {AbortSignal}
19691cb0ef41Sopenharmony_ci* Returns: {AbortSignal}
19701cb0ef41Sopenharmony_ci
19711cb0ef41Sopenharmony_ciMarks the given {AbortSignal} as transferable so that it can be used with
19721cb0ef41Sopenharmony_ci`structuredClone()` and `postMessage()`.
19731cb0ef41Sopenharmony_ci
19741cb0ef41Sopenharmony_ci```js
19751cb0ef41Sopenharmony_ciconst signal = transferableAbortSignal(AbortSignal.timeout(100));
19761cb0ef41Sopenharmony_ciconst channel = new MessageChannel();
19771cb0ef41Sopenharmony_cichannel.port2.postMessage(signal, [signal]);
19781cb0ef41Sopenharmony_ci```
19791cb0ef41Sopenharmony_ci
19801cb0ef41Sopenharmony_ci## `util.aborted(signal, resource)`
19811cb0ef41Sopenharmony_ci
19821cb0ef41Sopenharmony_ci<!-- YAML
19831cb0ef41Sopenharmony_ciadded: v18.16.0
19841cb0ef41Sopenharmony_ci-->
19851cb0ef41Sopenharmony_ci
19861cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
19871cb0ef41Sopenharmony_ci
19881cb0ef41Sopenharmony_ci* `signal` {AbortSignal}
19891cb0ef41Sopenharmony_ci* `resource` {Object} Any non-null entity, reference to which is held weakly.
19901cb0ef41Sopenharmony_ci* Returns: {Promise}
19911cb0ef41Sopenharmony_ci
19921cb0ef41Sopenharmony_ciListens to abort event on the provided `signal` and
19931cb0ef41Sopenharmony_cireturns a promise that is fulfilled when the `signal` is
19941cb0ef41Sopenharmony_ciaborted. If the passed `resource` is garbage collected before the `signal` is
19951cb0ef41Sopenharmony_ciaborted, the returned promise shall remain pending indefinitely.
19961cb0ef41Sopenharmony_ci
19971cb0ef41Sopenharmony_ci```cjs
19981cb0ef41Sopenharmony_ciconst { aborted } = require('node:util');
19991cb0ef41Sopenharmony_ci
20001cb0ef41Sopenharmony_ciconst dependent = obtainSomethingAbortable();
20011cb0ef41Sopenharmony_ci
20021cb0ef41Sopenharmony_ciaborted(dependent.signal, dependent).then(() => {
20031cb0ef41Sopenharmony_ci  // Do something when dependent is aborted.
20041cb0ef41Sopenharmony_ci});
20051cb0ef41Sopenharmony_ci
20061cb0ef41Sopenharmony_cidependent.on('event', () => {
20071cb0ef41Sopenharmony_ci  dependent.abort();
20081cb0ef41Sopenharmony_ci});
20091cb0ef41Sopenharmony_ci```
20101cb0ef41Sopenharmony_ci
20111cb0ef41Sopenharmony_ci```mjs
20121cb0ef41Sopenharmony_ciimport { aborted } from 'node:util';
20131cb0ef41Sopenharmony_ci
20141cb0ef41Sopenharmony_ciconst dependent = obtainSomethingAbortable();
20151cb0ef41Sopenharmony_ci
20161cb0ef41Sopenharmony_ciaborted(dependent.signal, dependent).then(() => {
20171cb0ef41Sopenharmony_ci  // Do something when dependent is aborted.
20181cb0ef41Sopenharmony_ci});
20191cb0ef41Sopenharmony_ci
20201cb0ef41Sopenharmony_cidependent.on('event', () => {
20211cb0ef41Sopenharmony_ci  dependent.abort();
20221cb0ef41Sopenharmony_ci});
20231cb0ef41Sopenharmony_ci```
20241cb0ef41Sopenharmony_ci
20251cb0ef41Sopenharmony_ci## `util.types`
20261cb0ef41Sopenharmony_ci
20271cb0ef41Sopenharmony_ci<!-- YAML
20281cb0ef41Sopenharmony_ciadded: v10.0.0
20291cb0ef41Sopenharmony_cichanges:
20301cb0ef41Sopenharmony_ci  - version: v15.3.0
20311cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34055
20321cb0ef41Sopenharmony_ci    description: Exposed as `require('util/types')`.
20331cb0ef41Sopenharmony_ci-->
20341cb0ef41Sopenharmony_ci
20351cb0ef41Sopenharmony_ci`util.types` provides type checks for different kinds of built-in objects.
20361cb0ef41Sopenharmony_ciUnlike `instanceof` or `Object.prototype.toString.call(value)`, these checks do
20371cb0ef41Sopenharmony_cinot inspect properties of the object that are accessible from JavaScript (like
20381cb0ef41Sopenharmony_citheir prototype), and usually have the overhead of calling into C++.
20391cb0ef41Sopenharmony_ci
20401cb0ef41Sopenharmony_ciThe result generally does not make any guarantees about what kinds of
20411cb0ef41Sopenharmony_ciproperties or behavior a value exposes in JavaScript. They are primarily
20421cb0ef41Sopenharmony_ciuseful for addon developers who prefer to do type checking in JavaScript.
20431cb0ef41Sopenharmony_ci
20441cb0ef41Sopenharmony_ciThe API is accessible via `require('node:util').types` or `require('node:util/types')`.
20451cb0ef41Sopenharmony_ci
20461cb0ef41Sopenharmony_ci### `util.types.isAnyArrayBuffer(value)`
20471cb0ef41Sopenharmony_ci
20481cb0ef41Sopenharmony_ci<!-- YAML
20491cb0ef41Sopenharmony_ciadded: v10.0.0
20501cb0ef41Sopenharmony_ci-->
20511cb0ef41Sopenharmony_ci
20521cb0ef41Sopenharmony_ci* `value` {any}
20531cb0ef41Sopenharmony_ci* Returns: {boolean}
20541cb0ef41Sopenharmony_ci
20551cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`ArrayBuffer`][] or
20561cb0ef41Sopenharmony_ci[`SharedArrayBuffer`][] instance.
20571cb0ef41Sopenharmony_ci
20581cb0ef41Sopenharmony_ciSee also [`util.types.isArrayBuffer()`][] and
20591cb0ef41Sopenharmony_ci[`util.types.isSharedArrayBuffer()`][].
20601cb0ef41Sopenharmony_ci
20611cb0ef41Sopenharmony_ci```js
20621cb0ef41Sopenharmony_ciutil.types.isAnyArrayBuffer(new ArrayBuffer());  // Returns true
20631cb0ef41Sopenharmony_ciutil.types.isAnyArrayBuffer(new SharedArrayBuffer());  // Returns true
20641cb0ef41Sopenharmony_ci```
20651cb0ef41Sopenharmony_ci
20661cb0ef41Sopenharmony_ci### `util.types.isArrayBufferView(value)`
20671cb0ef41Sopenharmony_ci
20681cb0ef41Sopenharmony_ci<!-- YAML
20691cb0ef41Sopenharmony_ciadded: v10.0.0
20701cb0ef41Sopenharmony_ci-->
20711cb0ef41Sopenharmony_ci
20721cb0ef41Sopenharmony_ci* `value` {any}
20731cb0ef41Sopenharmony_ci* Returns: {boolean}
20741cb0ef41Sopenharmony_ci
20751cb0ef41Sopenharmony_ciReturns `true` if the value is an instance of one of the [`ArrayBuffer`][]
20761cb0ef41Sopenharmony_civiews, such as typed array objects or [`DataView`][]. Equivalent to
20771cb0ef41Sopenharmony_ci[`ArrayBuffer.isView()`][].
20781cb0ef41Sopenharmony_ci
20791cb0ef41Sopenharmony_ci```js
20801cb0ef41Sopenharmony_ciutil.types.isArrayBufferView(new Int8Array());  // true
20811cb0ef41Sopenharmony_ciutil.types.isArrayBufferView(Buffer.from('hello world')); // true
20821cb0ef41Sopenharmony_ciutil.types.isArrayBufferView(new DataView(new ArrayBuffer(16)));  // true
20831cb0ef41Sopenharmony_ciutil.types.isArrayBufferView(new ArrayBuffer());  // false
20841cb0ef41Sopenharmony_ci```
20851cb0ef41Sopenharmony_ci
20861cb0ef41Sopenharmony_ci### `util.types.isArgumentsObject(value)`
20871cb0ef41Sopenharmony_ci
20881cb0ef41Sopenharmony_ci<!-- YAML
20891cb0ef41Sopenharmony_ciadded: v10.0.0
20901cb0ef41Sopenharmony_ci-->
20911cb0ef41Sopenharmony_ci
20921cb0ef41Sopenharmony_ci* `value` {any}
20931cb0ef41Sopenharmony_ci* Returns: {boolean}
20941cb0ef41Sopenharmony_ci
20951cb0ef41Sopenharmony_ciReturns `true` if the value is an `arguments` object.
20961cb0ef41Sopenharmony_ci
20971cb0ef41Sopenharmony_ci<!-- eslint-disable prefer-rest-params -->
20981cb0ef41Sopenharmony_ci
20991cb0ef41Sopenharmony_ci```js
21001cb0ef41Sopenharmony_cifunction foo() {
21011cb0ef41Sopenharmony_ci  util.types.isArgumentsObject(arguments);  // Returns true
21021cb0ef41Sopenharmony_ci}
21031cb0ef41Sopenharmony_ci```
21041cb0ef41Sopenharmony_ci
21051cb0ef41Sopenharmony_ci### `util.types.isArrayBuffer(value)`
21061cb0ef41Sopenharmony_ci
21071cb0ef41Sopenharmony_ci<!-- YAML
21081cb0ef41Sopenharmony_ciadded: v10.0.0
21091cb0ef41Sopenharmony_ci-->
21101cb0ef41Sopenharmony_ci
21111cb0ef41Sopenharmony_ci* `value` {any}
21121cb0ef41Sopenharmony_ci* Returns: {boolean}
21131cb0ef41Sopenharmony_ci
21141cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`ArrayBuffer`][] instance.
21151cb0ef41Sopenharmony_ciThis does _not_ include [`SharedArrayBuffer`][] instances. Usually, it is
21161cb0ef41Sopenharmony_cidesirable to test for both; See [`util.types.isAnyArrayBuffer()`][] for that.
21171cb0ef41Sopenharmony_ci
21181cb0ef41Sopenharmony_ci```js
21191cb0ef41Sopenharmony_ciutil.types.isArrayBuffer(new ArrayBuffer());  // Returns true
21201cb0ef41Sopenharmony_ciutil.types.isArrayBuffer(new SharedArrayBuffer());  // Returns false
21211cb0ef41Sopenharmony_ci```
21221cb0ef41Sopenharmony_ci
21231cb0ef41Sopenharmony_ci### `util.types.isAsyncFunction(value)`
21241cb0ef41Sopenharmony_ci
21251cb0ef41Sopenharmony_ci<!-- YAML
21261cb0ef41Sopenharmony_ciadded: v10.0.0
21271cb0ef41Sopenharmony_ci-->
21281cb0ef41Sopenharmony_ci
21291cb0ef41Sopenharmony_ci* `value` {any}
21301cb0ef41Sopenharmony_ci* Returns: {boolean}
21311cb0ef41Sopenharmony_ci
21321cb0ef41Sopenharmony_ciReturns `true` if the value is an [async function][].
21331cb0ef41Sopenharmony_ciThis only reports back what the JavaScript engine is seeing;
21341cb0ef41Sopenharmony_ciin particular, the return value may not match the original source code if
21351cb0ef41Sopenharmony_cia transpilation tool was used.
21361cb0ef41Sopenharmony_ci
21371cb0ef41Sopenharmony_ci```js
21381cb0ef41Sopenharmony_ciutil.types.isAsyncFunction(function foo() {});  // Returns false
21391cb0ef41Sopenharmony_ciutil.types.isAsyncFunction(async function foo() {});  // Returns true
21401cb0ef41Sopenharmony_ci```
21411cb0ef41Sopenharmony_ci
21421cb0ef41Sopenharmony_ci### `util.types.isBigInt64Array(value)`
21431cb0ef41Sopenharmony_ci
21441cb0ef41Sopenharmony_ci<!-- YAML
21451cb0ef41Sopenharmony_ciadded: v10.0.0
21461cb0ef41Sopenharmony_ci-->
21471cb0ef41Sopenharmony_ci
21481cb0ef41Sopenharmony_ci* `value` {any}
21491cb0ef41Sopenharmony_ci* Returns: {boolean}
21501cb0ef41Sopenharmony_ci
21511cb0ef41Sopenharmony_ciReturns `true` if the value is a `BigInt64Array` instance.
21521cb0ef41Sopenharmony_ci
21531cb0ef41Sopenharmony_ci```js
21541cb0ef41Sopenharmony_ciutil.types.isBigInt64Array(new BigInt64Array());   // Returns true
21551cb0ef41Sopenharmony_ciutil.types.isBigInt64Array(new BigUint64Array());  // Returns false
21561cb0ef41Sopenharmony_ci```
21571cb0ef41Sopenharmony_ci
21581cb0ef41Sopenharmony_ci### `util.types.isBigUint64Array(value)`
21591cb0ef41Sopenharmony_ci
21601cb0ef41Sopenharmony_ci<!-- YAML
21611cb0ef41Sopenharmony_ciadded: v10.0.0
21621cb0ef41Sopenharmony_ci-->
21631cb0ef41Sopenharmony_ci
21641cb0ef41Sopenharmony_ci* `value` {any}
21651cb0ef41Sopenharmony_ci* Returns: {boolean}
21661cb0ef41Sopenharmony_ci
21671cb0ef41Sopenharmony_ciReturns `true` if the value is a `BigUint64Array` instance.
21681cb0ef41Sopenharmony_ci
21691cb0ef41Sopenharmony_ci```js
21701cb0ef41Sopenharmony_ciutil.types.isBigUint64Array(new BigInt64Array());   // Returns false
21711cb0ef41Sopenharmony_ciutil.types.isBigUint64Array(new BigUint64Array());  // Returns true
21721cb0ef41Sopenharmony_ci```
21731cb0ef41Sopenharmony_ci
21741cb0ef41Sopenharmony_ci### `util.types.isBooleanObject(value)`
21751cb0ef41Sopenharmony_ci
21761cb0ef41Sopenharmony_ci<!-- YAML
21771cb0ef41Sopenharmony_ciadded: v10.0.0
21781cb0ef41Sopenharmony_ci-->
21791cb0ef41Sopenharmony_ci
21801cb0ef41Sopenharmony_ci* `value` {any}
21811cb0ef41Sopenharmony_ci* Returns: {boolean}
21821cb0ef41Sopenharmony_ci
21831cb0ef41Sopenharmony_ciReturns `true` if the value is a boolean object, e.g. created
21841cb0ef41Sopenharmony_ciby `new Boolean()`.
21851cb0ef41Sopenharmony_ci
21861cb0ef41Sopenharmony_ci```js
21871cb0ef41Sopenharmony_ciutil.types.isBooleanObject(false);  // Returns false
21881cb0ef41Sopenharmony_ciutil.types.isBooleanObject(true);   // Returns false
21891cb0ef41Sopenharmony_ciutil.types.isBooleanObject(new Boolean(false)); // Returns true
21901cb0ef41Sopenharmony_ciutil.types.isBooleanObject(new Boolean(true));  // Returns true
21911cb0ef41Sopenharmony_ciutil.types.isBooleanObject(Boolean(false)); // Returns false
21921cb0ef41Sopenharmony_ciutil.types.isBooleanObject(Boolean(true));  // Returns false
21931cb0ef41Sopenharmony_ci```
21941cb0ef41Sopenharmony_ci
21951cb0ef41Sopenharmony_ci### `util.types.isBoxedPrimitive(value)`
21961cb0ef41Sopenharmony_ci
21971cb0ef41Sopenharmony_ci<!-- YAML
21981cb0ef41Sopenharmony_ciadded: v10.11.0
21991cb0ef41Sopenharmony_ci-->
22001cb0ef41Sopenharmony_ci
22011cb0ef41Sopenharmony_ci* `value` {any}
22021cb0ef41Sopenharmony_ci* Returns: {boolean}
22031cb0ef41Sopenharmony_ci
22041cb0ef41Sopenharmony_ciReturns `true` if the value is any boxed primitive object, e.g. created
22051cb0ef41Sopenharmony_ciby `new Boolean()`, `new String()` or `Object(Symbol())`.
22061cb0ef41Sopenharmony_ci
22071cb0ef41Sopenharmony_ciFor example:
22081cb0ef41Sopenharmony_ci
22091cb0ef41Sopenharmony_ci```js
22101cb0ef41Sopenharmony_ciutil.types.isBoxedPrimitive(false); // Returns false
22111cb0ef41Sopenharmony_ciutil.types.isBoxedPrimitive(new Boolean(false)); // Returns true
22121cb0ef41Sopenharmony_ciutil.types.isBoxedPrimitive(Symbol('foo')); // Returns false
22131cb0ef41Sopenharmony_ciutil.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true
22141cb0ef41Sopenharmony_ciutil.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true
22151cb0ef41Sopenharmony_ci```
22161cb0ef41Sopenharmony_ci
22171cb0ef41Sopenharmony_ci### `util.types.isCryptoKey(value)`
22181cb0ef41Sopenharmony_ci
22191cb0ef41Sopenharmony_ci<!-- YAML
22201cb0ef41Sopenharmony_ciadded: v16.2.0
22211cb0ef41Sopenharmony_ci-->
22221cb0ef41Sopenharmony_ci
22231cb0ef41Sopenharmony_ci* `value` {Object}
22241cb0ef41Sopenharmony_ci* Returns: {boolean}
22251cb0ef41Sopenharmony_ci
22261cb0ef41Sopenharmony_ciReturns `true` if `value` is a {CryptoKey}, `false` otherwise.
22271cb0ef41Sopenharmony_ci
22281cb0ef41Sopenharmony_ci### `util.types.isDataView(value)`
22291cb0ef41Sopenharmony_ci
22301cb0ef41Sopenharmony_ci<!-- YAML
22311cb0ef41Sopenharmony_ciadded: v10.0.0
22321cb0ef41Sopenharmony_ci-->
22331cb0ef41Sopenharmony_ci
22341cb0ef41Sopenharmony_ci* `value` {any}
22351cb0ef41Sopenharmony_ci* Returns: {boolean}
22361cb0ef41Sopenharmony_ci
22371cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`DataView`][] instance.
22381cb0ef41Sopenharmony_ci
22391cb0ef41Sopenharmony_ci```js
22401cb0ef41Sopenharmony_ciconst ab = new ArrayBuffer(20);
22411cb0ef41Sopenharmony_ciutil.types.isDataView(new DataView(ab));  // Returns true
22421cb0ef41Sopenharmony_ciutil.types.isDataView(new Float64Array());  // Returns false
22431cb0ef41Sopenharmony_ci```
22441cb0ef41Sopenharmony_ci
22451cb0ef41Sopenharmony_ciSee also [`ArrayBuffer.isView()`][].
22461cb0ef41Sopenharmony_ci
22471cb0ef41Sopenharmony_ci### `util.types.isDate(value)`
22481cb0ef41Sopenharmony_ci
22491cb0ef41Sopenharmony_ci<!-- YAML
22501cb0ef41Sopenharmony_ciadded: v10.0.0
22511cb0ef41Sopenharmony_ci-->
22521cb0ef41Sopenharmony_ci
22531cb0ef41Sopenharmony_ci* `value` {any}
22541cb0ef41Sopenharmony_ci* Returns: {boolean}
22551cb0ef41Sopenharmony_ci
22561cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Date`][] instance.
22571cb0ef41Sopenharmony_ci
22581cb0ef41Sopenharmony_ci```js
22591cb0ef41Sopenharmony_ciutil.types.isDate(new Date());  // Returns true
22601cb0ef41Sopenharmony_ci```
22611cb0ef41Sopenharmony_ci
22621cb0ef41Sopenharmony_ci### `util.types.isExternal(value)`
22631cb0ef41Sopenharmony_ci
22641cb0ef41Sopenharmony_ci<!-- YAML
22651cb0ef41Sopenharmony_ciadded: v10.0.0
22661cb0ef41Sopenharmony_ci-->
22671cb0ef41Sopenharmony_ci
22681cb0ef41Sopenharmony_ci* `value` {any}
22691cb0ef41Sopenharmony_ci* Returns: {boolean}
22701cb0ef41Sopenharmony_ci
22711cb0ef41Sopenharmony_ciReturns `true` if the value is a native `External` value.
22721cb0ef41Sopenharmony_ci
22731cb0ef41Sopenharmony_ciA native `External` value is a special type of object that contains a
22741cb0ef41Sopenharmony_ciraw C++ pointer (`void*`) for access from native code, and has no other
22751cb0ef41Sopenharmony_ciproperties. Such objects are created either by Node.js internals or native
22761cb0ef41Sopenharmony_ciaddons. In JavaScript, they are [frozen][`Object.freeze()`] objects with a
22771cb0ef41Sopenharmony_ci`null` prototype.
22781cb0ef41Sopenharmony_ci
22791cb0ef41Sopenharmony_ci```c
22801cb0ef41Sopenharmony_ci#include <js_native_api.h>
22811cb0ef41Sopenharmony_ci#include <stdlib.h>
22821cb0ef41Sopenharmony_cinapi_value result;
22831cb0ef41Sopenharmony_cistatic napi_value MyNapi(napi_env env, napi_callback_info info) {
22841cb0ef41Sopenharmony_ci  int* raw = (int*) malloc(1024);
22851cb0ef41Sopenharmony_ci  napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result);
22861cb0ef41Sopenharmony_ci  if (status != napi_ok) {
22871cb0ef41Sopenharmony_ci    napi_throw_error(env, NULL, "napi_create_external failed");
22881cb0ef41Sopenharmony_ci    return NULL;
22891cb0ef41Sopenharmony_ci  }
22901cb0ef41Sopenharmony_ci  return result;
22911cb0ef41Sopenharmony_ci}
22921cb0ef41Sopenharmony_ci...
22931cb0ef41Sopenharmony_ciDECLARE_NAPI_PROPERTY("myNapi", MyNapi)
22941cb0ef41Sopenharmony_ci...
22951cb0ef41Sopenharmony_ci```
22961cb0ef41Sopenharmony_ci
22971cb0ef41Sopenharmony_ci```js
22981cb0ef41Sopenharmony_ciconst native = require('napi_addon.node');
22991cb0ef41Sopenharmony_ciconst data = native.myNapi();
23001cb0ef41Sopenharmony_ciutil.types.isExternal(data); // returns true
23011cb0ef41Sopenharmony_ciutil.types.isExternal(0); // returns false
23021cb0ef41Sopenharmony_ciutil.types.isExternal(new String('foo')); // returns false
23031cb0ef41Sopenharmony_ci```
23041cb0ef41Sopenharmony_ci
23051cb0ef41Sopenharmony_ciFor further information on `napi_create_external`, refer to
23061cb0ef41Sopenharmony_ci[`napi_create_external()`][].
23071cb0ef41Sopenharmony_ci
23081cb0ef41Sopenharmony_ci### `util.types.isFloat32Array(value)`
23091cb0ef41Sopenharmony_ci
23101cb0ef41Sopenharmony_ci<!-- YAML
23111cb0ef41Sopenharmony_ciadded: v10.0.0
23121cb0ef41Sopenharmony_ci-->
23131cb0ef41Sopenharmony_ci
23141cb0ef41Sopenharmony_ci* `value` {any}
23151cb0ef41Sopenharmony_ci* Returns: {boolean}
23161cb0ef41Sopenharmony_ci
23171cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Float32Array`][] instance.
23181cb0ef41Sopenharmony_ci
23191cb0ef41Sopenharmony_ci```js
23201cb0ef41Sopenharmony_ciutil.types.isFloat32Array(new ArrayBuffer());  // Returns false
23211cb0ef41Sopenharmony_ciutil.types.isFloat32Array(new Float32Array());  // Returns true
23221cb0ef41Sopenharmony_ciutil.types.isFloat32Array(new Float64Array());  // Returns false
23231cb0ef41Sopenharmony_ci```
23241cb0ef41Sopenharmony_ci
23251cb0ef41Sopenharmony_ci### `util.types.isFloat64Array(value)`
23261cb0ef41Sopenharmony_ci
23271cb0ef41Sopenharmony_ci<!-- YAML
23281cb0ef41Sopenharmony_ciadded: v10.0.0
23291cb0ef41Sopenharmony_ci-->
23301cb0ef41Sopenharmony_ci
23311cb0ef41Sopenharmony_ci* `value` {any}
23321cb0ef41Sopenharmony_ci* Returns: {boolean}
23331cb0ef41Sopenharmony_ci
23341cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Float64Array`][] instance.
23351cb0ef41Sopenharmony_ci
23361cb0ef41Sopenharmony_ci```js
23371cb0ef41Sopenharmony_ciutil.types.isFloat64Array(new ArrayBuffer());  // Returns false
23381cb0ef41Sopenharmony_ciutil.types.isFloat64Array(new Uint8Array());  // Returns false
23391cb0ef41Sopenharmony_ciutil.types.isFloat64Array(new Float64Array());  // Returns true
23401cb0ef41Sopenharmony_ci```
23411cb0ef41Sopenharmony_ci
23421cb0ef41Sopenharmony_ci### `util.types.isGeneratorFunction(value)`
23431cb0ef41Sopenharmony_ci
23441cb0ef41Sopenharmony_ci<!-- YAML
23451cb0ef41Sopenharmony_ciadded: v10.0.0
23461cb0ef41Sopenharmony_ci-->
23471cb0ef41Sopenharmony_ci
23481cb0ef41Sopenharmony_ci* `value` {any}
23491cb0ef41Sopenharmony_ci* Returns: {boolean}
23501cb0ef41Sopenharmony_ci
23511cb0ef41Sopenharmony_ciReturns `true` if the value is a generator function.
23521cb0ef41Sopenharmony_ciThis only reports back what the JavaScript engine is seeing;
23531cb0ef41Sopenharmony_ciin particular, the return value may not match the original source code if
23541cb0ef41Sopenharmony_cia transpilation tool was used.
23551cb0ef41Sopenharmony_ci
23561cb0ef41Sopenharmony_ci```js
23571cb0ef41Sopenharmony_ciutil.types.isGeneratorFunction(function foo() {});  // Returns false
23581cb0ef41Sopenharmony_ciutil.types.isGeneratorFunction(function* foo() {});  // Returns true
23591cb0ef41Sopenharmony_ci```
23601cb0ef41Sopenharmony_ci
23611cb0ef41Sopenharmony_ci### `util.types.isGeneratorObject(value)`
23621cb0ef41Sopenharmony_ci
23631cb0ef41Sopenharmony_ci<!-- YAML
23641cb0ef41Sopenharmony_ciadded: v10.0.0
23651cb0ef41Sopenharmony_ci-->
23661cb0ef41Sopenharmony_ci
23671cb0ef41Sopenharmony_ci* `value` {any}
23681cb0ef41Sopenharmony_ci* Returns: {boolean}
23691cb0ef41Sopenharmony_ci
23701cb0ef41Sopenharmony_ciReturns `true` if the value is a generator object as returned from a
23711cb0ef41Sopenharmony_cibuilt-in generator function.
23721cb0ef41Sopenharmony_ciThis only reports back what the JavaScript engine is seeing;
23731cb0ef41Sopenharmony_ciin particular, the return value may not match the original source code if
23741cb0ef41Sopenharmony_cia transpilation tool was used.
23751cb0ef41Sopenharmony_ci
23761cb0ef41Sopenharmony_ci```js
23771cb0ef41Sopenharmony_cifunction* foo() {}
23781cb0ef41Sopenharmony_ciconst generator = foo();
23791cb0ef41Sopenharmony_ciutil.types.isGeneratorObject(generator);  // Returns true
23801cb0ef41Sopenharmony_ci```
23811cb0ef41Sopenharmony_ci
23821cb0ef41Sopenharmony_ci### `util.types.isInt8Array(value)`
23831cb0ef41Sopenharmony_ci
23841cb0ef41Sopenharmony_ci<!-- YAML
23851cb0ef41Sopenharmony_ciadded: v10.0.0
23861cb0ef41Sopenharmony_ci-->
23871cb0ef41Sopenharmony_ci
23881cb0ef41Sopenharmony_ci* `value` {any}
23891cb0ef41Sopenharmony_ci* Returns: {boolean}
23901cb0ef41Sopenharmony_ci
23911cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Int8Array`][] instance.
23921cb0ef41Sopenharmony_ci
23931cb0ef41Sopenharmony_ci```js
23941cb0ef41Sopenharmony_ciutil.types.isInt8Array(new ArrayBuffer());  // Returns false
23951cb0ef41Sopenharmony_ciutil.types.isInt8Array(new Int8Array());  // Returns true
23961cb0ef41Sopenharmony_ciutil.types.isInt8Array(new Float64Array());  // Returns false
23971cb0ef41Sopenharmony_ci```
23981cb0ef41Sopenharmony_ci
23991cb0ef41Sopenharmony_ci### `util.types.isInt16Array(value)`
24001cb0ef41Sopenharmony_ci
24011cb0ef41Sopenharmony_ci<!-- YAML
24021cb0ef41Sopenharmony_ciadded: v10.0.0
24031cb0ef41Sopenharmony_ci-->
24041cb0ef41Sopenharmony_ci
24051cb0ef41Sopenharmony_ci* `value` {any}
24061cb0ef41Sopenharmony_ci* Returns: {boolean}
24071cb0ef41Sopenharmony_ci
24081cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Int16Array`][] instance.
24091cb0ef41Sopenharmony_ci
24101cb0ef41Sopenharmony_ci```js
24111cb0ef41Sopenharmony_ciutil.types.isInt16Array(new ArrayBuffer());  // Returns false
24121cb0ef41Sopenharmony_ciutil.types.isInt16Array(new Int16Array());  // Returns true
24131cb0ef41Sopenharmony_ciutil.types.isInt16Array(new Float64Array());  // Returns false
24141cb0ef41Sopenharmony_ci```
24151cb0ef41Sopenharmony_ci
24161cb0ef41Sopenharmony_ci### `util.types.isInt32Array(value)`
24171cb0ef41Sopenharmony_ci
24181cb0ef41Sopenharmony_ci<!-- YAML
24191cb0ef41Sopenharmony_ciadded: v10.0.0
24201cb0ef41Sopenharmony_ci-->
24211cb0ef41Sopenharmony_ci
24221cb0ef41Sopenharmony_ci* `value` {any}
24231cb0ef41Sopenharmony_ci* Returns: {boolean}
24241cb0ef41Sopenharmony_ci
24251cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Int32Array`][] instance.
24261cb0ef41Sopenharmony_ci
24271cb0ef41Sopenharmony_ci```js
24281cb0ef41Sopenharmony_ciutil.types.isInt32Array(new ArrayBuffer());  // Returns false
24291cb0ef41Sopenharmony_ciutil.types.isInt32Array(new Int32Array());  // Returns true
24301cb0ef41Sopenharmony_ciutil.types.isInt32Array(new Float64Array());  // Returns false
24311cb0ef41Sopenharmony_ci```
24321cb0ef41Sopenharmony_ci
24331cb0ef41Sopenharmony_ci### `util.types.isKeyObject(value)`
24341cb0ef41Sopenharmony_ci
24351cb0ef41Sopenharmony_ci<!-- YAML
24361cb0ef41Sopenharmony_ciadded: v16.2.0
24371cb0ef41Sopenharmony_ci-->
24381cb0ef41Sopenharmony_ci
24391cb0ef41Sopenharmony_ci* `value` {Object}
24401cb0ef41Sopenharmony_ci* Returns: {boolean}
24411cb0ef41Sopenharmony_ci
24421cb0ef41Sopenharmony_ciReturns `true` if `value` is a {KeyObject}, `false` otherwise.
24431cb0ef41Sopenharmony_ci
24441cb0ef41Sopenharmony_ci### `util.types.isMap(value)`
24451cb0ef41Sopenharmony_ci
24461cb0ef41Sopenharmony_ci<!-- YAML
24471cb0ef41Sopenharmony_ciadded: v10.0.0
24481cb0ef41Sopenharmony_ci-->
24491cb0ef41Sopenharmony_ci
24501cb0ef41Sopenharmony_ci* `value` {any}
24511cb0ef41Sopenharmony_ci* Returns: {boolean}
24521cb0ef41Sopenharmony_ci
24531cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Map`][] instance.
24541cb0ef41Sopenharmony_ci
24551cb0ef41Sopenharmony_ci```js
24561cb0ef41Sopenharmony_ciutil.types.isMap(new Map());  // Returns true
24571cb0ef41Sopenharmony_ci```
24581cb0ef41Sopenharmony_ci
24591cb0ef41Sopenharmony_ci### `util.types.isMapIterator(value)`
24601cb0ef41Sopenharmony_ci
24611cb0ef41Sopenharmony_ci<!-- YAML
24621cb0ef41Sopenharmony_ciadded: v10.0.0
24631cb0ef41Sopenharmony_ci-->
24641cb0ef41Sopenharmony_ci
24651cb0ef41Sopenharmony_ci* `value` {any}
24661cb0ef41Sopenharmony_ci* Returns: {boolean}
24671cb0ef41Sopenharmony_ci
24681cb0ef41Sopenharmony_ciReturns `true` if the value is an iterator returned for a built-in
24691cb0ef41Sopenharmony_ci[`Map`][] instance.
24701cb0ef41Sopenharmony_ci
24711cb0ef41Sopenharmony_ci```js
24721cb0ef41Sopenharmony_ciconst map = new Map();
24731cb0ef41Sopenharmony_ciutil.types.isMapIterator(map.keys());  // Returns true
24741cb0ef41Sopenharmony_ciutil.types.isMapIterator(map.values());  // Returns true
24751cb0ef41Sopenharmony_ciutil.types.isMapIterator(map.entries());  // Returns true
24761cb0ef41Sopenharmony_ciutil.types.isMapIterator(map[Symbol.iterator]());  // Returns true
24771cb0ef41Sopenharmony_ci```
24781cb0ef41Sopenharmony_ci
24791cb0ef41Sopenharmony_ci### `util.types.isModuleNamespaceObject(value)`
24801cb0ef41Sopenharmony_ci
24811cb0ef41Sopenharmony_ci<!-- YAML
24821cb0ef41Sopenharmony_ciadded: v10.0.0
24831cb0ef41Sopenharmony_ci-->
24841cb0ef41Sopenharmony_ci
24851cb0ef41Sopenharmony_ci* `value` {any}
24861cb0ef41Sopenharmony_ci* Returns: {boolean}
24871cb0ef41Sopenharmony_ci
24881cb0ef41Sopenharmony_ciReturns `true` if the value is an instance of a [Module Namespace Object][].
24891cb0ef41Sopenharmony_ci
24901cb0ef41Sopenharmony_ci<!-- eslint-skip -->
24911cb0ef41Sopenharmony_ci
24921cb0ef41Sopenharmony_ci```js
24931cb0ef41Sopenharmony_ciimport * as ns from './a.js';
24941cb0ef41Sopenharmony_ci
24951cb0ef41Sopenharmony_ciutil.types.isModuleNamespaceObject(ns);  // Returns true
24961cb0ef41Sopenharmony_ci```
24971cb0ef41Sopenharmony_ci
24981cb0ef41Sopenharmony_ci### `util.types.isNativeError(value)`
24991cb0ef41Sopenharmony_ci
25001cb0ef41Sopenharmony_ci<!-- YAML
25011cb0ef41Sopenharmony_ciadded: v10.0.0
25021cb0ef41Sopenharmony_ci-->
25031cb0ef41Sopenharmony_ci
25041cb0ef41Sopenharmony_ci* `value` {any}
25051cb0ef41Sopenharmony_ci* Returns: {boolean}
25061cb0ef41Sopenharmony_ci
25071cb0ef41Sopenharmony_ciReturns `true` if the value was returned by the constructor of a
25081cb0ef41Sopenharmony_ci[built-in `Error` type][].
25091cb0ef41Sopenharmony_ci
25101cb0ef41Sopenharmony_ci```js
25111cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(new Error()));  // true
25121cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(new TypeError()));  // true
25131cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(new RangeError()));  // true
25141cb0ef41Sopenharmony_ci```
25151cb0ef41Sopenharmony_ci
25161cb0ef41Sopenharmony_ciSubclasses of the native error types are also native errors:
25171cb0ef41Sopenharmony_ci
25181cb0ef41Sopenharmony_ci```js
25191cb0ef41Sopenharmony_ciclass MyError extends Error {}
25201cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(new MyError()));  // true
25211cb0ef41Sopenharmony_ci```
25221cb0ef41Sopenharmony_ci
25231cb0ef41Sopenharmony_ciA value being `instanceof` a native error class is not equivalent to `isNativeError()`
25241cb0ef41Sopenharmony_cireturning `true` for that value. `isNativeError()` returns `true` for errors
25251cb0ef41Sopenharmony_ciwhich come from a different [realm][] while `instanceof Error` returns `false`
25261cb0ef41Sopenharmony_cifor these errors:
25271cb0ef41Sopenharmony_ci
25281cb0ef41Sopenharmony_ci```js
25291cb0ef41Sopenharmony_ciconst vm = require('node:vm');
25301cb0ef41Sopenharmony_ciconst context = vm.createContext({});
25311cb0ef41Sopenharmony_ciconst myError = vm.runInContext('new Error()', context);
25321cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(myError)); // true
25331cb0ef41Sopenharmony_ciconsole.log(myError instanceof Error); // false
25341cb0ef41Sopenharmony_ci```
25351cb0ef41Sopenharmony_ci
25361cb0ef41Sopenharmony_ciConversely, `isNativeError()` returns `false` for all objects which were not
25371cb0ef41Sopenharmony_cireturned by the constructor of a native error. That includes values
25381cb0ef41Sopenharmony_ciwhich are `instanceof` native errors:
25391cb0ef41Sopenharmony_ci
25401cb0ef41Sopenharmony_ci```js
25411cb0ef41Sopenharmony_ciconst myError = { __proto__: Error.prototype };
25421cb0ef41Sopenharmony_ciconsole.log(util.types.isNativeError(myError)); // false
25431cb0ef41Sopenharmony_ciconsole.log(myError instanceof Error); // true
25441cb0ef41Sopenharmony_ci```
25451cb0ef41Sopenharmony_ci
25461cb0ef41Sopenharmony_ci### `util.types.isNumberObject(value)`
25471cb0ef41Sopenharmony_ci
25481cb0ef41Sopenharmony_ci<!-- YAML
25491cb0ef41Sopenharmony_ciadded: v10.0.0
25501cb0ef41Sopenharmony_ci-->
25511cb0ef41Sopenharmony_ci
25521cb0ef41Sopenharmony_ci* `value` {any}
25531cb0ef41Sopenharmony_ci* Returns: {boolean}
25541cb0ef41Sopenharmony_ci
25551cb0ef41Sopenharmony_ciReturns `true` if the value is a number object, e.g. created
25561cb0ef41Sopenharmony_ciby `new Number()`.
25571cb0ef41Sopenharmony_ci
25581cb0ef41Sopenharmony_ci```js
25591cb0ef41Sopenharmony_ciutil.types.isNumberObject(0);  // Returns false
25601cb0ef41Sopenharmony_ciutil.types.isNumberObject(new Number(0));   // Returns true
25611cb0ef41Sopenharmony_ci```
25621cb0ef41Sopenharmony_ci
25631cb0ef41Sopenharmony_ci### `util.types.isPromise(value)`
25641cb0ef41Sopenharmony_ci
25651cb0ef41Sopenharmony_ci<!-- YAML
25661cb0ef41Sopenharmony_ciadded: v10.0.0
25671cb0ef41Sopenharmony_ci-->
25681cb0ef41Sopenharmony_ci
25691cb0ef41Sopenharmony_ci* `value` {any}
25701cb0ef41Sopenharmony_ci* Returns: {boolean}
25711cb0ef41Sopenharmony_ci
25721cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Promise`][].
25731cb0ef41Sopenharmony_ci
25741cb0ef41Sopenharmony_ci```js
25751cb0ef41Sopenharmony_ciutil.types.isPromise(Promise.resolve(42));  // Returns true
25761cb0ef41Sopenharmony_ci```
25771cb0ef41Sopenharmony_ci
25781cb0ef41Sopenharmony_ci### `util.types.isProxy(value)`
25791cb0ef41Sopenharmony_ci
25801cb0ef41Sopenharmony_ci<!-- YAML
25811cb0ef41Sopenharmony_ciadded: v10.0.0
25821cb0ef41Sopenharmony_ci-->
25831cb0ef41Sopenharmony_ci
25841cb0ef41Sopenharmony_ci* `value` {any}
25851cb0ef41Sopenharmony_ci* Returns: {boolean}
25861cb0ef41Sopenharmony_ci
25871cb0ef41Sopenharmony_ciReturns `true` if the value is a [`Proxy`][] instance.
25881cb0ef41Sopenharmony_ci
25891cb0ef41Sopenharmony_ci```js
25901cb0ef41Sopenharmony_ciconst target = {};
25911cb0ef41Sopenharmony_ciconst proxy = new Proxy(target, {});
25921cb0ef41Sopenharmony_ciutil.types.isProxy(target);  // Returns false
25931cb0ef41Sopenharmony_ciutil.types.isProxy(proxy);  // Returns true
25941cb0ef41Sopenharmony_ci```
25951cb0ef41Sopenharmony_ci
25961cb0ef41Sopenharmony_ci### `util.types.isRegExp(value)`
25971cb0ef41Sopenharmony_ci
25981cb0ef41Sopenharmony_ci<!-- YAML
25991cb0ef41Sopenharmony_ciadded: v10.0.0
26001cb0ef41Sopenharmony_ci-->
26011cb0ef41Sopenharmony_ci
26021cb0ef41Sopenharmony_ci* `value` {any}
26031cb0ef41Sopenharmony_ci* Returns: {boolean}
26041cb0ef41Sopenharmony_ci
26051cb0ef41Sopenharmony_ciReturns `true` if the value is a regular expression object.
26061cb0ef41Sopenharmony_ci
26071cb0ef41Sopenharmony_ci```js
26081cb0ef41Sopenharmony_ciutil.types.isRegExp(/abc/);  // Returns true
26091cb0ef41Sopenharmony_ciutil.types.isRegExp(new RegExp('abc'));  // Returns true
26101cb0ef41Sopenharmony_ci```
26111cb0ef41Sopenharmony_ci
26121cb0ef41Sopenharmony_ci### `util.types.isSet(value)`
26131cb0ef41Sopenharmony_ci
26141cb0ef41Sopenharmony_ci<!-- YAML
26151cb0ef41Sopenharmony_ciadded: v10.0.0
26161cb0ef41Sopenharmony_ci-->
26171cb0ef41Sopenharmony_ci
26181cb0ef41Sopenharmony_ci* `value` {any}
26191cb0ef41Sopenharmony_ci* Returns: {boolean}
26201cb0ef41Sopenharmony_ci
26211cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Set`][] instance.
26221cb0ef41Sopenharmony_ci
26231cb0ef41Sopenharmony_ci```js
26241cb0ef41Sopenharmony_ciutil.types.isSet(new Set());  // Returns true
26251cb0ef41Sopenharmony_ci```
26261cb0ef41Sopenharmony_ci
26271cb0ef41Sopenharmony_ci### `util.types.isSetIterator(value)`
26281cb0ef41Sopenharmony_ci
26291cb0ef41Sopenharmony_ci<!-- YAML
26301cb0ef41Sopenharmony_ciadded: v10.0.0
26311cb0ef41Sopenharmony_ci-->
26321cb0ef41Sopenharmony_ci
26331cb0ef41Sopenharmony_ci* `value` {any}
26341cb0ef41Sopenharmony_ci* Returns: {boolean}
26351cb0ef41Sopenharmony_ci
26361cb0ef41Sopenharmony_ciReturns `true` if the value is an iterator returned for a built-in
26371cb0ef41Sopenharmony_ci[`Set`][] instance.
26381cb0ef41Sopenharmony_ci
26391cb0ef41Sopenharmony_ci```js
26401cb0ef41Sopenharmony_ciconst set = new Set();
26411cb0ef41Sopenharmony_ciutil.types.isSetIterator(set.keys());  // Returns true
26421cb0ef41Sopenharmony_ciutil.types.isSetIterator(set.values());  // Returns true
26431cb0ef41Sopenharmony_ciutil.types.isSetIterator(set.entries());  // Returns true
26441cb0ef41Sopenharmony_ciutil.types.isSetIterator(set[Symbol.iterator]());  // Returns true
26451cb0ef41Sopenharmony_ci```
26461cb0ef41Sopenharmony_ci
26471cb0ef41Sopenharmony_ci### `util.types.isSharedArrayBuffer(value)`
26481cb0ef41Sopenharmony_ci
26491cb0ef41Sopenharmony_ci<!-- YAML
26501cb0ef41Sopenharmony_ciadded: v10.0.0
26511cb0ef41Sopenharmony_ci-->
26521cb0ef41Sopenharmony_ci
26531cb0ef41Sopenharmony_ci* `value` {any}
26541cb0ef41Sopenharmony_ci* Returns: {boolean}
26551cb0ef41Sopenharmony_ci
26561cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`SharedArrayBuffer`][] instance.
26571cb0ef41Sopenharmony_ciThis does _not_ include [`ArrayBuffer`][] instances. Usually, it is
26581cb0ef41Sopenharmony_cidesirable to test for both; See [`util.types.isAnyArrayBuffer()`][] for that.
26591cb0ef41Sopenharmony_ci
26601cb0ef41Sopenharmony_ci```js
26611cb0ef41Sopenharmony_ciutil.types.isSharedArrayBuffer(new ArrayBuffer());  // Returns false
26621cb0ef41Sopenharmony_ciutil.types.isSharedArrayBuffer(new SharedArrayBuffer());  // Returns true
26631cb0ef41Sopenharmony_ci```
26641cb0ef41Sopenharmony_ci
26651cb0ef41Sopenharmony_ci### `util.types.isStringObject(value)`
26661cb0ef41Sopenharmony_ci
26671cb0ef41Sopenharmony_ci<!-- YAML
26681cb0ef41Sopenharmony_ciadded: v10.0.0
26691cb0ef41Sopenharmony_ci-->
26701cb0ef41Sopenharmony_ci
26711cb0ef41Sopenharmony_ci* `value` {any}
26721cb0ef41Sopenharmony_ci* Returns: {boolean}
26731cb0ef41Sopenharmony_ci
26741cb0ef41Sopenharmony_ciReturns `true` if the value is a string object, e.g. created
26751cb0ef41Sopenharmony_ciby `new String()`.
26761cb0ef41Sopenharmony_ci
26771cb0ef41Sopenharmony_ci```js
26781cb0ef41Sopenharmony_ciutil.types.isStringObject('foo');  // Returns false
26791cb0ef41Sopenharmony_ciutil.types.isStringObject(new String('foo'));   // Returns true
26801cb0ef41Sopenharmony_ci```
26811cb0ef41Sopenharmony_ci
26821cb0ef41Sopenharmony_ci### `util.types.isSymbolObject(value)`
26831cb0ef41Sopenharmony_ci
26841cb0ef41Sopenharmony_ci<!-- YAML
26851cb0ef41Sopenharmony_ciadded: v10.0.0
26861cb0ef41Sopenharmony_ci-->
26871cb0ef41Sopenharmony_ci
26881cb0ef41Sopenharmony_ci* `value` {any}
26891cb0ef41Sopenharmony_ci* Returns: {boolean}
26901cb0ef41Sopenharmony_ci
26911cb0ef41Sopenharmony_ciReturns `true` if the value is a symbol object, created
26921cb0ef41Sopenharmony_ciby calling `Object()` on a `Symbol` primitive.
26931cb0ef41Sopenharmony_ci
26941cb0ef41Sopenharmony_ci```js
26951cb0ef41Sopenharmony_ciconst symbol = Symbol('foo');
26961cb0ef41Sopenharmony_ciutil.types.isSymbolObject(symbol);  // Returns false
26971cb0ef41Sopenharmony_ciutil.types.isSymbolObject(Object(symbol));   // Returns true
26981cb0ef41Sopenharmony_ci```
26991cb0ef41Sopenharmony_ci
27001cb0ef41Sopenharmony_ci### `util.types.isTypedArray(value)`
27011cb0ef41Sopenharmony_ci
27021cb0ef41Sopenharmony_ci<!-- YAML
27031cb0ef41Sopenharmony_ciadded: v10.0.0
27041cb0ef41Sopenharmony_ci-->
27051cb0ef41Sopenharmony_ci
27061cb0ef41Sopenharmony_ci* `value` {any}
27071cb0ef41Sopenharmony_ci* Returns: {boolean}
27081cb0ef41Sopenharmony_ci
27091cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`TypedArray`][] instance.
27101cb0ef41Sopenharmony_ci
27111cb0ef41Sopenharmony_ci```js
27121cb0ef41Sopenharmony_ciutil.types.isTypedArray(new ArrayBuffer());  // Returns false
27131cb0ef41Sopenharmony_ciutil.types.isTypedArray(new Uint8Array());  // Returns true
27141cb0ef41Sopenharmony_ciutil.types.isTypedArray(new Float64Array());  // Returns true
27151cb0ef41Sopenharmony_ci```
27161cb0ef41Sopenharmony_ci
27171cb0ef41Sopenharmony_ciSee also [`ArrayBuffer.isView()`][].
27181cb0ef41Sopenharmony_ci
27191cb0ef41Sopenharmony_ci### `util.types.isUint8Array(value)`
27201cb0ef41Sopenharmony_ci
27211cb0ef41Sopenharmony_ci<!-- YAML
27221cb0ef41Sopenharmony_ciadded: v10.0.0
27231cb0ef41Sopenharmony_ci-->
27241cb0ef41Sopenharmony_ci
27251cb0ef41Sopenharmony_ci* `value` {any}
27261cb0ef41Sopenharmony_ci* Returns: {boolean}
27271cb0ef41Sopenharmony_ci
27281cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Uint8Array`][] instance.
27291cb0ef41Sopenharmony_ci
27301cb0ef41Sopenharmony_ci```js
27311cb0ef41Sopenharmony_ciutil.types.isUint8Array(new ArrayBuffer());  // Returns false
27321cb0ef41Sopenharmony_ciutil.types.isUint8Array(new Uint8Array());  // Returns true
27331cb0ef41Sopenharmony_ciutil.types.isUint8Array(new Float64Array());  // Returns false
27341cb0ef41Sopenharmony_ci```
27351cb0ef41Sopenharmony_ci
27361cb0ef41Sopenharmony_ci### `util.types.isUint8ClampedArray(value)`
27371cb0ef41Sopenharmony_ci
27381cb0ef41Sopenharmony_ci<!-- YAML
27391cb0ef41Sopenharmony_ciadded: v10.0.0
27401cb0ef41Sopenharmony_ci-->
27411cb0ef41Sopenharmony_ci
27421cb0ef41Sopenharmony_ci* `value` {any}
27431cb0ef41Sopenharmony_ci* Returns: {boolean}
27441cb0ef41Sopenharmony_ci
27451cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Uint8ClampedArray`][] instance.
27461cb0ef41Sopenharmony_ci
27471cb0ef41Sopenharmony_ci```js
27481cb0ef41Sopenharmony_ciutil.types.isUint8ClampedArray(new ArrayBuffer());  // Returns false
27491cb0ef41Sopenharmony_ciutil.types.isUint8ClampedArray(new Uint8ClampedArray());  // Returns true
27501cb0ef41Sopenharmony_ciutil.types.isUint8ClampedArray(new Float64Array());  // Returns false
27511cb0ef41Sopenharmony_ci```
27521cb0ef41Sopenharmony_ci
27531cb0ef41Sopenharmony_ci### `util.types.isUint16Array(value)`
27541cb0ef41Sopenharmony_ci
27551cb0ef41Sopenharmony_ci<!-- YAML
27561cb0ef41Sopenharmony_ciadded: v10.0.0
27571cb0ef41Sopenharmony_ci-->
27581cb0ef41Sopenharmony_ci
27591cb0ef41Sopenharmony_ci* `value` {any}
27601cb0ef41Sopenharmony_ci* Returns: {boolean}
27611cb0ef41Sopenharmony_ci
27621cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Uint16Array`][] instance.
27631cb0ef41Sopenharmony_ci
27641cb0ef41Sopenharmony_ci```js
27651cb0ef41Sopenharmony_ciutil.types.isUint16Array(new ArrayBuffer());  // Returns false
27661cb0ef41Sopenharmony_ciutil.types.isUint16Array(new Uint16Array());  // Returns true
27671cb0ef41Sopenharmony_ciutil.types.isUint16Array(new Float64Array());  // Returns false
27681cb0ef41Sopenharmony_ci```
27691cb0ef41Sopenharmony_ci
27701cb0ef41Sopenharmony_ci### `util.types.isUint32Array(value)`
27711cb0ef41Sopenharmony_ci
27721cb0ef41Sopenharmony_ci<!-- YAML
27731cb0ef41Sopenharmony_ciadded: v10.0.0
27741cb0ef41Sopenharmony_ci-->
27751cb0ef41Sopenharmony_ci
27761cb0ef41Sopenharmony_ci* `value` {any}
27771cb0ef41Sopenharmony_ci* Returns: {boolean}
27781cb0ef41Sopenharmony_ci
27791cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`Uint32Array`][] instance.
27801cb0ef41Sopenharmony_ci
27811cb0ef41Sopenharmony_ci```js
27821cb0ef41Sopenharmony_ciutil.types.isUint32Array(new ArrayBuffer());  // Returns false
27831cb0ef41Sopenharmony_ciutil.types.isUint32Array(new Uint32Array());  // Returns true
27841cb0ef41Sopenharmony_ciutil.types.isUint32Array(new Float64Array());  // Returns false
27851cb0ef41Sopenharmony_ci```
27861cb0ef41Sopenharmony_ci
27871cb0ef41Sopenharmony_ci### `util.types.isWeakMap(value)`
27881cb0ef41Sopenharmony_ci
27891cb0ef41Sopenharmony_ci<!-- YAML
27901cb0ef41Sopenharmony_ciadded: v10.0.0
27911cb0ef41Sopenharmony_ci-->
27921cb0ef41Sopenharmony_ci
27931cb0ef41Sopenharmony_ci* `value` {any}
27941cb0ef41Sopenharmony_ci* Returns: {boolean}
27951cb0ef41Sopenharmony_ci
27961cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`WeakMap`][] instance.
27971cb0ef41Sopenharmony_ci
27981cb0ef41Sopenharmony_ci```js
27991cb0ef41Sopenharmony_ciutil.types.isWeakMap(new WeakMap());  // Returns true
28001cb0ef41Sopenharmony_ci```
28011cb0ef41Sopenharmony_ci
28021cb0ef41Sopenharmony_ci### `util.types.isWeakSet(value)`
28031cb0ef41Sopenharmony_ci
28041cb0ef41Sopenharmony_ci<!-- YAML
28051cb0ef41Sopenharmony_ciadded: v10.0.0
28061cb0ef41Sopenharmony_ci-->
28071cb0ef41Sopenharmony_ci
28081cb0ef41Sopenharmony_ci* `value` {any}
28091cb0ef41Sopenharmony_ci* Returns: {boolean}
28101cb0ef41Sopenharmony_ci
28111cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`WeakSet`][] instance.
28121cb0ef41Sopenharmony_ci
28131cb0ef41Sopenharmony_ci```js
28141cb0ef41Sopenharmony_ciutil.types.isWeakSet(new WeakSet());  // Returns true
28151cb0ef41Sopenharmony_ci```
28161cb0ef41Sopenharmony_ci
28171cb0ef41Sopenharmony_ci### `util.types.isWebAssemblyCompiledModule(value)`
28181cb0ef41Sopenharmony_ci
28191cb0ef41Sopenharmony_ci<!-- YAML
28201cb0ef41Sopenharmony_ciadded: v10.0.0
28211cb0ef41Sopenharmony_cideprecated: v14.0.0
28221cb0ef41Sopenharmony_ci-->
28231cb0ef41Sopenharmony_ci
28241cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `value instanceof WebAssembly.Module` instead.
28251cb0ef41Sopenharmony_ci
28261cb0ef41Sopenharmony_ci* `value` {any}
28271cb0ef41Sopenharmony_ci* Returns: {boolean}
28281cb0ef41Sopenharmony_ci
28291cb0ef41Sopenharmony_ciReturns `true` if the value is a built-in [`WebAssembly.Module`][] instance.
28301cb0ef41Sopenharmony_ci
28311cb0ef41Sopenharmony_ci```js
28321cb0ef41Sopenharmony_ciconst module = new WebAssembly.Module(wasmBuffer);
28331cb0ef41Sopenharmony_ciutil.types.isWebAssemblyCompiledModule(module);  // Returns true
28341cb0ef41Sopenharmony_ci```
28351cb0ef41Sopenharmony_ci
28361cb0ef41Sopenharmony_ci## Deprecated APIs
28371cb0ef41Sopenharmony_ci
28381cb0ef41Sopenharmony_ciThe following APIs are deprecated and should no longer be used. Existing
28391cb0ef41Sopenharmony_ciapplications and modules should be updated to find alternative approaches.
28401cb0ef41Sopenharmony_ci
28411cb0ef41Sopenharmony_ci### `util._extend(target, source)`
28421cb0ef41Sopenharmony_ci
28431cb0ef41Sopenharmony_ci<!-- YAML
28441cb0ef41Sopenharmony_ciadded: v0.7.5
28451cb0ef41Sopenharmony_cideprecated: v6.0.0
28461cb0ef41Sopenharmony_ci-->
28471cb0ef41Sopenharmony_ci
28481cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Object.assign()`][] instead.
28491cb0ef41Sopenharmony_ci
28501cb0ef41Sopenharmony_ci* `target` {Object}
28511cb0ef41Sopenharmony_ci* `source` {Object}
28521cb0ef41Sopenharmony_ci
28531cb0ef41Sopenharmony_ciThe `util._extend()` method was never intended to be used outside of internal
28541cb0ef41Sopenharmony_ciNode.js modules. The community found and used it anyway.
28551cb0ef41Sopenharmony_ci
28561cb0ef41Sopenharmony_ciIt is deprecated and should not be used in new code. JavaScript comes with very
28571cb0ef41Sopenharmony_cisimilar built-in functionality through [`Object.assign()`][].
28581cb0ef41Sopenharmony_ci
28591cb0ef41Sopenharmony_ci### `util.isArray(object)`
28601cb0ef41Sopenharmony_ci
28611cb0ef41Sopenharmony_ci<!-- YAML
28621cb0ef41Sopenharmony_ciadded: v0.6.0
28631cb0ef41Sopenharmony_cideprecated: v4.0.0
28641cb0ef41Sopenharmony_ci-->
28651cb0ef41Sopenharmony_ci
28661cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Array.isArray()`][] instead.
28671cb0ef41Sopenharmony_ci
28681cb0ef41Sopenharmony_ci* `object` {any}
28691cb0ef41Sopenharmony_ci* Returns: {boolean}
28701cb0ef41Sopenharmony_ci
28711cb0ef41Sopenharmony_ciAlias for [`Array.isArray()`][].
28721cb0ef41Sopenharmony_ci
28731cb0ef41Sopenharmony_ciReturns `true` if the given `object` is an `Array`. Otherwise, returns `false`.
28741cb0ef41Sopenharmony_ci
28751cb0ef41Sopenharmony_ci```js
28761cb0ef41Sopenharmony_ciconst util = require('node:util');
28771cb0ef41Sopenharmony_ci
28781cb0ef41Sopenharmony_ciutil.isArray([]);
28791cb0ef41Sopenharmony_ci// Returns: true
28801cb0ef41Sopenharmony_ciutil.isArray(new Array());
28811cb0ef41Sopenharmony_ci// Returns: true
28821cb0ef41Sopenharmony_ciutil.isArray({});
28831cb0ef41Sopenharmony_ci// Returns: false
28841cb0ef41Sopenharmony_ci```
28851cb0ef41Sopenharmony_ci
28861cb0ef41Sopenharmony_ci### `util.isBoolean(object)`
28871cb0ef41Sopenharmony_ci
28881cb0ef41Sopenharmony_ci<!-- YAML
28891cb0ef41Sopenharmony_ciadded: v0.11.5
28901cb0ef41Sopenharmony_cideprecated: v4.0.0
28911cb0ef41Sopenharmony_ci-->
28921cb0ef41Sopenharmony_ci
28931cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `typeof value === 'boolean'` instead.
28941cb0ef41Sopenharmony_ci
28951cb0ef41Sopenharmony_ci* `object` {any}
28961cb0ef41Sopenharmony_ci* Returns: {boolean}
28971cb0ef41Sopenharmony_ci
28981cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
28991cb0ef41Sopenharmony_ci
29001cb0ef41Sopenharmony_ci```js
29011cb0ef41Sopenharmony_ciconst util = require('node:util');
29021cb0ef41Sopenharmony_ci
29031cb0ef41Sopenharmony_ciutil.isBoolean(1);
29041cb0ef41Sopenharmony_ci// Returns: false
29051cb0ef41Sopenharmony_ciutil.isBoolean(0);
29061cb0ef41Sopenharmony_ci// Returns: false
29071cb0ef41Sopenharmony_ciutil.isBoolean(false);
29081cb0ef41Sopenharmony_ci// Returns: true
29091cb0ef41Sopenharmony_ci```
29101cb0ef41Sopenharmony_ci
29111cb0ef41Sopenharmony_ci### `util.isBuffer(object)`
29121cb0ef41Sopenharmony_ci
29131cb0ef41Sopenharmony_ci<!-- YAML
29141cb0ef41Sopenharmony_ciadded: v0.11.5
29151cb0ef41Sopenharmony_cideprecated: v4.0.0
29161cb0ef41Sopenharmony_ci-->
29171cb0ef41Sopenharmony_ci
29181cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.isBuffer()`][] instead.
29191cb0ef41Sopenharmony_ci
29201cb0ef41Sopenharmony_ci* `object` {any}
29211cb0ef41Sopenharmony_ci* Returns: {boolean}
29221cb0ef41Sopenharmony_ci
29231cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Buffer`. Otherwise, returns `false`.
29241cb0ef41Sopenharmony_ci
29251cb0ef41Sopenharmony_ci```js
29261cb0ef41Sopenharmony_ciconst util = require('node:util');
29271cb0ef41Sopenharmony_ci
29281cb0ef41Sopenharmony_ciutil.isBuffer({ length: 0 });
29291cb0ef41Sopenharmony_ci// Returns: false
29301cb0ef41Sopenharmony_ciutil.isBuffer([]);
29311cb0ef41Sopenharmony_ci// Returns: false
29321cb0ef41Sopenharmony_ciutil.isBuffer(Buffer.from('hello world'));
29331cb0ef41Sopenharmony_ci// Returns: true
29341cb0ef41Sopenharmony_ci```
29351cb0ef41Sopenharmony_ci
29361cb0ef41Sopenharmony_ci### `util.isDate(object)`
29371cb0ef41Sopenharmony_ci
29381cb0ef41Sopenharmony_ci<!-- YAML
29391cb0ef41Sopenharmony_ciadded: v0.6.0
29401cb0ef41Sopenharmony_cideprecated: v4.0.0
29411cb0ef41Sopenharmony_ci-->
29421cb0ef41Sopenharmony_ci
29431cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`util.types.isDate()`][] instead.
29441cb0ef41Sopenharmony_ci
29451cb0ef41Sopenharmony_ci* `object` {any}
29461cb0ef41Sopenharmony_ci* Returns: {boolean}
29471cb0ef41Sopenharmony_ci
29481cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Date`. Otherwise, returns `false`.
29491cb0ef41Sopenharmony_ci
29501cb0ef41Sopenharmony_ci```js
29511cb0ef41Sopenharmony_ciconst util = require('node:util');
29521cb0ef41Sopenharmony_ci
29531cb0ef41Sopenharmony_ciutil.isDate(new Date());
29541cb0ef41Sopenharmony_ci// Returns: true
29551cb0ef41Sopenharmony_ciutil.isDate(Date());
29561cb0ef41Sopenharmony_ci// false (without 'new' returns a String)
29571cb0ef41Sopenharmony_ciutil.isDate({});
29581cb0ef41Sopenharmony_ci// Returns: false
29591cb0ef41Sopenharmony_ci```
29601cb0ef41Sopenharmony_ci
29611cb0ef41Sopenharmony_ci### `util.isError(object)`
29621cb0ef41Sopenharmony_ci
29631cb0ef41Sopenharmony_ci<!-- YAML
29641cb0ef41Sopenharmony_ciadded: v0.6.0
29651cb0ef41Sopenharmony_cideprecated: v4.0.0
29661cb0ef41Sopenharmony_ci-->
29671cb0ef41Sopenharmony_ci
29681cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`util.types.isNativeError()`][] instead.
29691cb0ef41Sopenharmony_ci
29701cb0ef41Sopenharmony_ci* `object` {any}
29711cb0ef41Sopenharmony_ci* Returns: {boolean}
29721cb0ef41Sopenharmony_ci
29731cb0ef41Sopenharmony_ciReturns `true` if the given `object` is an [`Error`][]. Otherwise, returns
29741cb0ef41Sopenharmony_ci`false`.
29751cb0ef41Sopenharmony_ci
29761cb0ef41Sopenharmony_ci```js
29771cb0ef41Sopenharmony_ciconst util = require('node:util');
29781cb0ef41Sopenharmony_ci
29791cb0ef41Sopenharmony_ciutil.isError(new Error());
29801cb0ef41Sopenharmony_ci// Returns: true
29811cb0ef41Sopenharmony_ciutil.isError(new TypeError());
29821cb0ef41Sopenharmony_ci// Returns: true
29831cb0ef41Sopenharmony_ciutil.isError({ name: 'Error', message: 'an error occurred' });
29841cb0ef41Sopenharmony_ci// Returns: false
29851cb0ef41Sopenharmony_ci```
29861cb0ef41Sopenharmony_ci
29871cb0ef41Sopenharmony_ciThis method relies on `Object.prototype.toString()` behavior. It is
29881cb0ef41Sopenharmony_cipossible to obtain an incorrect result when the `object` argument manipulates
29891cb0ef41Sopenharmony_ci`@@toStringTag`.
29901cb0ef41Sopenharmony_ci
29911cb0ef41Sopenharmony_ci```js
29921cb0ef41Sopenharmony_ciconst util = require('node:util');
29931cb0ef41Sopenharmony_ciconst obj = { name: 'Error', message: 'an error occurred' };
29941cb0ef41Sopenharmony_ci
29951cb0ef41Sopenharmony_ciutil.isError(obj);
29961cb0ef41Sopenharmony_ci// Returns: false
29971cb0ef41Sopenharmony_ciobj[Symbol.toStringTag] = 'Error';
29981cb0ef41Sopenharmony_ciutil.isError(obj);
29991cb0ef41Sopenharmony_ci// Returns: true
30001cb0ef41Sopenharmony_ci```
30011cb0ef41Sopenharmony_ci
30021cb0ef41Sopenharmony_ci### `util.isFunction(object)`
30031cb0ef41Sopenharmony_ci
30041cb0ef41Sopenharmony_ci<!-- YAML
30051cb0ef41Sopenharmony_ciadded: v0.11.5
30061cb0ef41Sopenharmony_cideprecated: v4.0.0
30071cb0ef41Sopenharmony_ci-->
30081cb0ef41Sopenharmony_ci
30091cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `typeof value === 'function'` instead.
30101cb0ef41Sopenharmony_ci
30111cb0ef41Sopenharmony_ci* `object` {any}
30121cb0ef41Sopenharmony_ci* Returns: {boolean}
30131cb0ef41Sopenharmony_ci
30141cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Function`. Otherwise, returns
30151cb0ef41Sopenharmony_ci`false`.
30161cb0ef41Sopenharmony_ci
30171cb0ef41Sopenharmony_ci```js
30181cb0ef41Sopenharmony_ciconst util = require('node:util');
30191cb0ef41Sopenharmony_ci
30201cb0ef41Sopenharmony_cifunction Foo() {}
30211cb0ef41Sopenharmony_ciconst Bar = () => {};
30221cb0ef41Sopenharmony_ci
30231cb0ef41Sopenharmony_ciutil.isFunction({});
30241cb0ef41Sopenharmony_ci// Returns: false
30251cb0ef41Sopenharmony_ciutil.isFunction(Foo);
30261cb0ef41Sopenharmony_ci// Returns: true
30271cb0ef41Sopenharmony_ciutil.isFunction(Bar);
30281cb0ef41Sopenharmony_ci// Returns: true
30291cb0ef41Sopenharmony_ci```
30301cb0ef41Sopenharmony_ci
30311cb0ef41Sopenharmony_ci### `util.isNull(object)`
30321cb0ef41Sopenharmony_ci
30331cb0ef41Sopenharmony_ci<!-- YAML
30341cb0ef41Sopenharmony_ciadded: v0.11.5
30351cb0ef41Sopenharmony_cideprecated: v4.0.0
30361cb0ef41Sopenharmony_ci-->
30371cb0ef41Sopenharmony_ci
30381cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `value === null` instead.
30391cb0ef41Sopenharmony_ci
30401cb0ef41Sopenharmony_ci* `object` {any}
30411cb0ef41Sopenharmony_ci* Returns: {boolean}
30421cb0ef41Sopenharmony_ci
30431cb0ef41Sopenharmony_ciReturns `true` if the given `object` is strictly `null`. Otherwise, returns
30441cb0ef41Sopenharmony_ci`false`.
30451cb0ef41Sopenharmony_ci
30461cb0ef41Sopenharmony_ci```js
30471cb0ef41Sopenharmony_ciconst util = require('node:util');
30481cb0ef41Sopenharmony_ci
30491cb0ef41Sopenharmony_ciutil.isNull(0);
30501cb0ef41Sopenharmony_ci// Returns: false
30511cb0ef41Sopenharmony_ciutil.isNull(undefined);
30521cb0ef41Sopenharmony_ci// Returns: false
30531cb0ef41Sopenharmony_ciutil.isNull(null);
30541cb0ef41Sopenharmony_ci// Returns: true
30551cb0ef41Sopenharmony_ci```
30561cb0ef41Sopenharmony_ci
30571cb0ef41Sopenharmony_ci### `util.isNullOrUndefined(object)`
30581cb0ef41Sopenharmony_ci
30591cb0ef41Sopenharmony_ci<!-- YAML
30601cb0ef41Sopenharmony_ciadded: v0.11.5
30611cb0ef41Sopenharmony_cideprecated: v4.0.0
30621cb0ef41Sopenharmony_ci-->
30631cb0ef41Sopenharmony_ci
30641cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use
30651cb0ef41Sopenharmony_ci> `value === undefined || value === null` instead.
30661cb0ef41Sopenharmony_ci
30671cb0ef41Sopenharmony_ci* `object` {any}
30681cb0ef41Sopenharmony_ci* Returns: {boolean}
30691cb0ef41Sopenharmony_ci
30701cb0ef41Sopenharmony_ciReturns `true` if the given `object` is `null` or `undefined`. Otherwise,
30711cb0ef41Sopenharmony_cireturns `false`.
30721cb0ef41Sopenharmony_ci
30731cb0ef41Sopenharmony_ci```js
30741cb0ef41Sopenharmony_ciconst util = require('node:util');
30751cb0ef41Sopenharmony_ci
30761cb0ef41Sopenharmony_ciutil.isNullOrUndefined(0);
30771cb0ef41Sopenharmony_ci// Returns: false
30781cb0ef41Sopenharmony_ciutil.isNullOrUndefined(undefined);
30791cb0ef41Sopenharmony_ci// Returns: true
30801cb0ef41Sopenharmony_ciutil.isNullOrUndefined(null);
30811cb0ef41Sopenharmony_ci// Returns: true
30821cb0ef41Sopenharmony_ci```
30831cb0ef41Sopenharmony_ci
30841cb0ef41Sopenharmony_ci### `util.isNumber(object)`
30851cb0ef41Sopenharmony_ci
30861cb0ef41Sopenharmony_ci<!-- YAML
30871cb0ef41Sopenharmony_ciadded: v0.11.5
30881cb0ef41Sopenharmony_cideprecated: v4.0.0
30891cb0ef41Sopenharmony_ci-->
30901cb0ef41Sopenharmony_ci
30911cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `typeof value === 'number'` instead.
30921cb0ef41Sopenharmony_ci
30931cb0ef41Sopenharmony_ci* `object` {any}
30941cb0ef41Sopenharmony_ci* Returns: {boolean}
30951cb0ef41Sopenharmony_ci
30961cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Number`. Otherwise, returns `false`.
30971cb0ef41Sopenharmony_ci
30981cb0ef41Sopenharmony_ci```js
30991cb0ef41Sopenharmony_ciconst util = require('node:util');
31001cb0ef41Sopenharmony_ci
31011cb0ef41Sopenharmony_ciutil.isNumber(false);
31021cb0ef41Sopenharmony_ci// Returns: false
31031cb0ef41Sopenharmony_ciutil.isNumber(Infinity);
31041cb0ef41Sopenharmony_ci// Returns: true
31051cb0ef41Sopenharmony_ciutil.isNumber(0);
31061cb0ef41Sopenharmony_ci// Returns: true
31071cb0ef41Sopenharmony_ciutil.isNumber(NaN);
31081cb0ef41Sopenharmony_ci// Returns: true
31091cb0ef41Sopenharmony_ci```
31101cb0ef41Sopenharmony_ci
31111cb0ef41Sopenharmony_ci### `util.isObject(object)`
31121cb0ef41Sopenharmony_ci
31131cb0ef41Sopenharmony_ci<!-- YAML
31141cb0ef41Sopenharmony_ciadded: v0.11.5
31151cb0ef41Sopenharmony_cideprecated: v4.0.0
31161cb0ef41Sopenharmony_ci-->
31171cb0ef41Sopenharmony_ci
31181cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated:
31191cb0ef41Sopenharmony_ci> Use `value !== null && typeof value === 'object'` instead.
31201cb0ef41Sopenharmony_ci
31211cb0ef41Sopenharmony_ci* `object` {any}
31221cb0ef41Sopenharmony_ci* Returns: {boolean}
31231cb0ef41Sopenharmony_ci
31241cb0ef41Sopenharmony_ciReturns `true` if the given `object` is strictly an `Object` **and** not a
31251cb0ef41Sopenharmony_ci`Function` (even though functions are objects in JavaScript).
31261cb0ef41Sopenharmony_ciOtherwise, returns `false`.
31271cb0ef41Sopenharmony_ci
31281cb0ef41Sopenharmony_ci```js
31291cb0ef41Sopenharmony_ciconst util = require('node:util');
31301cb0ef41Sopenharmony_ci
31311cb0ef41Sopenharmony_ciutil.isObject(5);
31321cb0ef41Sopenharmony_ci// Returns: false
31331cb0ef41Sopenharmony_ciutil.isObject(null);
31341cb0ef41Sopenharmony_ci// Returns: false
31351cb0ef41Sopenharmony_ciutil.isObject({});
31361cb0ef41Sopenharmony_ci// Returns: true
31371cb0ef41Sopenharmony_ciutil.isObject(() => {});
31381cb0ef41Sopenharmony_ci// Returns: false
31391cb0ef41Sopenharmony_ci```
31401cb0ef41Sopenharmony_ci
31411cb0ef41Sopenharmony_ci### `util.isPrimitive(object)`
31421cb0ef41Sopenharmony_ci
31431cb0ef41Sopenharmony_ci<!-- YAML
31441cb0ef41Sopenharmony_ciadded: v0.11.5
31451cb0ef41Sopenharmony_cideprecated: v4.0.0
31461cb0ef41Sopenharmony_ci-->
31471cb0ef41Sopenharmony_ci
31481cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use
31491cb0ef41Sopenharmony_ci> `(typeof value !== 'object' && typeof value !== 'function') || value === null`
31501cb0ef41Sopenharmony_ci> instead.
31511cb0ef41Sopenharmony_ci
31521cb0ef41Sopenharmony_ci* `object` {any}
31531cb0ef41Sopenharmony_ci* Returns: {boolean}
31541cb0ef41Sopenharmony_ci
31551cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a primitive type. Otherwise, returns
31561cb0ef41Sopenharmony_ci`false`.
31571cb0ef41Sopenharmony_ci
31581cb0ef41Sopenharmony_ci```js
31591cb0ef41Sopenharmony_ciconst util = require('node:util');
31601cb0ef41Sopenharmony_ci
31611cb0ef41Sopenharmony_ciutil.isPrimitive(5);
31621cb0ef41Sopenharmony_ci// Returns: true
31631cb0ef41Sopenharmony_ciutil.isPrimitive('foo');
31641cb0ef41Sopenharmony_ci// Returns: true
31651cb0ef41Sopenharmony_ciutil.isPrimitive(false);
31661cb0ef41Sopenharmony_ci// Returns: true
31671cb0ef41Sopenharmony_ciutil.isPrimitive(null);
31681cb0ef41Sopenharmony_ci// Returns: true
31691cb0ef41Sopenharmony_ciutil.isPrimitive(undefined);
31701cb0ef41Sopenharmony_ci// Returns: true
31711cb0ef41Sopenharmony_ciutil.isPrimitive({});
31721cb0ef41Sopenharmony_ci// Returns: false
31731cb0ef41Sopenharmony_ciutil.isPrimitive(() => {});
31741cb0ef41Sopenharmony_ci// Returns: false
31751cb0ef41Sopenharmony_ciutil.isPrimitive(/^$/);
31761cb0ef41Sopenharmony_ci// Returns: false
31771cb0ef41Sopenharmony_ciutil.isPrimitive(new Date());
31781cb0ef41Sopenharmony_ci// Returns: false
31791cb0ef41Sopenharmony_ci```
31801cb0ef41Sopenharmony_ci
31811cb0ef41Sopenharmony_ci### `util.isRegExp(object)`
31821cb0ef41Sopenharmony_ci
31831cb0ef41Sopenharmony_ci<!-- YAML
31841cb0ef41Sopenharmony_ciadded: v0.6.0
31851cb0ef41Sopenharmony_cideprecated: v4.0.0
31861cb0ef41Sopenharmony_ci-->
31871cb0ef41Sopenharmony_ci
31881cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated
31891cb0ef41Sopenharmony_ci
31901cb0ef41Sopenharmony_ci* `object` {any}
31911cb0ef41Sopenharmony_ci* Returns: {boolean}
31921cb0ef41Sopenharmony_ci
31931cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `RegExp`. Otherwise, returns `false`.
31941cb0ef41Sopenharmony_ci
31951cb0ef41Sopenharmony_ci```js
31961cb0ef41Sopenharmony_ciconst util = require('node:util');
31971cb0ef41Sopenharmony_ci
31981cb0ef41Sopenharmony_ciutil.isRegExp(/some regexp/);
31991cb0ef41Sopenharmony_ci// Returns: true
32001cb0ef41Sopenharmony_ciutil.isRegExp(new RegExp('another regexp'));
32011cb0ef41Sopenharmony_ci// Returns: true
32021cb0ef41Sopenharmony_ciutil.isRegExp({});
32031cb0ef41Sopenharmony_ci// Returns: false
32041cb0ef41Sopenharmony_ci```
32051cb0ef41Sopenharmony_ci
32061cb0ef41Sopenharmony_ci### `util.isString(object)`
32071cb0ef41Sopenharmony_ci
32081cb0ef41Sopenharmony_ci<!-- YAML
32091cb0ef41Sopenharmony_ciadded: v0.11.5
32101cb0ef41Sopenharmony_cideprecated: v4.0.0
32111cb0ef41Sopenharmony_ci-->
32121cb0ef41Sopenharmony_ci
32131cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `typeof value === 'string'` instead.
32141cb0ef41Sopenharmony_ci
32151cb0ef41Sopenharmony_ci* `object` {any}
32161cb0ef41Sopenharmony_ci* Returns: {boolean}
32171cb0ef41Sopenharmony_ci
32181cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `string`. Otherwise, returns `false`.
32191cb0ef41Sopenharmony_ci
32201cb0ef41Sopenharmony_ci```js
32211cb0ef41Sopenharmony_ciconst util = require('node:util');
32221cb0ef41Sopenharmony_ci
32231cb0ef41Sopenharmony_ciutil.isString('');
32241cb0ef41Sopenharmony_ci// Returns: true
32251cb0ef41Sopenharmony_ciutil.isString('foo');
32261cb0ef41Sopenharmony_ci// Returns: true
32271cb0ef41Sopenharmony_ciutil.isString(String('foo'));
32281cb0ef41Sopenharmony_ci// Returns: true
32291cb0ef41Sopenharmony_ciutil.isString(5);
32301cb0ef41Sopenharmony_ci// Returns: false
32311cb0ef41Sopenharmony_ci```
32321cb0ef41Sopenharmony_ci
32331cb0ef41Sopenharmony_ci### `util.isSymbol(object)`
32341cb0ef41Sopenharmony_ci
32351cb0ef41Sopenharmony_ci<!-- YAML
32361cb0ef41Sopenharmony_ciadded: v0.11.5
32371cb0ef41Sopenharmony_cideprecated: v4.0.0
32381cb0ef41Sopenharmony_ci-->
32391cb0ef41Sopenharmony_ci
32401cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `typeof value === 'symbol'` instead.
32411cb0ef41Sopenharmony_ci
32421cb0ef41Sopenharmony_ci* `object` {any}
32431cb0ef41Sopenharmony_ci* Returns: {boolean}
32441cb0ef41Sopenharmony_ci
32451cb0ef41Sopenharmony_ciReturns `true` if the given `object` is a `Symbol`. Otherwise, returns `false`.
32461cb0ef41Sopenharmony_ci
32471cb0ef41Sopenharmony_ci```js
32481cb0ef41Sopenharmony_ciconst util = require('node:util');
32491cb0ef41Sopenharmony_ci
32501cb0ef41Sopenharmony_ciutil.isSymbol(5);
32511cb0ef41Sopenharmony_ci// Returns: false
32521cb0ef41Sopenharmony_ciutil.isSymbol('foo');
32531cb0ef41Sopenharmony_ci// Returns: false
32541cb0ef41Sopenharmony_ciutil.isSymbol(Symbol('foo'));
32551cb0ef41Sopenharmony_ci// Returns: true
32561cb0ef41Sopenharmony_ci```
32571cb0ef41Sopenharmony_ci
32581cb0ef41Sopenharmony_ci### `util.isUndefined(object)`
32591cb0ef41Sopenharmony_ci
32601cb0ef41Sopenharmony_ci<!-- YAML
32611cb0ef41Sopenharmony_ciadded: v0.11.5
32621cb0ef41Sopenharmony_cideprecated: v4.0.0
32631cb0ef41Sopenharmony_ci-->
32641cb0ef41Sopenharmony_ci
32651cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use `value === undefined` instead.
32661cb0ef41Sopenharmony_ci
32671cb0ef41Sopenharmony_ci* `object` {any}
32681cb0ef41Sopenharmony_ci* Returns: {boolean}
32691cb0ef41Sopenharmony_ci
32701cb0ef41Sopenharmony_ciReturns `true` if the given `object` is `undefined`. Otherwise, returns `false`.
32711cb0ef41Sopenharmony_ci
32721cb0ef41Sopenharmony_ci```js
32731cb0ef41Sopenharmony_ciconst util = require('node:util');
32741cb0ef41Sopenharmony_ci
32751cb0ef41Sopenharmony_ciconst foo = undefined;
32761cb0ef41Sopenharmony_ciutil.isUndefined(5);
32771cb0ef41Sopenharmony_ci// Returns: false
32781cb0ef41Sopenharmony_ciutil.isUndefined(foo);
32791cb0ef41Sopenharmony_ci// Returns: true
32801cb0ef41Sopenharmony_ciutil.isUndefined(null);
32811cb0ef41Sopenharmony_ci// Returns: false
32821cb0ef41Sopenharmony_ci```
32831cb0ef41Sopenharmony_ci
32841cb0ef41Sopenharmony_ci### `util.log(string)`
32851cb0ef41Sopenharmony_ci
32861cb0ef41Sopenharmony_ci<!-- YAML
32871cb0ef41Sopenharmony_ciadded: v0.3.0
32881cb0ef41Sopenharmony_cideprecated: v6.0.0
32891cb0ef41Sopenharmony_ci-->
32901cb0ef41Sopenharmony_ci
32911cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use a third party module instead.
32921cb0ef41Sopenharmony_ci
32931cb0ef41Sopenharmony_ci* `string` {string}
32941cb0ef41Sopenharmony_ci
32951cb0ef41Sopenharmony_ciThe `util.log()` method prints the given `string` to `stdout` with an included
32961cb0ef41Sopenharmony_citimestamp.
32971cb0ef41Sopenharmony_ci
32981cb0ef41Sopenharmony_ci```js
32991cb0ef41Sopenharmony_ciconst util = require('node:util');
33001cb0ef41Sopenharmony_ci
33011cb0ef41Sopenharmony_ciutil.log('Timestamped message.');
33021cb0ef41Sopenharmony_ci```
33031cb0ef41Sopenharmony_ci
33041cb0ef41Sopenharmony_ci[Common System Errors]: errors.md#common-system-errors
33051cb0ef41Sopenharmony_ci[Custom inspection functions on objects]: #custom-inspection-functions-on-objects
33061cb0ef41Sopenharmony_ci[Custom promisified functions]: #custom-promisified-functions
33071cb0ef41Sopenharmony_ci[Customizing `util.inspect` colors]: #customizing-utilinspect-colors
33081cb0ef41Sopenharmony_ci[Internationalization]: intl.md
33091cb0ef41Sopenharmony_ci[Module Namespace Object]: https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects
33101cb0ef41Sopenharmony_ci[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
33111cb0ef41Sopenharmony_ci[`'uncaughtException'`]: process.md#event-uncaughtexception
33121cb0ef41Sopenharmony_ci[`'warning'`]: process.md#event-warning
33131cb0ef41Sopenharmony_ci[`Array.isArray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
33141cb0ef41Sopenharmony_ci[`ArrayBuffer.isView()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
33151cb0ef41Sopenharmony_ci[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
33161cb0ef41Sopenharmony_ci[`Buffer.isBuffer()`]: buffer.md#static-method-bufferisbufferobj
33171cb0ef41Sopenharmony_ci[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
33181cb0ef41Sopenharmony_ci[`Date`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
33191cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error
33201cb0ef41Sopenharmony_ci[`Float32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
33211cb0ef41Sopenharmony_ci[`Float64Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
33221cb0ef41Sopenharmony_ci[`Int16Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array
33231cb0ef41Sopenharmony_ci[`Int32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
33241cb0ef41Sopenharmony_ci[`Int8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
33251cb0ef41Sopenharmony_ci[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
33261cb0ef41Sopenharmony_ci[`MIMEparams`]: #class-utilmimeparams
33271cb0ef41Sopenharmony_ci[`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
33281cb0ef41Sopenharmony_ci[`Object.assign()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
33291cb0ef41Sopenharmony_ci[`Object.freeze()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
33301cb0ef41Sopenharmony_ci[`Promise`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
33311cb0ef41Sopenharmony_ci[`Proxy`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
33321cb0ef41Sopenharmony_ci[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
33331cb0ef41Sopenharmony_ci[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
33341cb0ef41Sopenharmony_ci[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
33351cb0ef41Sopenharmony_ci[`Uint16Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array
33361cb0ef41Sopenharmony_ci[`Uint32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
33371cb0ef41Sopenharmony_ci[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
33381cb0ef41Sopenharmony_ci[`Uint8ClampedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
33391cb0ef41Sopenharmony_ci[`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
33401cb0ef41Sopenharmony_ci[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
33411cb0ef41Sopenharmony_ci[`WebAssembly.Module`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
33421cb0ef41Sopenharmony_ci[`assert.deepStrictEqual()`]: assert.md#assertdeepstrictequalactual-expected-message
33431cb0ef41Sopenharmony_ci[`console.error()`]: console.md#consoleerrordata-args
33441cb0ef41Sopenharmony_ci[`mime.toString()`]: #mimetostring
33451cb0ef41Sopenharmony_ci[`mimeParams.entries()`]: #mimeparamsentries
33461cb0ef41Sopenharmony_ci[`napi_create_external()`]: n-api.md#napi_create_external
33471cb0ef41Sopenharmony_ci[`target` and `handler`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology
33481cb0ef41Sopenharmony_ci[`tty.hasColors()`]: tty.md#writestreamhascolorscount-env
33491cb0ef41Sopenharmony_ci[`util.format()`]: #utilformatformat-args
33501cb0ef41Sopenharmony_ci[`util.inspect()`]: #utilinspectobject-options
33511cb0ef41Sopenharmony_ci[`util.promisify()`]: #utilpromisifyoriginal
33521cb0ef41Sopenharmony_ci[`util.types.isAnyArrayBuffer()`]: #utiltypesisanyarraybuffervalue
33531cb0ef41Sopenharmony_ci[`util.types.isArrayBuffer()`]: #utiltypesisarraybuffervalue
33541cb0ef41Sopenharmony_ci[`util.types.isDate()`]: #utiltypesisdatevalue
33551cb0ef41Sopenharmony_ci[`util.types.isNativeError()`]: #utiltypesisnativeerrorvalue
33561cb0ef41Sopenharmony_ci[`util.types.isSharedArrayBuffer()`]: #utiltypesissharedarraybuffervalue
33571cb0ef41Sopenharmony_ci[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
33581cb0ef41Sopenharmony_ci[built-in `Error` type]: https://tc39.es/ecma262/#sec-error-objects
33591cb0ef41Sopenharmony_ci[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters
33601cb0ef41Sopenharmony_ci[constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
33611cb0ef41Sopenharmony_ci[default sort]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
33621cb0ef41Sopenharmony_ci[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
33631cb0ef41Sopenharmony_ci[list of deprecated APIS]: deprecations.md#list-of-deprecated-apis
33641cb0ef41Sopenharmony_ci[pkgjs/parseargs]: https://github.com/pkgjs/parseargs
33651cb0ef41Sopenharmony_ci[realm]: https://tc39.es/ecma262/#realm
33661cb0ef41Sopenharmony_ci[semantically incompatible]: https://github.com/nodejs/node/issues/4179
33671cb0ef41Sopenharmony_ci[util.inspect.custom]: #utilinspectcustom
3368