You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.9 KiB

8 years ago
  1. # yeast
  2. [![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)
  3. [![Sauce Test Status](https://saucelabs.com/browser-matrix/yeast.svg)](https://saucelabs.com/u/yeast)
  4. Yeast is a unique id generator. It has been primarily designed to generate a
  5. unique id which can be used for cache busting. A common practice for this is
  6. to use a timestamp, but there are couple of downsides when using timestamps.
  7. 1. The timestamp is already 13 chars long. This might not matter for 1 request
  8. but if you make hundreds of them this quickly adds up in bandwidth and
  9. processing time.
  10. 2. It's not unique enough. If you generate two stamps right after each other,
  11. they would be identical because the timing accuracy is limited to
  12. milliseconds.
  13. Yeast solves both of these issues by:
  14. 1. Compressing the generated timestamp using a custom `encode()` function that
  15. returns a string representation of the number.
  16. 2. Seeding the id in case of collision (when the id is identical to the previous
  17. one).
  18. To keep the strings unique it will use the `.` char to separate the generated
  19. stamp from the seed.
  20. ## Installation
  21. The module is intended to be used in browsers as well as in Node.js and is
  22. therefore released in the npm registry and can be installed using:
  23. ```
  24. npm install --save yeast
  25. ```
  26. ## Usage
  27. All the examples assume that this library is initialized as follow:
  28. ```js
  29. 'use strict';
  30. var yeast = require('yeast');
  31. ```
  32. To generate an id just call the `yeast` function.
  33. ```js
  34. console.log(yeast(), yeast(), yeast()); // outputs: KyxidwN KyxidwN.0 KyxidwN.1
  35. setTimeout(function () {
  36. console.log(yeast()); // outputs: KyxidwO
  37. });
  38. ```
  39. ### yeast.encode(num)
  40. An helper function that returns a string representing the specified number. The
  41. returned string contains only URL safe characters.
  42. ```js
  43. yeast.encode(+new Date()); // outputs: Kyxjuo1
  44. ```
  45. ### yeast.decode(str)
  46. An helper function that returns the integer value specified by the given string.
  47. This function can be used to retrieve the timestamp from a `yeast` id.
  48. ```js
  49. var id = yeast(); // holds the value: Kyxl1OU
  50. yeast.decode(id); // outputs: 1439816226334
  51. ```
  52. That's all folks. If you have ideas on how we can further compress the ids
  53. please open an issue!
  54. ## License
  55. [MIT](LICENSE)