crystal

Tiny math-based unique id generator.

npm install crystal
2 downloads in the last week
4 downloads in the last month

crystal Build Status

Tiny math-based unique id generator.

Installation

Node.js

crystal is available on npm.

$ npm install crystal

Component

crystal is available as a component.

$ component install qualiancy/crystal

Usage

  • @param {Object} options

Create an object to generate random ids and store them in memory to avoid collision.

var crystal = require('crystal')(opts);
Options
  • base {Number} base number format. Default 16.
  • bits {Number} bit length of key. Default 128.
  • grow {Number} increase bits by grow when heavy collision occurs. Default 32.

.claim ([id])

  • @param {String} (optional) id to claim
  • @return {String} id added to storage

Add an id to the internal storage or generate a new random id and add to the internal storage. If id is specified and it already exists an Error will be thrown.

var id = crystal.claim();

.release (id)

  • @param {String} id to release
  • @return {Boolean} release succussful

Remove an id from the internal storage thus allowing it to be used re-claimed at a later time.

crystal.release(id);

.has (id)

  • @param {String} id
  • @return {Boolean} in storage

Determine if a a given id has already been claimed.

.random ()

  • @return {String} id

Generate a single random id but do not add it to the storage for collision detection.

var id1 = crystal.random()
  , id2 = crystal.random();

License

(The MIT License)

Copyright (c) 2012 Jake Luer jake@qualiancy.com (http://qualiancy.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you