@@ -30,7 +30,15 @@ exports.skips = [];
30
30
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
31
31
*/
32
32
33
- exports . formatters = { } ;
33
+ exports . formatters = {
34
+ s : String ,
35
+ i : function ( v ) {
36
+ v = Number ( v ) ;
37
+ return v - ( v % 1 ) ;
38
+ } ,
39
+ d : Number ,
40
+ f : Number
41
+ } ;
34
42
35
43
/**
36
44
* Select a color.
@@ -50,6 +58,39 @@ function selectColor(namespace) {
50
58
return exports . colors [ Math . abs ( hash ) % exports . colors . length ] ;
51
59
}
52
60
61
+ /**
62
+ * Formats a sequence of arguments.
63
+ * @api private
64
+ */
65
+
66
+ function formatInlineArgs ( dbg , args ) {
67
+ args [ 0 ] = exports . coerce ( args [ 0 ] ) ;
68
+
69
+ if ( 'string' !== typeof args [ 0 ] ) {
70
+ // anything else let's inspect with %O
71
+ args . unshift ( '%O' ) ;
72
+ }
73
+
74
+ var index = 0 ;
75
+ args [ 0 ] = args [ 0 ] . replace ( / % ( [ a - z A - Z % ] ) / g, function ( match , format ) {
76
+ // if we encounter an escaped % then don't increase the array index
77
+ if ( match === '%%' ) return match ;
78
+ index ++ ;
79
+ var formatter = exports . formatters [ format ] ;
80
+ if ( 'function' === typeof formatter ) {
81
+ var val = args [ index ] ;
82
+ match = formatter . call ( dbg , val ) ;
83
+
84
+ // now we need to remove `args[index]` since it's inlined in the `format`
85
+ args . splice ( index , 1 ) ;
86
+ index -- ;
87
+ }
88
+ return match ;
89
+ } ) ;
90
+
91
+ return args ;
92
+ }
93
+
53
94
/**
54
95
* Create a debugger with the given `namespace`.
55
96
*
@@ -82,30 +123,8 @@ function createDebug(namespace) {
82
123
args [ i ] = rawArgs [ i ] ;
83
124
}
84
125
85
- args [ 0 ] = exports . coerce ( args [ 0 ] ) ;
86
-
87
- if ( 'string' !== typeof args [ 0 ] ) {
88
- // anything else let's inspect with %O
89
- args . unshift ( '%O' ) ;
90
- }
91
-
92
126
// apply any `formatters` transformations
93
- var index = 0 ;
94
- args [ 0 ] = args [ 0 ] . replace ( / % ( [ a - z A - Z % ] ) / g, function ( match , format ) {
95
- // if we encounter an escaped % then don't increase the array index
96
- if ( match === '%%' ) return match ;
97
- index ++ ;
98
- var formatter = exports . formatters [ format ] ;
99
- if ( 'function' === typeof formatter ) {
100
- var val = args [ index ] ;
101
- match = formatter . call ( self , val ) ;
102
-
103
- // now we need to remove `args[index]` since it's inlined in the `format`
104
- args . splice ( index , 1 ) ;
105
- index -- ;
106
- }
107
- return match ;
108
- } ) ;
127
+ formatInlineArgs ( self , args ) ;
109
128
110
129
// apply env-specific formatting (colors, etc.)
111
130
exports . formatArgs . call ( self , args , section ) ;
@@ -140,8 +159,14 @@ function createDebug(namespace) {
140
159
section . title = title ;
141
160
section . deltaTime = exports . hrtime ( beginTime ) ;
142
161
if ( extraArgs . length ) {
143
- var newArgParams = [ ] . slice . call ( args , 1 ) . concat ( [ ] . slice . call ( extraArgs , 1 ) )
144
- var newArgs = [ ( args [ 0 ] ? args [ 0 ] + ' :: ' : '' ) + ( extraArgs [ 0 ] || '' ) ] . concat ( newArgParams ) ;
162
+ var leftArgs = formatInlineArgs ( debug , [ ] . slice . call ( args ) ) ;
163
+ var newArgs ;
164
+ if ( extraArgs . length > 0 ) {
165
+ var rightArgs = formatInlineArgs ( debug , [ ] . slice . call ( extraArgs ) ) ;
166
+ newArgs = leftArgs . concat ( [ '::' ] ) . concat ( rightArgs )
167
+ } else {
168
+ newArgs = leftArgs ;
169
+ }
145
170
debugHandle ( newArgs , section ) ;
146
171
} else {
147
172
debugHandle ( args , section ) ;
0 commit comments