@@ -87,36 +87,64 @@ function setup(env) {
87
87
*/
88
88
89
89
createDebug . outputFormatters [ '+' ] = function ( format , args ) {
90
- const diff = '+' + createDebug . humanize ( this . diff ) ;
91
- if ( this . useColors ) {
92
- return this . applyColor ( diff ) ;
93
- } else {
94
- return diff ;
95
- }
90
+ return '+' + createDebug . humanize ( this . diff ) ;
96
91
}
97
92
98
93
/**
99
94
* Map %n to outputting namespace prefix
100
95
*/
101
96
102
97
createDebug . outputFormatters . n = function ( format , args ) {
103
- if ( this . useColors ) {
104
- return this . applyColor ( this . namespace , true ) ;
105
- } else {
106
- return this . namespace ;
107
- }
98
+ return this . namespace ;
108
99
}
109
100
110
101
/**
111
102
* Map %_time to handling time...?
112
103
*/
113
104
114
105
createDebug . outputFormatters . _time = function ( format , args ) {
115
- //doesn't respect `exports.inspectOpts.hideDate`
116
106
//browser doesn't have date
117
107
return new Date ( ) . toISOString ( ) ;
118
108
}
119
109
110
+
111
+ /**
112
+ * Map of meta-formatters which are applied to outputFormatters
113
+ */
114
+ createDebug . metaFormatters = { } ;
115
+
116
+ /**
117
+ * Map %J* to `JSON.stringify()`
118
+ */
119
+
120
+ createDebug . outputFormatters . J = function ( v ) {
121
+ return JSON . stringify ( v ) ;
122
+ }
123
+
124
+ /**
125
+ * Map %c* to to `applyColor()`
126
+ */
127
+
128
+ createDebug . outputFormatters . c = function ( v ) {
129
+ if ( this . useColors ) {
130
+ return this . applyColor ( v ) ;
131
+ } else {
132
+ return v ;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Map %C* to to `applyColor(arg, bold = true)` (node)
138
+ */
139
+
140
+ createDebug . outputFormatters . C = function ( v ) {
141
+ if ( this . useColors ) {
142
+ return this . applyColor ( v , true ) ;
143
+ } else {
144
+ return v ;
145
+ }
146
+ }
147
+
120
148
/**
121
149
* Selects a color for a debug namespace
122
150
* @param {String } namespace The namespace string for the for the debug instance to be colored
@@ -162,10 +190,10 @@ function setup(env) {
162
190
prevTime = curr ;
163
191
164
192
// Apply relevant `outputFormatters` to `format`
165
- let reg = / % ( J ? [ a - z A - Z + ] | J ? \{ .+ \} ) / , formattedArgs = [ ] , res ;
193
+ let reg = / % ( [ a - z A - Z + ] + | [ a - z A - Z ] * ?\{ .+ \} ) / , formattedArgs = [ ] , res ;
166
194
let outputFormat = self . format ; //make a copy of the format
167
195
while ( res = outputFormat . match ( reg ) ) {
168
- let [ matched , formatToken ] = res , stringify = false , formatter , formatted ;
196
+ let [ matched , formatToken ] = res , formatter , formatted ;
169
197
//split out the part before the matched format token
170
198
let split = outputFormat . slice ( 0 , res . index ) ;
171
199
outputFormat = outputFormat . slice ( res . index + matched . length ) ;
@@ -175,10 +203,12 @@ function setup(env) {
175
203
formattedArgs . push ( split ) ;
176
204
}
177
205
178
- //%J* are passed to JSON.stringify
179
- if ( formatToken . startsWith ( 'J' ) ) {
180
- formatToken = formatToken . replace ( / ^ J / , '' ) ;
181
- stringify = true ;
206
+ let metaFormatters = [ ] ;
207
+ // Extract metaformatters
208
+ while ( formatToken . length > 1 && ! formatToken . startsWith ( '{' ) ) {
209
+ const metaFormatterToken = formatToken . slice ( 0 , 1 ) ;
210
+ formatToken = formatToken . slice ( 1 ) ;
211
+ metaFormatters . push ( createDebug . outputFormatters [ metaFormatterToken ] ) ;
182
212
}
183
213
184
214
//not really sure how to handle time at this point
@@ -189,9 +219,13 @@ function setup(env) {
189
219
}
190
220
if ( typeof formatter === 'function' ) {
191
221
formatted = formatter . call ( self , formatToken , args ) ;
192
- if ( stringify ) {
193
- formatted = JSON . stringify ( formatted )
194
- }
222
+
223
+ // Apply metaFormatters
224
+ metaFormatters . forEach ( metaFormatter => {
225
+ if ( typeof metaFormatter === "function" ) {
226
+ formatted = metaFormatter . call ( self , formatted ) ;
227
+ }
228
+ } ) ;
195
229
196
230
if ( Array . isArray ( formatted ) ) { //intended to concatenate %m's args in the middle of the format
197
231
formattedArgs = formattedArgs . concat ( formatted ) ;
0 commit comments