randomstring

repository·master·Indexed 19 days ago

https://github.com/klughammer/node-randomstring

A 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.

Tokens
1.8K
Snippets
8
Records
9
Agent score
68%

What's inside randomstring

  1. Generate random strings with generate()

    master

    The 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:

    • Providing a number for a specific length.
    • Providing an options object with length and charset.
    • Providing a callback function 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);
    });
  2. Configure generate() options

    master

    The generate(options, cb) method accepts an optional options object with the following keys:

    KeyTypeDefaultDescription
    lengthnumber32The length of the random string.
    readablebooleanfalseIf true, excludes poorly readable characters: 0OIl.
    charsetstring or string[]'alphanumeric'Defines the character set.
    capitalizationstringnullForces output to lowercase or uppercase.

    Supported Charset Values

    • 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', '!']).
  3. Use randomstring via Command Line

    master

    You can install randomstring globally to use it directly from your terminal.

    Installation:

    npm install -g randomstring

    Usage Examples:

    • Generate a default string: randomstring
    • Generate a string with specific length: randomstring 7
    • Generate with specific options: randomstring length=24 charset=github readable
    $ randomstring
    $ randomstring 7
    $ randomstring length=24 charset=github readable
  4. Configure character sets using the Charset class

    master

    The 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('!@#$%');
  5. Clean up character sets with Charset methods

    master

    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();
  6. Reference the randomstring CLI options and flags

    master

    When using the randomstring command line interface, you can use the following arguments to configure the output:

    ArgumentDescription
    length=NSets the length of the generated string to N. If a single number is provided without a key, it is treated as the length.
    readableA flag that, when present, enables readable mode for the string generation.
    charset=STRINGDefines a custom set of characters to use for the generation.
    capitalization=TYPESets the capitalization rules for the generated string.
    length=N
    readable
    charset=STRING
    capitalization=TYPE
  7. Use the randomstring CLI to generate random strings

    master

    The 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