index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 (obj === global) {
  230. /* eslint-env browser */
  231. if (typeof window !== 'undefined') {
  232. return '{ [object Window] }';
  233. }
  234. return '{ [object global] }';
  235. }
  236. if (!isDate(obj) && !isRegExp(obj)) {
  237. var ys = arrObjKeys(obj, inspect);
  238. var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object;
  239. var protoTag = obj instanceof Object ? '' : 'null prototype';
  240. var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? $slice.call(toStr(obj), 8, -1) : protoTag ? 'Object' : '';
  241. var constructorTag = isPlainObject || typeof obj.constructor !== 'function' ? '' : obj.constructor.name ? obj.constructor.name + ' ' : '';
  242. var tag = constructorTag + (stringTag || protoTag ? '[' + $join.call($concat.call([], stringTag || [], protoTag || []), ': ') + '] ' : '');
  243. if (ys.length === 0) { return tag + '{}'; }
  244. if (indent) {
  245. return tag + '{' + indentedJoin(ys, indent) + '}';
  246. }
  247. return tag + '{ ' + $join.call(ys, ', ') + ' }';
  248. }
  249. return String(obj);
  250. };
  251. function wrapQuotes(s, defaultStyle, opts) {
  252. var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
  253. return quoteChar + s + quoteChar;
  254. }
  255. function quote(s) {
  256. return $replace.call(String(s), /"/g, '&quot;');
  257. }
  258. function isArray(obj) { return toStr(obj) === '[object Array]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  259. function isDate(obj) { return toStr(obj) === '[object Date]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  260. function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  261. function isError(obj) { return toStr(obj) === '[object Error]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  262. function isString(obj) { return toStr(obj) === '[object String]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  263. function isNumber(obj) { return toStr(obj) === '[object Number]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  264. function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
  265. // Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives
  266. function isSymbol(obj) {
  267. if (hasShammedSymbols) {
  268. return obj && typeof obj === 'object' && obj instanceof Symbol;
  269. }
  270. if (typeof obj === 'symbol') {
  271. return true;
  272. }
  273. if (!obj || typeof obj !== 'object' || !symToString) {
  274. return false;
  275. }
  276. try {
  277. symToString.call(obj);
  278. return true;
  279. } catch (e) {}
  280. return false;
  281. }
  282. function isBigInt(obj) {
  283. if (!obj || typeof obj !== 'object' || !bigIntValueOf) {
  284. return false;
  285. }
  286. try {
  287. bigIntValueOf.call(obj);
  288. return true;
  289. } catch (e) {}
  290. return false;
  291. }
  292. var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
  293. function has(obj, key) {
  294. return hasOwn.call(obj, key);
  295. }
  296. function toStr(obj) {
  297. return objectToString.call(obj);
  298. }
  299. function nameOf(f) {
  300. if (f.name) { return f.name; }
  301. var m = $match.call(functionToString.call(f), /^function\s*([\w$]+)/);
  302. if (m) { return m[1]; }
  303. return null;
  304. }
  305. function indexOf(xs, x) {
  306. if (xs.indexOf) { return xs.indexOf(x); }
  307. for (var i = 0, l = xs.length; i < l; i++) {
  308. if (xs[i] === x) { return i; }
  309. }
  310. return -1;
  311. }
  312. function isMap(x) {
  313. if (!mapSize || !x || typeof x !== 'object') {
  314. return false;
  315. }
  316. try {
  317. mapSize.call(x);
  318. try {
  319. setSize.call(x);
  320. } catch (s) {
  321. return true;
  322. }
  323. return x instanceof Map; // core-js workaround, pre-v2.5.0
  324. } catch (e) {}
  325. return false;
  326. }
  327. function isWeakMap(x) {
  328. if (!weakMapHas || !x || typeof x !== 'object') {
  329. return false;
  330. }
  331. try {
  332. weakMapHas.call(x, weakMapHas);
  333. try {
  334. weakSetHas.call(x, weakSetHas);
  335. } catch (s) {
  336. return true;
  337. }
  338. return x instanceof WeakMap; // core-js workaround, pre-v2.5.0
  339. } catch (e) {}
  340. return false;
  341. }
  342. function isWeakRef(x) {
  343. if (!weakRefDeref || !x || typeof x !== 'object') {
  344. return false;
  345. }
  346. try {
  347. weakRefDeref.call(x);
  348. return true;
  349. } catch (e) {}
  350. return false;
  351. }
  352. function isSet(x) {
  353. if (!setSize || !x || typeof x !== 'object') {
  354. return false;
  355. }
  356. try {
  357. setSize.call(x);
  358. try {
  359. mapSize.call(x);
  360. } catch (m) {
  361. return true;
  362. }
  363. return x instanceof Set; // core-js workaround, pre-v2.5.0
  364. } catch (e) {}
  365. return false;
  366. }
  367. function isWeakSet(x) {
  368. if (!weakSetHas || !x || typeof x !== 'object') {
  369. return false;
  370. }
  371. try {
  372. weakSetHas.call(x, weakSetHas);
  373. try {
  374. weakMapHas.call(x, weakMapHas);
  375. } catch (s) {
  376. return true;
  377. }
  378. return x instanceof WeakSet; // core-js workaround, pre-v2.5.0
  379. } catch (e) {}
  380. return false;
  381. }
  382. function isElement(x) {
  383. if (!x || typeof x !== 'object') { return false; }
  384. if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
  385. return true;
  386. }
  387. return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function';
  388. }
  389. function inspectString(str, opts) {
  390. if (str.length > opts.maxStringLength) {
  391. var remaining = str.length - opts.maxStringLength;
  392. var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : '');
  393. return inspectString($slice.call(str, 0, opts.maxStringLength), opts) + trailer;
  394. }
  395. // eslint-disable-next-line no-control-regex
  396. var s = $replace.call($replace.call(str, /(['\\])/g, '\\$1'), /[\x00-\x1f]/g, lowbyte);
  397. return wrapQuotes(s, 'single', opts);
  398. }
  399. function lowbyte(c) {
  400. var n = c.charCodeAt(0);
  401. var x = {
  402. 8: 'b',
  403. 9: 't',
  404. 10: 'n',
  405. 12: 'f',
  406. 13: 'r'
  407. }[n];
  408. if (x) { return '\\' + x; }
  409. return '\\x' + (n < 0x10 ? '0' : '') + $toUpperCase.call(n.toString(16));
  410. }
  411. function markBoxed(str) {
  412. return 'Object(' + str + ')';
  413. }
  414. function weakCollectionOf(type) {
  415. return type + ' { ? }';
  416. }
  417. function collectionOf(type, size, entries, indent) {
  418. var joinedEntries = indent ? indentedJoin(entries, indent) : $join.call(entries, ', ');
  419. return type + ' (' + size + ') {' + joinedEntries + '}';
  420. }
  421. function singleLineValues(xs) {
  422. for (var i = 0; i < xs.length; i++) {
  423. if (indexOf(xs[i], '\n') >= 0) {
  424. return false;
  425. }
  426. }
  427. return true;
  428. }
  429. function getIndent(opts, depth) {
  430. var baseIndent;
  431. if (opts.indent === '\t') {
  432. baseIndent = '\t';
  433. } else if (typeof opts.indent === 'number' && opts.indent > 0) {
  434. baseIndent = $join.call(Array(opts.indent + 1), ' ');
  435. } else {
  436. return null;
  437. }
  438. return {
  439. base: baseIndent,
  440. prev: $join.call(Array(depth + 1), baseIndent)
  441. };
  442. }
  443. function indentedJoin(xs, indent) {
  444. if (xs.length === 0) { return ''; }
  445. var lineJoiner = '\n' + indent.prev + indent.base;
  446. return lineJoiner + $join.call(xs, ',' + lineJoiner) + '\n' + indent.prev;
  447. }
  448. function arrObjKeys(obj, inspect) {
  449. var isArr = isArray(obj);
  450. var xs = [];
  451. if (isArr) {
  452. xs.length = obj.length;
  453. for (var i = 0; i < obj.length; i++) {
  454. xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
  455. }
  456. }
  457. var syms = typeof gOPS === 'function' ? gOPS(obj) : [];
  458. var symMap;
  459. if (hasShammedSymbols) {
  460. symMap = {};
  461. for (var k = 0; k < syms.length; k++) {
  462. symMap['$' + syms[k]] = syms[k];
  463. }
  464. }
  465. for (var key in obj) { // eslint-disable-line no-restricted-syntax
  466. if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
  467. if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
  468. if (hasShammedSymbols && symMap['$' + key] instanceof Symbol) {
  469. // this is to prevent shammed Symbols, which are stored as strings, from being included in the string key section
  470. continue; // eslint-disable-line no-restricted-syntax, no-continue
  471. } else if ($test.call(/[^\w$]/, key)) {
  472. xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
  473. } else {
  474. xs.push(key + ': ' + inspect(obj[key], obj));
  475. }
  476. }
  477. if (typeof gOPS === 'function') {
  478. for (var j = 0; j < syms.length; j++) {
  479. if (isEnumerable.call(obj, syms[j])) {
  480. xs.push('[' + inspect(syms[j]) + ']: ' + inspect(obj[syms[j]], obj));
  481. }
  482. }
  483. }
  484. return xs;
  485. }