Comparing version 0.0.0 to 0.1.1
76
index.js
'use strict'; | ||
var seed = 0 | ||
var alphabet = [ | ||
'0', '1', '2', '3', '4', '5', '6', '7', | ||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', | ||
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', | ||
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | ||
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', | ||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', | ||
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', | ||
'u', 'v', 'w', 'x', 'y', 'z', '-', '_' | ||
]; | ||
var length = alphabet.length | ||
, map = {} | ||
, seed = 0 | ||
, i = 0 | ||
, prev; | ||
/** | ||
* Return a string representing the specified number. | ||
* | ||
* @param {Number} num The number to convert. | ||
* @returns {String} The string representation of the number. | ||
* @api public | ||
*/ | ||
function encode(num) { | ||
var encoded = ''; | ||
do { | ||
encoded = alphabet[num % length] + encoded; | ||
num = Math.floor(num / length); | ||
} while (num > 0); | ||
return encoded; | ||
} | ||
/** | ||
* Return the integer value specified by the given string. | ||
* | ||
* @param {String} str The string to convert. | ||
* @returns {Number} The integer value represented by the string. | ||
* @api public | ||
*/ | ||
function decode(str) { | ||
var decoded = 0 | ||
, i = 0; | ||
while (i < str.length) { | ||
decoded = decoded * length + map[str.charAt(i)]; | ||
i++; | ||
} | ||
return decoded; | ||
} | ||
/** | ||
* Yeast: A tiny growing id generator. | ||
@@ -12,7 +63,22 @@ * | ||
*/ | ||
module.exports = function yeast() { | ||
var now = (+new Date()).toString(36); | ||
function yeast() { | ||
var now = encode(+new Date()); | ||
if (now !== prev) return seed = 0, prev = now; | ||
return now+':'+ (seed++).toString(36); | ||
}; | ||
return now +'.'+ encode(seed++); | ||
} | ||
// | ||
// Map each character to its index. | ||
// | ||
while (i < length) { | ||
map[alphabet[i]] = i; | ||
i++; | ||
} | ||
// | ||
// Expose the `yeast`, `encode` and `decode` functions. | ||
// | ||
yeast.encode = encode; | ||
yeast.decode = decode; | ||
module.exports = yeast; |
{ | ||
"name": "yeast", | ||
"version": "0.0.0", | ||
"version": "0.1.1", | ||
"description": "Tiny but linear growing unique id generator", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --reporter spec --ui bdd test.js" | ||
"100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", | ||
"test": "mocha test.js", | ||
"watch": "mocha --watch test.js", | ||
"coverage": "istanbul cover _mocha -- test.js", | ||
"test-travis": "istanbul cover _mocha --report lcovonly -- test.js" | ||
}, | ||
@@ -26,6 +30,7 @@ "repository": { | ||
"devDependencies": { | ||
"assume": "0.0.9", | ||
"mocha": "2.0.1", | ||
"pre-commit": "0.0.9" | ||
"assume": "1.2.x", | ||
"istanbul": "0.3.x", | ||
"mocha": "2.2.x", | ||
"pre-commit": "1.1.x" | ||
} | ||
} |
# yeast | ||
Yeast is a unique id generator. It was primary designed to generate a unique id | ||
which can be used for cache busting in requests. A common practise for this is | ||
to use a time stamp. But there are couple of down sides when using timestamps. | ||
[![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](https://img.shields.io/npm/v/yeast.svg?style=flat-square)](http://browsenpm.org/package/yeast)[![Build Status](https://img.shields.io/travis/unshiftio/yeast/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/yeast)[![Dependencies](https://img.shields.io/david/unshiftio/yeast.svg?style=flat-square)](https://david-dm.org/unshiftio/yeast)[![Coverage Status](https://img.shields.io/coveralls/unshiftio/yeast/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/yeast?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=unshift) | ||
1. The times tamp is already 13 chars long. This might not matter for 1 request | ||
Yeast is a unique id generator. It has been primary designed to generate a | ||
unique id which can be used for cache busting. A common practice for this is | ||
to use a timestamp, but there are couple of downsides when using timestamps. | ||
1. The timestamp is already 13 chars long. This might not matter for 1 request | ||
but if you make hundreds of them this quickly adds up in bandwidth and | ||
processing time. | ||
2. It's not unique enough. If you generate two stamps right after each other the | ||
time wouldn't have passed yet so they would be identical. | ||
2. It's not unique enough. If you generate two stamps right after each other, | ||
they would be identical because the timing accuracy is limited to | ||
milliseconds. | ||
Yeast solves both of these issues by: | ||
1. Compressing the generated time stamp to a string instead of a number using | ||
`toSting(36)`. | ||
2. Seed the generated id when the previous generated id is the same as the one | ||
it just generated. | ||
1. Compressing the generated timestamp using a custom `encode()` function that | ||
returns a string representation of the number. | ||
2. Seeding the id in case of collision (when the id is identical to the previous | ||
one). | ||
To keep the strings unique it will use the `:` char to separate the generated | ||
stamp from the seed. As we're using `toString(36)` to encode the time stamp you | ||
can still retrieve the actual time stamp by parsing it again using | ||
`parseInt(yeastID, 36)`. | ||
To keep the strings unique it will use the `.` char to separate the generated | ||
stamp from the seed. | ||
@@ -28,3 +29,3 @@ ## Installation | ||
The module is intended to be used in browsers as well as in Node.js and is | ||
therefor released in the npm registry and can be installed using: | ||
therefore released in the npm registry and can be installed using: | ||
@@ -37,4 +38,3 @@ ``` | ||
This module only exposes one single interface, so when you require yeast it will | ||
return a function that generates the unique ids: | ||
All the examples assume that this library is initialized as follow: | ||
@@ -45,10 +45,34 @@ ```js | ||
var yeast = require('yeast'); | ||
``` | ||
console.log(yeast(), yeast(), yeast()); // outputs: i24hcpc4 i24hcpc4:0 i24hcpc4:1 | ||
To generate an id just call the `yeast` function. | ||
```js | ||
console.log(yeast(), yeast(), yeast()); // outputs: KyxidwN KyxidwN:0 KyxidwN:1 | ||
setTimeout(function () { | ||
console.log(yeast()); // outputs: i24hd9hw | ||
console.log(yeast()); // outputs: KyxidwO | ||
}); | ||
``` | ||
### yeast.encode(num) | ||
An helper function that returns a string representing the specified number. The | ||
returned string contains only URL safe characters. | ||
```js | ||
yeast.encode(+new Date()); // outputs: Kyxjuo1 | ||
``` | ||
### yeast.decode(str) | ||
An helper function that returns the integer value specified by the given string. | ||
This function can be used to retrieve the timestamp from a `yeast` id. | ||
```js | ||
var id = yeast(); // holds the value: Kyxl1OU | ||
yeast.decode(id); // outputs: 1439816226334 | ||
``` | ||
That's all folks. If you have ideas on how we can further compress the ids | ||
@@ -59,2 +83,2 @@ please open an issue! | ||
MIT | ||
[MIT](LICENSE) |
6553
71
81
4
4