Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75d301c

Browse files
author
Travis CI
committed
Deploy ffccf3f to NPM branch
1 parent 3eb34d6 commit 75d301c

File tree

3 files changed

+282
-55
lines changed

3 files changed

+282
-55
lines changed

jsutils/inspect.js

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1111

1212
function _typeof(obj) { 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); }
1313

14+
var MAX_ARRAY_LENGTH = 10;
15+
var MAX_RECURSIVE_DEPTH = 2;
1416
/**
1517
* Used to print values in error messages.
1618
*/
19+
1720
function inspect(value) {
21+
return formatValue(value, []);
22+
}
23+
24+
function formatValue(value, seenValues) {
1825
switch (_typeof(value)) {
1926
case 'string':
2027
return JSON.stringify(value);
@@ -23,30 +30,84 @@ function inspect(value) {
2330
return value.name ? "[function ".concat(value.name, "]") : '[function]';
2431

2532
case 'object':
26-
if (value) {
27-
var customInspectFn = getCustomFn(value);
28-
29-
if (customInspectFn) {
30-
// $FlowFixMe(>=0.90.0)
31-
var customValue = customInspectFn.call(value);
32-
return typeof customValue === 'string' ? customValue : inspect(customValue);
33-
} else if (Array.isArray(value)) {
34-
return '[' + value.map(inspect).join(', ') + ']';
35-
}
36-
37-
var properties = Object.keys(value).map(function (k) {
38-
return "".concat(k, ": ").concat(inspect(value[k]));
39-
}).join(', ');
40-
return properties ? '{ ' + properties + ' }' : '{}';
41-
}
42-
43-
return String(value);
33+
return formatObjectValue(value, seenValues);
4434

4535
default:
4636
return String(value);
4737
}
4838
}
4939

40+
function formatObjectValue(value, previouslySeenValues) {
41+
if (previouslySeenValues.indexOf(value) !== -1) {
42+
return '[Circular]';
43+
}
44+
45+
var seenValues = [].concat(previouslySeenValues, [value]);
46+
47+
if (value) {
48+
var customInspectFn = getCustomFn(value);
49+
50+
if (customInspectFn) {
51+
// $FlowFixMe(>=0.90.0)
52+
var customValue = customInspectFn.call(value); // check for infinite recursion
53+
54+
if (customValue !== value) {
55+
return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
56+
}
57+
} else if (Array.isArray(value)) {
58+
return formatArray(value, seenValues);
59+
}
60+
61+
return formatObject(value, seenValues);
62+
}
63+
64+
return String(value);
65+
}
66+
67+
function formatObject(object, seenValues) {
68+
var keys = Object.keys(object);
69+
70+
if (keys.length === 0) {
71+
return '{}';
72+
}
73+
74+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
75+
return '[' + getObjectTag(object) + ']';
76+
}
77+
78+
var properties = keys.map(function (key) {
79+
var value = formatValue(object[key], seenValues);
80+
return key + ': ' + value;
81+
});
82+
return '{ ' + properties.join(', ') + ' }';
83+
}
84+
85+
function formatArray(array, seenValues) {
86+
if (array.length === 0) {
87+
return '[]';
88+
}
89+
90+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
91+
return '[Array]';
92+
}
93+
94+
var len = Math.min(MAX_ARRAY_LENGTH, array.length);
95+
var remaining = array.length - len;
96+
var items = [];
97+
98+
for (var i = 0; i < len; ++i) {
99+
items.push(formatValue(array[i], seenValues));
100+
}
101+
102+
if (remaining === 1) {
103+
items.push('... 1 more item');
104+
} else if (remaining > 1) {
105+
items.push("... ".concat(remaining, " more items"));
106+
}
107+
108+
return '[' + items.join(', ') + ']';
109+
}
110+
50111
function getCustomFn(object) {
51112
var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
52113

@@ -57,4 +118,18 @@ function getCustomFn(object) {
57118
if (typeof object.inspect === 'function') {
58119
return object.inspect;
59120
}
121+
}
122+
123+
function getObjectTag(object) {
124+
var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
125+
126+
if (tag === 'Object' && typeof object.constructor === 'function') {
127+
var name = object.constructor.name;
128+
129+
if (typeof name === 'string') {
130+
return name;
131+
}
132+
}
133+
134+
return tag;
60135
}

jsutils/inspect.js.flow

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,102 @@
99

1010
import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
1111

12+
const MAX_ARRAY_LENGTH = 10;
13+
const MAX_RECURSIVE_DEPTH = 2;
14+
1215
/**
1316
* Used to print values in error messages.
1417
*/
1518
export default function inspect(value: mixed): string {
19+
return formatValue(value, []);
20+
}
21+
22+
function formatValue(value, seenValues) {
1623
switch (typeof value) {
1724
case 'string':
1825
return JSON.stringify(value);
1926
case 'function':
2027
return value.name ? `[function ${value.name}]` : '[function]';
2128
case 'object':
22-
if (value) {
23-
const customInspectFn = getCustomFn(value);
24-
25-
if (customInspectFn) {
26-
// $FlowFixMe(>=0.90.0)
27-
const customValue = customInspectFn.call(value);
28-
return typeof customValue === 'string'
29-
? customValue
30-
: inspect(customValue);
31-
} else if (Array.isArray(value)) {
32-
return '[' + value.map(inspect).join(', ') + ']';
33-
}
34-
35-
const properties = Object.keys(value)
36-
.map(k => `${k}: ${inspect(value[k])}`)
37-
.join(', ');
38-
return properties ? '{ ' + properties + ' }' : '{}';
39-
}
40-
return String(value);
29+
return formatObjectValue(value, seenValues);
4130
default:
4231
return String(value);
4332
}
4433
}
4534

35+
function formatObjectValue(value, previouslySeenValues) {
36+
if (previouslySeenValues.indexOf(value) !== -1) {
37+
return '[Circular]';
38+
}
39+
const seenValues = [...previouslySeenValues, value];
40+
41+
if (value) {
42+
const customInspectFn = getCustomFn(value);
43+
44+
if (customInspectFn) {
45+
// $FlowFixMe(>=0.90.0)
46+
const customValue = customInspectFn.call(value);
47+
48+
// check for infinite recursion
49+
if (customValue !== value) {
50+
return typeof customValue === 'string'
51+
? customValue
52+
: formatValue(customValue, seenValues);
53+
}
54+
} else if (Array.isArray(value)) {
55+
return formatArray(value, seenValues);
56+
}
57+
58+
return formatObject(value, seenValues);
59+
}
60+
61+
return String(value);
62+
}
63+
64+
function formatObject(object, seenValues) {
65+
const keys = Object.keys(object);
66+
if (keys.length === 0) {
67+
return '{}';
68+
}
69+
70+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
71+
return '[' + getObjectTag(object) + ']';
72+
}
73+
74+
const properties = keys.map(key => {
75+
const value = formatValue(object[key], seenValues);
76+
return key + ': ' + value;
77+
});
78+
79+
return '{ ' + properties.join(', ') + ' }';
80+
}
81+
82+
function formatArray(array, seenValues) {
83+
if (array.length === 0) {
84+
return '[]';
85+
}
86+
87+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
88+
return '[Array]';
89+
}
90+
91+
const len = Math.min(MAX_ARRAY_LENGTH, array.length);
92+
const remaining = array.length - len;
93+
const items = [];
94+
95+
for (let i = 0; i < len; ++i) {
96+
items.push(formatValue(array[i], seenValues));
97+
}
98+
99+
if (remaining === 1) {
100+
items.push('... 1 more item');
101+
} else if (remaining > 1) {
102+
items.push(`... ${remaining} more items`);
103+
}
104+
105+
return '[' + items.join(', ') + ']';
106+
}
107+
46108
function getCustomFn(object) {
47109
const customInspectFn = object[String(nodejsCustomInspectSymbol)];
48110

@@ -54,3 +116,19 @@ function getCustomFn(object) {
54116
return object.inspect;
55117
}
56118
}
119+
120+
function getObjectTag(object) {
121+
const tag = Object.prototype.toString
122+
.call(object)
123+
.replace(/^\[object /, '')
124+
.replace(/]$/, '');
125+
126+
if (tag === 'Object' && typeof object.constructor === 'function') {
127+
const name = object.constructor.name;
128+
if (typeof name === 'string') {
129+
return name;
130+
}
131+
}
132+
133+
return tag;
134+
}

0 commit comments

Comments
 (0)