Projekt

Obecné

Profil

Stáhnout (9.61 KB) Statistiky
| Větev: | Revize:
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 match = String.prototype.match;
16
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
17

    
18
var inspectCustom = require('./util.inspect').custom;
19
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;
20

    
21
module.exports = function inspect_(obj, options, depth, seen) {
22
    var opts = options || {};
23

    
24
    if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
25
        throw new TypeError('option "quoteStyle" must be "single" or "double"');
26
    }
27

    
28
    if (typeof obj === 'undefined') {
29
        return 'undefined';
30
    }
31
    if (obj === null) {
32
        return 'null';
33
    }
34
    if (typeof obj === 'boolean') {
35
        return obj ? 'true' : 'false';
36
    }
37

    
38
    if (typeof obj === 'string') {
39
        return inspectString(obj, opts);
40
    }
41
    if (typeof obj === 'number') {
42
        if (obj === 0) {
43
            return Infinity / obj > 0 ? '0' : '-0';
44
        }
45
        return String(obj);
46
    }
47
    if (typeof obj === 'bigint') { // eslint-disable-line valid-typeof
48
        return String(obj) + 'n';
49
    }
50

    
51
    var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
52
    if (typeof depth === 'undefined') { depth = 0; }
53
    if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
54
        return '[Object]';
55
    }
56

    
57
    if (typeof seen === 'undefined') {
58
        seen = [];
59
    } else if (indexOf(seen, obj) >= 0) {
60
        return '[Circular]';
61
    }
62

    
63
    function inspect(value, from) {
64
        if (from) {
65
            seen = seen.slice();
66
            seen.push(from);
67
        }
68
        return inspect_(value, opts, depth + 1, seen);
69
    }
70

    
71
    if (typeof obj === 'function') {
72
        var name = nameOf(obj);
73
        return '[Function' + (name ? ': ' + name : '') + ']';
74
    }
75
    if (isSymbol(obj)) {
76
        var symString = Symbol.prototype.toString.call(obj);
77
        return typeof obj === 'object' ? markBoxed(symString) : symString;
78
    }
79
    if (isElement(obj)) {
80
        var s = '<' + String(obj.nodeName).toLowerCase();
81
        var attrs = obj.attributes || [];
82
        for (var i = 0; i < attrs.length; i++) {
83
            s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
84
        }
85
        s += '>';
86
        if (obj.childNodes && obj.childNodes.length) { s += '...'; }
87
        s += '</' + String(obj.nodeName).toLowerCase() + '>';
88
        return s;
89
    }
90
    if (isArray(obj)) {
91
        if (obj.length === 0) { return '[]'; }
92
        return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
93
    }
94
    if (isError(obj)) {
95
        var parts = arrObjKeys(obj, inspect);
96
        if (parts.length === 0) { return '[' + String(obj) + ']'; }
97
        return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
98
    }
99
    if (typeof obj === 'object') {
100
        if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
101
            return obj[inspectSymbol]();
102
        } else if (typeof obj.inspect === 'function') {
103
            return obj.inspect();
104
        }
105
    }
106
    if (isMap(obj)) {
107
        var mapParts = [];
108
        mapForEach.call(obj, function (value, key) {
109
            mapParts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
110
        });
111
        return collectionOf('Map', mapSize.call(obj), mapParts);
112
    }
113
    if (isSet(obj)) {
114
        var setParts = [];
115
        setForEach.call(obj, function (value) {
116
            setParts.push(inspect(value, obj));
117
        });
118
        return collectionOf('Set', setSize.call(obj), setParts);
119
    }
120
    if (isWeakMap(obj)) {
121
        return weakCollectionOf('WeakMap');
122
    }
123
    if (isWeakSet(obj)) {
124
        return weakCollectionOf('WeakSet');
125
    }
126
    if (isNumber(obj)) {
127
        return markBoxed(inspect(Number(obj)));
128
    }
129
    if (isBigInt(obj)) {
130
        return markBoxed(inspect(bigIntValueOf.call(obj)));
131
    }
132
    if (isBoolean(obj)) {
133
        return markBoxed(booleanValueOf.call(obj));
134
    }
135
    if (isString(obj)) {
136
        return markBoxed(inspect(String(obj)));
137
    }
138
    if (!isDate(obj) && !isRegExp(obj)) {
139
        var xs = arrObjKeys(obj, inspect);
140
        if (xs.length === 0) { return '{}'; }
141
        return '{ ' + xs.join(', ') + ' }';
142
    }
143
    return String(obj);
