index.js 16 KB

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