index.js 13 KB

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