index.js 18 KB

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