Tiny math-based unique id generator.
crystal
is available on npm.
$ npm install crystal
crystal
is available as a component.
$ component install qualiancy/crystal
- @param {Object} options
Create an object to generate random ids and store them in memory to avoid collision.
var crystal = require('crystal')(opts);
base
{Number} base number format. Default16
.bits
{Number} bit length of key. Default128
.grow
{Number} increasebits
bygrow
when heavy collision occurs. Default32
.
- @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();
- @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);
- @param {String} id
- @return {Boolean} in storage
Determine if a a given id
has already
been claimed.
- @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();
(The MIT License)
Copyright (c) 2012 Jake Luer [email protected] (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.