Skip to content

Commit 5313c69

Browse files
author
chagris
committed
implement metaformatters (color, JSON.stringify)
1 parent 1e18a8e commit 5313c69

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

src/common.js

+55-21
Original file line numberDiff line numberDiff line change
@@ -87,36 +87,64 @@ function setup(env) {
8787
*/
8888

8989
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);
9691
}
9792

9893
/**
9994
* Map %n to outputting namespace prefix
10095
*/
10196

10297
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;
10899
}
109100

110101
/**
111102
* Map %_time to handling time...?
112103
*/
113104

114105
createDebug.outputFormatters._time = function(format, args) {
115-
//doesn't respect `exports.inspectOpts.hideDate`
116106
//browser doesn't have date
117107
return new Date().toISOString();
118108
}
119109

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+
120148
/**
121149
* Selects a color for a debug namespace
122150
* @param {String} namespace The namespace string for the for the debug instance to be colored
@@ -162,10 +190,10 @@ function setup(env) {
162190
prevTime = curr;
163191

164192
// Apply relevant `outputFormatters` to `format`
165-
let reg = /%(J?[a-zA-Z+]|J?\{.+\})/, formattedArgs = [], res;
193+
let reg = /%([a-zA-Z+]+|[a-zA-Z]*?\{.+\})/, formattedArgs = [], res;
166194
let outputFormat = self.format; //make a copy of the format
167195
while (res = outputFormat.match(reg)) {
168-
let [matched, formatToken] = res, stringify = false, formatter, formatted;
196+
let [matched, formatToken] = res, formatter, formatted;
169197
//split out the part before the matched format token
170198
let split = outputFormat.slice(0, res.index);
171199
outputFormat = outputFormat.slice(res.index + matched.length);
@@ -175,10 +203,12 @@ function setup(env) {
175203
formattedArgs.push(split);
176204
}
177205

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]);
182212
}
183213

184214
//not really sure how to handle time at this point
@@ -189,9 +219,13 @@ function setup(env) {
189219
}
190220
if (typeof formatter === 'function') {
191221
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+
});
195229

196230
if (Array.isArray(formatted)) { //intended to concatenate %m's args in the middle of the format
197231
formattedArgs = formattedArgs.concat(formatted);

src/node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function getFormat() {
172172
return exports.inspectOpts.format;
173173
} else {
174174
if (useColors) {
175-
return ' %n%m%+'; //' %n %m %+'
175+
return ' %Cn%m%c+'; //' %n %m %+'
176176
} else if (exports.inspectOpts.hideDate) {
177177
return '%n%m'; //'%n %m'
178178
} else {

0 commit comments

Comments
 (0)