index.js 18 KB

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