How to use the inspect function from util
Find comprehensive JavaScript util.inspect code examples handpicked from public code repositorys.
util.inspect is a Node.js utility function that converts any JavaScript object to a human-readable string representation.
GitHub: firewalla/firewalla
177 178 179 180 181 182 183 184 185 186
return this.localizedMessage() + this.timestamp ? " %@" : ""; } toString() { return util.inspect(this); } // toJsonObject() { // let obj = {};
236 237 238 239 240 241 242 243 244 245
// false ``` <div id="inspect" class="anchor"></div> ## util.inspect(object[, options]) 返回 `object` 的字符串表示,这对调试有用。 可通过可选选项对象改变格式化字符串的某些方面:
How does util.inspect work?
util.inspect is a built-in utility function in Node.js that provides a way to convert any JavaScript object to a string representation that can be printed to the console or logged in a file. It provides many configuration options to customize the output format, such as colors, depth of recursion, and whether or not to show non-enumerable properties. The output can be used for debugging and logging purposes to help understand the structure of complex objects and their values. The util.inspect function can be used in both synchronous and asynchronous contexts, making it a versatile and powerful tool for working with JavaScript objects in Node.js.
40 41 42 43 44 45 46 47 48 49
* * If the placeholder does not have a corresponding argument, the placeholder is * not replaced. * * If there are more arguments than placeholders, the extra arguments are * coerced to strings (for objects and symbols, util.inspect() is used) and then * concatenated, delimited by a space. * * If the first argument is not a format string then util.format() returns a * string that is the concatenation of all its arguments separated by spaces.
10 11 12 13 14 15 16 17 18 19
} function formatWithInspect(val, colorize) { const prefix = isPrimitive(val) ? '' : '\n'; const shouldFormat = typeof val !== 'string'; return prefix + (shouldFormat ? inspect(val, { depth: null, colors: colorize }) : val); } function fancyPrintf(colorize) { return function(info) { const msg = formatWithInspect(info.message, colorize);
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const util = require("util"); const obj = { name: "John", age: 30, address: { street: "123 Main St", city: "Anytown", state: "CA", }, hobbies: ["reading", "hiking", "coding"], }; console.log(util.inspect(obj));
This would output the following string to the console: css Copy code
405 406 407 408 409 410 411 412 413 414
}; Formatter.prototype.formatObject = function(token) { // If no precision is specified, then reset it to null (infinite depth). var precision = (token.period === '.') ? token.precision : null; // Historically, inspect was called with 3 options // token.arg = util.inspect(token.arg, !token.alternative, precision, token.sign); // Now using an object but not sure colors make any sense here token.arg = util.inspect(token.arg, { showHidden: !token.alternative, depth: precision,
262 263 264 265 266 267 268 269 270 271
} var resp = "<No response>"; try { resp = await mainFunc(); } catch(e) { return "ERR: Run: \n" + util.inspect(e, { colors: includeColors }); } if(typeof resp != "string" && typeof resp != "number" && typeof resp != "bigint") { resp = util.inspect(resp, { colors: includeColors }); } else {
+ 8 other calls in file
26 27 28 29 30 31 32 33 34 35
} function formatWithInspect(val) { const prefix = isPrimitive(val) ? '' : '\n'; const shouldFormat = typeof val !== 'string' && !ansiRegex.test(val); return prefix + (shouldFormat ? inspect(val, { depth: null, colors: true }) : val); } // Custom format of the logs const generateItFormat = printf((info) => {
GitHub: Eveble/eveble
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
stringifyValue(value, inspectOptions = {}, prefix = '', suffix = '') { const prefixStr = this.isPrimitive(value) ? '' : prefix; const suffixStr = this.isPrimitive(value) ? '' : suffix; const isInspectable = typeof value !== 'string' || !hasAnsi(value); return (prefixStr + (isInspectable ? util.inspect(value, inspectOptions) : value) + suffixStr); } stringifyMethodParams(entry, metadata, inspectOptions = {}) { if (entry.method === undefined) {
32 33 34 35 36 37 38 39 40 41
switch (format) { case 's': str = String(str) break case 'r': str = inspect(str) break case 'd': case 'i': if (typeof str !== 'number') {
+ 3 other calls in file
83 84 85 86 87 88 89 90 91 92 93
const assert = require('assert') const util = require('util') const fs = require('fs') const sub = require('./lib/sub') const path = require('path') const repr = util.inspect function get_argv() { // omit first argument (which is assumed to be interpreter - `node`, `coffee`, `ts-node`, etc.) return process.argv.slice(1)
+ 3 other calls in file
GitHub: lojkofob/eeditor
160 161 162 163 164 165 166 167 168 169
if (meta) { if (typeof meta !== 'object') { output += ' ' + meta; } else if (Object.keys(meta).length > 0) { output += ' ' + (options.prettyPrint ? ('\n' + util.inspect(meta, false, null, options.colorize)) : exports.serialize(meta)); } } return output;
GitHub: dangthai228/GameCaro
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
return s; } } function inspect(something) { if (functionsHaveNames || !util.isFunction(something)) { return util.inspect(something); } var rawname = getName(something); var name = rawname ? ': ' + rawname : ''; return '[Function' + name + ']';
+ 2 other calls in file
81 82 83 84 85 86 87 88 89 90 91
* Map %o to `util.inspect()`, all on a single line. */ exports.formatters.o = function(v) { this.inspectOpts.colors = this.useColors; return util.inspect(v, this.inspectOpts) .split('\n').map(function(str) { return str.trim() }).join(' '); };
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
exportTo = wfschemaWizziModel.exportTos[i]; this.addExportTo(exportTo.wzName); } var ctx = { error: function(msg, node) { throw new Error(msg + '\n' + util.inspect(node, { depth: 2})); } }; this.wzSetup(ctx); this.wzVerify(ctx);
203 204 205 206 207 208 209 210 211 212 213 214 215
} } exports.APIRequestContext = APIRequestContext; _util$inspect$custom = util.inspect.custom; class APIResponse { constructor(context, initializer) { this._initializer = void 0;
GitHub: achansen121/chess-js
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
var str = args.map(function(arg) { if (arg != undefined && arg.constructor === String) { return arg; } else { return util.inspect(arg); } }).join(' '); if (!colors.enabled || !str) {
util.promisify is the most popular function in util (378 examples)