0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

use util.inspect to format objects and arrays printed to the console

This commit is contained in:
Sami Samhuri 2010-12-30 19:20:47 -05:00 committed by Ryan Dahl
parent ca8cc71bd0
commit e5a47c8ce1

View File

@ -3,8 +3,10 @@ var writeError = process.binding('stdio').writeError;
// console object
var formatRegExp = /%[sdj]/g;
function format(f) {
var util = require('util');
if (typeof f !== 'string') {
var objects = [], util = require('util');
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(util.inspect(arguments[i]));
}
@ -16,15 +18,19 @@ function format(f) {
var args = arguments;
var str = String(f).replace(formatRegExp, function(x) {
switch (x) {
case '%s': return args[i++];
case '%d': return +args[i++];
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j': return JSON.stringify(args[i++]);
default:
return x;
}
});
for (var len = args.length; i < len; ++i) {
str += ' ' + args[i];
for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + util.inspect(x);
}
}
return str;
}