Skip to content

Commit e179414

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

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/debug.js

+39-19
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,32 @@ 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+
var index = 0;
68+
args[0] = args[0].replace(/%([a-zA-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+
5387
/**
5488
* Create a debugger with the given `namespace`.
5589
*
@@ -90,22 +124,7 @@ function createDebug(namespace) {
90124
}
91125

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,9 @@ 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 rightArgs = formatInlineArgs(debug, [].slice.call(extraArgs));
164+
var newArgs = (leftArgs.length > 0 && rightArgs.length > 0) ? leftArgs.concat(['::']).concat(rightArgs) : leftArgs.concat(rightArgs);
145165
debugHandle(newArgs, section);
146166
} else {
147167
debugHandle(args, section);

0 commit comments

Comments
 (0)