HaikunatorJS

repository·master·Indexed 42 days ago

https://github.com/atrox/haikunatorjs

A library for generating random, Heroku-style names (e.g., 'wispy-dust-1337') for Node.js and Deno applications. Version 2.1.2 allows customization of adjectives, nouns, and token generation via the .haikunate() method and constructor options, including support for custom seeds, delimiters, and hexadecimal tokens.

Tokens
1.8K
Snippets
8
Records
8
Agent score
4%

What's inside Haikunator

  1. Use Haikunator to generate random names

    master

    Haikunator generates Heroku-like random names consisting of adjectives, nouns, and an optional token. You can instantiate the class with default settings or provide a configuration object to customize the word lists, seed, and default token behavior. You can then call the .haikunate() method to generate a name, optionally passing specific overrides for that single call.

    import Haikunator from 'haikunator'
    
    // Basic usage
    const haikunator = new Haikunator()
    const name = haikunator.haikunate() // e.g., "wispy-dust-1337"
    
    // Usage with overrides
    const customName = haikunator.haikunate({ tokenLength: 6, delimiter: '.' })
  2. Generate names without tokens

    master

    To generate a name consisting only of adjectives and nouns without a trailing token, set tokenLength to 0 in the .haikunate() options.

    // No token, default delimiter
    haikunator.haikunate({ tokenLength: 0 }) // => "cold-wildflower"
    
    // No token, space delimiter
    haikunator.haikunate({ tokenLength: 0, delimiter: ' ' }) // => "delicate haze"
    
    // No token, no delimiter
    haikunator.haikunate({ tokenLength: 0, delimiter: '' }) // => "billowingleaf"
  3. Configure Haikunator options

    master

    Options can be provided during instantiation via the constructor or passed directly to the .haikunate() method. Options provided to .haikunate() will override the class-wide defaults.

    Constructor Options

    • adjectives: Array of custom adjectives.
    • nouns: Array of custom nouns.
    • seed: A custom seed for reproducible randomness.
    • defaults: An object containing class-wide default settings:
      • delimiter: The string used to separate words and the token (e.g., '-').
      • tokenLength: The length of the numeric/alphanumeric token (default is 4).
      • tokenHex: Boolean. If true, uses hexadecimal characters instead of numbers. Note: If tokenHex is true, any characters specified in tokenChars are ignored.
      • tokenChars: A string of characters to use for the token (e.g., '0123456789').

    Method Overrides (haikunate(options))

    The .haikunate() method accepts the same option keys as the constructor to allow one-off customizations.

    import Haikunator from 'haikunator'
    
    const haikunator = new Haikunator({
      adjectives: ['custom', 'adjectives'],
      nouns: ['custom', 'nouns'],
      seed: 'custom-seed',
    
      defaults: {
        delimiter: '-',
        tokenLength: 4,
        tokenHex: false,
        tokenChars: '0123456789',
      },
    })
    
    // Overriding defaults for a single call
    haikunator.haikunate({
      delimiter: '.',
      tokenLength: 6,
      tokenHex: true
    })
  4. Initialize Haikunator with custom words or seeds

    master

    Use the Haikunator class to generate random, Heroku-style names. You can provide custom lists of adjectives and nouns, or a seed to ensure deterministic output via the random-seed library.

    To configure the structure of the generated tokens (like length or character sets), pass a defaults object of type Config in the constructor.

    import Haikunator from 'haikunatorjs';
    
    const haikunator = new Haikunator({
      adjectives: ['swift', 'brave'],
      nouns: ['lion', 'eagle'],
      seed: 'my-secret-seed',
      defaults: {
        delimiter: '_',
        tokenLength: 6
      }
    });
  5. Generate a random name with haikunate()

    master

    The haikunate() method generates a single string consisting of a random adjective, a random noun, and a random token, joined by a delimiter.

    You can override the instance's default configuration by passing a Config object directly to the haikunate() call.

    // Uses instance defaults
    const name = haikunator.haikunate();
    
    // Overrides configuration for this specific call
    const customName = haikunator.haikunate({
      delimiter: ':',
      tokenLength: 2
    });
  6. Configure token generation via Config options

    master

    The Config type controls how the random token portion of the name is generated. These options can be set globally in the Haikunator constructor via defaults or locally in the haikunate() method.

    KeyTypeDefaultDescription
    delimiterstring'-'The character used to join the adjective, noun, and token.
    tokenLengthnumber4The number of characters in the random token.
    tokenHexbooleanfalseIf true, tokenChars is automatically set to '0123456789abcdef'.
    tokenCharsstring'0123456789'The pool of characters used to build the token.

    Note: If tokenHex is set to true, it will override any custom tokenChars provided.

    type Config = {
      delimiter?: string;
      tokenLength?: number;
      tokenHex?: boolean;
      tokenChars?: string;
    };
  7. Configure Haikunator via Options

    master

    When instantiating the Haikunator class, you can provide an Options object to customize the generator's vocabulary and randomness.

    KeyTypeDescription
    defaultsConfigGlobal configuration for token generation (delimiter, length, etc.).
    adjectivesstring[]A custom list of adjectives to use. If omitted, uses defaultAdjectives.
    nounsstring[]A custom list of nouns to use. If omitted, uses defaultNouns.
    seedstringA seed for the random number generator to ensure reproducible results.
    type Options = {
      defaults?: Config;
      adjectives?: string[];
      nouns?: string[];
      seed?: string;
    };