index.js 15 KB

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