root / HServer / 00.Server / 00.Program / node_modules / yeast / README.md
이력 | 보기 | 이력해설 | 다운로드 (2.92 KB)
1 |
# yeast |
---|---|
2 |
|
3 |
[](http://unshift.io)[](http://browsenpm.org/package/yeast)[](https://travis-ci.org/unshiftio/yeast)[](https://david-dm.org/unshiftio/yeast)[](https://coveralls.io/r/unshiftio/yeast?branch=master)[](https://webchat.freenode.net/?channels=unshift) |
4 |
|
5 |
[](https://saucelabs.com/u/yeast) |
6 |
|
7 |
Yeast is a unique id generator. It has been primarily designed to generate a |
8 |
unique id which can be used for cache busting. A common practice for this is |
9 |
to use a timestamp, but there are couple of downsides when using timestamps. |
10 |
|
11 |
1. The timestamp is already 13 chars long. This might not matter for 1 request |
12 |
but if you make hundreds of them this quickly adds up in bandwidth and |
13 |
processing time. |
14 |
2. It's not unique enough. If you generate two stamps right after each other, |
15 |
they would be identical because the timing accuracy is limited to |
16 |
milliseconds. |
17 |
|
18 |
Yeast solves both of these issues by: |
19 |
|
20 |
1. Compressing the generated timestamp using a custom `encode()` function that |
21 |
returns a string representation of the number. |
22 |
2. Seeding the id in case of collision (when the id is identical to the previous |
23 |
one). |
24 |
|
25 |
To keep the strings unique it will use the `.` char to separate the generated |
26 |
stamp from the seed. |
27 |
|
28 |
## Installation |
29 |
|
30 |
The module is intended to be used in browsers as well as in Node.js and is |
31 |
therefore released in the npm registry and can be installed using: |
32 |
|
33 |
``` |
34 |
npm install --save yeast |
35 |
``` |
36 |
|
37 |
## Usage |
38 |
|
39 |
All the examples assume that this library is initialized as follow: |
40 |
|
41 |
```js |
42 |
'use strict'; |
43 |
|
44 |
var yeast = require('yeast'); |
45 |
``` |
46 |
|
47 |
To generate an id just call the `yeast` function. |
48 |
|
49 |
```js |
50 |
console.log(yeast(), yeast(), yeast()); // outputs: KyxidwN KyxidwN.0 KyxidwN.1 |
51 |
|
52 |
setTimeout(function () { |
53 |
console.log(yeast()); // outputs: KyxidwO |
54 |
}); |
55 |
``` |
56 |
|
57 |
### yeast.encode(num) |
58 |
|
59 |
An helper function that returns a string representing the specified number. The |
60 |
returned string contains only URL safe characters. |
61 |
|
62 |
```js |
63 |
yeast.encode(+new Date()); // outputs: Kyxjuo1 |
64 |
``` |
65 |
|
66 |
### yeast.decode(str) |
67 |
|
68 |
An helper function that returns the integer value specified by the given string. |
69 |
This function can be used to retrieve the timestamp from a `yeast` id. |
70 |
|
71 |
```js |
72 |
var id = yeast(); // holds the value: Kyxl1OU |
73 |
|
74 |
yeast.decode(id); // outputs: 1439816226334 |
75 |
``` |
76 |
|
77 |
That's all folks. If you have ideas on how we can further compress the ids |
78 |
please open an issue! |
79 |
|
80 |
## License |
81 |
|
82 |
[MIT](LICENSE) |