unique-names-generator

repository·main·Indexed 20 days ago

https://github.com/andreasonny83/unique-names-generator

A tree-shakeable Node package for generating random, unique, and memorable names using built-in or custom dictionaries. It supports TypeScript, deterministic generation via seeds, and customizable separators, casing styles, and name lengths. Built-in dictionaries include adjectives, animals, colors, countries, languages, names, and starWars, with an additional NumberDictionary helper for numeric components.

Tokens
4.6K
Snippets
21
Records
22
Agent score
65%

What's inside unique-names-generator

  1. Migrate from v3 to v4

    main

    Version 4 introduces a breaking change regarding how dictionaries are handled. In v3, dictionaries were included by default. In v4, you must explicitly provide the dictionaries array in the configuration object to enable tree-shaking and reduce bundle size.

    // v3 (Deprecated)
    import { uniqueNamesGenerator } from 'unique-names-generator';
    const randomName = uniqueNamesGenerator();
    
    // v4 (Current)
    import { uniqueNamesGenerator, adjectives, colors, animals } from 'unique-names-generator';
    const config = { dictionaries: [adjectives, colors, animals] };
    const randomName = uniqueNamesGenerator(config);
  2. Migrate from v1 or v2 to v4

    main

    If you are upgrading from version 1 or 2, note the following breaking changes in the current API:

    1. Mandatory Dictionaries: uniqueNamesGenerator() now requires a dictionaries array in the config object.
    2. Separator: The separator is no longer passed as a direct argument to the function; it must be a key in the configuration object.
    3. Length: The short boolean property has been replaced by the length property, which allows you to specify the exact number of words desired.
    // v2 (Old)
    const shortName = uniqueNamesGenerator('-');
    const shortNameBool = uniqueNamesGenerator(true);
    
    // v4 (Current)
    const shortName = uniqueNamesGenerator({
      dictionaries: [adjectives, colors],
      separator: '-'
    });
    const shortNameLen = uniqueNamesGenerator({
      dictionaries: [adjectives, colors],
      length: 2
    });
  3. Use built-in dictionaries

    main

    The library provides several pre-defined dictionaries that you can import and use. Starting from version 4, you must explicitly include these in your Config object's dictionaries array to support tree-shaking and smaller bundle sizes.

    Available built-in dictionaries include:

    • adjectives
    • animals
    • colors
    • countries
    • names
    • languages
    • starWars
    import { uniqueNamesGenerator, Config, adjectives, animals, colors } from 'unique-names-generator';
    
    const config: Config = {
      dictionaries: [adjectives, animals, colors]
    };
    
    const characterName: string = uniqueNamesGenerator(config); // e.g., "red_big_donkey"
  4. Use uniqueNamesGenerator for random name generation

    main

    Import uniqueNamesGenerator and a set of dictionaries (like adjectives, colors, or animals) to generate random names. You can control the name length and the separator used between words.

    const { uniqueNamesGenerator, adjectives, colors, animals } = require('unique-names-generator');
    
    const randomName = uniqueNamesGenerator({ dictionaries: [adjectives, colors, animals] }); // big_red_donkey
    
    const shortName = uniqueNamesGenerator({
      dictionaries: [adjectives, animals, colors],
      length: 2
    }); // big-donkey
  5. Use unique-names-generator with TypeScript

    main

    The package exports type definitions, allowing you to use the Config type for type-safe configuration in your TypeScript projects.

    import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator';
    
    const customConfig: Config = {
      dictionaries: [adjectives, colors],
      separator: '-',
      length: 2,
    };
    
    const randomName: string = uniqueNamesGenerator({
      dictionaries: [adjectives, colors, animals]
    }); // big_red_donkey
    
    const shortName: string = uniqueNamesGenerator(customConfig); // big-donkey
  6. Use custom dictionaries

    main

    You can provide your own arrays of strings as dictionaries to meet specific business requirements. You can also mix custom arrays with the library's built-in dictionaries.

    import { uniqueNamesGenerator, adjectives, colors } from 'unique-names-generator';
    
    // Example 1: Purely custom dictionaries
    const starWarsCharacters = ['Han Solo', 'Jabba The Hutt', 'R2-D2'];
    const customColors = ['Green', 'Red', 'Yellow'];
    
    const name1 = uniqueNamesGenerator({
      dictionaries: [customColors, starWarsCharacters],
      length: 2,
      separator: ' '
    }); // e.g., "Green Han Solo"
    
    // Example 2: Combining built-in and custom
    const improvedAdjectives = [...adjectives, 'abrasive', 'brash'];
    const xMen = ['professorX', 'wolverine'];
    
    const name2 = uniqueNamesGenerator({
      dictionaries: [improvedAdjectives, colors, xMen],
      length: 2,
      separator: '-'
    }); // e.g., "abrasive-blue-wolverine"
  7. Generate random numbers with NumberDictionary

    main

    The NumberDictionary helper allows you to inject random numbers into your generated names. You can use NumberDictionary.generate() to create a dictionary instance with specific constraints.

    import { uniqueNamesGenerator, NumberDictionary } from 'unique-names-generator';
    
    const numberDictionary = NumberDictionary.generate({ min: 100, max: 999 });
    
    const characterName: string = uniqueNamesGenerator({
      dictionaries: [['Dangerous'], ['Snake'], numberDictionary],
      length: 3,
      separator: '',
      style: 'capital'
    }); // e.g., "DangerousSnake123"
  8. NumberDictionary.generate() options

    main

    The NumberDictionary.generate method accepts a configuration object to define the range and format of the generated numbers.

    Options:

    • min (number, optional, default: 1): The minimum value to be returned.
    • max (number, optional, default: 999): The maximum value to be returned.
    • length (number, optional): The fixed length of the random number. Note: If length is set, it ignores min and max. For example, setting length: 3 is equivalent to setting min: 100 and max: 999.
    // Example of using length instead of min/max
    const numberDictionary = NumberDictionary.generate({ length: 3 });
  9. Configure uniqueNamesGenerator via Config options

    main

    The uniqueNamesGenerator function accepts a Config object.

    Options:

    • dictionaries (Array, Required): An array of dictionaries (arrays of strings) to use for generation. The order determines the word order.
    • separator (String, Optional): The string used to separate words. Defaults to _.
    • length (Number, Optional): The number of words in the generated name. Defaults to 3. Note: This must be less than or equal to the number of dictionaries provided.
    • style (String, Optional): The casing style for the generated name. Options: lowerCase (default), upperCase, or capital (capitalizes each word).
    • seed (Number | String, Optional): A value used to deterministically generate the same name every time.
    // Example of using different styles
    const capitalizedName = uniqueNamesGenerator({
      dictionaries: [colors, adjectives, animals],
      style: 'capital'
    }); // Red_Big_Donkey
    
    const upperCaseName = uniqueNamesGenerator({
      dictionaries: [colors, adjectives, animals],
      style: 'upperCase'
    }); // RED_BIG_DONKEY
    
    // Example of deterministic generation with a seed
    const config = {
      dictionaries: [adjectives, colors, animals],
      separator: '-',
      seed: 120498,
    };
    const nameFromSeed = uniqueNamesGenerator(config); // continuous-gray-dragonfly
  10. Use the uniqueNamesGenerator function

    main

    The primary entry point for the library is the uniqueNamesGenerator function. It is used to generate unique, randomized names based on provided configuration. You can import it directly from the package root.

    import { uniqueNamesGenerator } from 'unique-names-generator';
    
    // Example usage (implementation details depend on the generator logic)
    const names = uniqueNamesGenerator({ /* options */ });