2020-01-28 13:07:56 +08:00
|
|
|
var hasMap = typeof Map === 'function' && Map.prototype;
|
|
|
|
var mapSizeDescriptor = Object.getOwnPropertyDescriptor && hasMap ? Object.getOwnPropertyDescriptor(Map.prototype, 'size') : null;
|
|
|
|
var mapSize = hasMap && mapSizeDescriptor && typeof mapSizeDescriptor.get === 'function' ? mapSizeDescriptor.get : null;
|
|
|
|
var mapForEach = hasMap && Map.prototype.forEach;
|
|
|
|
var hasSet = typeof Set === 'function' && Set.prototype;
|
|
|
|
var setSizeDescriptor = Object.getOwnPropertyDescriptor && hasSet ? Object.getOwnPropertyDescriptor(Set.prototype, 'size') : null;
|
|
|
|
var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'function' ? setSizeDescriptor.get : null;
|
|
|
|
var setForEach = hasSet && Set.prototype.forEach;
|
2020-03-07 11:45:40 +08:00
|
|
|
var hasWeakMap = typeof WeakMap === 'function' && WeakMap.prototype;
|
|
|
|
var weakMapHas = hasWeakMap ? WeakMap.prototype.has : null;
|
|
|
|
var hasWeakSet = typeof WeakSet === 'function' && WeakSet.prototype;
|
|
|
|
var weakSetHas = hasWeakSet ? WeakSet.prototype.has : null;
|
2020-01-28 13:07:56 +08:00
|
|
|
var booleanValueOf = Boolean.prototype.valueOf;
|
|
|
|
var objectToString = Object.prototype.toString;
|
2020-03-07 11:45:40 +08:00
|
|
|
var match = String.prototype.match;
|
2020-01-28 13:07:56 +08:00
|
|
|
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
|
|
|
|
|
|
|
|
var inspectCustom = require('./util.inspect').custom;
|
2020-03-07 11:45:40 +08:00
|
|
|
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;
|
2020-01-28 13:07:56 +08:00
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
module.exports = function inspect_(obj, options, depth, seen) {
|
|
|
|
var opts = options || {};
|
2020-01-28 13:07:56 +08:00
|
|
|
|
|
|
|
if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
|
|
|
|
throw new TypeError('option "quoteStyle" must be "single" or "double"');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof obj === 'undefined') {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
if (obj === null) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
if (typeof obj === 'boolean') {
|
|
|
|
return obj ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof obj === 'string') {
|
|
|
|
return inspectString(obj, opts);
|
|
|
|
}
|
|
|
|
if (typeof obj === 'number') {
|
2020-03-07 11:45:40 +08:00
|
|
|
if (obj === 0) {
|
|
|
|
return Infinity / obj > 0 ? '0' : '-0';
|
|
|
|
}
|
|
|
|
return String(obj);
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
if (typeof obj === 'bigint') { // eslint-disable-line valid-typeof
|
|
|
|
return String(obj) + 'n';
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
|
2020-03-07 11:45:40 +08:00
|
|
|
if (typeof depth === 'undefined') { depth = 0; }
|
2020-01-28 13:07:56 +08:00
|
|
|
if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
|
|
|
|
return '[Object]';
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
if (typeof seen === 'undefined') {
|
|
|
|
seen = [];
|
|
|
|
} else if (indexOf(seen, obj) >= 0) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return '[Circular]';
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function inspect(value, from) {
|
2020-01-28 13:07:56 +08:00
|
|
|
if (from) {
|
|
|
|
seen = seen.slice();
|
|
|
|
seen.push(from);
|
|
|
|
}
|
|
|
|
return inspect_(value, opts, depth + 1, seen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof obj === 'function') {
|
|
|
|
var name = nameOf(obj);
|
|
|
|
return '[Function' + (name ? ': ' + name : '') + ']';
|
|
|
|
}
|
|
|
|
if (isSymbol(obj)) {
|
|
|
|
var symString = Symbol.prototype.toString.call(obj);
|
|
|
|
return typeof obj === 'object' ? markBoxed(symString) : symString;
|
|
|
|
}
|
|
|
|
if (isElement(obj)) {
|
|
|
|
var s = '<' + String(obj.nodeName).toLowerCase();
|
|
|
|
var attrs = obj.attributes || [];
|
|
|
|
for (var i = 0; i < attrs.length; i++) {
|
|
|
|
s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
|
|
|
|
}
|
|
|
|
s += '>';
|
2020-03-07 11:45:40 +08:00
|
|
|
if (obj.childNodes && obj.childNodes.length) { s += '...'; }
|
2020-01-28 13:07:56 +08:00
|
|
|
s += '</' + String(obj.nodeName).toLowerCase() + '>';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (isArray(obj)) {
|
2020-03-07 11:45:40 +08:00
|
|
|
if (obj.length === 0) { return '[]'; }
|
2020-01-28 13:07:56 +08:00
|
|
|
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
|
|
|
|
}
|
|
|
|
if (isError(obj)) {
|
|
|
|
var parts = arrObjKeys(obj, inspect);
|
2020-03-07 11:45:40 +08:00
|
|
|
if (parts.length === 0) { return '[' + String(obj) + ']'; }
|
2020-01-28 13:07:56 +08:00
|
|
|
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
|
|
|
|
}
|
|
|
|
if (typeof obj === 'object') {
|
|
|
|
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
|
|
|
|
return obj[inspectSymbol]();
|
|
|
|
} else if (typeof obj.inspect === 'function') {
|
|
|
|
return obj.inspect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isMap(obj)) {
|
2020-03-07 11:45:40 +08:00
|
|
|
var mapParts = [];
|
2020-01-28 13:07:56 +08:00
|
|
|
mapForEach.call(obj, function (value, key) {
|
2020-03-07 11:45:40 +08:00
|
|
|
mapParts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
|
2020-01-28 13:07:56 +08:00
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
return collectionOf('Map', mapSize.call(obj), mapParts);
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
if (isSet(obj)) {
|
2020-03-07 11:45:40 +08:00
|
|
|
var setParts = [];
|
|
|
|
setForEach.call(obj, function (value) {
|
|
|
|
setParts.push(inspect(value, obj));
|
2020-01-28 13:07:56 +08:00
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
return collectionOf('Set', setSize.call(obj), setParts);
|
|
|
|
}
|
|
|
|
if (isWeakMap(obj)) {
|
|
|
|
return weakCollectionOf('WeakMap');
|
|
|
|
}
|
|
|
|
if (isWeakSet(obj)) {
|
|
|
|
return weakCollectionOf('WeakSet');
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
if (isNumber(obj)) {
|
|
|
|
return markBoxed(inspect(Number(obj)));
|
|
|
|
}
|
|
|
|
if (isBigInt(obj)) {
|
|
|
|
return markBoxed(inspect(bigIntValueOf.call(obj)));
|
|
|
|
}
|
|
|
|
if (isBoolean(obj)) {
|
|
|
|
return markBoxed(booleanValueOf.call(obj));
|
|
|
|
}
|
|
|
|
if (isString(obj)) {
|
|
|
|
return markBoxed(inspect(String(obj)));
|
|
|
|
}
|
|
|
|
if (!isDate(obj) && !isRegExp(obj)) {
|
|
|
|
var xs = arrObjKeys(obj, inspect);
|
2020-03-07 11:45:40 +08:00
|
|
|
if (xs.length === 0) { return '{}'; }
|
2020-01-28 13:07:56 +08:00
|
|
|
return '{ ' + xs.join(', ') + ' }';
|
|
|
|
}
|
|
|
|
return String(obj);
|
|
|
|
};
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function wrapQuotes(s, defaultStyle, opts) {
|
2020-01-28 13:07:56 +08:00
|
|
|
var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
|
|
|
|
return quoteChar + s + quoteChar;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function quote(s) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return String(s).replace(/"/g, '"');
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function isArray(obj) { return toStr(obj) === '[object Array]'; }
|
|
|
|
function isDate(obj) { return toStr(obj) === '[object Date]'; }
|
|
|
|
function isRegExp(obj) { return toStr(obj) === '[object RegExp]'; }
|
|
|
|
function isError(obj) { return toStr(obj) === '[object Error]'; }
|
|
|
|
function isSymbol(obj) { return toStr(obj) === '[object Symbol]'; }
|
|
|
|
function isString(obj) { return toStr(obj) === '[object String]'; }
|
|
|
|
function isNumber(obj) { return toStr(obj) === '[object Number]'; }
|
|
|
|
function isBigInt(obj) { return toStr(obj) === '[object BigInt]'; }
|
|
|
|
function isBoolean(obj) { return toStr(obj) === '[object Boolean]'; }
|
2020-01-28 13:07:56 +08:00
|
|
|
|
|
|
|
var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
|
2020-03-07 11:45:40 +08:00
|
|
|
function has(obj, key) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return hasOwn.call(obj, key);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function toStr(obj) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return objectToString.call(obj);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function nameOf(f) {
|
|
|
|
if (f.name) { return f.name; }
|
|
|
|
var m = match.call(f, /^function\s*([\w$]+)/);
|
|
|
|
if (m) { return m[1]; }
|
|
|
|
return null;
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function indexOf(xs, x) {
|
|
|
|
if (xs.indexOf) { return xs.indexOf(x); }
|
2020-01-28 13:07:56 +08:00
|
|
|
for (var i = 0, l = xs.length; i < l; i++) {
|
2020-03-07 11:45:40 +08:00
|
|
|
if (xs[i] === x) { return i; }
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function isMap(x) {
|
|
|
|
if (!mapSize || !x || typeof x !== 'object') {
|
2020-01-28 13:07:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mapSize.call(x);
|
|
|
|
try {
|
|
|
|
setSize.call(x);
|
|
|
|
} catch (s) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return x instanceof Map; // core-js workaround, pre-v2.5.0
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function isWeakMap(x) {
|
|
|
|
if (!weakMapHas || !x || typeof x !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
weakMapHas.call(x, weakMapHas);
|
|
|
|
try {
|
|
|
|
weakSetHas.call(x, weakSetHas);
|
|
|
|
} catch (s) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return x instanceof WeakMap; // core-js workaround, pre-v2.5.0
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSet(x) {
|
|
|
|
if (!setSize || !x || typeof x !== 'object') {
|
2020-01-28 13:07:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
setSize.call(x);
|
|
|
|
try {
|
|
|
|
mapSize.call(x);
|
|
|
|
} catch (m) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return x instanceof Set; // core-js workaround, pre-v2.5.0
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function isWeakSet(x) {
|
|
|
|
if (!weakSetHas || !x || typeof x !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
weakSetHas.call(x, weakSetHas);
|
|
|
|
try {
|
|
|
|
weakMapHas.call(x, weakMapHas);
|
|
|
|
} catch (s) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return x instanceof WeakSet; // core-js workaround, pre-v2.5.0
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isElement(x) {
|
|
|
|
if (!x || typeof x !== 'object') { return false; }
|
2020-01-28 13:07:56 +08:00
|
|
|
if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function';
|
2020-01-28 13:07:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function inspectString(str, opts) {
|
|
|
|
// eslint-disable-next-line no-control-regex
|
2020-01-28 13:07:56 +08:00
|
|
|
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
|
|
|
|
return wrapQuotes(s, 'single', opts);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function lowbyte(c) {
|
2020-01-28 13:07:56 +08:00
|
|
|
var n = c.charCodeAt(0);
|
2020-03-07 11:45:40 +08:00
|
|
|
var x = {
|
|
|
|
8: 'b', 9: 't', 10: 'n', 12: 'f', 13: 'r'
|
|
|
|
}[n];
|
|
|
|
if (x) { return '\\' + x; }
|
2020-01-28 13:07:56 +08:00
|
|
|
return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function markBoxed(str) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return 'Object(' + str + ')';
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function weakCollectionOf(type) {
|
|
|
|
return type + ' { ? }';
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectionOf(type, size, entries) {
|
2020-01-28 13:07:56 +08:00
|
|
|
return type + ' (' + size + ') {' + entries.join(', ') + '}';
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:40 +08:00
|
|
|
function arrObjKeys(obj, inspect) {
|
2020-01-28 13:07:56 +08:00
|
|
|
var isArr = isArray(obj);
|
|
|
|
var xs = [];
|
|
|
|
if (isArr) {
|
|
|
|
xs.length = obj.length;
|
|
|
|
for (var i = 0; i < obj.length; i++) {
|
|
|
|
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
for (var key in obj) { // eslint-disable-line no-restricted-syntax
|
|
|
|
if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
|
|
|
|
if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
|
|
|
|
if ((/[^\w$]/).test(key)) {
|
2020-01-28 13:07:56 +08:00
|
|
|
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
|
|
|
|
} else {
|
|
|
|
xs.push(key + ': ' + inspect(obj[key], obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xs;
|
|
|
|
}
|