nanoid

  • Version 5.0.9
  • Published
  • 11 kB
  • No dependencies
  • MIT license

Install

npm i nanoid
yarn add nanoid
pnpm add nanoid

Overview

A tiny (118 bytes), secure URL-friendly unique string ID generator

Index

Variables

variable urlAlphabet

const urlAlphabet: string;
  • URL safe symbols.

    import { urlAlphabet } from 'nanoid'
    const nanoid = customAlphabet(urlAlphabet, 10)
    nanoid() //=> "Uakgb_J5m9"

Functions

function customAlphabet

customAlphabet: (
alphabet: string,
defaultSize?: number
) => (size?: number) => string;
  • Generate secure unique ID with custom alphabet.

    Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.

    Parameter alphabet

    Alphabet used to generate the ID.

    Parameter defaultSize

    Size of the ID. The default size is 21.

    Returns

    A random string generator.

    const { customAlphabet } = require('nanoid')
    const nanoid = customAlphabet('0123456789абвгдеё', 5)
    nanoid() //=> "8ё56а"

function customRandom

customRandom: (
alphabet: string,
size: number,
random: (bytes: number) => Uint8Array
) => () => string;
  • Generate unique ID with custom random generator and alphabet.

    Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.

    import { customRandom } from 'nanoid/format'
    const nanoid = customRandom('abcdef', 5, size => {
    const random = []
    for (let i = 0; i < size; i++) {
    random.push(randomByte())
    }
    return random
    })
    nanoid() //=> "fbaef"

    Parameter alphabet

    Alphabet used to generate a random string.

    Parameter size

    Size of the random string.

    Parameter random

    A random bytes generator.

    Returns

    A random string generator.

function nanoid

nanoid: (size?: number) => string;
  • Generate secure URL-friendly unique ID.

    By default, the ID will have 21 symbols to have a collision probability similar to UUID v4.

    import { nanoid } from 'nanoid'
    model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"

    Parameter size

    Size of the ID. The default size is 21.

    Returns

    A random string.

function random

random: (bytes: number) => Uint8Array;
  • Generate an array of random bytes collected from hardware noise.

    import { customRandom, random } from 'nanoid'
    const nanoid = customRandom("abcdef", 5, random)

    Parameter bytes

    Size of the array.

    Returns

    An array of random bytes.

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (0)

No dev dependencies.

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/nanoid.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/nanoid)
  • HTML
    <a href="https://www.jsdocs.io/package/nanoid"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>