2.2.0 • Published 2 years ago

random-html-id v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

random-html-id

MIT License NPM

Two functions for generating random HTML5 compliant ids.

Installation

Node

npm install random-html-id --save

Browser

<script src="https://joker876.github.io/random-html-id/dist/index.min.js"></script>

Usage

Node

import randomId from 'random-html-id';
// or
const randomId = require('random-html-id');

Browser

All declarations are automatically available in all other files.

Functions

id()

randomId.id(length: number): string

Returns a string of length length, consisting of characters a-z, A-Z, 0-9, "-", and "_" that always starts with a letter. The default length is 16, for approximately 8×10^18 possible combinations.

Example:

randomId.id();       -> 'xw7MrWHkx2HZVP7g'
randomId.id(24);     -> 'cqPiA_aGb2lhBZLPBdS1Op0p'

fromUuid()

randomId.fromUuid(): string

Returns a random uuid-based string, encoded in base58. The returned string always starts with the letter "a" to ensure that the id starts with a letter.

It always returns a string of 23 characters.

This method is the best way of ensuring that the id you are going to use for your HTML is unique.

Example:

randomId.fromUuid(); -> 'awxhFMb9XiCjsaZ39vsKrFk';