@@ -15,6 +15,10 @@ const MAX_ARRAY_LENGTH = 10;
15
15
* Used to print values in error messages.
16
16
*/
17
17
export default function inspect ( value : mixed ) : string {
18
+ return formatValue ( value ) ;
19
+ }
20
+
21
+ function formatValue ( value ) {
18
22
switch ( typeof value ) {
19
23
case 'string' :
20
24
return JSON . stringify ( value ) ;
@@ -29,29 +33,45 @@ export default function inspect(value: mixed): string {
29
33
const customValue = customInspectFn . call ( value ) ;
30
34
return typeof customValue === 'string'
31
35
? customValue
32
- : inspect ( customValue ) ;
36
+ : formatValue ( customValue ) ;
33
37
} else if ( Array . isArray ( value ) ) {
34
- return inspectArray ( value ) ;
38
+ return formatArray ( value ) ;
35
39
}
36
40
37
- const properties = Object . keys ( value )
38
- . map ( k => `${ k } : ${ inspect ( value [ k ] ) } ` )
39
- . join ( ', ' ) ;
40
- return properties ? '{ ' + properties + ' }' : '{}' ;
41
+ return formatObject ( value ) ;
41
42
}
43
+
42
44
return String ( value ) ;
43
45
default :
44
46
return String ( value ) ;
45
47
}
46
48
}
47
49
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
+
49
69
const len = Math . min ( MAX_ARRAY_LENGTH , array . length ) ;
50
70
const remaining = array . length - len ;
51
71
const items = [ ] ;
52
72
53
73
for ( let i = 0 ; i < len ; ++ i ) {
54
- items . push ( inspect ( array [ i ] ) ) ;
74
+ items . push ( formatValue ( array [ i ] ) ) ;
55
75
}
56
76
57
77
if ( remaining === 1 ) {
0 commit comments