util: allow symbol-based custom inspection methods

Add a `util.inspect.custom` Symbol which can be used to customize
`util.inspect()` output. Providing `obj[util.inspect.custom]`
works like providing `obj.inspect`, except that the former allows
avoiding name clashes with other `inspect()` methods.

Fixes: https://github.com/nodejs/node/issues/8071
PR-URL: https://github.com/nodejs/node/pull/8174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>

Refs: https://github.com/nodejs/node/pull/8437
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Anna Henningsen authored and Jeremiah Senkpiel committed 2016-09-09 11:06:02 -04:00
1 parent 6f269bed99
commit 4cb55ff392
5 files changed
+109 -21

No files matched your search

+4 -2
View File
@@ -4,6 +4,7 @@
const binding = process.binding('buffer');
const { isArrayBuffer } = process.binding('util');
const bindingObj = {};
const internalUtil = require('internal/util');
class FastBuffer extends Uint8Array {}
@@ -516,8 +517,8 @@ Buffer.prototype.equals = function equals(b) {
};
// Inspect
Buffer.prototype.inspect = function inspect() {
// Override how buffers are presented by util.inspect().
Buffer.prototype[internalUtil.inspectSymbol] = function inspect() {
var str = '';
var max = exports.INSPECT_MAX_BYTES;
if (this.length > 0) {
@@ -527,6 +528,7 @@ Buffer.prototype.inspect = function inspect() {
}
return '<' + this.constructor.name + ' ' + str + '>';
};
Buffer.prototype.inspect = Buffer.prototype[internalUtil.inspectSymbol];
Buffer.prototype.compare = function compare(target,
start,