This site uses cookies. To consent, continue navigating this site or click the "Accept cookies" button.

Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ShortUniqueId

Generate random or sequential UUID of any length.

Use as module

// Deno (web module) Import
import ShortUniqueId from 'https://cdn.jsdelivr.net/npm/short-unique-id@latest/short_uuid/mod.ts';

// ES6 / TypeScript Import
import ShortUniqueId from 'short-unique-id';

//or Node.js require
const {default: ShortUniqueId} = require('short-unique-id');

//Instantiate
const uid = new ShortUniqueId();

// Random UUID
console.log(uid());

// Sequential UUID
console.log(uid.seq());

Use in browser

<!-- Import -->
<script src="https://cdn.jsdelivr.net/npm/short-unique-id@latest/dist/short-unique-id.min.js"></script>

<!-- Usage -->
<script>
  // Instantiate
  var uid = new ShortUniqueId();

  // Random UUID
  document.write(uid());

  // Sequential UUID
  document.write(uid.seq());
</script>

Options

Options can be passed when instantiating uid:

const options = { ... };

const uid = new ShortUniqueId(options);

For more information take a look at the Options type definition.

Hierarchy

  • Function
    • ShortUniqueId

Index

Properties

Readonly name

name: string

Returns the name of the function. Function names are read-only and can not be changed.

Methods

[Symbol.hasInstance]

  • [Symbol.hasInstance](value: any): boolean
  • Determines whether the given value inherits from this function if this function was used as a constructor function.

    A constructor function can control which objects are recognized as its instances by 'instanceof' by overriding this method.

    Parameters

    • value: any

    Returns boolean

apply

  • apply(this: Function, thisArg: any, argArray?: any): any
  • Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.

    Parameters

    • this: Function
    • thisArg: any

      The object to be used as the this object.

    • Optional argArray: any

      A set of arguments to be passed to the function.

    Returns any

approxMaxBeforeCollision

  • approxMaxBeforeCollision(rounds?: number): number
  • Calculates approximate number of hashes before first collision.

    Given that:

    • H is the total number of possible UUIDs, or in terms of this library, the result of running availableUUIDs()
    • the expected number of values we have to choose before finding the first collision can be expressed as the quantity Q(H)

    Then Q(H) can be approximated as the square root of the of the product of half of pi times H:

    This function returns Q(H).

    Parameters

    • Default value rounds: number = this.availableUUIDs(this.uuidLength)

    Returns number

availableUUIDs

  • availableUUIDs(uuidLength?: number): number
  • Calculates total number of possible UUIDs.

    Given that:

    • H is the total number of possible UUIDs
    • n is the number of unique characters in the dictionary
    • l is the UUID length

    Then H is defined as n to the power of l:

    This function returns H.

    Parameters

    • Default value uuidLength: number = this.uuidLength

    Returns number

bind

  • bind(this: Function, thisArg: any, ...argArray: any[]): any
  • For a given function, creates a bound function that has the same body as the original function. The this object of the bound function is associated with the specified object, and has the specified initial parameters.

    Parameters

    • this: Function
    • thisArg: any

      An object to which the this keyword can refer inside the new function.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the new function.

    Returns any

call

  • call(this: Function, thisArg: any, ...argArray: any[]): any
  • Calls a method of an object, substituting another object for the current object.

    Parameters

    • this: Function
    • thisArg: any

      The object to be used as the current object.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the method.

    Returns any

collisionProbability

  • collisionProbability(rounds?: number, uuidLength?: number): number
  • Calculates probability of generating duplicate UUIDs (a collision) in a given number of UUID generation rounds.

    Given that:

    • r is the maximum number of times that randomUUID() will be called, or better said the number of rounds
    • H is the total number of possible UUIDs, or in terms of this library, the result of running availableUUIDs()

    Then the probability of collision p(r; H) can be approximated as the result of dividing the square root of the of the product of half of pi times H by H:

    This function returns p(r; H).

    (Useful if you are wondering "If I use this lib and expect to perform at most r rounds of UUID generations, what is the probability that I will hit a duplicate UUID?".)

    Parameters

    • Default value rounds: number = this.availableUUIDs(this.uuidLength)
    • Default value uuidLength: number = this.uuidLength

    Returns number

getVersion

  • getVersion(): string
  • Return the version of this module.

    Returns string

randomUUID

  • randomUUID(uuidLength?: number): string
  • Generates UUID by creating each part randomly.

    alias

    const uid = new ShortUniqueId(); uid(uuidLength: number);

    Parameters

    • Default value uuidLength: number = this.uuidLength || DEFAULT_UUID_LENGTH

    Returns string

sequentialUUID

  • sequentialUUID(): string
  • Generates UUID based on internal counter that's incremented after each ID generation.

    alias

    const uid = new ShortUniqueId(); uid.seq();

    Returns string

setDictionary

  • setDictionary(dictionary: string[]): void
  • Change the dictionary after initialization.

    Parameters

    • dictionary: string[]

    Returns void

toString

  • toString(): string

uniqueness

  • uniqueness(rounds?: number): number
  • Calculate a "uniqueness" score (from 0 to 1) of UUIDs based on size of dictionary and chosen UUID length.

    Given that:

    • H is the total number of possible UUIDs, or in terms of this library, the result of running availableUUIDs()
    • Q(H) is the approximate number of hashes before first collision, or in terms of this library, the result of running approxMaxBeforeCollision()

    Then uniqueness can be expressed as the additive inverse of the probability of generating a "word" I had previously generated (a duplicate) at any given iteration up to the the total number of possible UUIDs expressed as the quotiend of Q(H) and H:

    (Useful if you need a value to rate the "quality" of the combination of given dictionary and UUID length. The closer to 1, higher the uniqueness and thus better the quality.)

    Parameters

    • Default value rounds: number = this.availableUUIDs(this.uuidLength)

    Returns number