Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console, util: add dirxml method #17146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ Console.prototype.dir = function dir(object, options) {
};


Console.prototype.dirxml = function dirxml(object, options, xml = false) {
write(this._ignoreErrors,
this._stdout,
util.xmlInspect(object, options, xml),
this._stdoutErrorHandler,
this[kGroupIndent]);
};


Console.prototype.time = function time(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
Expand Down
29 changes: 29 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ function inspect(obj, opts) {
}
inspect.custom = customInspectSymbol;

/**
* Echos the value of a value. Tries to print the value out
* in the best way possible given the different types. Allows
* passing a custom inspection function for xml parsing.
*
* @param {Object} obj The object to print out
* @param {Object} opts Optional options object that alters the output.
* @param {Function|boolean} xml The custom inspect function or flag
*/
function xmlInspect(obj, opts, xml = false) {
let custom = false;
if (xml !== false ||
(opts.customInspect && opts.customInspect !== false)) {
custom = true;
if (typeof xml === 'function') {
obj[inspect.custom] = xml;
} else if (xml === true) {
if (!(obj.inspect && typeof obj.inspect === 'function')) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'xml', ['function', 'boolean']
);
}
}
}
opts = Object.assign({ customInspect: custom }, opts);
return inspect(obj, opts);
}

Object.defineProperty(inspect, 'defaultOptions', {
get() {
return inspectDefaultOptions;
Expand Down Expand Up @@ -1098,6 +1126,7 @@ module.exports = exports = {
format,
inherits,
inspect,
xmlInspect,
isArray: Array.isArray,
isBoolean,
isBuffer,
Expand Down