انتقل إلى المحتوى

مرجع واجهة ⁦node:util⁩

التنسيق والفحص ودوال الاستدعاء ومساعدات الوسائط ومسندات الأنواع

في هذه الصفحة

تشمل المساعدات الشائعة المتاحة في ⁦node:util⁩:

  • ⁦format⁩ و⁦formatWithOptions⁩ و⁦inspect⁩ و ⁦stripVTControlCharacters⁩؛
  • ⁦promisify⁩ و⁦callbackify⁩ و⁦inherits⁩ و⁦deprecate⁩ و⁦debuglog⁩؛
  • ⁦isDeepStrictEqual⁩ و⁦parseArgs⁩ و⁦parseEnv⁩ و⁦styleText⁩؛
  • ⁦getSystemErrorName⁩ و⁦getSystemErrorMessage⁩ و ⁦getSystemErrorMap⁩؛
  • ⁦getCallSites⁩؛
  • المُنشئين العامين ⁦TextEncoder⁩ و⁦TextDecoder⁩.

‏⁦toUSVString()⁩ والمساعد القديم ⁦log()⁩ موجودان لكنهما يرميان خطأ تنفيذ.

أمثلة

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 }); // true

يعالج ⁦inspect()⁩ الحاويات الشائعة والمصفوفات المنمّطة والمخازن المؤقتة والدوال والمراجع الدائرية والوعود و⁦util.inspect.custom⁩. وتتوفر خيارات منها ⁦depth⁩ و⁦breakLength⁩ و⁦compact⁩ و⁦maxArrayLength⁩.

يحترم ⁦promisify()⁩ ‏⁦util.promisify.custom⁩.

util.types

تغطي عائلة المسندات المصفوفات والعروض الشائعة والتواريخ والخرائط والمجموعات والأخطاء والوعود والوكلاء والتعابير النمطية والمجموعات الضعيفة والقيم البدائية المغلَّفة وأنواع الدوال والمكرِّرات وتخصصات المصفوفات المنمّطة. وليست كل مسندات ⁦Node⁩ موجودة؛ تحقق من وجود المسندات الأحدث مثل ⁦isCryptoKey⁩ قبل استدعائها.

انظر أيضًا

Reference

Status: Partial · 9 of 18 suite tests pass (50.0%) · since 1.1.0

Exports

ExportKindStatusNotes
format()functionFullFormats a string using printf-style substitution patterns (%s, %d, %o, etc.)
formatWithOptions()functionFullLike format(), but accepts an explicit util.inspect options object for the first argument
inspect()functionFullReturns a string representation of an object for debugging
promisify()functionFullWraps a callback-based function so it returns a Promise
inherits()functionFullSets up prototype inheritance between a constructor and a superclass
isDeepStrictEqual()functionFullReturns true if two values are deeply equal using the strict equality algorithm
deprecate()functionFullWraps a function to emit a deprecation warning when called
debuglog()functionFullReturns a logging function that writes to stderr only if the NODE_DEBUG env variable includes section
callbackify()functionFullWraps an async function (or a function that returns a Promise) so it follows the error-first callback…
getSystemErrorName()functionFullReturns the string name for a numeric error code (the errno value)
getSystemErrorMessage()functionFullReturns the string message for a numeric error code (the errno value)
getSystemErrorMap()functionFullReturns a Map of all system error codes to [name, message] pairs
toUSVString()functionFullReturns a string after replacing any lone surrogates with the Unicode replacement character U+FFFD
stripVTControlCharacters()functionFullRemoves ANSI escape codes (including color and cursor movement) from a string
styleText()functionFullFormats text with ANSI styling for terminal output
getCallSites()functionFullReturns an array of call site objects representing the current call stack
parseEnv()functionFullParses a .env file content string into a key-value object
log()functionFullWrites a formatted timestamp + message to stdout (similar to console.log but with a timestamp)
typespropertyFullObject with type-checking predicates (e.g. util.types.isDate, util.types.isMap)
TextEncoderpropertyFullThe WHATWG TextEncoder class
TextDecoderpropertyFullThe 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.

هل كانت هذه الصفحة مفيدة؟