source: node_modules/object-inspect/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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