Skip to content

Commit b63a553

Browse files
committed
console, util: add dirxml method
This is an absolute first draft. This method was previously exposed by V8 (since Node v8.0.0) and not implemented in Node directly. Tests coming soon. Refs: #17128
1 parent 07a4fa3 commit b63a553

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/console.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ Console.prototype.dir = function dir(object, options) {
162162
};
163163

164164

165+
Console.prototype.dirxml = function dirxml(object, options, xml = false) {
166+
write(this._ignoreErrors,
167+
this._stdout,
168+
util.xmlInspect(object, options, xml),
169+
this._stdoutErrorHandler,
170+
this[kGroupIndent]);
171+
};
172+
173+
165174
Console.prototype.time = function time(label = 'default') {
166175
// Coerces everything other than Symbol to a string
167176
label = `${label}`;

lib/util.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,34 @@ function inspect(obj, opts) {
299299
}
300300
inspect.custom = customInspectSymbol;
301301

302+
/**
303+
* Echos the value of a value. Tries to print the value out
304+
* in the best way possible given the different types. Allows
305+
* passing a custom inspection function for xml parsing.
306+
*
307+
* @param {Object} obj The object to print out
308+
* @param {Object} opts Optional options object that alters the output.
309+
* @param {Function|boolean} xml The custom inspect function or flag
310+
*/
311+
function xmlInspect(obj, opts, xml = false) {
312+
let custom = false;
313+
if (xml !== false ||
314+
(opts.customInspect && opts.customInspect !== false)) {
315+
custom = true;
316+
if (typeof xml === 'function') {
317+
obj[inspect.custom] = xml;
318+
} else if (xml === true) {
319+
if (!(obj.inspect && typeof obj.inspect === 'function')) {
320+
throw new errors.TypeError(
321+
'ERR_INVALID_ARG_TYPE', 'xml', ['function', 'boolean']
322+
);
323+
}
324+
}
325+
}
326+
opts = Object.assign({ customInspect: custom }, opts);
327+
return inspect(obj, opts);
328+
}
329+
302330
Object.defineProperty(inspect, 'defaultOptions', {
303331
get() {
304332
return inspectDefaultOptions;
@@ -1098,6 +1126,7 @@ module.exports = exports = {
10981126
format,
10991127
inherits,
11001128
inspect,
1129+
xmlInspect,
11011130
isArray: Array.isArray,
11021131
isBoolean,
11031132
isBuffer,

0 commit comments

Comments
 (0)