function cuid() { // Starting with a lowercase letter makes // it HTML element ID friendly. var letter = 'c', // hard-coded allows for sequential access // timestamp // warning: this exposes the exact date and time // that the uid was created. timestamp = (new Date().getTime()).toString(base), // Prevent same-machine collisions. counter, // A few chars to generate distinct ids for different // clients (so different computers are far less // likely to generate the same id) fingerprint = api.fingerprint(), // Grab some more chars from Math.random() random = randomBlock() + randomBlock(); counter = pad(safeCounter().toString(base), blockSize); return (letter + timestamp + counter + fingerprint + random); }
...
}
return pass;
};
test('cuid()', assert => {
assert.ok(typeof cuid() === 'string',
'.cuid() should return a string.');
assert.ok(collisionTest(cuid),
'cuids should not collide.');
assert.ok(collisionTest(slug),
'slugs should not collide.');
...
function nodePrint() { var os = require('os'), padding = 2, pid = pad((process.pid).toString(36), padding), hostname = os.hostname(), length = hostname.length, hostId = pad((hostname) .split('') .reduce(function (prev, char) { return +prev + char.charCodeAt(0); }, +length + 36) .toString(36), padding); return pid + hostId; }
...
// Prevent same-machine collisions.
counter,
// A few chars to generate distinct ids for different
// clients (so different computers are far less
// likely to generate the same id)
fingerprint = api.fingerprint(),
// Grab some more chars from Math.random()
random = randomBlock() + randomBlock();
counter = pad(safeCounter().toString(base), blockSize);
return (letter + timestamp + counter + fingerprint + random);
...
function slug() { var date = new Date().getTime().toString(36), counter, print = api.fingerprint().slice(0,1) + api.fingerprint().slice(-1), random = randomBlock().slice(-2); counter = safeCounter().toString(36).slice(-4); return date.slice(-2) + counter + print + random; }
...
* [cuid for PHP](https://github.com/endyjasmi/cuid) - [Endy Jasmi](https://github.com/endyjasmi)
* [cuid for Elixir](https://github.com/duailibe/cuid) - [Lucas Duailibe](https://github.com/duailibe)
* [cuid for Haskell](https://github.com/eightyeight/hscuid) - [Daniel Buckmaster](https://github.com/eightyeight)
# Short URLs
Need a smaller ID? `cuid.slug()` is for you. With fewer than 10 characters, `.slug()`
is a great solution for short urls. They're good for things like URL slug disambiguation (i.e., `example.com/some-post-title
-<slug>`) but **absolutely not recommended for database unique IDs**. Stick to the full cuid for database keys.
Be aware, slugs:
* are less likely to be sequential. Stick to full cuids for database lookups, if possible.
* have less random data, less room for the counter, and less room for the fingerprint, which means that all of them are more likely
to collide or be guessed, especially as CPU speeds increase.
...