Skip to content

Commit e8b827f

Browse files
committed
inspect: move implementation into formatValue
1 parent 9b39df9 commit e8b827f

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/jsutils/inspect.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const MAX_ARRAY_LENGTH = 10;
1515
* Used to print values in error messages.
1616
*/
1717
export default function inspect(value: mixed): string {
18+
return formatValue(value);
19+
}
20+
21+
function formatValue(value) {
1822
switch (typeof value) {
1923
case 'string':
2024
return JSON.stringify(value);
@@ -29,29 +33,45 @@ export default function inspect(value: mixed): string {
2933
const customValue = customInspectFn.call(value);
3034
return typeof customValue === 'string'
3135
? customValue
32-
: inspect(customValue);
36+
: formatValue(customValue);
3337
} else if (Array.isArray(value)) {
34-
return inspectArray(value);
38+
return formatArray(value);
3539
}
3640

37-
const properties = Object.keys(value)
38-
.map(k => `${k}: ${inspect(value[k])}`)
39-
.join(', ');
40-
return properties ? '{ ' + properties + ' }' : '{}';
41+
return formatObject(value);
4142
}
43+
4244
return String(value);
4345
default:
4446
return String(value);
4547
}
4648
}
4749

48-
function inspectArray(array) {
50+
function formatObject(object) {
51+
const keys = Object.keys(object);
52+
if (keys.length === 0) {
53+
return '{}';
54+
}
55+
56+
const properties = keys.map(key => {
57+
const value = formatValue(object[key]);
58+
return key + ': ' + value;
59+
});
60+
61+
return '{ ' + properties.join(', ') + ' }';
62+
}
63+
64+
function formatArray(array) {
65+
if (array.length === 0) {
66+
return '[]';
67+
}
68+
4969
const len = Math.min(MAX_ARRAY_LENGTH, array.length);
5070
const remaining = array.length - len;
5171
const items = [];
5272

5373
for (let i = 0; i < len; ++i) {
54-
items.push(inspect(array[i]));
74+
items.push(formatValue(array[i]));
5575
}
5676

5777
if (remaining === 1) {

0 commit comments

Comments
 (0)