Skip to content

Commit 501521f

Browse files
lchenaythebigredgeek
authored andcommitted
Use same color for same namespace. (#338)
* Use same color for same namespace. CF #258 * Remove unused var.
1 parent e2a1955 commit 501521f

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

debug.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ var prevTime;
4242

4343
/**
4444
* Select a color.
45-
*
45+
* @param {String} namespace
4646
* @return {Number}
4747
* @api private
4848
*/
4949

50-
function selectColor() {
51-
return exports.colors[prevColor++ % exports.colors.length];
50+
function selectColor(namespace) {
51+
var hash = 0, i;
52+
53+
for (i in namespace) {
54+
hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
55+
hash |= 0; // Convert to 32bit integer
56+
}
57+
58+
return exports.colors[Math.abs(hash) % exports.colors.length];
5259
}
5360

5461
/**
@@ -81,7 +88,7 @@ function debug(namespace) {
8188

8289
// add the `color` if not set
8390
if (null == self.useColors) self.useColors = exports.useColors();
84-
if (null == self.color && self.useColors) self.color = selectColor();
91+
if (null == self.color && self.useColors) self.color = selectColor(namespace);
8592

8693
var args = new Array(arguments.length);
8794
for (var i = 0; i < args.length; i++) {

0 commit comments

Comments
 (0)