2318 lines
174 KiB
HTML
2318 lines
174 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width">
|
|
<meta name="nodejs.org:node-version" content="v12.22.12">
|
|
<title>Util | Node.js v12.22.12 Documentation</title>
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
|
|
<link rel="stylesheet" href="assets/style.css">
|
|
<link rel="stylesheet" href="assets/hljs.css">
|
|
<link rel="canonical" href="https://nodejs.org/api/util.html">
|
|
</head>
|
|
<body class="alt apidoc" id="api-section-util">
|
|
<div id="content" class="clearfix">
|
|
<div id="column2" class="interior">
|
|
<div id="intro" class="interior">
|
|
<a href="/" title="Go back to the home page">
|
|
Node.js
|
|
</a>
|
|
</div>
|
|
<ul>
|
|
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
|
|
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
|
|
</ul>
|
|
<hr class="line">
|
|
<ul>
|
|
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
|
|
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
|
|
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
|
|
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
|
|
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons with N-API</a></li>
|
|
<li><a href="embedding.html" class="nav-embedding">C++ Embedder API</a></li>
|
|
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
|
|
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
|
|
<li><a href="cli.html" class="nav-cli">Command line options</a></li>
|
|
<li><a href="console.html" class="nav-console">Console</a></li>
|
|
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
|
|
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
|
|
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
|
|
<li><a href="dns.html" class="nav-dns">DNS</a></li>
|
|
<li><a href="domain.html" class="nav-domain">Domain</a></li>
|
|
<li><a href="errors.html" class="nav-errors">Errors</a></li>
|
|
<li><a href="events.html" class="nav-events">Events</a></li>
|
|
<li><a href="fs.html" class="nav-fs">File system</a></li>
|
|
<li><a href="globals.html" class="nav-globals">Globals</a></li>
|
|
<li><a href="http.html" class="nav-http">HTTP</a></li>
|
|
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
|
|
<li><a href="https.html" class="nav-https">HTTPS</a></li>
|
|
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
|
|
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
|
|
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
|
|
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
|
|
<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
|
|
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
|
|
<li><a href="net.html" class="nav-net">Net</a></li>
|
|
<li><a href="os.html" class="nav-os">OS</a></li>
|
|
<li><a href="path.html" class="nav-path">Path</a></li>
|
|
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
|
|
<li><a href="policy.html" class="nav-policy">Policies</a></li>
|
|
<li><a href="process.html" class="nav-process">Process</a></li>
|
|
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
|
|
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
|
|
<li><a href="readline.html" class="nav-readline">Readline</a></li>
|
|
<li><a href="repl.html" class="nav-repl">REPL</a></li>
|
|
<li><a href="report.html" class="nav-report">Report</a></li>
|
|
<li><a href="stream.html" class="nav-stream">Stream</a></li>
|
|
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
|
|
<li><a href="timers.html" class="nav-timers">Timers</a></li>
|
|
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
|
|
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
|
|
<li><a href="tty.html" class="nav-tty">TTY</a></li>
|
|
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
|
|
<li><a href="url.html" class="nav-url">URL</a></li>
|
|
<li><a href="util.html" class="nav-util active">Utilities</a></li>
|
|
<li><a href="v8.html" class="nav-v8">V8</a></li>
|
|
<li><a href="vm.html" class="nav-vm">VM</a></li>
|
|
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
|
|
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
|
|
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
|
|
</ul>
|
|
<hr class="line">
|
|
<ul>
|
|
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div id="column1" data-id="util" class="interior">
|
|
<header>
|
|
<h1>Node.js v12.22.12 Documentation</h1>
|
|
<div id="gtoc">
|
|
<ul>
|
|
<li>
|
|
<a href="index.html">Index</a>
|
|
</li>
|
|
<li>
|
|
<a href="all.html">View on single page</a>
|
|
</li>
|
|
<li>
|
|
<a href="util.json">View as JSON</a>
|
|
</li>
|
|
|
|
<li class="version-picker">
|
|
<a href="#">View another version <span>▼</span></a>
|
|
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/util.html">17.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v16.x/api/util.html">16.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v15.x/api/util.html">15.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v14.x/api/util.html">14.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v13.x/api/util.html">13.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v12.x/api/util.html">12.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v11.x/api/util.html">11.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v10.x/api/util.html">10.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v9.x/api/util.html">9.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v8.x/api/util.html">8.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v7.x/api/util.html">7.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v6.x/api/util.html">6.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v5.x/api/util.html">5.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v4.x/api/util.html">4.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/util.html">0.12.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/util.html">0.10.x</a></li></ol>
|
|
</li>
|
|
|
|
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/util.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
|
|
</ul>
|
|
</div>
|
|
<hr>
|
|
</header>
|
|
|
|
<div id="toc">
|
|
<h2>Table of Contents</h2>
|
|
<ul>
|
|
<li><span class="stability_2"><a href="#util_util">Util</a></span>
|
|
<ul>
|
|
<li><a href="#util_util_callbackify_original"><code>util.callbackify(original)</code></a></li>
|
|
<li><a href="#util_util_debuglog_section_callback"><code>util.debuglog(section[, callback])</code></a></li>
|
|
<li><a href="#util_util_deprecate_fn_msg_code"><code>util.deprecate(fn, msg[, code])</code></a></li>
|
|
<li><a href="#util_util_format_format_args"><code>util.format(format[, ...args])</code></a></li>
|
|
<li><a href="#util_util_formatwithoptions_inspectoptions_format_args"><code>util.formatWithOptions(inspectOptions, format[, ...args])</code></a></li>
|
|
<li><a href="#util_util_getsystemerrorname_err"><code>util.getSystemErrorName(err)</code></a></li>
|
|
<li><a href="#util_util_inherits_constructor_superconstructor"><code>util.inherits(constructor, superConstructor)</code></a></li>
|
|
<li><a href="#util_util_inspect_object_options"><code>util.inspect(object[, options])</code></a></li>
|
|
<li><a href="#util_util_inspect_object_showhidden_depth_colors"><code>util.inspect(object[, showHidden[, depth[, colors]]])</code></a>
|
|
<ul>
|
|
<li><a href="#util_customizing_util_inspect_colors">Customizing <code>util.inspect</code> colors</a>
|
|
<ul>
|
|
<li><a href="#util_modifiers">Modifiers</a></li>
|
|
<li><a href="#util_foreground_colors">Foreground colors</a></li>
|
|
<li><a href="#util_background_colors">Background colors</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_custom_inspection_functions_on_objects">Custom inspection functions on objects</a></li>
|
|
<li><a href="#util_util_inspect_custom"><code>util.inspect.custom</code></a></li>
|
|
<li><a href="#util_util_inspect_defaultoptions"><code>util.inspect.defaultOptions</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_util_isdeepstrictequal_val1_val2"><code>util.isDeepStrictEqual(val1, val2)</code></a></li>
|
|
<li><a href="#util_util_promisify_original"><code>util.promisify(original)</code></a>
|
|
<ul>
|
|
<li><a href="#util_custom_promisified_functions">Custom promisified functions</a></li>
|
|
<li><a href="#util_util_promisify_custom"><code>util.promisify.custom</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_class_util_textdecoder">Class: <code>util.TextDecoder</code></a>
|
|
<ul>
|
|
<li><a href="#util_whatwg_supported_encodings">WHATWG supported encodings</a>
|
|
<ul>
|
|
<li><a href="#util_encodings_supported_without_icu">Encodings Supported Without ICU</a></li>
|
|
<li><a href="#util_encodings_supported_by_default_with_icu">Encodings Supported by Default (With ICU)</a></li>
|
|
<li><a href="#util_encodings_requiring_full_icu_data">Encodings requiring full ICU data</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_new_textdecoder_encoding_options"><code>new TextDecoder([encoding[, options]])</code></a></li>
|
|
<li><a href="#util_textdecoder_decode_input_options"><code>textDecoder.decode([input[, options]])</code></a></li>
|
|
<li><a href="#util_textdecoder_encoding"><code>textDecoder.encoding</code></a></li>
|
|
<li><a href="#util_textdecoder_fatal"><code>textDecoder.fatal</code></a></li>
|
|
<li><a href="#util_textdecoder_ignorebom"><code>textDecoder.ignoreBOM</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_class_util_textencoder">Class: <code>util.TextEncoder</code></a>
|
|
<ul>
|
|
<li><a href="#util_textencoder_encode_input"><code>textEncoder.encode([input])</code></a></li>
|
|
<li><a href="#util_textencoder_encodeinto_src_dest"><code>textEncoder.encodeInto(src, dest)</code></a></li>
|
|
<li><a href="#util_textencoder_encoding"><code>textEncoder.encoding</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_util_types"><code>util.types</code></a>
|
|
<ul>
|
|
<li><a href="#util_util_types_isanyarraybuffer_value"><code>util.types.isAnyArrayBuffer(value)</code></a></li>
|
|
<li><a href="#util_util_types_isarraybufferview_value"><code>util.types.isArrayBufferView(value)</code></a></li>
|
|
<li><a href="#util_util_types_isargumentsobject_value"><code>util.types.isArgumentsObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_isarraybuffer_value"><code>util.types.isArrayBuffer(value)</code></a></li>
|
|
<li><a href="#util_util_types_isasyncfunction_value"><code>util.types.isAsyncFunction(value)</code></a></li>
|
|
<li><a href="#util_util_types_isbigint64array_value"><code>util.types.isBigInt64Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isbiguint64array_value"><code>util.types.isBigUint64Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isbooleanobject_value"><code>util.types.isBooleanObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_isboxedprimitive_value"><code>util.types.isBoxedPrimitive(value)</code></a></li>
|
|
<li><a href="#util_util_types_isdataview_value"><code>util.types.isDataView(value)</code></a></li>
|
|
<li><a href="#util_util_types_isdate_value"><code>util.types.isDate(value)</code></a></li>
|
|
<li><a href="#util_util_types_isexternal_value"><code>util.types.isExternal(value)</code></a></li>
|
|
<li><a href="#util_util_types_isfloat32array_value"><code>util.types.isFloat32Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isfloat64array_value"><code>util.types.isFloat64Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isgeneratorfunction_value"><code>util.types.isGeneratorFunction(value)</code></a></li>
|
|
<li><a href="#util_util_types_isgeneratorobject_value"><code>util.types.isGeneratorObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_isint8array_value"><code>util.types.isInt8Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isint16array_value"><code>util.types.isInt16Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isint32array_value"><code>util.types.isInt32Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_ismap_value"><code>util.types.isMap(value)</code></a></li>
|
|
<li><a href="#util_util_types_ismapiterator_value"><code>util.types.isMapIterator(value)</code></a></li>
|
|
<li><a href="#util_util_types_ismodulenamespaceobject_value"><code>util.types.isModuleNamespaceObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_isnativeerror_value"><code>util.types.isNativeError(value)</code></a></li>
|
|
<li><a href="#util_util_types_isnumberobject_value"><code>util.types.isNumberObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_ispromise_value"><code>util.types.isPromise(value)</code></a></li>
|
|
<li><a href="#util_util_types_isproxy_value"><code>util.types.isProxy(value)</code></a></li>
|
|
<li><a href="#util_util_types_isregexp_value"><code>util.types.isRegExp(value)</code></a></li>
|
|
<li><a href="#util_util_types_isset_value"><code>util.types.isSet(value)</code></a></li>
|
|
<li><a href="#util_util_types_issetiterator_value"><code>util.types.isSetIterator(value)</code></a></li>
|
|
<li><a href="#util_util_types_issharedarraybuffer_value"><code>util.types.isSharedArrayBuffer(value)</code></a></li>
|
|
<li><a href="#util_util_types_isstringobject_value"><code>util.types.isStringObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_issymbolobject_value"><code>util.types.isSymbolObject(value)</code></a></li>
|
|
<li><a href="#util_util_types_istypedarray_value"><code>util.types.isTypedArray(value)</code></a></li>
|
|
<li><a href="#util_util_types_isuint8array_value"><code>util.types.isUint8Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isuint8clampedarray_value"><code>util.types.isUint8ClampedArray(value)</code></a></li>
|
|
<li><a href="#util_util_types_isuint16array_value"><code>util.types.isUint16Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isuint32array_value"><code>util.types.isUint32Array(value)</code></a></li>
|
|
<li><a href="#util_util_types_isweakmap_value"><code>util.types.isWeakMap(value)</code></a></li>
|
|
<li><a href="#util_util_types_isweakset_value"><code>util.types.isWeakSet(value)</code></a></li>
|
|
<li><a href="#util_util_types_iswebassemblycompiledmodule_value"><code>util.types.isWebAssemblyCompiledModule(value)</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#util_deprecated_apis">Deprecated APIs</a>
|
|
<ul>
|
|
<li><span class="stability_0"><a href="#util_util_extend_target_source"><code>util._extend(target, source)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isarray_object"><code>util.isArray(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isboolean_object"><code>util.isBoolean(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isbuffer_object"><code>util.isBuffer(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isdate_object"><code>util.isDate(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_iserror_object"><code>util.isError(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isfunction_object"><code>util.isFunction(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isnull_object"><code>util.isNull(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isnullorundefined_object"><code>util.isNullOrUndefined(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isnumber_object"><code>util.isNumber(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isobject_object"><code>util.isObject(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isprimitive_object"><code>util.isPrimitive(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isregexp_object"><code>util.isRegExp(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isstring_object"><code>util.isString(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_issymbol_object"><code>util.isSymbol(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_isundefined_object"><code>util.isUndefined(object)</code></a></span></li>
|
|
<li><span class="stability_0"><a href="#util_util_log_string"><code>util.log(string)</code></a></span></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div id="apicontent">
|
|
<h1>Util<span><a class="mark" href="#util_util" id="util_util">#</a></span></h1>
|
|
|
|
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
|
|
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v12.22.12/lib/util.js">lib/util.js</a></p>
|
|
<p>The <code>util</code> module supports the needs of Node.js internal APIs. Many of the
|
|
utilities are useful for application and module developers as well. To access
|
|
it:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);</code></pre>
|
|
<h2><code>util.callbackify(original)</code><span><a class="mark" href="#util_util_callbackify_original" id="util_util_callbackify_original">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v8.2.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An <code>async</code> function</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> a callback style function</li>
|
|
</ul>
|
|
<p>Takes an <code>async</code> function (or a function that returns a <code>Promise</code>) and returns a
|
|
function following the error-first callback style, i.e. taking
|
|
an <code>(err, value) => ...</code> callback as the last argument. In the callback, the
|
|
first argument will be the rejection reason (or <code>null</code> if the <code>Promise</code>
|
|
resolved), and the second argument will be the resolved value.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>;
|
|
}
|
|
<span class="hljs-keyword">const</span> callbackFunction = util.callbackify(fn);
|
|
|
|
callbackFunction(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> {
|
|
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
|
|
<span class="hljs-built_in">console</span>.log(ret);
|
|
});</code></pre>
|
|
<p>Will print:</p>
|
|
<pre><code class="language-text">hello world</code></pre>
|
|
<p>The callback is executed asynchronously, and will have a limited stack trace.
|
|
If the callback throws, the process will emit an <a href="process.html#process_event_uncaughtexception"><code>'uncaughtException'</code></a>
|
|
event, and if not handled will exit.</p>
|
|
<p>Since <code>null</code> has a special meaning as the first argument to a callback, if a
|
|
wrapped function rejects a <code>Promise</code> with a falsy value as a reason, the value
|
|
is wrapped in an <code>Error</code> with the original value stored in a field named
|
|
<code>reason</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-literal">null</span>);
|
|
}
|
|
<span class="hljs-keyword">const</span> callbackFunction = util.callbackify(fn);
|
|
|
|
callbackFunction(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> {
|
|
<span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Error and</span>
|
|
<span class="hljs-comment">// the original value is stored in `reason`.</span>
|
|
err && err.hasOwnProperty(<span class="hljs-string">'reason'</span>) && err.reason === <span class="hljs-literal">null</span>; <span class="hljs-comment">// true</span>
|
|
});</code></pre>
|
|
<h2><code>util.debuglog(section[, callback])</code><span><a class="mark" href="#util_util_debuglog_section_callback" id="util_util_debuglog_section_callback">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.3</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A string identifying the portion of the application for
|
|
which the <code>debuglog</code> function is being created.</li>
|
|
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback invoked the first time the logging function
|
|
is called with a function argument that is a more optimized logging function.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The logging function</li>
|
|
</ul>
|
|
<p>The <code>util.debuglog()</code> method is used to create a function that conditionally
|
|
writes debug messages to <code>stderr</code> based on the existence of the <code>NODE_DEBUG</code>
|
|
environment variable. If the <code>section</code> name appears within the value of that
|
|
environment variable, then the returned function operates similar to
|
|
<a href="console.html#console_console_error_data_args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> debuglog = util.debuglog(<span class="hljs-string">'foo'</span>);
|
|
|
|
debuglog(<span class="hljs-string">'hello from foo [%d]'</span>, <span class="hljs-number">123</span>);</code></pre>
|
|
<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then
|
|
it will output something like:</p>
|
|
<pre><code class="language-console">FOO 3245: hello from foo [123]</code></pre>
|
|
<p>where <code>3245</code> is the process id. If it is not run with that
|
|
environment variable set, then it will not print anything.</p>
|
|
<p>The <code>section</code> supports wildcard also:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> debuglog = util.debuglog(<span class="hljs-string">'foo-bar'</span>);
|
|
|
|
debuglog(<span class="hljs-string">'hi there, it\'s foo-bar [%d]'</span>, <span class="hljs-number">2333</span>);</code></pre>
|
|
<p>if it is run with <code>NODE_DEBUG=foo*</code> in the environment, then it will output
|
|
something like:</p>
|
|
<pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code></pre>
|
|
<p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</code>
|
|
environment variable: <code>NODE_DEBUG=fs,net,tls</code>.</p>
|
|
<p>The optional <code>callback</code> argument can be used to replace the logging function
|
|
with a different function that doesn't have any initialization or
|
|
unnecessary wrapping.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">let</span> debuglog = util.debuglog(<span class="hljs-string">'internals'</span>, <span class="hljs-function">(<span class="hljs-params">debug</span>) =></span> {
|
|
<span class="hljs-comment">// Replace with a logging function that optimizes out</span>
|
|
<span class="hljs-comment">// testing if the section is enabled</span>
|
|
debuglog = debug;
|
|
});</code></pre>
|
|
<h2><code>util.deprecate(fn, msg[, code])</code><span><a class="mark" href="#util_util_deprecate_fn_msg_code" id="util_util_deprecate_fn_msg_code">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v10.0.0</td>
|
|
<td><p>Deprecation warnings are only emitted once for each code.</p></td></tr>
|
|
<tr><td>v0.8.0</td>
|
|
<td><p><span>Added in: v0.8.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function that is being deprecated.</li>
|
|
<li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A warning message to display when the deprecated function is
|
|
invoked.</li>
|
|
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A deprecation code. See the <a href="deprecations.html#deprecations_list_of_deprecated_apis">list of deprecated APIs</a> for a
|
|
list of codes.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The deprecated function wrapped to emit a warning.</li>
|
|
</ul>
|
|
<p>The <code>util.deprecate()</code> method wraps <code>fn</code> (which may be a function or class) in
|
|
such a way that it is marked as deprecated.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-built_in">exports</span>.obsoleteFunction = util.deprecate(<span class="hljs-function">() =></span> {
|
|
<span class="hljs-comment">// Do something here.</span>
|
|
}, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</span>);</code></pre>
|
|
<p>When called, <code>util.deprecate()</code> will return a function that will emit a
|
|
<code>DeprecationWarning</code> using the <a href="process.html#process_event_warning"><code>'warning'</code></a> event. The warning will
|
|
be emitted and printed to <code>stderr</code> the first time the returned function is
|
|
called. After the warning is emitted, the wrapped function is called without
|
|
emitting a warning.</p>
|
|
<p>If the same optional <code>code</code> is supplied in multiple calls to <code>util.deprecate()</code>,
|
|
the warning will be emitted only once for that <code>code</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> fn1 = util.deprecate(someFunction, someMessage, <span class="hljs-string">'DEP0001'</span>);
|
|
<span class="hljs-keyword">const</span> fn2 = util.deprecate(someOtherFunction, someOtherMessage, <span class="hljs-string">'DEP0001'</span>);
|
|
fn1(); <span class="hljs-comment">// Emits a deprecation warning with code DEP0001</span>
|
|
fn2(); <span class="hljs-comment">// Does not emit a deprecation warning because it has the same code</span></code></pre>
|
|
<p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command line flags are
|
|
used, or if the <code>process.noDeprecation</code> property is set to <code>true</code> <em>prior</em> to
|
|
the first deprecation warning, the <code>util.deprecate()</code> method does nothing.</p>
|
|
<p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command line flags are set,
|
|
or the <code>process.traceDeprecation</code> property is set to <code>true</code>, a warning and a
|
|
stack trace are printed to <code>stderr</code> the first time the deprecated function is
|
|
called.</p>
|
|
<p>If the <code>--throw-deprecation</code> command line flag is set, or the
|
|
<code>process.throwDeprecation</code> property is set to <code>true</code>, then an exception will be
|
|
thrown when the deprecated function is called.</p>
|
|
<p>The <code>--throw-deprecation</code> command line flag and <code>process.throwDeprecation</code>
|
|
property take precedence over <code>--trace-deprecation</code> and
|
|
<code>process.traceDeprecation</code>.</p>
|
|
<h2><code>util.format(format[, ...args])</code><span><a class="mark" href="#util_util_format_format_args" id="util_util_format_format_args">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.11.0</td>
|
|
<td><p>The <code>%c</code> specifier is ignored now.</p></td></tr>
|
|
<tr><td>v12.0.0</td>
|
|
<td><p>The <code>format</code> argument is now only taken as such if it actually contains format specifiers.</p></td></tr>
|
|
<tr><td>v12.0.0</td>
|
|
<td><p>If the <code>format</code> argument is not a format string, the output string's formatting is no longer dependent on the type of the first argument. This change removes previously present quotes from strings that were being output when the first argument was not a string.</p></td></tr>
|
|
<tr><td>v11.4.0</td>
|
|
<td><p>The <code>%d</code>, <code>%f</code> and <code>%i</code> specifiers now support Symbols properly.</p></td></tr>
|
|
<tr><td>v11.4.0</td>
|
|
<td><p>The <code>%o</code> specifier's <code>depth</code> has default depth of 4 again.</p></td></tr>
|
|
<tr><td>v11.0.0</td>
|
|
<td><p>The <code>%o</code> specifier's <code>depth</code> option will now fall back to the default depth.</p></td></tr>
|
|
<tr><td>v10.12.0</td>
|
|
<td><p>The <code>%d</code> and <code>%i</code> specifiers now support BigInt.</p></td></tr>
|
|
<tr><td>v8.4.0</td>
|
|
<td><p>The <code>%o</code> and <code>%O</code> specifiers are supported now.</p></td></tr>
|
|
<tr><td>v0.5.3</td>
|
|
<td><p><span>Added in: v0.5.3</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>printf</code>-like format string.</li>
|
|
</ul>
|
|
<p>The <code>util.format()</code> method returns a formatted string using the first argument
|
|
as a <code>printf</code>-like format string which can contain zero or more format
|
|
specifiers. Each specifier is replaced with the converted value from the
|
|
corresponding argument. Supported specifiers are:</p>
|
|
<ul>
|
|
<li><code>%s</code>: <code>String</code> will be used to convert all values except <code>BigInt</code>, <code>Object</code>
|
|
and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Objects that
|
|
have no user defined <code>toString</code> function are inspected using <code>util.inspect()</code>
|
|
with options <code>{ depth: 0, colors: false, compact: 3 }</code>.</li>
|
|
<li><code>%d</code>: <code>Number</code> will be used to convert all values except <code>BigInt</code> and
|
|
<code>Symbol</code>.</li>
|
|
<li><code>%i</code>: <code>parseInt(value, 10)</code> is used for all values except <code>BigInt</code> and
|
|
<code>Symbol</code>.</li>
|
|
<li><code>%f</code>: <code>parseFloat(value)</code> is used for all values expect <code>Symbol</code>.</li>
|
|
<li><code>%j</code>: JSON. Replaced with the string <code>'[Circular]'</code> if the argument contains
|
|
circular references.</li>
|
|
<li><code>%o</code>: <code>Object</code>. A string representation of an object with generic JavaScript
|
|
object formatting. Similar to <code>util.inspect()</code> with options
|
|
<code>{ showHidden: true, showProxy: true }</code>. This will show the full object
|
|
including non-enumerable properties and proxies.</li>
|
|
<li><code>%O</code>: <code>Object</code>. A string representation of an object with generic JavaScript
|
|
object formatting. Similar to <code>util.inspect()</code> without options. This will show
|
|
the full object not including non-enumerable properties and proxies.</li>
|
|
<li><code>%c</code>: <code>CSS</code>. This specifier is ignored and will skip any CSS passed in.</li>
|
|
<li><code>%%</code>: single percent sign (<code>'%'</code>). This does not consume an argument.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The formatted string</li>
|
|
</ul>
|
|
<p>If a specifier does not have a corresponding argument, it is not replaced:</p>
|
|
<pre><code class="language-js">util.format(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>);
|
|
<span class="hljs-comment">// Returns: 'foo:%s'</span></code></pre>
|
|
<p>Values that are not part of the format string are formatted using
|
|
<code>util.inspect()</code> if their type is not <code>string</code>.</p>
|
|
<p>If there are more arguments passed to the <code>util.format()</code> method than the
|
|
number of specifiers, the extra arguments are concatenated to the returned
|
|
string, separated by spaces:</p>
|
|
<pre><code class="language-js">util.format(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>, <span class="hljs-string">'baz'</span>);
|
|
<span class="hljs-comment">// Returns: 'foo:bar baz'</span></code></pre>
|
|
<p>If the first argument does not contain a valid format specifier, <code>util.format()</code>
|
|
returns a string that is the concatenation of all arguments separated by spaces:</p>
|
|
<pre><code class="language-js">util.format(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
|
|
<span class="hljs-comment">// Returns: '1 2 3'</span></code></pre>
|
|
<p>If only one argument is passed to <code>util.format()</code>, it is returned as it is
|
|
without any formatting:</p>
|
|
<pre><code class="language-js">util.format(<span class="hljs-string">'%% %s'</span>);
|
|
<span class="hljs-comment">// Returns: '%% %s'</span></code></pre>
|
|
<p><code>util.format()</code> is a synchronous method that is intended as a debugging tool.
|
|
Some input values can have a significant performance overhead that can block the
|
|
event loop. Use this function with care and never in a hot code path.</p>
|
|
<h2><code>util.formatWithOptions(inspectOptions, format[, ...args])</code><span><a class="mark" href="#util_util_formatwithoptions_inspectoptions_format_args" id="util_util_formatwithoptions_inspectoptions_format_args">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
|
|
<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>This function is identical to <a href="#util_util_format_format_args"><code>util.format()</code></a>, except in that it takes
|
|
an <code>inspectOptions</code> argument which specifies options that are passed along to
|
|
<a href="#util_util_inspect_object_options"><code>util.inspect()</code></a>.</p>
|
|
<pre><code class="language-js">util.formatWithOptions({ <span class="hljs-attr">colors</span>: <span class="hljs-literal">true</span> }, <span class="hljs-string">'See object %O'</span>, { <span class="hljs-attr">foo</span>: <span class="hljs-number">42</span> });
|
|
<span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</span>
|
|
<span class="hljs-comment">// when printed to a terminal.</span></code></pre>
|
|
<h2><code>util.getSystemErrorName(err)</code><span><a class="mark" href="#util_util_getsystemerrorname_err" id="util_util_getsystemerrorname_err">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v9.7.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>Returns the string name for a numeric error code that comes from a Node.js API.
|
|
The mapping between error codes and error names is platform-dependent.
|
|
See <a href="errors.html#errors_common_system_errors">Common System Errors</a> for the names of common errors.</p>
|
|
<pre><code class="language-js">fs.access(<span class="hljs-string">'file/that/does/not/exist'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
|
|
<span class="hljs-keyword">const</span> name = util.getSystemErrorName(err.errno);
|
|
<span class="hljs-built_in">console</span>.error(name); <span class="hljs-comment">// ENOENT</span>
|
|
});</code></pre>
|
|
<h2><code>util.inherits(constructor, superConstructor)</code><span><a class="mark" href="#util_util_inherits_constructor_superconstructor" id="util_util_inherits_constructor_superconstructor">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v5.0.0</td>
|
|
<td><p>The <code>constructor</code> parameter can refer to an ES6 class now.</p></td></tr>
|
|
<tr><td>v0.3.0</td>
|
|
<td><p><span>Added in: v0.3.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
|
|
<li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
|
|
</ul>
|
|
<p>Usage of <code>util.inherits()</code> is discouraged. Please use the ES6 <code>class</code> and
|
|
<code>extends</code> keywords to get language level inheritance support. Also note
|
|
that the two styles are <a href="https://github.com/nodejs/node/issues/4179">semantically incompatible</a>.</p>
|
|
<p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor">constructor</a> into another. The
|
|
prototype of <code>constructor</code> will be set to a new object created from
|
|
<code>superConstructor</code>.</p>
|
|
<p>This mainly adds some input validation on top of
|
|
<code>Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)</code>.
|
|
As an additional convenience, <code>superConstructor</code> will be accessible
|
|
through the <code>constructor.super_</code> property.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyStream</span>(<span class="hljs-params"></span>) </span>{
|
|
EventEmitter.call(<span class="hljs-built_in">this</span>);
|
|
}
|
|
|
|
util.inherits(MyStream, EventEmitter);
|
|
|
|
MyStream.prototype.write = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{
|
|
<span class="hljs-built_in">this</span>.emit(<span class="hljs-string">'data'</span>, data);
|
|
};
|
|
|
|
<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> MyStream();
|
|
|
|
<span class="hljs-built_in">console</span>.log(stream <span class="hljs-keyword">instanceof</span> EventEmitter); <span class="hljs-comment">// true</span>
|
|
<span class="hljs-built_in">console</span>.log(MyStream.super_ === EventEmitter); <span class="hljs-comment">// true</span>
|
|
|
|
stream.on(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>);
|
|
});
|
|
stream.write(<span class="hljs-string">'It works!'</span>); <span class="hljs-comment">// Received data: "It works!"</span></code></pre>
|
|
<p>ES6 example using <code>class</code> and <code>extends</code>:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyStream</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">EventEmitter</span> </span>{
|
|
write(data) {
|
|
<span class="hljs-built_in">this</span>.emit(<span class="hljs-string">'data'</span>, data);
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> MyStream();
|
|
|
|
stream.on(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>);
|
|
});
|
|
stream.write(<span class="hljs-string">'With ES6'</span>);</code></pre>
|
|
<h2><code>util.inspect(object[, options])</code><span><a class="mark" href="#util_util_inspect_object_options" id="util_util_inspect_object_options">#</a></span></h2>
|
|
<h2><code>util.inspect(object[, showHidden[, depth[, colors]]])</code><span><a class="mark" href="#util_util_inspect_object_showhidden_depth_colors" id="util_util_inspect_object_showhidden_depth_colors">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v14.6.0, v12.19.0</td>
|
|
<td><p>If <code>object</code> is from a different <code>vm.Context</code> now, a custom inspection function on it will not receive context-specific arguments anymore.</p></td></tr>
|
|
<tr><td>v12.17.0</td>
|
|
<td><p>The <code>maxStringLength</code> option is supported now.</p></td></tr>
|
|
<tr><td>v12.16.0</td>
|
|
<td><p>User defined prototype properties are inspected in case <code>showHidden</code> is <code>true</code>.</p></td></tr>
|
|
<tr><td>v12.0.0</td>
|
|
<td><p>The <code>compact</code> options default is changed to <code>3</code> and the <code>breakLength</code> options default is changed to <code>80</code>.</p></td></tr>
|
|
<tr><td>v12.0.0</td>
|
|
<td><p>Internal properties no longer appear in the context argument of a custom inspection function.</p></td></tr>
|
|
<tr><td>v11.11.0</td>
|
|
<td><p>The <code>compact</code> option accepts numbers for a new output mode.</p></td></tr>
|
|
<tr><td>v11.7.0</td>
|
|
<td><p>ArrayBuffers now also show their binary contents.</p></td></tr>
|
|
<tr><td>v11.5.0</td>
|
|
<td><p>The <code>getters</code> option is supported now.</p></td></tr>
|
|
<tr><td>v11.4.0</td>
|
|
<td><p>The <code>depth</code> default changed back to <code>2</code>.</p></td></tr>
|
|
<tr><td>v11.0.0</td>
|
|
<td><p>The <code>depth</code> default changed to <code>20</code>.</p></td></tr>
|
|
<tr><td>v11.0.0</td>
|
|
<td><p>The inspection output is now limited to about 128 MB. Data above that size will not be fully inspected.</p></td></tr>
|
|
<tr><td>v10.12.0</td>
|
|
<td><p>The <code>sorted</code> option is supported now.</p></td></tr>
|
|
<tr><td>v10.6.0</td>
|
|
<td><p>Inspecting linked lists and similar objects is now possible up to the maximum call stack size.</p></td></tr>
|
|
<tr><td>v10.0.0</td>
|
|
<td><p>The <code>WeakMap</code> and <code>WeakSet</code> entries can now be inspected as well.</p></td></tr>
|
|
<tr><td>v9.9.0</td>
|
|
<td><p>The <code>compact</code> option is supported now.</p></td></tr>
|
|
<tr><td>v6.6.0</td>
|
|
<td><p>Custom inspection functions can now return <code>this</code>.</p></td></tr>
|
|
<tr><td>v6.3.0</td>
|
|
<td><p>The <code>breakLength</code> option is supported now.</p></td></tr>
|
|
<tr><td>v6.1.0</td>
|
|
<td><p>The <code>maxArrayLength</code> option is supported now; in particular, long arrays are truncated by default.</p></td></tr>
|
|
<tr><td>v6.1.0</td>
|
|
<td><p>The <code>showProxy</code> option is supported now.</p></td></tr>
|
|
<tr><td>v0.3.0</td>
|
|
<td><p><span>Added in: v0.3.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any JavaScript primitive or <code>Object</code>.</li>
|
|
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
|
|
<ul>
|
|
<li><code>showHidden</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>object</code>'s non-enumerable symbols and
|
|
properties are included in the formatted result. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries are also included as well as user defined prototype
|
|
properties (excluding method properties). <strong>Default:</strong> <code>false</code>.</li>
|
|
<li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the number of times to recurse while formatting
|
|
<code>object</code>. This is useful for inspecting large objects. To recurse up to
|
|
the maximum call stack size pass <code>Infinity</code> or <code>null</code>.
|
|
<strong>Default:</strong> <code>2</code>.</li>
|
|
<li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the output is styled with ANSI color
|
|
codes. Colors are customizable. See <a href="#util_customizing_util_inspect_colors">Customizing <code>util.inspect</code> colors</a>.
|
|
<strong>Default:</strong> <code>false</code>.</li>
|
|
<li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>false</code>,
|
|
<code>[util.inspect.custom](depth, opts)</code> functions are not invoked.
|
|
<strong>Default:</strong> <code>true</code>.</li>
|
|
<li><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>Proxy</code> inspection includes
|
|
the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology"><code>target</code> and <code>handler</code></a> objects. <strong>Default:</strong> <code>false</code>.</li>
|
|
<li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Specifies the maximum number of <code>Array</code>,
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> elements to include when
|
|
formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. Set to <code>0</code> or
|
|
negative to show no elements. <strong>Default:</strong> <code>100</code>.</li>
|
|
<li><code>maxStringLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Specifies the maximum number of characters to
|
|
include when formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements.
|
|
Set to <code>0</code> or negative to show no characters. <strong>Default:</strong> <code>Infinity</code>.</li>
|
|
<li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The length at which input values are split across
|
|
multiple lines. Set to <code>Infinity</code> to format the input as a single line
|
|
(in combination with <code>compact</code> set to <code>true</code> or any number >= <code>1</code>).
|
|
<strong>Default:</strong> <code>80</code>.</li>
|
|
<li><code>compact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Setting this to <code>false</code> causes each object key
|
|
to be displayed on a new line. It will also add new lines to text that is
|
|
longer than <code>breakLength</code>. If set to a number, the most <code>n</code> inner elements
|
|
are united on a single line as long as all properties fit into
|
|
<code>breakLength</code>. Short array elements are also grouped together. No
|
|
text will be reduced below 16 characters, no matter the <code>breakLength</code> size.
|
|
For more information, see the example below. <strong>Default:</strong> <code>3</code>.</li>
|
|
<li><code>sorted</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> If set to <code>true</code> or a function, all properties
|
|
of an object, and <code>Set</code> and <code>Map</code> entries are sorted in the resulting
|
|
string. If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">default sort</a> is used. If set to a function,
|
|
it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters">compare function</a>.</li>
|
|
<li><code>getters</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If set to <code>true</code>, getters are inspected. If set
|
|
to <code>'get'</code>, only getters without a corresponding setter are inspected. If
|
|
set to <code>'set'</code>, only getters with a corresponding setter are inspected.
|
|
This might cause side effects depending on the getter function.
|
|
<strong>Default:</strong> <code>false</code>.</li>
|
|
</ul>
|
|
</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The representation of <code>object</code>.</li>
|
|
</ul>
|
|
<p>The <code>util.inspect()</code> method returns a string representation of <code>object</code> that is
|
|
intended for debugging. The output of <code>util.inspect</code> may change at any time
|
|
and should not be depended upon programmatically. Additional <code>options</code> may be
|
|
passed that alter the result.
|
|
<code>util.inspect()</code> will use the constructor's name and/or <code>@@toStringTag</code> to make
|
|
an identifiable tag for an inspected value.</p>
|
|
<pre><code class="language-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span> </span>{
|
|
get [<span class="hljs-built_in">Symbol</span>.toStringTag]() {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>;
|
|
}
|
|
}
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bar</span> </span>{}
|
|
|
|
<span class="hljs-keyword">const</span> baz = <span class="hljs-built_in">Object</span>.create(<span class="hljs-literal">null</span>, { [<span class="hljs-built_in">Symbol</span>.toStringTag]: { <span class="hljs-attr">value</span>: <span class="hljs-string">'foo'</span> } });
|
|
|
|
util.inspect(<span class="hljs-keyword">new</span> Foo()); <span class="hljs-comment">// 'Foo [bar] {}'</span>
|
|
util.inspect(<span class="hljs-keyword">new</span> Bar()); <span class="hljs-comment">// 'Bar {}'</span>
|
|
util.inspect(baz); <span class="hljs-comment">// '[foo] {}'</span></code></pre>
|
|
<p>Circular references are marked as <code>'[Circular]'</code>:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> obj = {};
|
|
obj.a = [obj];
|
|
obj.b = {};
|
|
obj.b.inner = obj.b;
|
|
obj.b.obj = obj;
|
|
|
|
<span class="hljs-built_in">console</span>.log(inspect(obj));
|
|
<span class="hljs-comment">// {</span>
|
|
<span class="hljs-comment">// a: [ [Circular] ],</span>
|
|
<span class="hljs-comment">// b: { inner: [Circular], obj: [Circular] }</span>
|
|
<span class="hljs-comment">// }</span></code></pre>
|
|
<p>The following example inspects all properties of the <code>util</code> object:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-built_in">console</span>.log(util.inspect(util, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-literal">null</span> }));</code></pre>
|
|
<p>The following example highlights the effect of the <code>compact</code> option:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> o = {
|
|
<span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [[
|
|
<span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '</span> +
|
|
<span class="hljs-string">'eiusmod tempor incididunt ut labore et dolore magna aliqua.'</span>,
|
|
<span class="hljs-string">'test'</span>,
|
|
<span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>],
|
|
<span class="hljs-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>([[<span class="hljs-string">'za'</span>, <span class="hljs-number">1</span>], [<span class="hljs-string">'zb'</span>, <span class="hljs-string">'test'</span>]])
|
|
};
|
|
<span class="hljs-built_in">console</span>.log(util.inspect(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> }));
|
|
|
|
<span class="hljs-comment">// { a:</span>
|
|
<span class="hljs-comment">// [ 1,</span>
|
|
<span class="hljs-comment">// 2,</span>
|
|
<span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line</span>
|
|
<span class="hljs-comment">// 'test',</span>
|
|
<span class="hljs-comment">// 'foo' ] ],</span>
|
|
<span class="hljs-comment">// 4 ],</span>
|
|
<span class="hljs-comment">// b: Map { 'za' => 1, 'zb' => 'test' } }</span>
|
|
|
|
<span class="hljs-comment">// Setting `compact` to false changes the output to be more reader friendly.</span>
|
|
<span class="hljs-built_in">console</span>.log(util.inspect(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> }));
|
|
|
|
<span class="hljs-comment">// {</span>
|
|
<span class="hljs-comment">// a: [</span>
|
|
<span class="hljs-comment">// 1,</span>
|
|
<span class="hljs-comment">// 2,</span>
|
|
<span class="hljs-comment">// [</span>
|
|
<span class="hljs-comment">// [</span>
|
|
<span class="hljs-comment">// 'Lorem ipsum dolor sit amet, consectetur ' +</span>
|
|
<span class="hljs-comment">// 'adipiscing elit, sed do eiusmod tempor ' +</span>
|
|
<span class="hljs-comment">// 'incididunt ut labore et dolore magna ' +</span>
|
|
<span class="hljs-comment">// 'aliqua.,</span>
|
|
<span class="hljs-comment">// 'test',</span>
|
|
<span class="hljs-comment">// 'foo'</span>
|
|
<span class="hljs-comment">// ]</span>
|
|
<span class="hljs-comment">// ],</span>
|
|
<span class="hljs-comment">// 4</span>
|
|
<span class="hljs-comment">// ],</span>
|
|
<span class="hljs-comment">// b: Map {</span>
|
|
<span class="hljs-comment">// 'za' => 1,</span>
|
|
<span class="hljs-comment">// 'zb' => 'test'</span>
|
|
<span class="hljs-comment">// }</span>
|
|
<span class="hljs-comment">// }</span>
|
|
|
|
<span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a</span>
|
|
<span class="hljs-comment">// single line.</span>
|
|
<span class="hljs-comment">// Reducing the `breakLength` will split the "Lorem ipsum" text in smaller</span>
|
|
<span class="hljs-comment">// chunks.</span></code></pre>
|
|
<p>The <code>showHidden</code> option allows <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries to be
|
|
inspected. If there are more entries than <code>maxArrayLength</code>, there is no
|
|
guarantee which entries are displayed. That means retrieving the same
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries twice may result in different output. Furthermore, entries
|
|
with no remaining strong references may be garbage collected at any time.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> };
|
|
<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> };
|
|
<span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>([obj, obj2]);
|
|
|
|
<span class="hljs-built_in">console</span>.log(inspect(weakSet, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span> }));
|
|
<span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code></pre>
|
|
<p>The <code>sorted</code> option ensures that an object's property insertion order does not
|
|
impact the result of <code>util.inspect()</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> o1 = {
|
|
<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>],
|
|
<span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
|
|
<span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>])
|
|
};
|
|
<span class="hljs-built_in">console</span>.log(inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }));
|
|
<span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }</span>
|
|
<span class="hljs-built_in">console</span>.log(inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> b.localeCompare(a) }));
|
|
<span class="hljs-comment">// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }</span>
|
|
|
|
<span class="hljs-keyword">const</span> o2 = {
|
|
<span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]),
|
|
<span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
|
|
<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>]
|
|
};
|
|
assert.strict.equal(
|
|
inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }),
|
|
inspect(o2, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> })
|
|
);</code></pre>
|
|
<p><code>util.inspect()</code> is a synchronous method intended for debugging. Its maximum
|
|
output length is approximately 128 MB. Inputs that result in longer output will
|
|
be truncated.</p>
|
|
<h3>Customizing <code>util.inspect</code> colors<span><a class="mark" href="#util_customizing_util_inspect_colors" id="util_customizing_util_inspect_colors">#</a></span></h3>
|
|
|
|
<p>Color output (if enabled) of <code>util.inspect</code> is customizable globally
|
|
via the <code>util.inspect.styles</code> and <code>util.inspect.colors</code> properties.</p>
|
|
<p><code>util.inspect.styles</code> is a map associating a style name to a color from
|
|
<code>util.inspect.colors</code>.</p>
|
|
<p>The default styles and associated colors are:</p>
|
|
<ul>
|
|
<li><code>bigint</code>: <code>yellow</code></li>
|
|
<li><code>boolean</code>: <code>yellow</code></li>
|
|
<li><code>date</code>: <code>magenta</code></li>
|
|
<li><code>module</code>: <code>underline</code></li>
|
|
<li><code>name</code>: (no styling)</li>
|
|
<li><code>null</code>: <code>bold</code></li>
|
|
<li><code>number</code>: <code>yellow</code></li>
|
|
<li><code>regexp</code>: <code>red</code></li>
|
|
<li><code>special</code>: <code>cyan</code> (e.g., <code>Proxies</code>)</li>
|
|
<li><code>string</code>: <code>green</code></li>
|
|
<li><code>symbol</code>: <code>green</code></li>
|
|
<li><code>undefined</code>: <code>grey</code></li>
|
|
</ul>
|
|
<p>Color styling uses ANSI control codes that may not be supported on all
|
|
terminals. To verify color support use <a href="tty.html#tty_writestream_hascolors_count_env"><code>tty.hasColors()</code></a>.</p>
|
|
<p>Predefined control codes are listed below (grouped as "Modifiers", "Foreground
|
|
colors", and "Background colors").</p>
|
|
<h4>Modifiers<span><a class="mark" href="#util_modifiers" id="util_modifiers">#</a></span></h4>
|
|
<p>Modifier support varies throughout different terminals. They will mostly be
|
|
ignored, if not supported.</p>
|
|
<ul>
|
|
<li><code>reset</code> - Resets all (color) modifiers to their defaults</li>
|
|
<li><strong>bold</strong> - Make text bold</li>
|
|
<li><em>italic</em> - Make text italic</li>
|
|
<li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li>
|
|
<li><del>strikethrough</del> - Puts a horizontal line through the center of the text
|
|
(Alias: <code>strikeThrough</code>, <code>crossedout</code>, <code>crossedOut</code>)</li>
|
|
<li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li>
|
|
<li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
|
|
<code>faint</code>)</li>
|
|
<li><span style="border-top: 1px">overlined</span> - Make text overlined</li>
|
|
<li>blink - Hides and shows the text in an interval</li>
|
|
<li><span style="filter: invert(100%)">inverse</span> - Swap foreground and
|
|
background colors (Alias: <code>swapcolors</code>, <code>swapColors</code>)</li>
|
|
<li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text
|
|
double underlined (Alias: <code>doubleUnderline</code>)</li>
|
|
<li><span style="border: 1px">framed</span> - Draw a frame around the text</li>
|
|
</ul>
|
|
<h4>Foreground colors<span><a class="mark" href="#util_foreground_colors" id="util_foreground_colors">#</a></span></h4>
|
|
<ul>
|
|
<li><code>black</code></li>
|
|
<li><code>red</code></li>
|
|
<li><code>green</code></li>
|
|
<li><code>yellow</code></li>
|
|
<li><code>blue</code></li>
|
|
<li><code>magenta</code></li>
|
|
<li><code>cyan</code></li>
|
|
<li><code>white</code></li>
|
|
<li><code>gray</code> (alias: <code>grey</code>, <code>blackBright</code>)</li>
|
|
<li><code>redBright</code></li>
|
|
<li><code>greenBright</code></li>
|
|
<li><code>yellowBright</code></li>
|
|
<li><code>blueBright</code></li>
|
|
<li><code>magentaBright</code></li>
|
|
<li><code>cyanBright</code></li>
|
|
<li><code>whiteBright</code></li>
|
|
</ul>
|
|
<h4>Background colors<span><a class="mark" href="#util_background_colors" id="util_background_colors">#</a></span></h4>
|
|
<ul>
|
|
<li><code>bgBlack</code></li>
|
|
<li><code>bgRed</code></li>
|
|
<li><code>bgGreen</code></li>
|
|
<li><code>bgYellow</code></li>
|
|
<li><code>bgBlue</code></li>
|
|
<li><code>bgMagenta</code></li>
|
|
<li><code>bgCyan</code></li>
|
|
<li><code>bgWhite</code></li>
|
|
<li><code>bgGray</code> (alias: <code>bgGrey</code>, <code>bgBlackBright</code>)</li>
|
|
<li><code>bgRedBright</code></li>
|
|
<li><code>bgGreenBright</code></li>
|
|
<li><code>bgYellowBright</code></li>
|
|
<li><code>bgBlueBright</code></li>
|
|
<li><code>bgMagentaBright</code></li>
|
|
<li><code>bgCyanBright</code></li>
|
|
<li><code>bgWhiteBright</code></li>
|
|
</ul>
|
|
<h3>Custom inspection functions on objects<span><a class="mark" href="#util_custom_inspection_functions_on_objects" id="util_custom_inspection_functions_on_objects">#</a></span></h3>
|
|
|
|
<p>Objects may also define their own
|
|
<a href="#util_util_inspect_custom"><code>[util.inspect.custom](depth, opts)</code></a> function,
|
|
which <code>util.inspect()</code> will invoke and use the result of when inspecting
|
|
the object:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Box</span> </span>{
|
|
<span class="hljs-keyword">constructor</span>(value) {
|
|
<span class="hljs-built_in">this</span>.value = value;
|
|
}
|
|
|
|
[util.inspect.custom](depth, options) {
|
|
<span class="hljs-keyword">if</span> (depth < <span class="hljs-number">0</span>) {
|
|
<span class="hljs-keyword">return</span> options.stylize(<span class="hljs-string">'[Box]'</span>, <span class="hljs-string">'special'</span>);
|
|
}
|
|
|
|
<span class="hljs-keyword">const</span> newOptions = <span class="hljs-built_in">Object</span>.assign({}, options, {
|
|
<span class="hljs-attr">depth</span>: options.depth === <span class="hljs-literal">null</span> ? <span class="hljs-literal">null</span> : options.depth - <span class="hljs-number">1</span>
|
|
});
|
|
|
|
<span class="hljs-comment">// Five space padding because that's the size of "Box< ".</span>
|
|
<span class="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.repeat(<span class="hljs-number">5</span>);
|
|
<span class="hljs-keyword">const</span> inner = util.inspect(<span class="hljs-built_in">this</span>.value, newOptions)
|
|
.replace(<span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">`\n<span class="hljs-subst">${padding}</span>`</span>);
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<span class="hljs-string">'Box'</span>, <span class="hljs-string">'special'</span>)}</span>< <span class="hljs-subst">${inner}</span> >`</span>;
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> Box(<span class="hljs-literal">true</span>);
|
|
|
|
util.inspect(box);
|
|
<span class="hljs-comment">// Returns: "Box< true >"</span></code></pre>
|
|
<p>Custom <code>[util.inspect.custom](depth, opts)</code> functions typically return a string
|
|
but may return a value of any type that will be formatted accordingly by
|
|
<code>util.inspect()</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hljs-string">'this will not show up in the inspect() output'</span> };
|
|
obj[util.inspect.custom] = <span class="hljs-function">(<span class="hljs-params">depth</span>) =></span> {
|
|
<span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-string">'baz'</span> };
|
|
};
|
|
|
|
util.inspect(obj);
|
|
<span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code></pre>
|
|
<h3><code>util.inspect.custom</code><span><a class="mark" href="#util_util_inspect_custom" id="util_util_inspect_custom">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v10.12.0</td>
|
|
<td><p>This is now defined as a shared symbol.</p></td></tr>
|
|
<tr><td>v6.6.0</td>
|
|
<td><p><span>Added in: v6.6.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom inspect functions.</li>
|
|
</ul>
|
|
<p>In addition to being accessible through <code>util.inspect.custom</code>, this
|
|
symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be
|
|
accessed in any environment as <code>Symbol.for('nodejs.util.inspect.custom')</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> inspect = <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">'nodejs.util.inspect.custom'</span>);
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Password</span> </span>{
|
|
<span class="hljs-keyword">constructor</span>(value) {
|
|
<span class="hljs-built_in">this</span>.value = value;
|
|
}
|
|
|
|
toString() {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>;
|
|
}
|
|
|
|
[inspect]() {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-string">`Password <<span class="hljs-subst">${<span class="hljs-built_in">this</span>.toString()}</span>>`</span>;
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> Password(<span class="hljs-string">'r0sebud'</span>);
|
|
<span class="hljs-built_in">console</span>.log(password);
|
|
<span class="hljs-comment">// Prints Password <xxxxxxxx></span></code></pre>
|
|
<p>See <a href="#util_custom_inspection_functions_on_objects">Custom inspection functions on Objects</a> for more details.</p>
|
|
<h3><code>util.inspect.defaultOptions</code><span><a class="mark" href="#util_util_inspect_defaultoptions" id="util_util_inspect_defaultoptions">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v6.4.0</span>
|
|
</div>
|
|
<p>The <code>defaultOptions</code> value allows customization of the default options used by
|
|
<code>util.inspect</code>. This is useful for functions like <code>console.log</code> or
|
|
<code>util.format</code> which implicitly call into <code>util.inspect</code>. It shall be set to an
|
|
object containing one or more valid <a href="#util_util_inspect_object_options"><code>util.inspect()</code></a> options. Setting
|
|
option properties directly is also supported.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> arr = <span class="hljs-built_in">Array</span>(<span class="hljs-number">101</span>).fill(<span class="hljs-number">0</span>);
|
|
|
|
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// Logs the truncated array</span>
|
|
util.inspect.defaultOptions.maxArrayLength = <span class="hljs-literal">null</span>;
|
|
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// logs the full array</span></code></pre>
|
|
<h2><code>util.isDeepStrictEqual(val1, val2)</code><span><a class="mark" href="#util_util_isdeepstrictequal_val1_val2" id="util_util_isdeepstrictequal_val1_val2">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v9.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if there is deep strict equality between <code>val1</code> and <code>val2</code>.
|
|
Otherwise, returns <code>false</code>.</p>
|
|
<p>See <a href="assert.html#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> for more information about deep strict
|
|
equality.</p>
|
|
<h2><code>util.promisify(original)</code><span><a class="mark" href="#util_util_promisify_original" id="util_util_promisify_original">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v8.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
|
|
</ul>
|
|
<p>Takes a function following the common error-first callback style, i.e. taking
|
|
an <code>(err, value) => ...</code> callback as the last argument, and returns a version
|
|
that returns promises.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> stat = util.promisify(fs.stat);
|
|
stat(<span class="hljs-string">'.'</span>).then(<span class="hljs-function">(<span class="hljs-params">stats</span>) =></span> {
|
|
<span class="hljs-comment">// Do something with `stats`</span>
|
|
}).catch(<span class="hljs-function">(<span class="hljs-params">error</span>) =></span> {
|
|
<span class="hljs-comment">// Handle the error.</span>
|
|
});</code></pre>
|
|
<p>Or, equivalently using <code>async function</code>s:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> stat = util.promisify(fs.stat);
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callStat</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> stat(<span class="hljs-string">'.'</span>);
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`This directory is owned by <span class="hljs-subst">${stats.uid}</span>`</span>);
|
|
}</code></pre>
|
|
<p>If there is an <code>original[util.promisify.custom]</code> property present, <code>promisify</code>
|
|
will return its value, see <a href="#util_custom_promisified_functions">Custom promisified functions</a>.</p>
|
|
<p><code>promisify()</code> assumes that <code>original</code> is a function taking a callback as its
|
|
final argument in all cases. If <code>original</code> is not a function, <code>promisify()</code>
|
|
will throw an error. If <code>original</code> is a function but its last argument is not
|
|
an error-first callback, it will still be passed an error-first
|
|
callback as its last argument.</p>
|
|
<p>Using <code>promisify()</code> on class methods or other methods that use <code>this</code> may not
|
|
work as expected unless handled specially:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span> </span>{
|
|
<span class="hljs-keyword">constructor</span>() {
|
|
<span class="hljs-built_in">this</span>.a = <span class="hljs-number">42</span>;
|
|
}
|
|
|
|
bar(callback) {
|
|
callback(<span class="hljs-literal">null</span>, <span class="hljs-built_in">this</span>.a);
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> Foo();
|
|
|
|
<span class="hljs-keyword">const</span> naiveBar = util.promisify(foo.bar);
|
|
<span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span>
|
|
<span class="hljs-comment">// naiveBar().then(a => console.log(a));</span>
|
|
|
|
naiveBar.call(foo).then(<span class="hljs-function">(<span class="hljs-params">a</span>) =></span> <span class="hljs-built_in">console</span>.log(a)); <span class="hljs-comment">// '42'</span>
|
|
|
|
<span class="hljs-keyword">const</span> bindBar = naiveBar.bind(foo);
|
|
bindBar().then(<span class="hljs-function">(<span class="hljs-params">a</span>) =></span> <span class="hljs-built_in">console</span>.log(a)); <span class="hljs-comment">// '42'</span></code></pre>
|
|
<h3>Custom promisified functions<span><a class="mark" href="#util_custom_promisified_functions" id="util_custom_promisified_functions">#</a></span></h3>
|
|
<p>Using the <code>util.promisify.custom</code> symbol one can override the return value of
|
|
<a href="#util_util_promisify_original"><code>util.promisify()</code></a>:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span>(<span class="hljs-params">foo, callback</span>) </span>{
|
|
<span class="hljs-comment">// ...</span>
|
|
}
|
|
|
|
doSomething[util.promisify.custom] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> {
|
|
<span class="hljs-keyword">return</span> getPromiseSomehow();
|
|
};
|
|
|
|
<span class="hljs-keyword">const</span> promisified = util.promisify(doSomething);
|
|
<span class="hljs-built_in">console</span>.log(promisified === doSomething[util.promisify.custom]);
|
|
<span class="hljs-comment">// prints 'true'</span></code></pre>
|
|
<p>This can be useful for cases where the original function does not follow the
|
|
standard format of taking an error-first callback as the last argument.</p>
|
|
<p>For example, with a function that takes in
|
|
<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p>
|
|
<pre><code class="language-js">doSomething[util.promisify.custom] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
|
|
doSomething(foo, resolve, reject);
|
|
});
|
|
};</code></pre>
|
|
<p>If <code>promisify.custom</code> is defined but is not a function, <code>promisify()</code> will
|
|
throw an error.</p>
|
|
<h3><code>util.promisify.custom</code><span><a class="mark" href="#util_util_promisify_custom" id="util_util_promisify_custom">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.16.2</td>
|
|
<td><p>This is now defined as a shared symbol.</p></td></tr>
|
|
<tr><td>v8.0.0</td>
|
|
<td><p><span>Added in: v8.0.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom promisified variants of functions,
|
|
see <a href="#util_custom_promisified_functions">Custom promisified functions</a>.</li>
|
|
</ul>
|
|
<p>In addition to being accessible through <code>util.promisify.custom</code>, this
|
|
symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be
|
|
accessed in any environment as <code>Symbol.for('nodejs.util.promisify.custom')</code>.</p>
|
|
<p>For example, with a function that takes in
|
|
<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> kCustomPromisifiedSymbol = <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">'nodejs.util.promisify.custom'</span>);
|
|
|
|
doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
|
|
doSomething(foo, resolve, reject);
|
|
});
|
|
};</code></pre>
|
|
<h2>Class: <code>util.TextDecoder</code><span><a class="mark" href="#util_class_util_textdecoder" id="util_class_util_textdecoder">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v8.3.0</span>
|
|
</div>
|
|
<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextDecoder</code> API.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> TextDecoder(<span class="hljs-string">'shift_jis'</span>);
|
|
<span class="hljs-keyword">let</span> string = <span class="hljs-string">''</span>;
|
|
<span class="hljs-keyword">let</span> buffer;
|
|
<span class="hljs-keyword">while</span> (buffer = getNextChunkSomehow()) {
|
|
string += decoder.decode(buffer, { <span class="hljs-attr">stream</span>: <span class="hljs-literal">true</span> });
|
|
}
|
|
string += decoder.decode(); <span class="hljs-comment">// end-of-stream</span></code></pre>
|
|
<h3>WHATWG supported encodings<span><a class="mark" href="#util_whatwg_supported_encodings" id="util_whatwg_supported_encodings">#</a></span></h3>
|
|
<p>Per the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a>, the encodings supported by the
|
|
<code>TextDecoder</code> API are outlined in the tables below. For each encoding,
|
|
one or more aliases may be used.</p>
|
|
<p>Different Node.js build configurations support different sets of encodings.
|
|
While a very basic set of encodings is supported even on Node.js builds without
|
|
ICU enabled, support for some encodings is provided only when Node.js is built
|
|
with ICU and using the full ICU data (see <a href="intl.html">Internationalization</a>).</p>
|
|
<h4>Encodings Supported Without ICU<span><a class="mark" href="#util_encodings_supported_without_icu" id="util_encodings_supported_without_icu">#</a></span></h4>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'utf-16le'</code></td><td><code>'utf-16'</code></td></tr></tbody></table>
|
|
<h4>Encodings Supported by Default (With ICU)<span><a class="mark" href="#util_encodings_supported_by_default_with_icu" id="util_encodings_supported_by_default_with_icu">#</a></span></h4>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'utf-16le'</code></td><td><code>'utf-16'</code></td></tr><tr><td><code>'utf-16be'</code></td><td></td></tr></tbody></table>
|
|
<h4>Encodings requiring full ICU data<span><a class="mark" href="#util_encodings_requiring_full_icu_data" id="util_encodings_requiring_full_icu_data">#</a></span></h4>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'ibm866'</code></td><td><code>'866'</code>, <code>'cp866'</code>, <code>'csibm866'</code></td></tr><tr><td><code>'iso-8859-2'</code></td><td><code>'csisolatin2'</code>, <code>'iso-ir-101'</code>, <code>'iso8859-2'</code>, <code>'iso88592'</code>, <code>'iso_8859-2'</code>, <code>'iso_8859-2:1987'</code>, <code>'l2'</code>, <code>'latin2'</code></td></tr><tr><td><code>'iso-8859-3'</code></td><td><code>'csisolatin3'</code>, <code>'iso-ir-109'</code>, <code>'iso8859-3'</code>, <code>'iso88593'</code>, <code>'iso_8859-3'</code>, <code>'iso_8859-3:1988'</code>, <code>'l3'</code>, <code>'latin3'</code></td></tr><tr><td><code>'iso-8859-4'</code></td><td><code>'csisolatin4'</code>, <code>'iso-ir-110'</code>, <code>'iso8859-4'</code>, <code>'iso88594'</code>, <code>'iso_8859-4'</code>, <code>'iso_8859-4:1988'</code>, <code>'l4'</code>, <code>'latin4'</code></td></tr><tr><td><code>'iso-8859-5'</code></td><td><code>'csisolatincyrillic'</code>, <code>'cyrillic'</code>, <code>'iso-ir-144'</code>, <code>'iso8859-5'</code>, <code>'iso88595'</code>, <code>'iso_8859-5'</code>, <code>'iso_8859-5:1988'</code></td></tr><tr><td><code>'iso-8859-6'</code></td><td><code>'arabic'</code>, <code>'asmo-708'</code>, <code>'csiso88596e'</code>, <code>'csiso88596i'</code>, <code>'csisolatinarabic'</code>, <code>'ecma-114'</code>, <code>'iso-8859-6-e'</code>, <code>'iso-8859-6-i'</code>, <code>'iso-ir-127'</code>, <code>'iso8859-6'</code>, <code>'iso88596'</code>, <code>'iso_8859-6'</code>, <code>'iso_8859-6:1987'</code></td></tr><tr><td><code>'iso-8859-7'</code></td><td><code>'csisolatingreek'</code>, <code>'ecma-118'</code>, <code>'elot_928'</code>, <code>'greek'</code>, <code>'greek8'</code>, <code>'iso-ir-126'</code>, <code>'iso8859-7'</code>, <code>'iso88597'</code>, <code>'iso_8859-7'</code>, <code>'iso_8859-7:1987'</code>, <code>'sun_eu_greek'</code></td></tr><tr><td><code>'iso-8859-8'</code></td><td><code>'csiso88598e'</code>, <code>'csisolatinhebrew'</code>, <code>'hebrew'</code>, <code>'iso-8859-8-e'</code>, <code>'iso-ir-138'</code>, <code>'iso8859-8'</code>, <code>'iso88598'</code>, <code>'iso_8859-8'</code>, <code>'iso_8859-8:1988'</code>, <code>'visual'</code></td></tr><tr><td><code>'iso-8859-8-i'</code></td><td><code>'csiso88598i'</code>, <code>'logical'</code></td></tr><tr><td><code>'iso-8859-10'</code></td><td><code>'csisolatin6'</code>, <code>'iso-ir-157'</code>, <code>'iso8859-10'</code>, <code>'iso885910'</code>, <code>'l6'</code>, <code>'latin6'</code></td></tr><tr><td><code>'iso-8859-13'</code></td><td><code>'iso8859-13'</code>, <code>'iso885913'</code></td></tr><tr><td><code>'iso-8859-14'</code></td><td><code>'iso8859-14'</code>, <code>'iso885914'</code></td></tr><tr><td><code>'iso-8859-15'</code></td><td><code>'csisolatin9'</code>, <code>'iso8859-15'</code>, <code>'iso885915'</code>, <code>'iso_8859-15'</code>, <code>'l9'</code></td></tr><tr><td><code>'koi8-r'</code></td><td><code>'cskoi8r'</code>, <code>'koi'</code>, <code>'koi8'</code>, <code>'koi8_r'</code></td></tr><tr><td><code>'koi8-u'</code></td><td><code>'koi8-ru'</code></td></tr><tr><td><code>'macintosh'</code></td><td><code>'csmacintosh'</code>, <code>'mac'</code>, <code>'x-mac-roman'</code></td></tr><tr><td><code>'windows-874'</code></td><td><code>'dos-874'</code>, <code>'iso-8859-11'</code>, <code>'iso8859-11'</code>, <code>'iso885911'</code>, <code>'tis-620'</code></td></tr><tr><td><code>'windows-1250'</code></td><td><code>'cp1250'</code>, <code>'x-cp1250'</code></td></tr><tr><td><code>'windows-1251'</code></td><td><code>'cp1251'</code>, <code>'x-cp1251'</code></td></tr><tr><td><code>'windows-1252'</code></td><td><code>'ansi_x3.4-1968'</code>, <code>'ascii'</code>, <code>'cp1252'</code>, <code>'cp819'</code>, <code>'csisolatin1'</code>, <code>'ibm819'</code>, <code>'iso-8859-1'</code>, <code>'iso-ir-100'</code>, <code>'iso8859-1'</code>, <code>'iso88591'</code>, <code>'iso_8859-1'</code>, <code>'iso_8859-1:1987'</code>, <code>'l1'</code>, <code>'latin1'</code>, <code>'us-ascii'</code>, <code>'x-cp1252'</code></td></tr><tr><td><code>'windows-1253'</code></td><td><code>'cp1253'</code>, <code>'x-cp1253'</code></td></tr><tr><td><code>'windows-1254'</code></td><td><code>'cp1254'</code>, <code>'csisolatin5'</code>, <code>'iso-8859-9'</code>, <code>'iso-ir-148'</code>, <code>'iso8859-9'</code>, <code>'iso88599'</code>, <code>'iso_8859-9'</code>, <code>'iso_8859-9:1989'</code>, <code>'l5'</code>, <code>'latin5'</code>, <code>'x-cp1254'</code></td></tr><tr><td><code>'windows-1255'</code></td><td><code>'cp1255'</code>, <code>'x-cp1255'</code></td></tr><tr><td><code>'windows-1256'</code></td><td><code>'cp1256'</code>, <code>'x-cp1256'</code></td></tr><tr><td><code>'windows-1257'</code></td><td><code>'cp1257'</code>, <code>'x-cp1257'</code></td></tr><tr><td><code>'windows-1258'</code></td><td><code>'cp1258'</code>, <code>'x-cp1258'</code></td></tr><tr><td><code>'x-mac-cyrillic'</code></td><td><code>'x-mac-ukrainian'</code></td></tr><tr><td><code>'gbk'</code></td><td><code>'chinese'</code>, <code>'csgb2312'</code>, <code>'csiso58gb231280'</code>, <code>'gb2312'</code>, <code>'gb_2312'</code>, <code>'gb_2312-80'</code>, <code>'iso-ir-58'</code>, <code>'x-gbk'</code></td></tr><tr><td><code>'gb18030'</code></td><td></td></tr><tr><td><code>'big5'</code></td><td><code>'big5-hkscs'</code>, <code>'cn-big5'</code>, <code>'csbig5'</code>, <code>'x-x-big5'</code></td></tr><tr><td><code>'euc-jp'</code></td><td><code>'cseucpkdfmtjapanese'</code>, <code>'x-euc-jp'</code></td></tr><tr><td><code>'iso-2022-jp'</code></td><td><code>'csiso2022jp'</code></td></tr><tr><td><code>'shift_jis'</code></td><td><code>'csshiftjis'</code>, <code>'ms932'</code>, <code>'ms_kanji'</code>, <code>'shift-jis'</code>, <code>'sjis'</code>, <code>'windows-31j'</code>, <code>'x-sjis'</code></td></tr><tr><td><code>'euc-kr'</code></td><td><code>'cseuckr'</code>, <code>'csksc56011987'</code>, <code>'iso-ir-149'</code>, <code>'korean'</code>, <code>'ks_c_5601-1987'</code>, <code>'ks_c_5601-1989'</code>, <code>'ksc5601'</code>, <code>'ksc_5601'</code>, <code>'windows-949'</code></td></tr></tbody></table>
|
|
<p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a>
|
|
is not supported.</p>
|
|
<h3><code>new TextDecoder([encoding[, options]])</code><span><a class="mark" href="#util_new_textdecoder_encoding_options" id="util_new_textdecoder_encoding_options">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v11.0.0</td>
|
|
<td><p>The class is now available on the global object.</p></td></tr>
|
|
<tr><td>v8.3.0</td>
|
|
<td><p><span>Added in: v8.3.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Identifies the <code>encoding</code> that this <code>TextDecoder</code> instance
|
|
supports. <strong>Default:</strong> <code>'utf-8'</code>.</li>
|
|
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
|
|
<ul>
|
|
<li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if decoding failures are fatal. This option is only
|
|
supported when ICU is enabled (see <a href="intl.html">Internationalization</a>). <strong>Default:</strong>
|
|
<code>false</code>.</li>
|
|
<li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>TextDecoder</code> will include the byte
|
|
order mark in the decoded result. When <code>false</code>, the byte order mark will
|
|
be removed from the output. This option is only used when <code>encoding</code> is
|
|
<code>'utf-8'</code>, <code>'utf-16be'</code> or <code>'utf-16le'</code>. <strong>Default:</strong> <code>false</code>.</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<p>Creates an new <code>TextDecoder</code> instance. The <code>encoding</code> may specify one of the
|
|
supported encodings or an alias.</p>
|
|
<p>The <code>TextDecoder</code> class is also available on the global object.</p>
|
|
<h3><code>textDecoder.decode([input[, options]])</code><span><a class="mark" href="#util_textdecoder_decode_input_options" id="util_textdecoder_decode_input_options">#</a></span></h3>
|
|
<ul>
|
|
<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> An <code>ArrayBuffer</code>, <code>DataView</code> or
|
|
<code>TypedArray</code> instance containing the encoded data.</li>
|
|
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
|
|
<ul>
|
|
<li><code>stream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if additional chunks of data are expected.
|
|
<strong>Default:</strong> <code>false</code>.</li>
|
|
</ul>
|
|
</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>Decodes the <code>input</code> and returns a string. If <code>options.stream</code> is <code>true</code>, any
|
|
incomplete byte sequences occurring at the end of the <code>input</code> are buffered
|
|
internally and emitted after the next call to <code>textDecoder.decode()</code>.</p>
|
|
<p>If <code>textDecoder.fatal</code> is <code>true</code>, decoding errors that occur will result in a
|
|
<code>TypeError</code> being thrown.</p>
|
|
<h3><code>textDecoder.encoding</code><span><a class="mark" href="#util_textdecoder_encoding" id="util_textdecoder_encoding">#</a></span></h3>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>The encoding supported by the <code>TextDecoder</code> instance.</p>
|
|
<h3><code>textDecoder.fatal</code><span><a class="mark" href="#util_textdecoder_fatal" id="util_textdecoder_fatal">#</a></span></h3>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>The value will be <code>true</code> if decoding errors result in a <code>TypeError</code> being
|
|
thrown.</p>
|
|
<h3><code>textDecoder.ignoreBOM</code><span><a class="mark" href="#util_textdecoder_ignorebom" id="util_textdecoder_ignorebom">#</a></span></h3>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>The value will be <code>true</code> if the decoding result will include the byte order
|
|
mark.</p>
|
|
<h2>Class: <code>util.TextEncoder</code><span><a class="mark" href="#util_class_util_textencoder" id="util_class_util_textencoder">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v11.0.0</td>
|
|
<td><p>The class is now available on the global object.</p></td></tr>
|
|
<tr><td>v8.3.0</td>
|
|
<td><p><span>Added in: v8.3.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextEncoder</code> API. All
|
|
instances of <code>TextEncoder</code> only support UTF-8 encoding.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> TextEncoder();
|
|
<span class="hljs-keyword">const</span> uint8array = encoder.encode(<span class="hljs-string">'this is some data'</span>);</code></pre>
|
|
<p>The <code>TextEncoder</code> class is also available on the global object.</p>
|
|
<h3><code>textEncoder.encode([input])</code><span><a class="mark" href="#util_textencoder_encode_input" id="util_textencoder_encode_input">#</a></span></h3>
|
|
<ul>
|
|
<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The text to encode. <strong>Default:</strong> an empty string.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li>
|
|
</ul>
|
|
<p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the
|
|
encoded bytes.</p>
|
|
<h3><code>textEncoder.encodeInto(src, dest)</code><span><a class="mark" href="#util_textencoder_encodeinto_src_dest" id="util_textencoder_encodeinto_src_dest">#</a></span></h3>
|
|
<ul>
|
|
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The text to encode.</li>
|
|
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> The array to hold the encode result.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
|
|
<ul>
|
|
<li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The read Unicode code units of src.</li>
|
|
<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The written UTF-8 bytes of dest.</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an object
|
|
containing the read Unicode code units and written UTF-8 bytes.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> TextEncoder();
|
|
<span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>;
|
|
<span class="hljs-keyword">const</span> dest = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(<span class="hljs-number">10</span>);
|
|
<span class="hljs-keyword">const</span> { read, written } = encoder.encodeInto(src, dest);</code></pre>
|
|
<h3><code>textEncoder.encoding</code><span><a class="mark" href="#util_textencoder_encoding" id="util_textencoder_encoding">#</a></span></h3>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</code>.</p>
|
|
<h2><code>util.types</code><span><a class="mark" href="#util_util_types" id="util_util_types">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<p><code>util.types</code> provides type checks for different kinds of built-in objects.
|
|
Unlike <code>instanceof</code> or <code>Object.prototype.toString.call(value)</code>, these checks do
|
|
not inspect properties of the object that are accessible from JavaScript (like
|
|
their prototype), and usually have the overhead of calling into C++.</p>
|
|
<p>The result generally does not make any guarantees about what kinds of
|
|
properties or behavior a value exposes in JavaScript. They are primarily
|
|
useful for addon developers who prefer to do type checking in JavaScript.</p>
|
|
<h3><code>util.types.isAnyArrayBuffer(value)</code><span><a class="mark" href="#util_util_types_isanyarraybuffer_value" id="util_util_types_isanyarraybuffer_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instance.</p>
|
|
<p>See also <a href="#util_util_types_isarraybuffer_value"><code>util.types.isArrayBuffer()</code></a> and
|
|
<a href="#util_util_types_issharedarraybuffer_value"><code>util.types.isSharedArrayBuffer()</code></a>.</p>
|
|
<pre><code class="language-js">util.types.isAnyArrayBuffer(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isAnyArrayBuffer(<span class="hljs-keyword">new</span> SharedArrayBuffer()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isArrayBufferView(value)</code><span><a class="mark" href="#util_util_types_isarraybufferview_value" id="util_util_types_isarraybufferview_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an instance of one of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>
|
|
views, such as typed array objects or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>. Equivalent to
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p>
|
|
<pre><code class="language-js">util.types.isArrayBufferView(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Int8Array</span>()); <span class="hljs-comment">// true</span>
|
|
util.types.isArrayBufferView(Buffer.from(<span class="hljs-string">'hello world'</span>)); <span class="hljs-comment">// true</span>
|
|
util.types.isArrayBufferView(<span class="hljs-keyword">new</span> <span class="hljs-built_in">DataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>(<span class="hljs-number">16</span>))); <span class="hljs-comment">// true</span>
|
|
util.types.isArrayBufferView(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// false</span></code></pre>
|
|
<h3><code>util.types.isArgumentsObject(value)</code><span><a class="mark" href="#util_util_types_isargumentsobject_value" id="util_util_types_isargumentsobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an <code>arguments</code> object.</p>
|
|
<!-- eslint-disable prefer-rest-params -->
|
|
<pre><code class="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
|
|
util.types.isArgumentsObject(<span class="hljs-built_in">arguments</span>); <span class="hljs-comment">// Returns true</span>
|
|
}</code></pre>
|
|
<h3><code>util.types.isArrayBuffer(value)</code><span><a class="mark" href="#util_util_types_isarraybuffer_value" id="util_util_types_isarraybuffer_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> instance.
|
|
This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instances. Usually, it is
|
|
desirable to test for both; See <a href="#util_util_types_isanyarraybuffer_value"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p>
|
|
<pre><code class="language-js">util.types.isArrayBuffer(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isArrayBuffer(<span class="hljs-keyword">new</span> SharedArrayBuffer()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isAsyncFunction(value)</code><span><a class="mark" href="#util_util_types_isasyncfunction_value" id="util_util_types_isasyncfunction_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async function</a>.
|
|
This only reports back what the JavaScript engine is seeing;
|
|
in particular, the return value may not match the original source code if
|
|
a transpilation tool was used.</p>
|
|
<pre><code class="language-js">util.types.isAsyncFunction(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isAsyncFunction(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isBigInt64Array(value)</code><span><a class="mark" href="#util_util_types_isbigint64array_value" id="util_util_types_isbigint64array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a <code>BigInt64Array</code> instance.</p>
|
|
<pre><code class="language-js">util.types.isBigInt64Array(<span class="hljs-keyword">new</span> BigInt64Array()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isBigInt64Array(<span class="hljs-keyword">new</span> BigUint64Array()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isBigUint64Array(value)</code><span><a class="mark" href="#util_util_types_isbiguint64array_value" id="util_util_types_isbiguint64array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a <code>BigUint64Array</code> instance.</p>
|
|
<pre><code class="language-js">util.types.isBigUint64Array(<span class="hljs-keyword">new</span> BigInt64Array()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBigUint64Array(<span class="hljs-keyword">new</span> BigUint64Array()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isBooleanObject(value)</code><span><a class="mark" href="#util_util_types_isbooleanobject_value" id="util_util_types_isbooleanobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a boolean object, e.g. created
|
|
by <code>new Boolean()</code>.</p>
|
|
<pre><code class="language-js">util.types.isBooleanObject(<span class="hljs-literal">false</span>); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBooleanObject(<span class="hljs-literal">true</span>); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBooleanObject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isBooleanObject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Boolean</span>(<span class="hljs-literal">true</span>)); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isBooleanObject(<span class="hljs-built_in">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBooleanObject(<span class="hljs-built_in">Boolean</span>(<span class="hljs-literal">true</span>)); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isBoxedPrimitive(value)</code><span><a class="mark" href="#util_util_types_isboxedprimitive_value" id="util_util_types_isboxedprimitive_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.11.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is any boxed primitive object, e.g. created
|
|
by <code>new Boolean()</code>, <code>new String()</code> or <code>Object(Symbol())</code>.</p>
|
|
<p>For example:</p>
|
|
<pre><code class="language-js">util.types.isBoxedPrimitive(<span class="hljs-literal">false</span>); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBoxedPrimitive(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isBoxedPrimitive(<span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isBoxedPrimitive(<span class="hljs-built_in">Object</span>(<span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'foo'</span>))); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isBoxedPrimitive(<span class="hljs-built_in">Object</span>(BigInt(<span class="hljs-number">5</span>))); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isDataView(value)</code><span><a class="mark" href="#util_util_types_isdataview_value" id="util_util_types_isdataview_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a> instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>(<span class="hljs-number">20</span>);
|
|
util.types.isDataView(<span class="hljs-keyword">new</span> <span class="hljs-built_in">DataView</span>(ab)); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isDataView(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p>
|
|
<h3><code>util.types.isDate(value)</code><span><a class="mark" href="#util_util_types_isdate_value" id="util_util_types_isdate_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isExternal(value)</code><span><a class="mark" href="#util_util_types_isexternal_value" id="util_util_types_isexternal_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a native <code>External</code> value.</p>
|
|
<p>A native <code>External</code> value is a special type of object that contains a
|
|
raw C++ pointer (<code>void*</code>) for access from native code, and has no other
|
|
properties. Such objects are created either by Node.js internals or native
|
|
addons. In JavaScript, they are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze">frozen</a> objects with a
|
|
<code>null</code> prototype.</p>
|
|
<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><js_native_api.h></span></span>
|
|
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
|
|
napi_value result;
|
|
<span class="hljs-function"><span class="hljs-keyword">static</span> napi_value <span class="hljs-title">MyNapi</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> </span>{
|
|
<span class="hljs-keyword">int</span>* raw = (<span class="hljs-keyword">int</span>*) <span class="hljs-built_in">malloc</span>(<span class="hljs-number">1024</span>);
|
|
napi_status status = napi_create_external(env, (<span class="hljs-keyword">void</span>*) raw, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, &result);
|
|
<span class="hljs-keyword">if</span> (status != napi_ok) {
|
|
napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_create_external failed"</span>);
|
|
<span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
|
|
}
|
|
<span class="hljs-keyword">return</span> result;
|
|
}
|
|
...
|
|
DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi)
|
|
...</code></pre>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> native = <span class="hljs-built_in">require</span>(<span class="hljs-string">'napi_addon.node'</span>);
|
|
<span class="hljs-keyword">const</span> data = native.myNapi();
|
|
util.types.isExternal(data); <span class="hljs-comment">// returns true</span>
|
|
util.types.isExternal(<span class="hljs-number">0</span>); <span class="hljs-comment">// returns false</span>
|
|
util.types.isExternal(<span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// returns false</span></code></pre>
|
|
<p>For further information on <code>napi_create_external</code>, refer to
|
|
<a href="n-api.html#n_api_napi_create_external"><code>napi_create_external()</code></a>.</p>
|
|
<h3><code>util.types.isFloat32Array(value)</code><span><a class="mark" href="#util_util_types_isfloat32array_value" id="util_util_types_isfloat32array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array"><code>Float32Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isFloat32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isFloat32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float32Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isFloat32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isFloat64Array(value)</code><span><a class="mark" href="#util_util_types_isfloat64array_value" id="util_util_types_isfloat64array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array"><code>Float64Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isFloat64Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isFloat64Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isFloat64Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isGeneratorFunction(value)</code><span><a class="mark" href="#util_util_types_isgeneratorfunction_value" id="util_util_types_isgeneratorfunction_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a generator function.
|
|
This only reports back what the JavaScript engine is seeing;
|
|
in particular, the return value may not match the original source code if
|
|
a transpilation tool was used.</p>
|
|
<pre><code class="language-js">util.types.isGeneratorFunction(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isGeneratorFunction(<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isGeneratorObject(value)</code><span><a class="mark" href="#util_util_types_isgeneratorobject_value" id="util_util_types_isgeneratorobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a generator object as returned from a
|
|
built-in generator function.
|
|
This only reports back what the JavaScript engine is seeing;
|
|
in particular, the return value may not match the original source code if
|
|
a transpilation tool was used.</p>
|
|
<pre><code class="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}
|
|
<span class="hljs-keyword">const</span> generator = foo();
|
|
util.types.isGeneratorObject(generator); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isInt8Array(value)</code><span><a class="mark" href="#util_util_types_isint8array_value" id="util_util_types_isint8array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array"><code>Int8Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isInt8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isInt8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Int8Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isInt8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isInt16Array(value)</code><span><a class="mark" href="#util_util_types_isint16array_value" id="util_util_types_isint16array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array"><code>Int16Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isInt16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isInt16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Int16Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isInt16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isInt32Array(value)</code><span><a class="mark" href="#util_util_types_isint32array_value" id="util_util_types_isint32array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array"><code>Int32Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isInt32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isInt32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Int32Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isInt32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isMap(value)</code><span><a class="mark" href="#util_util_types_ismap_value" id="util_util_types_ismap_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isMap(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isMapIterator(value)</code><span><a class="mark" href="#util_util_types_ismapiterator_value" id="util_util_types_ismapiterator_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an iterator returned for a built-in
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
|
|
util.types.isMapIterator(map.keys()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isMapIterator(map.values()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isMapIterator(map.entries()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isMapIterator(map[<span class="hljs-built_in">Symbol</span>.iterator]()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isModuleNamespaceObject(value)</code><span><a class="mark" href="#util_util_types_ismodulenamespaceobject_value" id="util_util_types_ismodulenamespaceobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an instance of a <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects">Module Namespace Object</a>.</p>
|
|
<!-- eslint-skip -->
|
|
<pre><code class="language-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ns <span class="hljs-keyword">from</span> <span class="hljs-string">'./a.js'</span>;
|
|
|
|
util.types.isModuleNamespaceObject(ns); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isNativeError(value)</code><span><a class="mark" href="#util_util_types_isnativeerror_value" id="util_util_types_isnativeerror_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an instance of a built-in <a href="errors.html#errors_class_error"><code>Error</code></a> type.</p>
|
|
<pre><code class="language-js">util.types.isNativeError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isNativeError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isNativeError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">RangeError</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isNumberObject(value)</code><span><a class="mark" href="#util_util_types_isnumberobject_value" id="util_util_types_isnumberobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a number object, e.g. created
|
|
by <code>new Number()</code>.</p>
|
|
<pre><code class="language-js">util.types.isNumberObject(<span class="hljs-number">0</span>); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isNumberObject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Number</span>(<span class="hljs-number">0</span>)); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isPromise(value)</code><span><a class="mark" href="#util_util_types_ispromise_value" id="util_util_types_ispromise_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"><code>Promise</code></a>.</p>
|
|
<pre><code class="language-js">util.types.isPromise(<span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-number">42</span>)); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isProxy(value)</code><span><a class="mark" href="#util_util_types_isproxy_value" id="util_util_types_isproxy_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"><code>Proxy</code></a> instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> target = {};
|
|
<span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Proxy</span>(target, {});
|
|
util.types.isProxy(target); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isProxy(proxy); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isRegExp(value)</code><span><a class="mark" href="#util_util_types_isregexp_value" id="util_util_types_isregexp_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a regular expression object.</p>
|
|
<pre><code class="language-js">util.types.isRegExp(<span class="hljs-regexp">/abc/</span>); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isRegExp(<span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-string">'abc'</span>)); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isSet(value)</code><span><a class="mark" href="#util_util_types_isset_value" id="util_util_types_isset_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isSet(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isSetIterator(value)</code><span><a class="mark" href="#util_util_types_issetiterator_value" id="util_util_types_issetiterator_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is an iterator returned for a built-in
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>();
|
|
util.types.isSetIterator(set.keys()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isSetIterator(set.values()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isSetIterator(set.entries()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isSetIterator(set[<span class="hljs-built_in">Symbol</span>.iterator]()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isSharedArrayBuffer(value)</code><span><a class="mark" href="#util_util_types_issharedarraybuffer_value" id="util_util_types_issharedarraybuffer_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instance.
|
|
This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> instances. Usually, it is
|
|
desirable to test for both; See <a href="#util_util_types_isanyarraybuffer_value"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p>
|
|
<pre><code class="language-js">util.types.isSharedArrayBuffer(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isSharedArrayBuffer(<span class="hljs-keyword">new</span> SharedArrayBuffer()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isStringObject(value)</code><span><a class="mark" href="#util_util_types_isstringobject_value" id="util_util_types_isstringobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a string object, e.g. created
|
|
by <code>new String()</code>.</p>
|
|
<pre><code class="language-js">util.types.isStringObject(<span class="hljs-string">'foo'</span>); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isStringObject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isSymbolObject(value)</code><span><a class="mark" href="#util_util_types_issymbolobject_value" id="util_util_types_issymbolobject_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a symbol object, created
|
|
by calling <code>Object()</code> on a <code>Symbol</code> primitive.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> symbol = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'foo'</span>);
|
|
util.types.isSymbolObject(symbol); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isSymbolObject(<span class="hljs-built_in">Object</span>(symbol)); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isTypedArray(value)</code><span><a class="mark" href="#util_util_types_istypedarray_value" id="util_util_types_istypedarray_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isTypedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isTypedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isTypedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p>
|
|
<h3><code>util.types.isUint8Array(value)</code><span><a class="mark" href="#util_util_types_isuint8array_value" id="util_util_types_isuint8array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isUint8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isUint8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isUint8Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isUint8ClampedArray(value)</code><span><a class="mark" href="#util_util_types_isuint8clampedarray_value" id="util_util_types_isuint8clampedarray_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray"><code>Uint8ClampedArray</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isUint8ClampedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isUint8ClampedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8ClampedArray</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isUint8ClampedArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isUint16Array(value)</code><span><a class="mark" href="#util_util_types_isuint16array_value" id="util_util_types_isuint16array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array"><code>Uint16Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isUint16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isUint16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint16Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isUint16Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isUint32Array(value)</code><span><a class="mark" href="#util_util_types_isuint32array_value" id="util_util_types_isuint32array_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array"><code>Uint32Array</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isUint32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span>
|
|
util.types.isUint32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint32Array</span>()); <span class="hljs-comment">// Returns true</span>
|
|
util.types.isUint32Array(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code></pre>
|
|
<h3><code>util.types.isWeakMap(value)</code><span><a class="mark" href="#util_util_types_isweakmap_value" id="util_util_types_isweakmap_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isWeakMap(<span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isWeakSet(value)</code><span><a class="mark" href="#util_util_types_isweakset_value" id="util_util_types_isweakset_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> instance.</p>
|
|
<pre><code class="language-js">util.types.isWeakSet(<span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>()); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h3><code>util.types.isWebAssemblyCompiledModule(value)</code><span><a class="mark" href="#util_util_types_iswebassemblycompiledmodule_value" id="util_util_types_iswebassemblycompiledmodule_value">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v10.0.0</span>
|
|
</div>
|
|
<ul>
|
|
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module"><code>WebAssembly.Module</code></a> instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-built_in">module</span> = <span class="hljs-keyword">new</span> WebAssembly.Module(wasmBuffer);
|
|
util.types.isWebAssemblyCompiledModule(<span class="hljs-built_in">module</span>); <span class="hljs-comment">// Returns true</span></code></pre>
|
|
<h2>Deprecated APIs<span><a class="mark" href="#util_deprecated_apis" id="util_deprecated_apis">#</a></span></h2>
|
|
<p>The following APIs are deprecated and should no longer be used. Existing
|
|
applications and modules should be updated to find alternative approaches.</p>
|
|
<h3><code>util._extend(target, source)</code><span><a class="mark" href="#util_util_extend_target_source" id="util_util_extend_target_source">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.7.5</span><span>Deprecated since: v6.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><code>Object.assign()</code></a> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
|
|
<li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
|
|
</ul>
|
|
<p>The <code>util._extend()</code> method was never intended to be used outside of internal
|
|
Node.js modules. The community found and used it anyway.</p>
|
|
<p>It is deprecated and should not be used in new code. JavaScript comes with very
|
|
similar built-in functionality through <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><code>Object.assign()</code></a>.</p>
|
|
<h3><code>util.isArray(object)</code><span><a class="mark" href="#util_util_isarray_object" id="util_util_isarray_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray"><code>Array.isArray()</code></a> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Alias for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray"><code>Array.isArray()</code></a>.</p>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is an <code>Array</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isArray([]);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isArray(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>());
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isArray({});
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isBoolean(object)</code><span><a class="mark" href="#util_util_isboolean_object" id="util_util_isboolean_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'boolean'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Boolean</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isBoolean(<span class="hljs-number">1</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isBoolean(<span class="hljs-number">0</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isBoolean(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isBuffer(object)</code><span><a class="mark" href="#util_util_isbuffer_object" id="util_util_isbuffer_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="buffer.html#buffer_static_method_buffer_isbuffer_obj"><code>Buffer.isBuffer()</code></a> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Buffer</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isBuffer({ <span class="hljs-attr">length</span>: <span class="hljs-number">0</span> });
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isBuffer([]);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isBuffer(Buffer.from(<span class="hljs-string">'hello world'</span>));
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isDate(object)</code><span><a class="mark" href="#util_util_isdate_object" id="util_util_isdate_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#util_util_types_isdate_value"><code>util.types.isDate()</code></a> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Date</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isDate(<span class="hljs-built_in">Date</span>());
|
|
<span class="hljs-comment">// false (without 'new' returns a String)</span>
|
|
util.isDate({});
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isError(object)</code><span><a class="mark" href="#util_util_iserror_object" id="util_util_iserror_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#util_util_types_isnativeerror_value"><code>util.types.isNativeError()</code></a> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is an <a href="errors.html#errors_class_error"><code>Error</code></a>. Otherwise, returns
|
|
<code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>());
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>());
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isError({ <span class="hljs-attr">name</span>: <span class="hljs-string">'Error'</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">'an error occurred'</span> });
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<p>This method relies on <code>Object.prototype.toString()</code> behavior. It is
|
|
possible to obtain an incorrect result when the <code>object</code> argument manipulates
|
|
<code>@@toStringTag</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Error'</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">'an error occurred'</span> };
|
|
|
|
util.isError(obj);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
obj[<span class="hljs-built_in">Symbol</span>.toStringTag] = <span class="hljs-string">'Error'</span>;
|
|
util.isError(obj);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isFunction(object)</code><span><a class="mark" href="#util_util_isfunction_object" id="util_util_isfunction_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'function'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Function</code>. Otherwise, returns
|
|
<code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Foo</span>(<span class="hljs-params"></span>) </span>{}
|
|
<span class="hljs-keyword">const</span> Bar = <span class="hljs-function">() =></span> {};
|
|
|
|
util.isFunction({});
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isFunction(Foo);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isFunction(Bar);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isNull(object)</code><span><a class="mark" href="#util_util_isnull_object" id="util_util_isnull_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>value === null</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is strictly <code>null</code>. Otherwise, returns
|
|
<code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isNull(<span class="hljs-number">0</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isNull(<span class="hljs-literal">undefined</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isNull(<span class="hljs-literal">null</span>);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isNullOrUndefined(object)</code><span><a class="mark" href="#util_util_isnullorundefined_object" id="util_util_isnullorundefined_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use
|
|
<code>value === undefined || value === null</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is <code>null</code> or <code>undefined</code>. Otherwise,
|
|
returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isNullOrUndefined(<span class="hljs-number">0</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isNullOrUndefined(<span class="hljs-literal">undefined</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isNullOrUndefined(<span class="hljs-literal">null</span>);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isNumber(object)</code><span><a class="mark" href="#util_util_isnumber_object" id="util_util_isnumber_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'number'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Number</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isNumber(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isNumber(<span class="hljs-literal">Infinity</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isNumber(<span class="hljs-number">0</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isNumber(<span class="hljs-literal">NaN</span>);
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isObject(object)</code><span><a class="mark" href="#util_util_isobject_object" id="util_util_isobject_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated:
|
|
Use <code>value !== null && typeof value === 'object'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is strictly an <code>Object</code> <strong>and</strong> not a
|
|
<code>Function</code> (even though functions are objects in JavaScript).
|
|
Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isObject(<span class="hljs-number">5</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isObject(<span class="hljs-literal">null</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isObject({});
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isObject(<span class="hljs-function">() =></span> {});
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isPrimitive(object)</code><span><a class="mark" href="#util_util_isprimitive_object" id="util_util_isprimitive_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use
|
|
<code>(typeof value !== 'object' && typeof value !== 'function') || value === null</code>
|
|
instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a primitive type. Otherwise, returns
|
|
<code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isPrimitive(<span class="hljs-number">5</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isPrimitive(<span class="hljs-string">'foo'</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isPrimitive(<span class="hljs-literal">false</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isPrimitive(<span class="hljs-literal">null</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isPrimitive(<span class="hljs-literal">undefined</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isPrimitive({});
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isPrimitive(<span class="hljs-function">() =></span> {});
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isPrimitive(<span class="hljs-regexp">/^$/</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isPrimitive(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isRegExp(object)</code><span><a class="mark" href="#util_util_isregexp_object" id="util_util_isregexp_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>RegExp</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isRegExp(<span class="hljs-regexp">/some regexp/</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isRegExp(<span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-string">'another regexp'</span>));
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isRegExp({});
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isString(object)</code><span><a class="mark" href="#util_util_isstring_object" id="util_util_isstring_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'string'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>string</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isString(<span class="hljs-string">''</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isString(<span class="hljs-string">'foo'</span>);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isString(<span class="hljs-built_in">String</span>(<span class="hljs-string">'foo'</span>));
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isString(<span class="hljs-number">5</span>);
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.isSymbol(object)</code><span><a class="mark" href="#util_util_issymbol_object" id="util_util_issymbol_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'symbol'</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is a <code>Symbol</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.isSymbol(<span class="hljs-number">5</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isSymbol(<span class="hljs-string">'foo'</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isSymbol(<span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'foo'</span>));
|
|
<span class="hljs-comment">// Returns: true</span></code></pre>
|
|
<h3><code>util.isUndefined(object)</code><span><a class="mark" href="#util_util_isundefined_object" id="util_util_isundefined_object">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>value === undefined</code> instead.</div><p></p>
|
|
<ul>
|
|
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
|
|
</ul>
|
|
<p>Returns <code>true</code> if the given <code>object</code> is <code>undefined</code>. Otherwise, returns <code>false</code>.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>;
|
|
util.isUndefined(<span class="hljs-number">5</span>);
|
|
<span class="hljs-comment">// Returns: false</span>
|
|
util.isUndefined(foo);
|
|
<span class="hljs-comment">// Returns: true</span>
|
|
util.isUndefined(<span class="hljs-literal">null</span>);
|
|
<span class="hljs-comment">// Returns: false</span></code></pre>
|
|
<h3><code>util.log(string)</code><span><a class="mark" href="#util_util_log_string" id="util_util_log_string">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.3.0</span><span>Deprecated since: v6.0.0</span>
|
|
</div>
|
|
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use a third party module instead.</div><p></p>
|
|
<ul>
|
|
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
|
|
</ul>
|
|
<p>The <code>util.log()</code> method prints the given <code>string</code> to <code>stdout</code> with an included
|
|
timestamp.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
|
|
|
|
util.log(<span class="hljs-string">'Timestamped message.'</span>);</code></pre>
|
|
<!-- API END -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|