Install randomstring via npm
masterTo use randomstring in your project, install it using npm:
npm install randomstringrepository·master·Indexed 19 days ago
https://github.com/klughammer/node-randomstringA Node.js module and CLI tool for generating random strings with customizable lengths, character sets, and readability settings. Version 1.3.1 supports predefined charsets such as alphanumeric, alphabetic, numeric, hex, binary, and octal, as well as custom character sets and capitalization options.
To use randomstring in your project, install it using npm:
npm install randomstringThe generate() method is the primary way to create random strings. You can call it without arguments for a default string, or pass a length or an options object to customize the output.
Common usage patterns include:
length and charset.cb to use the asynchronous version of crypto.randombytes.var randomstring = require("randomstring");
// Default length (32)
randomstring.generate();
// Specific length
randomstring.generate(7);
// Custom length and charset
randomstring.generate({
length: 12,
charset: 'alphabetic'
});
// Custom charset
randomstring.generate({
charset: 'abc'
});
// Array of charsets
randomstring.generate({
charset: ['numeric', '!']
});
// Asynchronous usage with callback
randomstring.generate({
charset: 'abc'
}, function(generatedString) {
console.log(generatedString);
});The generate(options, cb) method accepts an optional options object with the following keys:
| Key | Type | Default | Description |
|---|---|---|---|
length | number | 32 | The length of the random string. |
readable | boolean | false | If true, excludes poorly readable characters: 0OIl. |
charset | string or string[] | 'alphanumeric' | Defines the character set. |
capitalization | string | null | Forces output to lowercase or uppercase. |
alphanumeric: [0-9 a-z A-Z]alphabetic: [a-z A-Z]numeric: [0-9]hex: [0-9 a-f]binary: [01]octal: [0-7]custom: Any given characters provided in a string or array.[]: An array containing any of the above values (e.g., ['numeric', '!']).You can install randomstring globally to use it directly from your terminal.
Installation:
npm install -g randomstringUsage Examples:
randomstringrandomstring 7randomstring length=24 charset=github readable$ randomstring
$ randomstring 7
$ randomstring length=24 charset=github readableThe Charset class allows you to define the pool of characters used for random string generation. You can set the character pool using predefined types or by providing a custom string.
Supported predefined types include:
alphanumeric: Numbers (0-9), lowercase (a-z), and uppercase (A-Z).numeric: Numbers (0-9).alphabetic: Lowercase (a-z) and uppercase (A-Z).hex: Numbers (0-9) and hex characters (a-f).binary: 01.octal: 01234567.If you provide a value that does not match these types, the class treats the input as a literal string of characters to be used.
const Charset = require('randomstring/lib/charset');
const charset = new Charset();
// Set to alphanumeric
charset.setType('alphanumeric');
// Or set using an array of types to combine them
charset.setType(['numeric', 'alphabetic']);
// Or set using a custom string of characters
charset.setType('!@#$%');After setting a character set, you can use several methods to modify the pool of available characters:
removeUnreadable(): Removes characters that are often confused with numbers, specifically 0, O, I, and l.setcapitalization(capitalization): Forces the entire character set to either 'uppercase' or 'lowercase'.removeDuplicates(): Ensures every character in the set is unique by removing any duplicate characters.const Charset = require('randomstring/lib/charset');
const charset = new Charset();
charset.setType('alphanumeric');
// Remove confusing characters like '0', 'O', 'I', 'l'
charset.removeUnreadable();
// Ensure all characters are uppercase
charset.setcapitalization('uppercase');
// Remove any duplicate characters in the pool
charset.removeDuplicates();The randomstring package can be imported using standard CommonJS require. The entry point exports the core functionality defined in the library's internal modules.
const randomstring = require('randomstring');When using the randomstring command line interface, you can use the following arguments to configure the output:
| Argument | Description |
|---|---|
length=N | Sets the length of the generated string to N. If a single number is provided without a key, it is treated as the length. |
readable | A flag that, when present, enables readable mode for the string generation. |
charset=STRING | Defines a custom set of characters to use for the generation. |
capitalization=TYPE | Sets the capitalization rules for the generated string. |
length=N
readable
charset=STRING
capitalization=TYPEThe randomstring CLI allows you to generate random strings directly from your terminal. You can specify the length of the string, a custom character set, capitalization rules, or use the readable flag to generate more human-friendly strings.
If you provide a single numeric argument, it is treated as the length of the string.
# Generate a random string of default length
randomstring
# Generate a random string of length 10
randomstring 10
# Generate a random string with specific options
randomstring length=15 readable charset=abc123