Skip to content

Commit

Permalink
console, util: add dirxml method
Browse files Browse the repository at this point in the history
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: nodejs#17128
  • Loading branch information
Tiriel committed Nov 20, 2017
1 parent 07a4fa3 commit b63a553
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit b63a553

Please sign in to comment.