Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 33 additions and 10 deletions.
  1. +31 −8 lib/shortid.js
  2. +2 −2 package.json
View
39 lib/shortid.js
@@ -3,8 +3,16 @@
* by Dylan Greene
*
*/
-var randomBytes = require('crypto').randomBytes
+var randomBytes = require('crypto').randomBytes;
+var DEBUG = false;
+
+// Older (pre 0.6) versions of Node don't have a randomBytes function
+if (!randomBytes) {
+ randomBytes = function(){
+ return [Math.random()*256];
+ }
+}
// remove from Date.now all the milliseconds before this thing was created
var LESS_TIME = 1324151035201;
@@ -24,13 +32,22 @@ var options = {
var counter = -1;
var previousSeconds;
+function toString(buffer) {
+ var str = '';
+ var i;
+ for(i = 0; i < buffer.length; i++) {
+ str = str + ALPHABET_SHUFFLED[buffer[i]];
+ }
+ return str;
+}
+
/**
* Generate the id
* Returns a unique short id.
*/
function generate() {
- var seconds = Date.now() - LESS_TIME >> 10; //divide by 10 to get seconds
+ var seconds = Math.round((Date.now() - LESS_TIME) * 0.01); // using >> got us negative numbers somehow
counter = seconds == previousSeconds ? counter + 1 : 0;
@@ -47,20 +64,26 @@ function generate() {
}
encode(options.version);
+
+ DEBUG && console.log('just version', toString(buffer));
+
encode(options.worker);
+
+ DEBUG && console.log('worker', toString(buffer));
+
if (counter > 0) {
encode(counter);
+ DEBUG && console.log('counter', toString(buffer));
+
}
encode(seconds); //seconds since this module was created. >> 10 removes the milliseconds more or less.
+ DEBUG && console.log('seconds', toString(buffer), seconds);
+
previousSeconds = seconds;
- var str = '';
- var i;
- for(i = 0; i < buffer.length; i++) {
- str = str + ALPHABET_SHUFFLED[buffer[i]];
- }
- return str;
+ return toString(buffer);
+
}
/**
View
4 package.json
@@ -1,6 +1,6 @@
{
"name":"shortid",
- "version":"1.0.3",
+ "version":"1.0.6",
"description":"Amazingly short non-sequential url-friendly unique id generator.",
"keywords": ["short", "tiny", "id", "uuid", "bitly", "shorten", "mongoid", "shortid", "tinyid", "id"],
"homepage":"http://github.com/dylang/shortid",
@@ -23,7 +23,7 @@
},
"main":"index",
"engines":{
- "node":">=0.6"
+ "node":">=0.4.x"
},
"scripts":{
"test": "make test"

No commit comments for this range

Something went wrong with that request. Please try again.