Les utilitaires courants disponibles dans node:util comprennent :
format,formatWithOptions,inspectetstripVTControlCharacters;promisify,callbackify,inherits,deprecateetdebuglog;isDeepStrictEqual,parseArgs,parseEnvetstyleText;getSystemErrorName,getSystemErrorMessageetgetSystemErrorMap;getCallSites;- les constructeurs globaux
TextEncoderetTextDecoder.
toUSVString() et l’ancien utilitaire log() sont présents mais lèvent une
erreur d’implémentation.
Exemples
const util = require("node:util");
util.inspect({ a: [1, 2] }); // "{ a: [ 1, 2 ] }"
util.format("%s:%d", "port", 8080); // "port:8080"
util.isDeepStrictEqual({ a: 1 }, { a: 1 }); // trueinspect() gère les conteneurs courants, les tableaux typés, les buffers, les
fonctions, les références circulaires, les promesses et util.inspect.custom.
Des options telles que depth, breakLength, compact et maxArrayLength
sont disponibles.
promisify() respecte util.promisify.custom.
util.types
La famille de prédicats couvre les tableaux et vues courants, les dates, les
maps, les sets, les erreurs, les promesses, les proxys, les expressions
régulières, les collections faibles, les primitives encapsulées, les types de
fonctions, les itérateurs et les spécialisations de tableaux typés. Tous les
prédicats de Node ne sont pas présents ; testez l’existence des prédicats plus
récents, tels que isCryptoKey, avant de les appeler.
Voir aussi
Reference
Status: Partial · 9 of 18 suite tests pass (50.0%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
format() | function | Full | Formats a string using printf-style substitution patterns (%s, %d, %o, etc.) |
formatWithOptions() | function | Full | Like format(), but accepts an explicit util.inspect options object for the first argument |
inspect() | function | Full | Returns a string representation of an object for debugging |
promisify() | function | Full | Wraps a callback-based function so it returns a Promise |
inherits() | function | Full | Sets up prototype inheritance between a constructor and a superclass |
isDeepStrictEqual() | function | Full | Returns true if two values are deeply equal using the strict equality algorithm |
deprecate() | function | Full | Wraps a function to emit a deprecation warning when called |
debuglog() | function | Full | Returns a logging function that writes to stderr only if the NODE_DEBUG env variable includes section |
callbackify() | function | Full | Wraps an async function (or a function that returns a Promise) so it follows the error-first callback… |
getSystemErrorName() | function | Full | Returns the string name for a numeric error code (the errno value) |
getSystemErrorMessage() | function | Full | Returns the string message for a numeric error code (the errno value) |
getSystemErrorMap() | function | Full | Returns a Map of all system error codes to [name, message] pairs |
toUSVString() | function | Full | Returns a string after replacing any lone surrogates with the Unicode replacement character U+FFFD |
stripVTControlCharacters() | function | Full | Removes ANSI escape codes (including color and cursor movement) from a string |
styleText() | function | Full | Formats text with ANSI styling for terminal output |
getCallSites() | function | Full | Returns an array of call site objects representing the current call stack |
parseEnv() | function | Full | Parses a .env file content string into a key-value object |
log() | function | Full | Writes a formatted timestamp + message to stdout (similar to console.log but with a timestamp) |
types | property | Full | Object with type-checking predicates (e.g. util.types.isDate, util.types.isMap) |
TextEncoder | property | Full | The WHATWG TextEncoder class |
TextDecoder | property | Full | The WHATWG TextDecoder class |
Not implemented
convertProcessSignalToExitCode, debug, diff, isArray, setTraceSigInt, transferableAbortController, transferableAbortSignal.
Tests: node-api node:util: 9 pass, 8 fail, 1 error, 12 skipped. Failing: test-util-callbackify.js, test-util-format.js, test-util-getcallsites-preparestacktrace.js, test-util-getcallsites-sourcemap.js, test-util-inspect-getters-accessing-this.js, test-util-inspect-long-running.js, test-util-inspect-namespace.js, test-util-inspect-regexp.js, and 1 more.