From 9c7b678210ba8a53f281cff11980d47c16e19425 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 28 Nov 2016 16:00:00 +0100 Subject: [PATCH 1/2] Use same color for same namespace. CF #258 --- debug.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/debug.js b/debug.js index 6e9f5213..34a47f3a 100644 --- a/debug.js +++ b/debug.js @@ -42,13 +42,20 @@ var prevTime; /** * Select a color. - * + * @param {String} namespace * @return {Number} * @api private */ -function selectColor() { - return exports.colors[prevColor++ % exports.colors.length]; +function selectColor(namespace) { + var hash = 0, i, chr, len; + + for (i in namespace) { + hash = ((hash << 5) - hash) + namespace.charCodeAt(i); + hash |= 0; // Convert to 32bit integer + } + + return exports.colors[Math.abs(hash) % exports.colors.length]; } /** @@ -81,7 +88,7 @@ function debug(namespace) { // add the `color` if not set if (null == self.useColors) self.useColors = exports.useColors(); - if (null == self.color && self.useColors) self.color = selectColor(); + if (null == self.color && self.useColors) self.color = selectColor(namespace); var args = new Array(arguments.length); for (var i = 0; i < args.length; i++) { From 8e387222e17c596e799884dce453c7d0aacc14fa Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 28 Nov 2016 18:25:11 +0100 Subject: [PATCH 2/2] Remove unused var. --- debug.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug.js b/debug.js index 34a47f3a..b7fc5176 100644 --- a/debug.js +++ b/debug.js @@ -48,7 +48,7 @@ var prevTime; */ function selectColor(namespace) { - var hash = 0, i, chr, len; + var hash = 0, i; for (i in namespace) { hash = ((hash << 5) - hash) + namespace.charCodeAt(i);