Two functions for generating random HTML5 compliant ids.
npm install random-html-id --save
<script src="https://joker876.github.io/random-html-id/dist/index.min.js"></script>
import randomId from 'random-html-id';
// or
const randomId = require('random-html-id');
All declarations are automatically available in all other files.
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'
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';