util: improve inspection.
This commit is contained in:
parent
340d4e3272
commit
963c2173dd
@ -16,6 +16,20 @@ const nodeUtil = require('util');
|
||||
|
||||
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).
|
||||
* @param {Array} time
|
||||
@ -278,9 +292,11 @@ util.isFloat = function isFloat(value) {
|
||||
*/
|
||||
|
||||
util.inspectify = function inspectify(obj, color) {
|
||||
return typeof obj !== 'string'
|
||||
? nodeUtil.inspect(obj, null, 20, color !== false)
|
||||
: obj;
|
||||
if (typeof obj === 'string')
|
||||
return obj;
|
||||
|
||||
inspectOptions.colors = color !== false;
|
||||
return nodeUtil.inspect(obj, inspectOptions);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -300,12 +316,12 @@ util.fmt = nodeUtil.format;
|
||||
*/
|
||||
|
||||
util.format = function format(args, color) {
|
||||
if (color == null)
|
||||
color = process.stdout ? process.stdout.isTTY : false;
|
||||
|
||||
return typeof args[0] === 'object'
|
||||
? util.inspectify(args[0], color)
|
||||
: util.fmt.apply(util, args);
|
||||
if (args.length > 0 && args[0] && typeof args[0] === 'object') {
|
||||
if (color == null)
|
||||
color = !!(process.stdout && process.stdout.isTTY);
|
||||
return util.inspectify(args[0], color);
|
||||
}
|
||||
return util.fmt(...args);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -318,14 +334,17 @@ util.log = function log(...args) {
|
||||
let msg;
|
||||
|
||||
if (!process.stdout) {
|
||||
msg = typeof args[0] !== 'object'
|
||||
? util.format(args, false)
|
||||
: args[0];
|
||||
if (args.length > 0) {
|
||||
msg = typeof args[0] !== 'object'
|
||||
? util.fmt(...args)
|
||||
: args[0];
|
||||
}
|
||||
console.log(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
msg = util.format(args);
|
||||
|
||||
process.stdout.write(msg + '\n');
|
||||
};
|
||||
|
||||
@ -339,14 +358,17 @@ util.error = function error(...args) {
|
||||
let msg;
|
||||
|
||||
if (!process.stderr) {
|
||||
msg = typeof args[0] !== 'object'
|
||||
? util.format(args, false)
|
||||
: args[0];
|
||||
if (args.length > 0) {
|
||||
msg = typeof args[0] !== 'object'
|
||||
? util.fmt(...args)
|
||||
: args[0];
|
||||
}
|
||||
console.error(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
msg = util.format(args);
|
||||
|
||||
process.stderr.write(msg + '\n');
|
||||
};
|
||||
|
||||
@ -365,9 +387,7 @@ util.now = function now() {
|
||||
*/
|
||||
|
||||
util.ms = function ms() {
|
||||
if (Date.now)
|
||||
return Date.now();
|
||||
return +new Date();
|
||||
return Date.now();
|
||||
};
|
||||
|
||||
/**
|
||||
@ -385,7 +405,7 @@ util.date = function date(ts) {
|
||||
|
||||
/**
|
||||
* Get unix seconds from a Date string.
|
||||
* @param {String} date - Date ISO String.
|
||||
* @param {String?} date - Date ISO String.
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user