Skip to content

Ilushling/IdGenerator

Repository files navigation

IdGenerator

IdGenerator

  1. Features;
  2. Usage:
    1. Prepare:
      1. Create id generator:
        1. Unsafe;
        2. Safe;
        3. Dictionary and random pool size.
    2. Commands:
      1. Create.

Features

  1. Unlimited id size;
  2. Configurable random pool size;
  3. Replacable dictionary;
  4. Support replacable Crypto (requires Buffer);
  5. Fallback to Math.random (if Crypto & Buffer not used).

Usage

Prepare

import { IdGenerator } 'mainid';

Create id generator

Unsafe

Math.random for random

const idGenerator = new IdGenerator();
Safe
  • Crypto - usage: crypto.getRandomValues(buffer);
  • Buffer is required - usage: buffer = buffer.alloc(randomPoolSize).
const idGenerator = new IdGenerator({
  crypto: /** @type {Crypto} */ (crypto)
});
Dictionary and random pool size

Optinal.

const idGenerator = new IdGenerator({
  dictionary: 'abc',
  randomPoolSize: 3
});

Commands

  1. Create;

Create

const id = idGenerator.create();