144
};
145

    
146
function wrapQuotes(s, defaultStyle, opts) {
147
    var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
148
    return quoteChar + s + quoteChar;
149
}
150

    
151
function quote(s) {
152
    return String(s).replace(/"/g, '&quot;');
153
}
154

    
155
function isArray(obj) { return toStr(obj) === '[object Array]'; }
156
function isDate(obj) { return toStr(obj) === '[object Date]'; }
157
function isRegExp(obj) { return toStr(obj) === '[object RegExp]'; }
158
function isError(obj) { return toStr(obj) === '[object Error]'; }
159
function isSymbol(obj) { return toStr(obj) === '[object Symbol]'; }
160
function isString(obj) { return toStr(obj) === '[object String]'; }
161
function isNumber(obj) { return toStr(obj) === '[object Number]'; }
162
function isBigInt(obj) { return toStr(obj) === '[object BigInt]'; }
163
function isBoolean(obj) { return toStr(obj) === '[object Boolean]'; }
164

    
165
var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
166
function has(obj, key) {
167
    return hasOwn.call(obj, key);
168
}
169

    
170
function toStr(obj) {
171
    return objectToString.call(obj);
172
}
173

    
174
function nameOf(f) {
175
    if (f.name) { return f.name; }
176
    var m = match.call(f, /^function\s*([\w$]+)/);
177
    if (m) { return m[1]; }
178
    return null;
179
}
180

    
181
function indexOf(xs, x) {
182
    if (xs.indexOf) { return xs.indexOf(x); }
183
    for (var i = 0, l = xs.length; i < l; i++) {
184
        if (xs[i] === x) { return i; }
185
    }
186
    return -1;
187
}
188

    
189
function isMap(x) {
190
    if (!mapSize || !x || typeof x !== 'object') {
191
        return false;
192
    }
193
    try {
194
        mapSize.call(x);
195
        try {
196
            setSize.call(x);
197
        } catch (s) {
198
            return true;
199
        }
200
        return x instanceof Map; // core-js workaround, pre-v2.5.0
201
    } catch (e) {}
202
    return false;
203
}
204

    
205
function isWeakMap(x) {
206
    if (!weakMapHas || !x || typeof x !== 'object') {
207
        return false;
208
    }
209
    try {
210
        weakMapHas.call(x, weakMapHas);
211
        try {
212
            weakSetHas.call(x, weakSetHas);
213
        } catch (s) {
214
            return true;
215
        }
216
        return x instanceof WeakMap; // core-js workaround, pre-v2.5.0
217
    } catch (e) {}
218
    return false;
219
}
220

    
221
function isSet(x) {
222
    if (!setSize || !x || typeof x !== 'object') {
223
        return false;
224
    }
225
    try {
226
        setSize.call(x);
227
        try {
228
            mapSize.call(x);
229
        } catch (m) {
230
            return true;
231
        }
232
        return x instanceof Set; // core-js workaround, pre-v2.5.0
233
    } catch (e) {}
234
    return false;
235
}
236

    
237
function isWeakSet(x) {
238
    if (!weakSetHas || !x || typeof x !== 'object') {
239
        return false;
240
    }
241
    try {
242
        weakSetHas.call(x, weakSetHas);
243
        try {
244
            weakMapHas.call(x, weakMapHas);
245
        } catch (s) {
246
            return true;
247
        }
248
        return x instanceof WeakSet; // core-js workaround, pre-v2.5.0
249
    } catch (e) {}
250
    return false;
251
}
252

    
253
function isElement(x) {
254
    if (!x || typeof x !== 'object') { return false; }
255
    if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
256
        return true;
257
    }
258
    return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function';
259
}
260

    
261
function inspectString(str, opts) {
262
    // eslint-disable-next-line no-control-regex
263
    var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
264
    return wrapQuotes(s, 'single', opts);
265
}
266

    
267
function lowbyte(c) {
268
    var n = c.charCodeAt(0);
269
    var x = {
270
        8: 'b', 9: 't', 10: 'n', 12: 'f', 13: 'r'
271
    }[n];
272
    if (x) { return '\\' + x; }
273
    return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16);
274
}
275

    
276
function markBoxed(str) {
277
    return 'Object(' + str + ')';
278
}
279

    
280
function weakCollectionOf(type) {
281
    return type + ' { ? }';
282
}
283

    
284
function collectionOf(type, size, entries) {
285
    return type + ' (' + size + ') {' + entries.join(', ') + '}';
286
}
287

    
288
function arrObjKeys(obj, inspect) {
289
    var isArr = isArray(obj);
290
    var xs = [];
291
    if (isArr) {
292
        xs.length = obj.length;
293
        for (var i = 0; i < obj.length; i++) {
294
            xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
295
        }
296
    }
297
    for (var key in obj) { // eslint-disable-line no-restricted-syntax
298
        if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
299
        if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
300
        if ((/[^\w$]/).test(key)) {
301
            xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
302
        } else {
303
            xs.push(key + ': ' + inspect(obj[key], obj));
304
        }
305
    }
306
    return xs;
307
}
(6-6/10)