@@ -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,32 @@ 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
+ var index = 0 ;
68
+ args [ 0 ] = args [ 0 ] . replace ( / % ( [ a - z A - Z % ] ) / g, function ( match , format ) {
69
+ // if we encounter an escaped % then don't increase the array index
70
+ if ( match === '%%' ) return match ;
71
+ index ++ ;
72
+ var formatter = exports . formatters [ format ] ;
73
+ if ( 'function' === typeof formatter ) {
74
+ var val = args [ index ] ;
75
+ match = formatter . call ( dbg , val ) ;
76
+
77
+ // now we need to remove `args[index]` since it's inlined in the `format`
78
+ args . splice ( index , 1 ) ;
79
+ index -- ;
80
+ }
81
+ return match ;
82
+ } ) ;
83
+
84
+ return args ;
85
+ }
86
+
53
87
/**
54
88
* Create a debugger with the given `namespace`.
55
89
*
@@ -90,22 +124,7 @@ function createDebug(namespace) {
90
124
}
91
125
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,9 @@ 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 rightArgs = formatInlineArgs ( debug , [ ] . slice . call ( extraArgs ) ) ;
164
+ var newArgs = ( leftArgs . length > 0 && rightArgs . length > 0 ) ? leftArgs . concat ( [ '::' ] ) . concat ( rightArgs ) : leftArgs . concat ( rightArgs ) ;
145
165
debugHandle ( newArgs , section ) ;
146
166
} else {
147
167
debugHandle ( args , section ) ;
0 commit comments