Skip to content

Commit

Permalink
increment counter within the second, not the millisecond. fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
dylang committed Nov 7, 2014
1 parent f9833c1 commit a646a14
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/encode.js
@@ -1,6 +1,6 @@
var randomBytes = require('crypto').randomBytes;

module.exports = function encode(lookup, number) {
function encode(lookup, number) {
var loopCounter = 0;
var done;

Expand All @@ -12,4 +12,6 @@ module.exports = function encode(lookup, number) {
loopCounter++;
}
return str;
};
}

module.exports = encode;
16 changes: 8 additions & 8 deletions shortid.js → lib/shortid.js
Expand Up @@ -3,17 +3,17 @@
* by Dylan Greene
*/

var alphabet = require('./lib/alphabet'),
encode = require('./lib/encode');
var alphabet = require('./alphabet'),
encode = require('./encode');

// Ignore all milliseconds before a certain time to reduce the size of the date entropy without sacrificing uniqueness.
// This number should be updated every year or so to keep the generated id short.
// To regenerate `new Date() - 0` and bump the version. Always bump the version!
var REDUCE_TIME = 1414337829912;
var REDUCE_TIME = 1415358116771;

// don't change unless we change the algos or REDUCE_TIME
// must be an integer and less than 16
var version = 3;
var version = 4;

// if you are using cluster or multiple servers use this to make each instance
// has a unique value for worker
Expand All @@ -33,9 +33,9 @@ function generate() {

var str = '';

var seconds = Math.round((Date.now() - REDUCE_TIME) * 0.01);
var seconds = Math.round((Date.now() - REDUCE_TIME) * 0.001);

if (seconds == previousSeconds) {
if (seconds === previousSeconds) {
counter++;
} else {
counter = 0;
Expand All @@ -59,8 +59,8 @@ function generate() {
* exposed as ShortId.seed(int)
* @param seed Integer value to seed the random alphabet. ALWAYS USE THE SAME SEED or you might get overlaps.
*/
function seed(seed) {
alphabet.seed(seed);
function seed(seedValue) {
alphabet.seed(seedValue);
return module.exports;
}

Expand Down
8 changes: 2 additions & 6 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"name": "shortid",
"version": "2.1.2",
"description": "Amazingly short non-sequential url-friendly unique id generator.",
"main": "shortid.js",
"main": "lib/shortid.js",
"scripts": {
"test": "grunt test"
},
Expand All @@ -27,19 +27,15 @@
"url": "https://github.com/dylang/shortid"
},
"devDependencies": {
"chai": "~1.9.1",
"chai": "^1.9.1",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0",
"grunt-mocha-test": "^0.12.2",
"grunt-notify": "^0.4.1",
"grunt-release": "^0.7.0",
"grunt-templates-dylang": "^1.0.1",
"load-grunt-tasks": "^1.0.0",
"mocha": "~2.0.1",
"time-grunt": "^1.0.0"
},
"engines": {
"node": ">=0.8.x"
},
"license": "MIT"
}
4 changes: 2 additions & 2 deletions test/shortid.test.js
@@ -1,4 +1,4 @@
var shortId = require('../shortid');
var shortId = require('../lib/shortid');
var expect = require('chai').expect;

describe('testing shortid', function(done) {
Expand All @@ -15,7 +15,7 @@ describe('testing shortid', function(done) {
var ids = {};
var id;

var i=50000;
var i=5000;
while(i--) {
id = shortId.generate();
expect(id.length).to.be.below(17);
Expand Down

0 comments on commit a646a14

Please sign in to comment.