index.js 12 KB

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