Git Product home page Git Product logo

yeast's Introduction

yeast

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Sauce Test Status

Yeast is a unique id generator. It has been primarily 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, they would be identical because the timing accuracy is limited to milliseconds.

Yeast solves both of these issues by:

  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.

Installation

The module is intended to be used in browsers as well as in Node.js and is therefore released in the npm registry and can be installed using:

npm install --save yeast

Usage

All the examples assume that this library is initialized as follow:

'use strict';

var yeast = require('yeast');

To generate an id just call the yeast function.

console.log(yeast(), yeast(), yeast()); // outputs: KyxidwN KyxidwN.0 KyxidwN.1

setTimeout(function () {
  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.

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.

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 please open an issue!

License

MIT

yeast's People

Contributors

3rd-eden avatar greenkeeper[bot] avatar lpinca avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.