@@ -11,10 +11,17 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
11
11
12
12
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 ) ; }
13
13
14
+ var MAX_ARRAY_LENGTH = 10 ;
15
+ var MAX_RECURSIVE_DEPTH = 2 ;
14
16
/**
15
17
* Used to print values in error messages.
16
18
*/
19
+
17
20
function inspect ( value ) {
21
+ return formatValue ( value , [ ] ) ;
22
+ }
23
+
24
+ function formatValue ( value , seenValues ) {
18
25
switch ( _typeof ( value ) ) {
19
26
case 'string' :
20
27
return JSON . stringify ( value ) ;
@@ -23,30 +30,84 @@ function inspect(value) {
23
30
return value . name ? "[function " . concat ( value . name , "]" ) : '[function]' ;
24
31
25
32
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 ) ;
44
34
45
35
default :
46
36
return String ( value ) ;
47
37
}
48
38
}
49
39
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
+
50
111
function getCustomFn ( object ) {
51
112
var customInspectFn = object [ String ( _nodejsCustomInspectSymbol . default ) ] ;
52
113
@@ -57,4 +118,18 @@ function getCustomFn(object) {
57
118
if ( typeof object . inspect === 'function' ) {
58
119
return object . inspect ;
59
120
}
121
+ }
122
+
123
+ function getObjectTag ( object ) {
124
+ var tag = Object . prototype . toString . call ( object ) . replace ( / ^ \[ o b j e c t / , '' ) . 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 ;
60
135
}
0 commit comments