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