index.js
1 var hasMap = typeof Map === 'function' && Map.prototype; 2 var mapSizeDescriptor = Object.getOwnPropertyDescriptor && hasMap ? Object.getOwnPropertyDescriptor(Map.prototype, 'size') : null; 3 var mapSize = hasMap && mapSizeDescriptor && typeof mapSizeDescriptor.get === 'function' ? mapSizeDescriptor.get : null; 4 var mapForEach = hasMap && Map.prototype.forEach; 5 var hasSet = typeof Set === 'function' && Set.prototype; 6 var setSizeDescriptor = Object.getOwnPropertyDescriptor && hasSet ? Object.getOwnPropertyDescriptor(Set.prototype, 'size') : null; 7 var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'function' ? setSizeDescriptor.get : null; 8 var setForEach = hasSet && Set.prototype.forEach; 9 var hasWeakMap = typeof WeakMap === 'function' && WeakMap.prototype; 10 var weakMapHas = hasWeakMap ? WeakMap.prototype.has : null; 11 var hasWeakSet = typeof WeakSet === 'function' && WeakSet.prototype; 12 var weakSetHas = hasWeakSet ? WeakSet.prototype.has : null; 13 var hasWeakRef = typeof WeakRef === 'function' && WeakRef.prototype; 14 var weakRefDeref = hasWeakRef ? WeakRef.prototype.deref : null; 15 var booleanValueOf = Boolean.prototype.valueOf; 16 var objectToString = Object.prototype.toString; 17 var functionToString = Function.prototype.toString; 18 var $match = String.prototype.match; 19 var $slice = String.prototype.slice; 20 var $replace = String.prototype.replace; 21 var $toUpperCase = String.prototype.toUpperCase; 22 var $toLowerCase = String.prototype.toLowerCase; 23 var $test = RegExp.prototype.test; 24 var $concat = Array.prototype.concat; 25 var $join = Array.prototype.join; 26 var $arrSlice = Array.prototype.slice; 27 var $floor = Math.floor; 28 var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null; 29 var gOPS = Object.getOwnPropertySymbols; 30 var symToString = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? Symbol.prototype.toString : null; 31 var hasShammedSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'object'; 32 // ie, `has-tostringtag/shams 33 var toStringTag = typeof Symbol === 'function' && Symbol.toStringTag && (typeof Symbol.toStringTag === hasShammedSymbols ? 'object' : 'symbol') 34 ? Symbol.toStringTag 35 : null; 36 var isEnumerable = Object.prototype.propertyIsEnumerable; 37 38 var gPO = (typeof Reflect === 'function' ? Reflect.getPrototypeOf : Object.getPrototypeOf) || ( 39 [].__proto__ === Array.prototype // eslint-disable-line no-proto 40 ? function (O) { 41 return O.__proto__; // eslint-disable-line no-proto 42 } 43 : null 44 ); 45 46 function addNumericSeparator(num, str) { 47 if ( 48 num === Infinity 49 || num === -Infinity 50 || num !== num 51 || (num && num > -1000 && num < 1000) 52 || $test.call(/e/, str) 53 ) { 54 return str; 55 } 56 var sepRegex = /[0-9](?=(?:[0-9]{3})+(?![0-9]))/g; 57 if (typeof num === 'number') { 58 var int = num < 0 ? -$floor(-num) : $floor(num); // trunc(num) 59 if (int !== num) { 60 var intStr = String(int); 61 var dec = $slice.call(str, intStr.length + 1); 62 return $replace.call(intStr, sepRegex, '$&_') + '.' + $replace.call($replace.call(dec, /([0-9]{3})/g, '$&_'), /_$/, ''); 63 } 64 } 65 return $replace.call(str, sepRegex, '$&_'); 66 } 67 68 var utilInspect = require('./util.inspect'); 69 var inspectCustom = utilInspect.custom; 70 var inspectSymbol = isSymbol(inspectCustom) ? inspectCustom : null; 71 72 var quotes = { 73 __proto__: null, 74 'double': '"', 75 single: "'" 76 }; 77 var quoteREs = { 78 __proto__: null, 79 'double': /(["\\])/g, 80 single: /(['\\])/g 81 }; 82 83 module.exports = function inspect_(obj, options, depth, seen) { 84 var opts = options || {}; 85 86 if (has(opts, 'quoteStyle') && !has(quotes, opts.quoteStyle)) { 87 throw new TypeError('option "quoteStyle" must be "single" or "double"'); 88 } 89 if ( 90 has(opts, 'maxStringLength') && (typeof opts.maxStringLength === 'number' 91 ? opts.maxStringLength < 0 && opts.maxStringLength !== Infinity 92 : opts.maxStringLength !== null 93 ) 94 ) { 95 throw new TypeError('option "maxStringLength", if provided, must be a positive integer, Infinity, or `null`'); 96 } 97 var customInspect = has(opts, 'customInspect') ? opts.customInspect : true; 98 if (typeof customInspect !== 'boolean' && customInspect !== 'symbol') { 99 throw new TypeError('option "customInspect", if provided, must be `true`, `false`, or `\'symbol\'`'); 100 } 101 102 if ( 103 has(opts, 'indent') 104 && opts.indent !== null 105 && opts.indent !== '\t' 106 && !(parseInt(opts.indent, 10) === opts.indent && opts.indent > 0) 107 ) { 108 throw new TypeError('option "indent" must be "\\t", an integer > 0, or `null`'); 109 } 110 if (has(opts, 'numericSeparator') && typeof opts.numericSeparator !== 'boolean') { 111 throw new TypeError('option "numericSeparator", if provided, must be `true` or `false`'); 112 } 113 var numericSeparator = opts.numericSeparator; 114 115 if (typeof obj === 'undefined') { 116 return 'undefined'; 117 } 118 if (obj === null) { 119 return 'null'; 120 } 121 if (typeof obj === 'boolean') { 122 return obj ? 'true' : 'false'; 123 } 124 125 if (typeof obj === 'string') { 126 return inspectString(obj, opts); 127 } 128 if (typeof obj === 'number') { 129 if (obj === 0) { 130 return Infinity / obj > 0 ? '0' : '-0'; 131 } 132 var str = String(obj); 133 return numericSeparator ? addNumericSeparator(obj, str) : str; 134 } 135 if (typeof obj === 'bigint') { 136 var bigIntStr = String(obj) + 'n'; 137 return numericSeparator ? addNumericSeparator(obj, bigIntStr) : bigIntStr; 138 } 139 140 var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth; 141 if (typeof depth === 'undefined') { depth = 0; } 142 if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') { 143 return isArray(obj) ? '[Array]' : '[Object]'; 144 } 145 146 var indent = getIndent(opts, depth); 147 148 if (typeof seen === 'undefined') { 149 seen = []; 150 } else if (indexOf(seen, obj) >= 0) { 151 return '[Circular]'; 152 } 153 154 function inspect(value, from, noIndent) { 155 if (from) { 156 seen = $arrSlice.call(seen); 157 seen.push(from); 158 } 159 if (noIndent) { 160 var newOpts = { 161 depth: opts.depth 162 }; 163 if (has(opts, 'quoteStyle')) { 164 newOpts.quoteStyle = opts.quoteStyle; 165 } 166 return inspect_(value, newOpts, depth + 1, seen); 167 } 168 return inspect_(value, opts, depth + 1, seen); 169 } 170 171 if (typeof obj === 'function' && !isRegExp(obj)) { // in older engines, regexes are callable 172 var name = nameOf(obj); 173 var keys = arrObjKeys(obj, inspect); 174 return '[Function' + (name ? ': ' + name : ' (anonymous)') + ']' + (keys.length > 0 ? ' { ' + $join.call(keys, ', ') + ' }' : ''); 175 } 176 if (isSymbol(obj)) { 177 var symString = hasShammedSymbols ? $replace.call(String(obj), /^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj); 178 return typeof obj === 'object' && !hasShammedSymbols ? markBoxed(symString) : symString; 179 } 180 if (isElement(obj)) { 181 var s = '<' + $toLowerCase.call(String(obj.nodeName)); 182 var attrs = obj.attributes || []; 183 for (var i = 0; i < attrs.length; i++) { 184 s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts); 185 } 186 s += '>'; 187 if (obj.childNodes && obj.childNodes.length) { s += '...'; } 188 s += '</' + $toLowerCase.call(String(obj.nodeName)) + '>'; 189 return s; 190 } 191 if (isArray(obj)) { 192 if (obj.length === 0) { return '[]'; } 193 var xs = arrObjKeys(obj, inspect); 194 if (indent && !singleLineValues(xs)) { 195 return '[' + indentedJoin(xs, indent) + ']'; 196 } 197 return '[ ' + $join.call(xs, ', ') + ' ]'; 198 } 199 if (isError(obj)) { 200 var parts = arrObjKeys(obj, inspect); 201 if (!('cause' in Error.prototype) && 'cause' in obj && !isEnumerable.call(obj, 'cause')) { 202 return '{ [' + String(obj) + '] ' + $join.call($concat.call('[cause]: ' + inspect(obj.cause), parts), ', ') + ' }'; 203 } 204 if (parts.length === 0) { return '[' + String(obj) + ']'; } 205 return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }'; 206 } 207 if (typeof obj === 'object' && customInspect) { 208 if (inspectSymbol && typeof obj[inspectSymbol] === 'function' && utilInspect) { 209 return utilInspect(obj, { depth: maxDepth - depth }); 210 } else if (customInspect !== 'symbol' && typeof obj.inspect === 'function') { 211 return obj.inspect(); 212 } 213 } 214 if (isMap(obj)) { 215 var mapParts = []; 216 if (mapForEach) { 217 mapForEach.call(obj, function (value, key) { 218 mapParts.push(inspect(key, obj, true) + ' => ' + inspect(value, obj)); 219 }); 220 } 221 return collectionOf('Map', mapSize.call(obj), mapParts, indent); 222 } 223 if (isSet(obj)) { 224 var setParts = []; 225 if (setForEach) { 226 setForEach.call(obj, function (value) { 227 setParts.push(inspect(value, obj)); 228 }); 229 } 230 return collectionOf('Set', setSize.call(obj), setParts, indent); 231 } 232 if (isWeakMap(obj)) { 233 return weakCollectionOf('WeakMap'); 234 } 235 if (isWeakSet(obj)) { 236 return weakCollectionOf('WeakSet'); 237 } 238 if (isWeakRef(obj)) { 239 return weakCollectionOf('WeakRef'); 240 } 241 if (isNumber(obj)) { 242 return markBoxed(inspect(Number(obj))); 243 } 244 if (isBigInt(obj)) { 245 return markBoxed(inspect(bigIntValueOf.call(obj))); 246 } 247 if (isBoolean(obj)) { 248 return markBoxed(booleanValueOf.call(obj)); 249 } 250 if (isString(obj)) { 251 return markBoxed(inspect(String(obj))); 252 } 253 // note: in IE 8, sometimes `global !== window` but both are the prototypes of each other 254 /* eslint-env browser */ 255 if (typeof window !== 'undefined' && obj === window) { 256 return '{ [object Window] }'; 257 } 258 if ( 259 (typeof globalThis !== 'undefined' && obj === globalThis) 260 || (typeof global !== 'undefined' && obj === global) 261 ) { 262 return '{ [object globalThis] }'; 263 } 264 if (!isDate(obj) && !isRegExp(obj)) { 265 var ys = arrObjKeys(obj, inspect); 266 var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object; 267 var protoTag = obj instanceof Object ? '' : 'null prototype'; 268 var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? $slice.call(toStr(obj), 8, -1) : protoTag ? 'Object' : ''; 269 var constructorTag = isPlainObject || typeof obj.constructor !== 'function' ? '' : obj.constructor.name ? obj.constructor.name + ' ' : ''; 270 var tag = constructorTag + (stringTag || protoTag ? '[' + $join.call($concat.call([], stringTag || [], protoTag || []), ': ') + '] ' : ''); 271 if (ys.length === 0) { return tag + '{}'; } 272 if (indent) { 273 return tag + '{' + indentedJoin(ys, indent) + '}'; 274 } 275 return tag + '{ ' + $join.call(ys, ', ') + ' }'; 276 } 277 return String(obj); 278 }; 279 280 function wrapQuotes(s, defaultStyle, opts) { 281 var style = opts.quoteStyle || defaultStyle; 282 var quoteChar = quotes[style]; 283 return quoteChar + s + quoteChar; 284 } 285 286 function quote(s) { 287 return $replace.call(String(s), /"/g, '"'); 288 } 289 290 function canTrustToString(obj) { 291 return !toStringTag || !(typeof obj === 'object' && (toStringTag in obj || typeof obj[toStringTag] !== 'undefined')); 292 } 293 function isArray(obj) { return toStr(obj) === '[object Array]' && canTrustToString(obj); } 294 function isDate(obj) { return toStr(obj) === '[object Date]' && canTrustToString(obj); } 295 function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && canTrustToString(obj); } 296 function isError(obj) { return toStr(obj) === '[object Error]' && canTrustToString(obj); } 297 function isString(obj) { return toStr(obj) === '[object String]' && canTrustToString(obj); } 298 function isNumber(obj) { return toStr(obj) === '[object Number]' && canTrustToString(obj); } 299 function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && canTrustToString(obj); } 300 301 // Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives 302 function isSymbol(obj) { 303 if (hasShammedSymbols) { 304 return obj && typeof obj === 'object' && obj instanceof Symbol; 305 } 306 if (typeof obj === 'symbol') { 307 return true; 308 } 309 if (!obj || typeof obj !== 'object' || !symToString) { 310 return false; 311 } 312 try { 313 symToString.call(obj); 314 return true; 315 } catch (e) {} 316 return false; 317 } 318 319 function isBigInt(obj) { 320 if (!obj || typeof obj !== 'object' || !bigIntValueOf) { 321 return false; 322 } 323 try { 324 bigIntValueOf.call(obj); 325 return true; 326 } catch (e) {} 327 return false; 328 } 329 330 var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; }; 331 function has(obj, key) { 332 return hasOwn.call(obj, key); 333 } 334 335 function toStr(obj) { 336 return objectToString.call(obj); 337 } 338 339 function nameOf(f) { 340 if (f.name) { return f.name; } 341 var m = $match.call(functionToString.call(f), /^function\s*([\w$]+)/); 342 if (m) { return m[1]; } 343 return null; 344 } 345 346 function indexOf(xs, x) { 347 if (xs.indexOf) { return xs.indexOf(x); } 348 for (var i = 0, l = xs.length; i < l; i++) { 349 if (xs[i] === x) { return i; } 350 } 351 return -1; 352 } 353 354 function isMap(x) { 355 if (!mapSize || !x || typeof x !== 'object') { 356 return false; 357 } 358 try { 359 mapSize.call(x); 360 try { 361 setSize.call(x); 362 } catch (s) { 363 return true; 364 } 365 return x instanceof Map; // core-js workaround, pre-v2.5.0 366 } catch (e) {} 367 return false; 368 } 369 370 function isWeakMap(x) { 371 if (!weakMapHas || !x || typeof x !== 'object') { 372 return false; 373 } 374 try { 375 weakMapHas.call(x, weakMapHas); 376 try { 377 weakSetHas.call(x, weakSetHas); 378 } catch (s) { 379 return true; 380 } 381 return x instanceof WeakMap; // core-js workaround, pre-v2.5.0 382 } catch (e) {} 383 return false; 384 } 385 386 function isWeakRef(x) { 387 if (!weakRefDeref || !x || typeof x !== 'object') { 388 return false; 389 } 390 try { 391 weakRefDeref.call(x); 392 return true; 393 } catch (e) {} 394 return false; 395 } 396 397 function isSet(x) { 398 if (!setSize || !x || typeof x !== 'object') { 399 return false; 400 } 401 try { 402 setSize.call(x); 403 try { 404 mapSize.call(x); 405 } catch (m) { 406 return true; 407 } 408 return x instanceof Set; // core-js workaround, pre-v2.5.0 409 } catch (e) {} 410 return false; 411 } 412 413 function isWeakSet(x) { 414 if (!weakSetHas || !x || typeof x !== 'object') { 415 return false; 416 } 417 try { 418 weakSetHas.call(x, weakSetHas); 419 try { 420 weakMapHas.call(x, weakMapHas); 421 } catch (s) { 422 return true; 423 } 424 return x instanceof WeakSet; // core-js workaround, pre-v2.5.0 425 } catch (e) {} 426 return false; 427 } 428 429 function isElement(x) { 430 if (!x || typeof x !== 'object') { return false; } 431 if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) { 432 return true; 433 } 434 return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function'; 435 } 436 437 function inspectString(str, opts) { 438 if (str.length > opts.maxStringLength) { 439 var remaining = str.length - opts.maxStringLength; 440 var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : ''); 441 return inspectString($slice.call(str, 0, opts.maxStringLength), opts) + trailer; 442 } 443 var quoteRE = quoteREs[opts.quoteStyle || 'single']; 444 quoteRE.lastIndex = 0; 445 // eslint-disable-next-line no-control-regex 446 var s = $replace.call($replace.call(str, quoteRE, '\\$1'), /[\x00-\x1f]/g, lowbyte); 447 return wrapQuotes(s, 'single', opts); 448 } 449 450 function lowbyte(c) { 451 var n = c.charCodeAt(0); 452 var x = { 453 8: 'b', 454 9: 't', 455 10: 'n', 456 12: 'f', 457 13: 'r' 458 }[n]; 459 if (x) { return '\\' + x; } 460 return '\\x' + (n < 0x10 ? '0' : '') + $toUpperCase.call(n.toString(16)); 461 } 462 463 function markBoxed(str) { 464 return 'Object(' + str + ')'; 465 } 466 467 function weakCollectionOf(type) { 468 return type + ' { ? }'; 469 } 470 471 function collectionOf(type, size, entries, indent) { 472 var joinedEntries = indent ? indentedJoin(entries, indent) : $join.call(entries, ', '); 473 return type + ' (' + size + ') {' + joinedEntries + '}'; 474 } 475 476 function singleLineValues(xs) { 477 for (var i = 0; i < xs.length; i++) { 478 if (indexOf(xs[i], '\n') >= 0) { 479 return false; 480 } 481 } 482 return true; 483 } 484 485 function getIndent(opts, depth) { 486 var baseIndent; 487 if (opts.indent === '\t') { 488 baseIndent = '\t'; 489 } else if (typeof opts.indent === 'number' && opts.indent > 0) { 490 baseIndent = $join.call(Array(opts.indent + 1), ' '); 491 } else { 492 return null; 493 } 494 return { 495 base: baseIndent, 496 prev: $join.call(Array(depth + 1), baseIndent) 497 }; 498 } 499 500 function indentedJoin(xs, indent) { 501 if (xs.length === 0) { return ''; } 502 var lineJoiner = '\n' + indent.prev + indent.base; 503 return lineJoiner + $join.call(xs, ',' + lineJoiner) + '\n' + indent.prev; 504 } 505 506 function arrObjKeys(obj, inspect) { 507 var isArr = isArray(obj); 508 var xs = []; 509 if (isArr) { 510 xs.length = obj.length; 511 for (var i = 0; i < obj.length; i++) { 512 xs[i] = has(obj, i) ? inspect(obj[i], obj) : ''; 513 } 514 } 515 var syms = typeof gOPS === 'function' ? gOPS(obj) : []; 516 var symMap; 517 if (hasShammedSymbols) { 518 symMap = {}; 519 for (var k = 0; k < syms.length; k++) { 520 symMap['$' + syms[k]] = syms[k]; 521 } 522 } 523 524 for (var key in obj) { // eslint-disable-line no-restricted-syntax 525 if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue 526 if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue 527 if (hasShammedSymbols && symMap['$' + key] instanceof Symbol) { 528 // this is to prevent shammed Symbols, which are stored as strings, from being included in the string key section 529 continue; // eslint-disable-line no-restricted-syntax, no-continue 530 } else if ($test.call(/[^\w$]/, key)) { 531 xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj)); 532 } else { 533 xs.push(key + ': ' + inspect(obj[key], obj)); 534 } 535 } 536 if (typeof gOPS === 'function') { 537 for (var j = 0; j < syms.length; j++) { 538 if (isEnumerable.call(obj, syms[j])) { 539 xs.push('[' + inspect(syms[j]) + ']: ' + inspect(obj[syms[j]], obj)); 540 } 541 } 542 } 543 return xs; 544 }