From a646a14e02f34a4fd5587a83e185a22680a6f140 Mon Sep 17 00:00:00 2001 From: Dylan Greene Date: Fri, 7 Nov 2014 06:12:32 -0500 Subject: [PATCH] increment counter within the second, not the millisecond. fixes #21 --- lib/encode.js | 6 ++++-- shortid.js => lib/shortid.js | 16 ++++++++-------- package.json | 8 ++------ test/shortid.test.js | 4 ++-- 4 files changed, 16 insertions(+), 18 deletions(-) rename shortid.js => lib/shortid.js (90%) diff --git a/lib/encode.js b/lib/encode.js index 6978cf7..683056e 100644 --- a/lib/encode.js +++ b/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; @@ -12,4 +12,6 @@ module.exports = function encode(lookup, number) { loopCounter++; } return str; -}; \ No newline at end of file +} + +module.exports = encode; diff --git a/shortid.js b/lib/shortid.js similarity index 90% rename from shortid.js rename to lib/shortid.js index 94032e5..0264822 100644 --- a/shortid.js +++ b/lib/shortid.js @@ -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 @@ -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; @@ -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; } diff --git a/package.json b/package.json index 3ed553f..7e45d89 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -27,7 +27,7 @@ "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", @@ -35,11 +35,7 @@ "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" } diff --git a/test/shortid.test.js b/test/shortid.test.js index 646c7b8..bce82c2 100644 --- a/test/shortid.test.js +++ b/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) { @@ -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);