Fixed format, so it wouldn’t blow up if %d argument is null or undefined + ensure that numbers will be numbers

v0.7.4-release
Dmitry Baranovskiy 2010-07-22 21:26:55 +10:00 committed by Ryan Dahl
parent 2c1ca4078d
commit a585c5bbb3
1 changed files with 3 additions and 4 deletions

View File

@ -190,15 +190,14 @@ process.openStdin = function () {
// console object // console object
var formatRegExp = /%[sdj]/g;
function format (f) { function format (f) {
var i = 1; var i = 1;
var args = arguments; var args = arguments;
if (!(f instanceof String)) f = String(f); return String(f).replace(formatRegExp, function (x) {
return f.replace(/%([sdj])/g, function (x) {
switch (x) { switch (x) {
case '%s': return args[i++]; case '%s': return args[i++];
case '%d': return args[i++].toString(); case '%d': return +args[i++];
case '%j': return JSON.stringify(args[i++]); case '%j': return JSON.stringify(args[i++]);
default: default:
return x; return x;