Skip to content

Commit 7815952

Browse files
committed
fix multi-arg and format handling
1 parent 58ceaa0 commit 7815952

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

src/debug.js

+51-26
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ exports.skips = [];
3030
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
3131
*/
3232

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+
};
3442

3543
/**
3644
* Select a color.
@@ -50,6 +58,39 @@ function selectColor(namespace) {
5058
return exports.colors[Math.abs(hash) % exports.colors.length];
5159
}
5260

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-zA-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+
5394
/**
5495
* Create a debugger with the given `namespace`.
5596
*
@@ -82,30 +123,8 @@ function createDebug(namespace) {
82123
args[i] = rawArgs[i];
83124
}
84125

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-
92126
// apply any `formatters` transformations
93-
var index = 0;
94-
args[0] = args[0].replace(/%([a-zA-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);
109128

110129
// apply env-specific formatting (colors, etc.)
111130
exports.formatArgs.call(self, args, section);
@@ -140,8 +159,14 @@ function createDebug(namespace) {
140159
section.title = title;
141160
section.deltaTime = exports.hrtime(beginTime);
142161
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+
}
145170
debugHandle(newArgs, section);
146171
} else {
147172
debugHandle(args, section);

0 commit comments

Comments
 (0)