source: imaps-frontend/node_modules/object-inspect/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 18.6 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 (
248 (typeof globalThis !== 'undefined' && obj === globalThis)
249 || (typeof global !== 'undefined' && obj === global)
250 ) {
251 return '{ [object globalThis] }';
252 }
253 if (!isDate(obj) && !isRegExp(obj)) {
254 var ys = arrObjKeys(obj, inspect);
255 var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object;
256 var protoTag = obj instanceof Object ? '' : 'null prototype';
257 var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? $slice.call(toStr(obj), 8, -1) : protoTag ? 'Object' : '';
258 var constructorTag = isPlainObject || typeof obj.constructor !== 'function' ? '' : obj.constructor.name ? obj.constructor.name + ' ' : '';
259 var tag = constructorTag + (stringTag || protoTag ? '[' + $join.call($concat.call([], stringTag || [], protoTag || []), ': ') + '] ' : '');
260 if (ys.length === 0) { return tag + '{}'; }
261 if (indent) {
262 return tag + '{' + indentedJoin(ys, indent) + '}';
263 }
264 return tag + '{ ' + $join.call(ys, ', ') + ' }';
265 }
266 return String(obj);
267};
268
269function wrapQuotes(s, defaultStyle, opts) {
270 var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
271 return quoteChar + s + quoteChar;
272}
273
274function quote(s) {
275 return $replace.call(String(s), /"/g, '&quot;');
276}
277
278function isArray(obj) { return toStr(obj) === '[object Array]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
279function isDate(obj) { return toStr(obj) === '[object Date]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
280function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
281function isError(obj) { return toStr(obj) === '[object Error]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
282function isString(obj) { return toStr(obj) === '[object String]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
283function isNumber(obj) { return toStr(obj) === '[object Number]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
284function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
285
286// Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives
287function isSymbol(obj) {
288 if (hasShammedSymbols) {
289 return obj && typeof obj === 'object' && obj instanceof Symbol;
290 }
291 if (typeof obj === 'symbol') {
292 return true;
293 }
294 if (!obj || typeof obj !== 'object' || !symToString) {
295 return false;
296 }
297 try {
298 symToString.call(obj);
299 return true;
300 } catch (e) {}
301 return false;
302}
303
304function isBigInt(obj) {
305 if (!obj || typeof obj !== 'object' || !bigIntValueOf) {
306 return false;
307 }
308 try {
309 bigIntValueOf.call(obj);
310 return true;
311 } catch (e) {}
312 return false;
313}
314
315var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
316function has(obj, key) {
317 return hasOwn.call(obj, key);
318}
319
320function toStr(obj) {
321 return objectToString.call(obj);
322}
323
324function nameOf(f) {
325 if (f.name) { return f.name; }
326 var m = $match.call(functionToString.call(f), /^function\s*([\w$]+)/);
327 if (m) { return m[1]; }
328 return null;
329}
330
331function indexOf(xs, x) {
332 if (xs.indexOf) { return xs.indexOf(x); }
333 for (var i = 0, l = xs.length; i < l; i++) {
334 if (xs[i] === x) { return i; }
335 }
336 return -1;
337}
338
339function isMap(x) {
340 if (!mapSize || !x || typeof x !== 'object') {
341 return false;
342 }
343 try {
344 mapSize.call(x);
345 try {
346 setSize.call(x);
347 } catch (s) {
348 return true;
349 }
350 return x instanceof Map; // core-js workaround, pre-v2.5.0
351 } catch (e) {}
352 return false;
353}
354
355function isWeakMap(x) {
356 if (!weakMapHas || !x || typeof x !== 'object') {
357 return false;
358 }
359 try {
360 weakMapHas.call(x, weakMapHas);
361 try {
362 weakSetHas.call(x, weakSetHas);
363 } catch (s) {
364 return true;
365 }
366 return x instanceof WeakMap; // core-js workaround, pre-v2.5.0
367 } catch (e) {}
368 return false;
369}
370
371function isWeakRef(x) {
372 if (!weakRefDeref || !x || typeof x !== 'object') {
373 return false;
374 }
375 try {
376 weakRefDeref.call(x);
377 return true;
378 } catch (e) {}
379 return false;
380}
381
382function isSet(x) {
383 if (!setSize || !x || typeof x !== 'object') {
384 return false;
385 }
386 try {
387 setSize.call(x);
388 try {
389 mapSize.call(x);
390 } catch (m) {
391 return true;
392 }
393 return x instanceof Set; // core-js workaround, pre-v2.5.0
394 } catch (e) {}
395 return false;
396}
397
398function isWeakSet(x) {
399 if (!weakSetHas || !x || typeof x !== 'object') {
400 return false;
401 }
402 try {
403 weakSetHas.call(x, weakSetHas);
404 try {
405 weakMapHas.call(x, weakMapHas);
406 } catch (s) {
407 return true;
408 }
409 return x instanceof WeakSet; // core-js workaround, pre-v2.5.0
410 } catch (e) {}
411 return false;
412}
413
414function isElement(x) {
415 if (!x || typeof x !== 'object') { return false; }
416 if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
417 return true;
418 }
419 return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function';
420}
421
422function inspectString(str, opts) {
423 if (str.length > opts.maxStringLength) {
424 var remaining = str.length - opts.maxStringLength;
425 var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : '');
426 return inspectString($slice.call(str, 0, opts.maxStringLength), opts) + trailer;
427 }
428 // eslint-disable-next-line no-control-regex
429 var s = $replace.call($replace.call(str, /(['\\])/g, '\\$1'), /[\x00-\x1f]/g, lowbyte);
430 return wrapQuotes(s, 'single', opts);
431}
432
433function lowbyte(c) {
434 var n = c.charCodeAt(0);
435 var x = {
436 8: 'b',
437 9: 't',
438 10: 'n',
439 12: 'f',
440 13: 'r'
441 }[n];
442 if (x) { return '\\' + x; }
443 return '\\x' + (n < 0x10 ? '0' : '') + $toUpperCase.call(n.toString(16));
444}
445
446function markBoxed(str) {
447 return 'Object(' + str + ')';
448}
449
450function weakCollectionOf(type) {
451 return type + ' { ? }';
452}
453
454function collectionOf(type, size, entries, indent) {
455 var joinedEntries = indent ? indentedJoin(entries, indent) : $join.call(entries, ', ');
456 return type + ' (' + size + ') {' + joinedEntries + '}';
457}
458
459function singleLineValues(xs) {
460 for (var i = 0; i < xs.length; i++) {
461 if (indexOf(xs[i], '\n') >= 0) {
462 return false;
463 }
464 }
465 return true;
466}
467
468function getIndent(opts, depth) {
469 var baseIndent;
470 if (opts.indent === '\t') {
471 baseIndent = '\t';
472 } else if (typeof opts.indent === 'number' && opts.indent > 0) {
473 baseIndent = $join.call(Array(opts.indent + 1), ' ');
474 } else {
475 return null;
476 }
477 return {
478 base: baseIndent,
479 prev: $join.call(Array(depth + 1), baseIndent)
480 };
481}
482
483function indentedJoin(xs, indent) {
484 if (xs.length === 0) { return ''; }
485 var lineJoiner = '\n' + indent.prev + indent.base;
486 return lineJoiner + $join.call(xs, ',' + lineJoiner) + '\n' + indent.prev;
487}
488
489function arrObjKeys(obj, inspect) {
490 var isArr = isArray(obj);
491 var xs = [];
492 if (isArr) {
493 xs.length = obj.length;
494 for (var i = 0; i < obj.length; i++) {
495 xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
496 }
497 }
498 var syms = typeof gOPS === 'function' ? gOPS(obj) : [];
499 var symMap;
500 if (hasShammedSymbols) {
501 symMap = {};
502 for (var k = 0; k < syms.length; k++) {
503 symMap['$' + syms[k]] = syms[k];
504 }
505 }
506
507 for (var key in obj) { // eslint-disable-line no-restricted-syntax
508 if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
509 if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
510 if (hasShammedSymbols && symMap['$' + key] instanceof Symbol) {
511 // this is to prevent shammed Symbols, which are stored as strings, from being included in the string key section
512 continue; // eslint-disable-line no-restricted-syntax, no-continue
513 } else if ($test.call(/[^\w$]/, key)) {
514 xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
515 } else {
516 xs.push(key + ': ' + inspect(obj[key], obj));
517 }
518 }
519 if (typeof gOPS === 'function') {
520 for (var j = 0; j < syms.length; j++) {
521 if (isEnumerable.call(obj, syms[j])) {
522 xs.push('[' + inspect(syms[j]) + ']: ' + inspect(obj[syms[j]], obj));
523 }
524 }
525 }
526 return xs;
527}
Note: See TracBrowser for help on using the repository browser.