Skip to content

Commit

Permalink
Use same color for same namespace. (#338)
Browse files Browse the repository at this point in the history
* Use same color for same namespace.

CF #258

* Remove unused var.
  • Loading branch information
lchenay authored and thebigredgeek committed Dec 6, 2016
1 parent e2a1955 commit 501521f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions debug.js
Expand Up @@ -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;

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];
}

/**
Expand Down Expand Up @@ -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++) {
Expand Down

0 comments on commit 501521f

Please sign in to comment.