@@ -17,26 +17,11 @@ exports.error = function (x) {
17
17
/**
18
18
* Echos the value of a value. Trys to print the value out
19
19
* in the best way possible given the different types.
20
- *
20
+ *
21
21
* @param {Object } value The object to print out
22
22
*/
23
23
exports . inspect = function ( value ) {
24
- if ( value === 0 ) return "0" ;
25
- if ( value === false ) return "false" ;
26
- if ( value === "" ) return '""' ;
27
- if ( typeof ( value ) == "function" ) return "[Function]" ;
28
- if ( value === undefined ) return ;
29
-
30
- try {
31
- return JSON . stringify ( value , undefined , 1 ) ;
32
- } catch ( e ) {
33
- // TODO make this recusrive and do a partial JSON output of object.
34
- if ( e . message . search ( "circular" ) ) {
35
- return "[Circular Object]" ;
36
- } else {
37
- throw e ;
38
- }
39
- }
24
+ return formatter ( value , '' , [ ] ) ;
40
25
} ;
41
26
42
27
exports . p = function ( x ) {
@@ -50,13 +35,13 @@ exports.exec = function (command) {
50
35
var promise = new process . Promise ( ) ;
51
36
52
37
child . addListener ( "output" , function ( chunk ) {
53
- if ( chunk ) stdout += chunk ;
38
+ if ( chunk ) stdout += chunk ;
54
39
} ) ;
55
40
56
41
child . addListener ( "error" , function ( chunk ) {
57
- if ( chunk ) stderr += chunk ;
42
+ if ( chunk ) stderr += chunk ;
58
43
} ) ;
59
-
44
+
60
45
child . addListener ( "exit" , function ( code ) {
61
46
if ( code == 0 ) {
62
47
promise . emitSuccess ( stdout , stderr ) ;
@@ -82,3 +67,62 @@ exports.exec = function (command) {
82
67
* @param {function } superCtor Constructor function to inherit prototype from
83
68
*/
84
69
exports . inherits = process . inherits ;
70
+
71
+ /**
72
+ * A recursive function to format an object - used by inspect.
73
+ *
74
+ * @param {Object } value
75
+ * the value to format
76
+ * @param {String } indent
77
+ * the indent level of any nested objects, since they are formatted over
78
+ * more than one line
79
+ * @param {Array } parents
80
+ * contains all objects above the current one in the heirachy, used to
81
+ * prevent getting stuck in a loop on circular references
82
+ */
83
+ var formatter = function ( value , indent , parents ) {
84
+ switch ( typeof ( value ) ) {
85
+ case 'string' : return '"' + value + '"' ;
86
+ case 'number' : return '' + value ;
87
+ case 'function' : return '[Function]' ;
88
+ case 'boolean' : return '' + value ;
89
+ case 'undefined' : return 'undefined' ;
90
+ case 'object' :
91
+ if ( value == null ) return 'null' ;
92
+ if ( parents . indexOf ( value ) >= 0 ) return '[Circular]' ;
93
+ parents . push ( value ) ;
94
+
95
+ if ( value instanceof Array ) {
96
+ return formatObject ( value , indent , parents , '[]' , function ( x , f ) {
97
+ return f ( value [ x ] ) ;
98
+ } ) ;
99
+ } else {
100
+ return formatObject ( value , indent , parents , '{}' , function ( x , f ) {
101
+ return f ( x ) + ': ' + f ( value [ x ] ) ;
102
+ } ) ;
103
+ }
104
+ return buffer ;
105
+ default :
106
+ throw ( 'inspect unimplemented for ' + typeof ( value ) ) ;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Helper function for formatting either an array or an object, used internally by formatter
112
+ */
113
+ var formatObject = function ( obj , indent , parents , parenthesis , entryFormatter ) {
114
+ var buffer = parenthesis [ 0 ] ;
115
+ var values = [ ] ;
116
+
117
+ var localFormatter = function ( value ) {
118
+ return formatter ( value , indent + ' ' , parents ) ;
119
+ } ;
120
+ for ( x in obj ) {
121
+ values . push ( indent + ' ' + entryFormatter ( x , localFormatter ) ) ;
122
+ }
123
+ if ( values . length > 0 ) {
124
+ buffer += "\n" + values . join ( ",\n" ) + "\n" + indent ;
125
+ }
126
+ buffer += parenthesis [ 1 ] ;
127
+ return buffer ;
128
+ }
0 commit comments