1 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
2 |
|
3 |
|
4 | import nodejsCustomInspectSymbol from "./nodejsCustomInspectSymbol.mjs";
|
5 | var MAX_ARRAY_LENGTH = 10;
|
6 | var MAX_RECURSIVE_DEPTH = 2;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export default function inspect(value) {
|
12 | return formatValue(value, []);
|
13 | }
|
14 |
|
15 | function formatValue(value, seenValues) {
|
16 | switch (_typeof(value)) {
|
17 | case 'string':
|
18 | return JSON.stringify(value);
|
19 |
|
20 | case 'function':
|
21 | return value.name ? "[function ".concat(value.name, "]") : '[function]';
|
22 |
|
23 | case 'object':
|
24 | if (value === null) {
|
25 | return 'null';
|
26 | }
|
27 |
|
28 | return formatObjectValue(value, seenValues);
|
29 |
|
30 | default:
|
31 | return String(value);
|
32 | }
|
33 | }
|
34 |
|
35 | function formatObjectValue(value, previouslySeenValues) {
|
36 | if (previouslySeenValues.indexOf(value) !== -1) {
|
37 | return '[Circular]';
|
38 | }
|
39 |
|
40 | var seenValues = [].concat(previouslySeenValues, [value]);
|
41 | var customInspectFn = getCustomFn(value);
|
42 |
|
43 | if (customInspectFn !== undefined) {
|
44 | var customValue = customInspectFn.call(value);
|
45 |
|
46 | if (customValue !== value) {
|
47 | return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
|
48 | }
|
49 | } else if (Array.isArray(value)) {
|
50 | return formatArray(value, seenValues);
|
51 | }
|
52 |
|
53 | return formatObject(value, seenValues);
|
54 | }
|
55 |
|
56 | function formatObject(object, seenValues) {
|
57 | var keys = Object.keys(object);
|
58 |
|
59 | if (keys.length === 0) {
|
60 | return '{}';
|
61 | }
|
62 |
|
63 | if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
64 | return '[' + getObjectTag(object) + ']';
|
65 | }
|
66 |
|
67 | var properties = keys.map(function (key) {
|
68 | var value = formatValue(object[key], seenValues);
|
69 | return key + ': ' + value;
|
70 | });
|
71 | return '{ ' + properties.join(', ') + ' }';
|
72 | }
|
73 |
|
74 | function formatArray(array, seenValues) {
|
75 | if (array.length === 0) {
|
76 | return '[]';
|
77 | }
|
78 |
|
79 | if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
80 | return '[Array]';
|
81 | }
|
82 |
|
83 | var len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
84 | var remaining = array.length - len;
|
85 | var items = [];
|
86 |
|
87 | for (var i = 0; i < len; ++i) {
|
88 | items.push(formatValue(array[i], seenValues));
|
89 | }
|
90 |
|
91 | if (remaining === 1) {
|
92 | items.push('... 1 more item');
|
93 | } else if (remaining > 1) {
|
94 | items.push("... ".concat(remaining, " more items"));
|
95 | }
|
96 |
|
97 | return '[' + items.join(', ') + ']';
|
98 | }
|
99 |
|
100 | function getCustomFn(object) {
|
101 | var customInspectFn = object[String(nodejsCustomInspectSymbol)];
|
102 |
|
103 | if (typeof customInspectFn === 'function') {
|
104 | return customInspectFn;
|
105 | }
|
106 |
|
107 | if (typeof object.inspect === 'function') {
|
108 | return object.inspect;
|
109 | }
|
110 | }
|
111 |
|
112 | function getObjectTag(object) {
|
113 | var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
|
114 |
|
115 | if (tag === 'Object' && typeof object.constructor === 'function') {
|
116 | var name = object.constructor.name;
|
117 |
|
118 | if (typeof name === 'string' && name !== '') {
|
119 | return name;
|
120 | }
|
121 | }
|
122 |
|
123 | return tag;
|
124 | }
|