From aa9b0b65fb6a1a71fc46b5cf035787136175fcf6 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Thu, 2 Feb 2012 16:52:37 -0800 Subject: [PATCH] Added: humanize diffs. Closes #8 --- lib/debug.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/debug.js b/lib/debug.js index 6d7c333..b1457d6 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -69,6 +69,25 @@ function color() { return colors[prevColor++ % colors.length]; } +/** + * Humanize the given `ms`. + * + * @param {Number} m + * @return {String} + * @api private + */ + +function humanize(ms) { + var sec = 1000 + , min = 60 * 1000 + , hour = 60 * min; + + if (ms >= hour) return (ms / hour).toFixed(1) + 'h'; + if (ms >= min) return (ms / min).toFixed(1) + 'm'; + if (ms >= sec) return (ms / sec | 0) + 's'; + return ms + 'ms'; +} + /** * Create a debugger with the given `name`. * @@ -93,7 +112,7 @@ function debug(name) { fmt = ' \033[9' + c + 'm' + name + ' ' + '\033[3' + c + 'm\033[90m' + fmt + '\033[3' + c + 'm' - + ' +' + ms + 'ms\033[0m'; + + ' +' + humanize(ms) + '\033[0m'; console.error.apply(this, arguments); }