Skip to content

Commit

Permalink
Add two more url-safe characters to make the distribution even.
Browse files Browse the repository at this point in the history
  • Loading branch information
STRML committed Dec 15, 2014
1 parent de1b409 commit a3b8def
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
var crypto = require('crypto');

/**
* 62 characters in the ascii range that can be used in URLs without special
* 64 characters in the ascii range that can be used in URLs without special
* encoding.
*/
var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';

/**
* Make a Buffer into a string ready for use in URLs
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "strong uid",
"tags": ["uid"],
"version": "0.0.3",
"scripts": {
"test": "node test.js"
},
"dependencies": {
}
}
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
var uid = require('./index');
var assert = require('assert');

var freqs = {};
var ITERATIONS = 1000000;
var LENGTH = 64;
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var i;

for (i = 0; i < chars.length; i++) {
freqs[chars[i]] = 0;
}

console.log("Running", ITERATIONS, "iterations...");

while(ITERATIONS--) {
var str = uid(LENGTH);
for (var i = 0; i < str.length; i++) {
freqs[str[i]]++;
}
}

console.log("Done. Distribution:");
console.log(JSON.stringify(freqs, undefined, 2));

var vals = Object.keys(freqs).map(function(key) {
return freqs[key];
});
var min = Math.min.apply(null, vals);
var max = Math.max.apply(null, vals);

console.log("Min freq:", min, "Max freq:", max);
var diffPcnt = Math.abs(min / max - 1) * 100;
console.log("Min and max frequencies are " + diffPcnt + "% apart.");
assert(diffPcnt < 1);

0 comments on commit a3b8def

Please sign in to comment.