index.js 16 KB

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