index.js 18 KB

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