create-random-id
create-random-id
is a tiny utility function to generate random IDs (strings). It uses Math.random()
, and as such, it provides no level of cryptographic security. But it should be perfectly fine whenever you simply need to generate a pseudo-random string.
Usage
After installation, import the package:
import { createRandomId } from '@toolz/create-random-id';
createRandomId()
createRandomId()
generates a random string. The first character in the string will be a letter (unless useUppercaseLetters
and useLowercaseLetters
are both set to FALSE
). All subsequent characters will be uppercase letters (unless useUppercaseLetters
is set to FALSE
), lowercase letters (unless useLowercaseLetters
is set to FALSE
), and numbers (unless useNumbers
is set to FALSE
).
If useUppercaseLetters
, useLowercaseLetters
and useNumbers
are all set to FALSE
, the function will ignore the settings and generate a string containing uppercase letters, lowercase letters, and numbers.
const API = {
arguments: {
length: {
optional,
format: 'positive integer',
defaultValue: 32,
},
useUppercaseLetters: {
optional,
format: Boolean,
defaultValue: true,
},
useLowercaseLetters: {
optional,
format: Boolean,
defaultValue: true,
},
useNumbers: {
optional,
format: Boolean,
defaultValue: true,
},
},
returns: string,
}
Examples:
createRandomId();
createRandomId(100);
createRandomId(32, false);
createRandomId(32, false, false);
createRandomId(32, false, false, false);
createRandomId(32, true, false);
createRandomId(32, true, true, false);