UNPKG

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