util: improve inspection.

This commit is contained in:
Christopher Jeffrey 2017-07-24 19:05:23 -07:00
parent 340d4e3272
commit 963c2173dd
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -16,6 +16,20 @@ const nodeUtil = require('util');
const util = exports; const util = exports;
/*
* Constants
*/
const inspectOptions = {
showHidden: false,
depth: 20,
colors: false,
customInspect: true,
showProxy: false,
maxArrayLength: Infinity,
breakLength: 60
};
/** /**
* Return hrtime (shim for browser). * Return hrtime (shim for browser).
* @param {Array} time * @param {Array} time
@ -278,9 +292,11 @@ util.isFloat = function isFloat(value) {
*/ */
util.inspectify = function inspectify(obj, color) { util.inspectify = function inspectify(obj, color) {
return typeof obj !== 'string' if (typeof obj === 'string')
? nodeUtil.inspect(obj, null, 20, color !== false) return obj;
: obj;
inspectOptions.colors = color !== false;
return nodeUtil.inspect(obj, inspectOptions);
}; };
/** /**
@ -300,12 +316,12 @@ util.fmt = nodeUtil.format;
*/ */
util.format = function format(args, color) { util.format = function format(args, color) {
if (color == null) if (args.length > 0 && args[0] && typeof args[0] === 'object') {
color = process.stdout ? process.stdout.isTTY : false; if (color == null)
color = !!(process.stdout && process.stdout.isTTY);
return typeof args[0] === 'object' return util.inspectify(args[0], color);
? util.inspectify(args[0], color) }
: util.fmt.apply(util, args); return util.fmt(...args);
}; };
/** /**
@ -318,14 +334,17 @@ util.log = function log(...args) {
let msg; let msg;
if (!process.stdout) { if (!process.stdout) {
msg = typeof args[0] !== 'object' if (args.length > 0) {
? util.format(args, false) msg = typeof args[0] !== 'object'
: args[0]; ? util.fmt(...args)
: args[0];
}
console.log(msg); console.log(msg);
return; return;
} }
msg = util.format(args); msg = util.format(args);
process.stdout.write(msg + '\n'); process.stdout.write(msg + '\n');
}; };
@ -339,14 +358,17 @@ util.error = function error(...args) {
let msg; let msg;
if (!process.stderr) { if (!process.stderr) {
msg = typeof args[0] !== 'object' if (args.length > 0) {
? util.format(args, false) msg = typeof args[0] !== 'object'
: args[0]; ? util.fmt(...args)
: args[0];
}
console.error(msg); console.error(msg);
return; return;
} }
msg = util.format(args); msg = util.format(args);
process.stderr.write(msg + '\n'); process.stderr.write(msg + '\n');
}; };
@ -365,9 +387,7 @@ util.now = function now() {
*/ */
util.ms = function ms() { util.ms = function ms() {
if (Date.now) return Date.now();
return Date.now();
return +new Date();
}; };
/** /**
@ -385,7 +405,7 @@ util.date = function date(ts) {
/** /**
* Get unix seconds from a Date string. * Get unix seconds from a Date string.
* @param {String} date - Date ISO String. * @param {String?} date - Date ISO String.
* @returns {Number} * @returns {Number}
*/ */