@@ -21,9 +21,99 @@ exports.error = function (x) {
21
21
* in the best way possible given the different types.
22
22
*
23
23
* @param {Object } value The object to print out
24
+ * @param {Boolean } showHidden Flag that shows hidden (not enumerable) properties of objects.
24
25
*/
25
- exports . inspect = function ( value ) {
26
- return formatter ( value , '' , [ ] ) ;
26
+ exports . inspect = function ( obj , showHidden ) {
27
+ var seen = [ ] ;
28
+ function format ( value ) {
29
+ var keys , visible_keys , base , type , braces ;
30
+ // Primitive types cannot have properties
31
+ switch ( typeof value ) {
32
+ case 'undefined' : return 'undefined' ;
33
+ case 'string' : return JSON . stringify ( value ) ;
34
+ case 'number' : return '' + value ;
35
+ case 'boolean' : return '' + value ;
36
+ }
37
+ // For some reason typeof null is "object", so special case here.
38
+ if ( value === null ) {
39
+ return 'null' ;
40
+ }
41
+
42
+ // Look up the keys of the object.
43
+ keys = showHidden ? Object . getOwnPropertyNames ( value ) . map ( function ( key ) {
44
+ return '' + key ;
45
+ } ) : Object . keys ( value ) ;
46
+ visible_keys = Object . keys ( value ) ;
47
+
48
+ // Functions without properties can be shortcutted.
49
+ if ( typeof value === 'function' && keys . length === 0 ) {
50
+ if ( value instanceof RegExp ) {
51
+ return '' + value ;
52
+ } else {
53
+ return '[Function]' ;
54
+ }
55
+ }
56
+
57
+ // Determine the object type
58
+ if ( value instanceof Array ) {
59
+ type = 'Array' ;
60
+ braces = [ "[" , "]" ] ;
61
+ } else {
62
+ type = 'Object' ;
63
+ braces = [ "{" , "}" ] ;
64
+ }
65
+
66
+ // Make functions say that they are functions
67
+ if ( typeof value === 'function' ) {
68
+ base = ( value instanceof RegExp ) ? ' ' + value : ' [Function]' ;
69
+ } else {
70
+ base = "" ;
71
+ }
72
+
73
+ seen . push ( value ) ;
74
+
75
+ if ( keys . length === 0 ) {
76
+ return braces [ 0 ] + base + braces [ 1 ] ;
77
+ }
78
+
79
+ return braces [ 0 ] + base + "\n" + ( keys . map ( function ( key ) {
80
+ var name , str ;
81
+ if ( value . __lookupGetter__ ) {
82
+ if ( value . __lookupGetter__ ( key ) ) {
83
+ if ( value . __lookupSetter__ ( key ) ) {
84
+ str = "[Getter/Setter]" ;
85
+ } else {
86
+ str = "[Getter]" ;
87
+ }
88
+ } else {
89
+ if ( value . __lookupSetter__ ( key ) ) {
90
+ str = "[Setter]" ;
91
+ }
92
+ }
93
+ }
94
+ if ( visible_keys . indexOf ( key ) < 0 ) {
95
+ name = "[" + key + "]" ;
96
+ }
97
+ if ( ! str ) {
98
+ if ( seen . indexOf ( value [ key ] ) < 0 ) {
99
+ str = format ( value [ key ] ) ;
100
+ } else {
101
+ str = '[Circular]' ;
102
+ }
103
+ }
104
+ if ( typeof name === 'undefined' ) {
105
+ if ( type === 'Array' && key . match ( / ^ \d + $ / ) ) {
106
+ return str ;
107
+ }
108
+ name = JSON . stringify ( '' + key ) ;
109
+ }
110
+
111
+ return name + ": " + str ;
112
+ } ) . join ( ",\n" ) ) . split ( "\n" ) . map ( function ( line ) {
113
+ return ' ' + line ;
114
+ } ) . join ( '\n' ) + "\n" + braces [ 1 ] ;
115
+ }
116
+ return format ( obj ) ;
27
117
} ;
28
118
29
119
exports . p = function ( x ) {
@@ -70,76 +160,4 @@ exports.exec = function (command) {
70
160
*/
71
161
exports . inherits = process . inherits ;
72
162
73
- /**
74
- * A recursive function to format an object - used by inspect.
75
- *
76
- * @param {Object } value
77
- * the value to format
78
- * @param {String } indent
79
- * the indent level of any nested objects, since they are formatted over
80
- * more than one line
81
- * @param {Array } parents
82
- * contains all objects above the current one in the heirachy, used to
83
- * prevent getting stuck in a loop on circular references
84
- */
85
- var formatter = function ( value , indent , parents ) {
86
- switch ( typeof ( value ) ) {
87
- case 'string' : return JSON . stringify ( value ) ;
88
- case 'number' : return '' + value ;
89
- case 'function' : return '[Function]' ;
90
- case 'boolean' : return '' + value ;
91
- case 'undefined' : return 'undefined' ;
92
- case 'object' :
93
- if ( value == null ) return 'null' ;
94
- if ( parents . indexOf ( value ) >= 0 ) return '[Circular]' ;
95
- parents . push ( value ) ;
96
-
97
- if ( value instanceof Array && Object . keys ( value ) . length === value . length ) {
98
- return formatObject ( value , indent , parents , '[]' , function ( x , f ) {
99
- return f ( value [ x ] ) ;
100
- } ) ;
101
- } else {
102
- return formatObject ( value , indent , parents , '{}' , function ( x , f ) {
103
- var child ;
104
- if ( value . __lookupGetter__ ( x ) ) {
105
- if ( value . __lookupSetter__ ( x ) ) {
106
- child = "[Getter/Setter]" ;
107
- } else {
108
- child = "[Getter]" ;
109
- }
110
- } else {
111
- if ( value . __lookupSetter__ ( x ) ) {
112
- child = "[Setter]" ;
113
- } else {
114
- child = f ( value [ x ] ) ;
115
- }
116
- }
117
- return f ( x ) + ': ' + child ;
118
- } ) ;
119
- }
120
- return buffer ;
121
- default :
122
- throw ( 'inspect unimplemented for ' + typeof ( value ) ) ;
123
- }
124
- }
125
-
126
- /**
127
- * Helper function for formatting either an array or an object, used internally by formatter
128
- */
129
- var formatObject = function ( obj , indent , parents , parenthesis , entryFormatter ) {
130
- var buffer = parenthesis [ 0 ] ;
131
- var values = [ ] ;
132
- var x ;
133
-
134
- var localFormatter = function ( value ) {
135
- return formatter ( value , indent + ' ' , parents ) ;
136
- } ;
137
- for ( x in obj ) {
138
- values . push ( indent + ' ' + entryFormatter ( x , localFormatter ) ) ;
139
- }
140
- if ( values . length > 0 ) {
141
- buffer += "\n" + values . join ( ",\n" ) + "\n" + indent ;
142
- }
143
- buffer += parenthesis [ 1 ] ;
144
- return buffer ;
145
- }
163
+ // Object.create(null, {name: {value: "Tim", enumerable: true}})
0 commit comments