### Install Dependencies Source: https://github.com/andreasonny83/unique-names-generator/blob/main/CONTRIBUTING.md Install the project's dependencies using npm. ```bash npm install ``` -------------------------------- ### Check Node and NPM Versions Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Verify that Node.js and NPM are installed and accessible by checking their versions. ```sh $ node --version v7.10.1 $ npm --version 4.2.0 ``` -------------------------------- ### Full Configuration Example Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/configuration.md Demonstrates specifying all available options: dictionaries, separator, length, style, and seed for deterministic output. ```typescript const config = { dictionaries: [adjectives, animals, colors], separator: '-', length: 3, style: 'capital', seed: 99999 }; // Output: "Big-Donkey-Red" (deterministic) ``` -------------------------------- ### Install unique-names-generator with Yarn Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Install the package as a dependency in your project using Yarn. ```sh $ yarn add unique-names-generator ``` -------------------------------- ### Install unique-names-generator with NPM Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Install the package as a dependency in your project using npm. ```sh $ npm i -S unique-names-generator ``` -------------------------------- ### Example Usage of Config Interface Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/types.md Demonstrates how to use the Config interface to customize name generation with specific dictionaries, separator, length, style, and a seed for reproducibility. ```typescript import { uniqueNamesGenerator, Config, adjectives, animals, colors } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, animals, colors], separator: '-', length: 2, style: 'capital', seed: 12345 }; const name = uniqueNamesGenerator(config); ``` -------------------------------- ### NumberDictionary Generation Examples Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/types.md Demonstrates how to use NumberDictionary.generate() with different configuration options. You can specify min/max values, a desired length, or use the default settings. ```typescript import { NumberDictionary } from 'unique-names-generator'; // Using min and max const dict1 = NumberDictionary.generate({ min: 10, max: 99 }); // Using length (3-digit numbers from 100-999) const dict2 = NumberDictionary.generate({ length: 3 }); // Default configuration (1-999) const dict3 = NumberDictionary.generate(); ``` -------------------------------- ### Custom Separator and Length Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/configuration.md Example demonstrating custom separator and a reduced length for name generation. ```typescript const config = { dictionaries: [adjectives, animals, colors], separator: '-', length: 2 }; // Output: "big-donkey" ``` -------------------------------- ### NumberDictionary Generation Example Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Generate a numeric dictionary with a specified length and use it to create a name with a predefined prefix. ```typescript const numbers = NumberDictionary.generate({ length: 3 }); const name = uniqueNamesGenerator({ dictionaries: [['Agent'], numbers] }); // Returns: "Agent_427" ``` -------------------------------- ### Complete Example with Multiple Components and Number Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/NumberDictionary.md Combines adjectives, animals, and a custom-ranged number dictionary to generate a complex unique name. ```typescript import { uniqueNamesGenerator, NumberDictionary, adjectives, animals } from 'unique-names-generator'; const numbers = NumberDictionary.generate({ min: 100, max: 999 }); const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, numbers], length: 3, separator: '', style: 'capital' }); // Output: "DangerousWolf234" ``` -------------------------------- ### UniqueNamesGenerator Class Example Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Instantiate the UniqueNamesGenerator class with custom dictionaries and a separator, then generate random names. ```typescript import { UniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const generator = new UniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '-' }); const name = generator.generate(); // "big-donkey" const name2 = generator.generate(); // "small-cat" ``` -------------------------------- ### Generate a Basic Unique Name Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/START-HERE.md Use the generator with default dictionaries for a simple name. This is a minimal example. ```typescript uniqueNamesGenerator({ dictionaries: [adjectives, animals] }) ``` -------------------------------- ### Custom Dictionaries with Generated Numbers Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/configuration.md Example of using custom dictionaries, including a generated number dictionary, with specific separator and style. ```typescript import { uniqueNamesGenerator, NumberDictionary } from 'unique-names-generator'; const numbers = NumberDictionary.generate({ length: 3 }); const config = { dictionaries: [['Project'], numbers], separator: '-', style: 'capital' }; // Output: "Project-427" ``` -------------------------------- ### Generate Unique Name with Custom Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Example of generating a unique name using `uniqueNamesGenerator` with custom dictionaries, separator, and style. Ensure dictionaries are provided and valid. ```typescript const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], separator: '-', style: 'capital' }); // Returns: "Big-Donkey-Red" ``` -------------------------------- ### Generate Unique Name with Options Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md Customize name generation by specifying a separator and style. This example uses a hyphen and capitalizes the first letter of each word. ```typescript uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '-', style: 'capital' }) // "Big-Donkey" ``` -------------------------------- ### Generate Name Using Countries Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Generate a two-word name by combining `adjectives` and `countries` dictionaries. This example demonstrates using a specific length for the generated name. ```typescript import { uniqueNamesGenerator, countries, adjectives } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, countries], length: 2 }); // Output: "powerful_France" ``` -------------------------------- ### Next.js API Route for Name Generation Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Implement a Next.js API route to serve generated unique names. This example uses adjectives, animals, and colors dictionaries with 'capital' style. ```typescript // pages/api/generate-name.ts import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler( req: NextApiRequest, res: NextApiResponse<{ name: string }> ) { const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'capital' }); res.status(200).json({ name }); } ``` -------------------------------- ### Branded Name Generation Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Generate branded names by combining custom prefixes with extended animal names. This example uses a length of 2 and no separator for a compact brand name. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const brandPrefixes = ['Tech', 'Swift', 'Smart', 'Prime']; const extendedAnimals = [...animals, 'Phoenix', 'Dragon', 'Eagle']; function generateBrandName(): string { return uniqueNamesGenerator({ dictionaries: [brandPrefixes, extendedAnimals], separator: '', style: 'capital', length: 2 }); } // Output: "TechDragon", "SwiftPhoenix", etc. ``` -------------------------------- ### Avoiding Length Exceeds Dictionaries Error Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/errors.md Provides examples of how to correctly set the length option to be less than or equal to the number of dictionaries, or how to add more dictionaries to accommodate a larger length. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; // Correct: length (2) <= dictionaries (3) const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], length: 2 }); // Correct: add more dictionaries const name2 = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], length: 4 // Error: still exceeds }); // To fix, add 4th dictionary: const name3 = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors, ['word']], length: 4 // Now correct }); ``` -------------------------------- ### Using Built-in and Custom Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/types.md Shows how to import and use pre-built dictionaries like 'adjectives' and 'animals', as well as how to create and use custom dictionaries. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors, countries, languages, names, starWars } from 'unique-names-generator'; // Using built-in dictionaries const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals] }); ``` ```typescript // Using custom dictionaries const customDict = ['custom1', 'custom2', 'custom3']; const customName = uniqueNamesGenerator({ dictionaries: [customDict, animals] }); ``` ```typescript // Combining and extending dictionaries const extendedAdjectives = [...adjectives, 'awesome', 'wonderful']; const name = uniqueNamesGenerator({ dictionaries: [extendedAdjectives, animals] }); ``` -------------------------------- ### Clone the Repository Source: https://github.com/andreasonny83/unique-names-generator/blob/main/CONTRIBUTING.md Clone the project repository to your local machine. ```bash git clone git@github.com:your-username/unique-names-generator.git ``` -------------------------------- ### Package.json Entry Points Configuration (JSON) Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/module-architecture.md Specifies the main entry points for different module formats (CommonJS, ES Module, UMD) and the source and type declaration files. ```json { "main": "dist/index.js", // CommonJS "module": "dist/index.m.js", // ES Module "umd:main": "dist/index.umd.js", // UMD "source": "src/index.ts", // Source "types": "dist/index.d.ts" // TypeScript } ``` -------------------------------- ### Basic Configuration with Defaults Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/configuration.md A minimal configuration using default values for separator, style, and seed. ```typescript const config = { dictionaries: [adjectives, animals, colors] }; // Uses: 3 words, underscore separator, lowercase style, no seed ``` -------------------------------- ### Configuration Options Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/DOCUMENTATION-MANIFEST.txt Reference for configuration options including 'dictionaries' (required), 'separator' (optional), 'length' (optional), 'style' (optional), and 'seed' (optional). ```plaintext dictionaries (required) separator (optional, default: '_') length (optional, default: dictionaries.length) style (optional, default: 'lowerCase') seed (optional, no default) ``` -------------------------------- ### Pre-built Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md Lists the available pre-built dictionaries that can be used within the configuration. Each dictionary is an array of strings. ```typescript adjectives // string[] (1,200+ entries) animals // string[] (350+ entries) colors // string[] (50+ entries) countries // string[] (250+ entries) languages // string[] (98 entries) names // string[] (4,900+ entries) starWars // string[] (80+ entries) ``` -------------------------------- ### Run Tests Source: https://github.com/andreasonny83/unique-names-generator/blob/main/CONTRIBUTING.md Execute the project's test suite to ensure code integrity. This should be run before and after making changes. ```bash npm test ``` -------------------------------- ### Explicit Dictionaries Required in v4+ Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md Starting with v4, dictionaries must be explicitly passed to `uniqueNamesGenerator`. The implicit default dictionaries from v3 are no longer valid. ```typescript // ✅ v4 — explicit dictionaries required uniqueNamesGenerator({ dictionaries: [adjectives, animals] }); // ❌ v3 — implicit defaults (not valid in v4) uniqueNamesGenerator(); ``` -------------------------------- ### Default Import and Usage Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import the main function and common dictionaries for basic name generation. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors] }); ``` -------------------------------- ### Using Pre-built Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Import and use pre-built dictionaries like 'adjectives' and 'animals' directly in the `dictionaries` configuration option when generating names. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals] }); // Output: "bright_donkey" ``` -------------------------------- ### Format Code Source: https://github.com/andreasonny83/unique-names-generator/blob/main/CONTRIBUTING.md Automatically format code according to Prettier standards. This should be run after making any code changes. ```bash npm run format ``` -------------------------------- ### Use Star Wars Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Demonstrates how to use the starWars dictionary with other dictionaries to generate names. Ensure the necessary dictionaries and the generator function are imported. ```typescript import { uniqueNamesGenerator, starWars, colors } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [colors, starWars], separator: ' ', length: 2 }); // Output: "Blue Luke Skywalker" ``` -------------------------------- ### uniqueNamesGenerator - Migration from v2 to v4 (Separator) Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Demonstrates how to configure the separator in v4, replacing the v2 method. ```APIDOC ## uniqueNamesGenerator Migration (v2 to v4) - Separator ### Description This example shows the v4 equivalent of specifying a separator, which was previously done by passing it as a direct argument in v2. ### Method Signature `uniqueNamesGenerator(config?: Config): string` ### Parameters #### config (Config) - **dictionaries** (Array) - Required - An array of dictionaries to use for name generation. - **separator** (string) - Required - The separator to use between words in the generated name. ### Request Example (v4) ```typescript import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, colors, animals], separator: '-' } const shortName = uniqueNamesGenerator(config); // Example output: "big-red-donkey" ``` ``` -------------------------------- ### Migration to v4: Length Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Shows how the `length` option in v4 replaces the boolean `short` argument from v2 for specifying name length. ```typescript import { uniqueNamesGenerator } from 'unique-names-generator'; const shortName = uniqueNamesGenerator(true); // big-donkey ``` ```typescript import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, colors, animals], length: 2 } const shortName = uniqueNamesGenerator(config); // big-donkey ``` -------------------------------- ### Migration to v4: Separator Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Demonstrates how to configure a custom separator in v4, replacing the previous direct argument method. ```typescript import { uniqueNamesGenerator } from 'unique-names-generator'; const shortName = uniqueNamesGenerator('-'); // big-red-donkey ``` ```typescript import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, colors, animals], separator: '-' } const shortName = uniqueNamesGenerator(config); // big-red-donkey ``` -------------------------------- ### Generate Names with Different Styles Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/types.md Demonstrates how to use the 'style' option to generate names in lowercase, uppercase, or capitalized format. Ensure the 'unique-names-generator' library is imported. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; // lowerCase style (default) uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'lowerCase' }); // Output: "big_red_donkey" ``` ```typescript // upperCase style uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'upperCase' }); // Output: "BIG_RED_DONKEY" ``` ```typescript // capital style uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'capital' }); // Output: "Big_Red_Donkey" ``` -------------------------------- ### uniqueNamesGenerator - Migration from v2 to v4 (Length) Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Demonstrates how the 'short' option in v2 has been replaced by the 'length' option in v4. ```APIDOC ## uniqueNamesGenerator Migration (v2 to v4) - Length ### Description This example shows how the `short` property in v2 has been replaced by the `length` property in v4, allowing specification of the exact number of words. ### Method Signature `uniqueNamesGenerator(config?: Config): string` ### Parameters #### config (Config) - **dictionaries** (Array) - Required - An array of dictionaries to use for name generation. - **length** (number) - Required - The number of words to include in the generated name. ### Request Example (v4) ```typescript import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, colors, animals], length: 2 } const shortName = uniqueNamesGenerator(config); // Example output: "big-donkey" ``` ``` -------------------------------- ### Importing Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import pre-defined word dictionaries for use in name generation. Available dictionaries include adjectives, animals, colors, countries, languages, names, and Star Wars references. ```typescript import { adjectives, // string[] (1,200+ adjectives) animals, // string[] (350+ animals) colors, // string[] (50+ colors) countries, // string[] (250+ countries) languages, // string[] (98 languages) names, // string[] (4,900+ names) starWars // string[] (80+ Star Wars references) } from 'unique-names-generator'; ``` -------------------------------- ### Error Handling: No Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Demonstrates an error scenario where 'generate()' is called without any dictionaries configured. This will throw an error indicating that no dictionary could be found. ```typescript // Error: no dictionaries generator.generate(); // "Cannot find any dictionary..." ``` -------------------------------- ### Custom Separator Options Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Demonstrates various separator options for name generation, including default, hyphen, none, space, and multi-character separators. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; // Underscore (default) const name1 = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '_' }); // Output: "big_donkey" // Hyphen const name2 = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '-' }); // Output: "big-donkey" // No separator const name3 = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '' }); // Output: "bigdonkey" // Space const name4 = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: ' ' }); // Output: "big donkey" // Multiple character separator const name5 = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: ':::' }); // Output: "big:::donkey" ``` -------------------------------- ### Configure Tree-Shaking Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md Set `sideEffects` to `false` in your `package.json` to enable tree-shaking for unused dictionaries. ```json { "sideEffects": false } ``` -------------------------------- ### Import with TypeScript Config Type Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import the Config type for strongly-typed configuration objects when using TypeScript. ```typescript import { uniqueNamesGenerator, Config, adjectives, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, animals], separator: '-' }; const name = uniqueNamesGenerator(config); ``` -------------------------------- ### Instantiate UniqueNamesGenerator with Custom Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Demonstrates how to instantiate the UniqueNamesGenerator class with a custom configuration object, including dictionaries, separator, length, and style. The generated name will follow these settings. ```typescript import { UniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; const config = { dictionaries: [adjectives, animals, colors], separator: '-', length: 2, style: 'capital' }; const generator = new UniqueNamesGenerator(config); const name = generator.generate(); // Output: "Big-Donkey" ``` -------------------------------- ### Generate Name Using Colors and Animals Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Combine the `colors` and `animals` dictionaries to create a two-word name separated by a hyphen. This allows for more descriptive and unique name combinations. ```typescript import { uniqueNamesGenerator, colors, animals } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [colors, animals], separator: '-' }); // Output: "red-panda" ``` -------------------------------- ### File Dependencies Graph Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/module-architecture.md Visualizes the dependency structure between the TypeScript files in the project. It shows how `index.ts` imports `unique-names-generator.ts` and `dictionaries/index.ts`, illustrating the modular organization. ```text index.ts ├── unique-names-generator.ts │ └── unique-names-generator.constructor.ts │ ├── seed.ts (no dependencies) │ └── (no external deps) │ └── dictionaries/index.ts ├── adjectives.ts ├── animals.ts ├── colors.ts ├── countries.ts ├── languages.ts ├── names.ts ├── star-wars.ts └── numbers.ts └── (no external deps) ``` -------------------------------- ### generate() Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Generates a random name based on the configuration provided during the generator's instantiation. It validates the configuration before proceeding and supports deterministic generation with seeds. ```APIDOC ## generate() ### Description Generates a random name using the configuration provided to the constructor. This method validates the configuration, including the existence of dictionaries and the validity of the specified length, before proceeding with the name generation algorithm. ### Method Signature ```typescript public generate(): string ``` ### Return Value **Type:** `string` A randomly generated name composed of words from the configured dictionaries, formatted according to the specified options. ### Validation Before generating a name, the method performs the following validations: 1. **Dictionaries exist:** Throws an error if `dictionaries` is falsy or empty. 2. **Length is valid:** Throws an error if `length <= 0`. 3. **Length <= dictionaries:** Throws an error if `length > dictionaries.length`. ### Errors Refer to the [Errors Reference](../errors.md) for complete error documentation. Common errors include: - `Error: no dictionaries` - `Error: invalid length provided` ### Algorithm 1. Validates configuration. 2. Resets temporary seed for deterministic generation if a seed is provided. 3. Iterates through the first `length` dictionaries. 4. For each dictionary, it generates a random float, selects a word, and applies the configured style transformation. 5. Joins the selected words with the configured separator. ### Seed Behavior - **With Seed:** Uses the Mulberry32 pseudo-random algorithm for deterministic output. The same seed always produces the same name. String seeds are converted to numeric values by summing character codes. - **Without Seed:** Uses `Math.random()` for non-deterministic generation, resulting in different output on each call. ### Style Application Each word is transformed according to the configured style: - `'lowerCase'`: `word` - `'upperCase'`: `WORD` - `'capital'`: `Word` ### Examples #### Basic Generation ```typescript import { UniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const generator = new UniqueNamesGenerator({ dictionaries: [adjectives, animals] }); const name1 = generator.generate(); // e.g., "big_donkey" const name2 = generator.generate(); // e.g., "small_cat" ``` #### With Deterministic Seed ```typescript import { UniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const config = { dictionaries: [adjectives, animals], seed: 999 }; const generator = new UniqueNamesGenerator(config); // All three calls produce the same output const name1 = generator.generate(); const name2 = generator.generate(); const name3 = generator.generate(); // All output: "abashed_emu" ``` #### Reusing Generator Instance ```typescript import { UniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; const generator = new UniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], separator: '-', style: 'capital' }); // Generate multiple names with the same configuration const names = [ generator.generate(), // "Big-Donkey-Red" generator.generate(), // "Small-Cat-Blue" generator.generate(), // "Tall-Eagle-Green" ]; ``` #### With Custom Configuration ```typescript import { UniqueNamesGenerator } from 'unique-names-generator'; const customWords = ['Phoenix', 'Dragon', 'Tiger']; const customNumbers = ['001', '002', '003']; const generator = new UniqueNamesGenerator({ dictionaries: [customWords, customNumbers], separator: '-', style: 'capital', length: 2 }); const name = generator.generate(); // "Phoenix-001" ``` #### String Seed Example ```typescript import { UniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const config = { dictionaries: [adjectives, animals], seed: 'my-custom-seed' }; const generator = new UniqueNamesGenerator(config); // Same seed string always produces same output const name = generator.generate(); ``` ``` -------------------------------- ### Name Generation Flow Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/module-architecture.md Illustrates the sequence of operations involved in generating a unique name, from user input to the final formatted string. This flow includes configuration validation, dictionary merging, and the iterative process of selecting and formatting words. ```text User Input ↓ uniqueNamesGenerator(config) ↓ Validate dictionaries exist ↓ Merge with defaults ↓ new UniqueNamesGenerator(config) ↓ UniqueNamesGenerator.generate() ├─ Validate config ├─ Reset seed state ├─ For each dictionary (up to length): │ ├─ Get random float │ │ ├─ If seed exists: use getFromSeed() │ │ └─ Else: use Math.random() │ ├─ Select random word from dictionary │ ├─ Format word (style transformation) │ └─ Append to result string with separator └─ Return generated name ↓ Return: string ``` -------------------------------- ### Import Patterns Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/DOCUMENTATION-MANIFEST.txt Common import patterns for using the unique-names-generator library. ```javascript import uniqueNamesGenerator from 'unique-names-generator'; ``` -------------------------------- ### Generate Name with Star Wars Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Use the Star Wars character names dictionary to generate a name. Ensure the 'starWars' dictionary is imported. ```typescript import { uniqueNamesGenerator, Config, starWars } from 'unique-names-generator'; const config: Config = { dictionaries: [starWars] } const characterName: string = uniqueNamesGenerator(config); // Han Solo ``` -------------------------------- ### Main Function Signature Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md The main function `uniqueNamesGenerator` takes a custom configuration object and returns a unique name string. ```typescript function uniqueNamesGenerator(customConfig: Config): string ``` -------------------------------- ### Type-Safe Configuration Builder Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Utilize a builder pattern for constructing configuration objects in a type-safe and fluent manner. This improves code readability and reduces errors when setting up complex configurations. ```typescript import { uniqueNamesGenerator, Config, adjectives, animals, colors } from 'unique-names-generator'; class NameConfigBuilder { private config: Config = { dictionaries: [] }; withDictionaries(...dicts: string[][]): this { this.config.dictionaries = dicts; return this; } withSeparator(separator: string): this { this.config.separator = separator; return this; } withLength(length: number): this { this.config.length = length; return this; } withStyle(style: 'lowerCase' | 'upperCase' | 'capital'): this { this.config.style = style; return this; } withSeed(seed: number | string): this { this.config.seed = seed; return this; } generate(): string { return uniqueNamesGenerator(this.config); } } const name = new NameConfigBuilder() .withDictionaries(adjectives, animals, colors) .withSeparator('-') .withStyle('capital') .generate(); // Output: "Big-Donkey-Red" ``` -------------------------------- ### Generate Name with Colors Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Use the colors dictionary to generate a name. Ensure the 'colors' dictionary is imported. ```typescript import { uniqueNamesGenerator, Config, colors } from 'unique-names-generator'; const config: Config = { dictionaries: [colors] } const characterName: string = uniqueNamesGenerator(config); // red ``` -------------------------------- ### Import with NumberDictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import uniqueNamesGenerator and NumberDictionary to include generated numbers in names. ```typescript import { uniqueNamesGenerator, NumberDictionary } from 'unique-names-generator'; const numbers = NumberDictionary.generate(); const name = uniqueNamesGenerator({ dictionaries: [['Prefix'], numbers] }); ``` -------------------------------- ### Generate Name with Animals Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Use the animals dictionary to generate a name. Ensure the 'animals' dictionary is imported. ```typescript import { uniqueNamesGenerator, Config, animals } from 'unique-names-generator'; const config: Config = { dictionaries: [animals] } const characterName: string = uniqueNamesGenerator(config); // donkey ``` -------------------------------- ### Seed-Based Name Mapping from Input Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Create a consistent name for any input, such as a user ID, by deriving a seed from the input string. This ensures that the same input always maps to the same generated name. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; // Generate deterministic name from any input function seedFromInput(input: string): number { return input.split('').reduce((sum, char) => sum + char.charCodeAt(0), 0); } function getConsistentName(userId: string): string { return uniqueNamesGenerator({ dictionaries: [adjectives, animals], seed: seedFromInput(userId) }); } const user1 = getConsistentName('user-abc123'); const user2 = getConsistentName('user-abc123'); // Same user, same output console.assert(user1 === user2); // True ``` -------------------------------- ### Main Function Signature Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/DOCUMENTATION-MANIFEST.txt The main function `uniqueNamesGenerator` takes a configuration object and returns a generated unique name string. Full signature, parameter table, return value, and error conditions are documented. ```typescript uniqueNamesGenerator(config: Config): string ``` -------------------------------- ### Configure Separator for Word Joining Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/configuration.md Specify a string to join words in the generated name. Defaults to an underscore. Common values include '-', ' ', or an empty string. ```typescript const config = { dictionaries: [adjectives, animals], separator: '-' // Output: "big-donkey" }; ``` ```typescript const config2 = { dictionaries: [adjectives, animals], separator: '' // Output: "bigdonkey" }; ``` ```typescript const config3 = { dictionaries: [adjectives, animals], separator: ' ' // Output: "big donkey" }; ``` -------------------------------- ### Tree-Shakeable Individual Imports Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import only the necessary components to enable tree-shaking and reduce bundle size. ```typescript // Only import what you need import { uniqueNamesGenerator } from 'unique-names-generator'; import adjectives from 'unique-names-generator/dist/dictionaries/adjectives'; import animals from 'unique-names-generator/dist/dictionaries/animals'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals] }); ``` -------------------------------- ### Generate Name with Style Formatting Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/uniqueNamesGenerator.md Applies specified formatting styles (capital, upperCase) to each word in the generated name. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; // Capital style const capitalName = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'capital' }); // Output: "Big_Donkey_Red" // Upper case style const upperName = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], style: 'upperCase' }); // Output: "BIG_DONKEY_RED" ``` -------------------------------- ### Combine Static Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Mix multiple predefined static dictionaries to create diverse names. Ensure all required dictionaries are imported. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors, countries } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors, countries], separator: '-' }); // Output: "bright-eagle-red-France" ``` -------------------------------- ### Generate Name with Default Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Generates a random name using the default dictionaries and settings. Ensure 'adjectives' and 'animals' are imported. ```typescript import { UniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const generator = new UniqueNamesGenerator({ dictionaries: [adjectives, animals] }); const name1 = generator.generate(); // e.g., "big_donkey" const name2 = generator.generate(); // e.g., "small_cat" ``` -------------------------------- ### Generate with Reusable Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Defines a configuration object once and reuses it for multiple name generations. ```typescript import { uniqueNamesGenerator, Config, adjectives, animals, colors } from 'unique-names-generator'; const config: Config = { dictionaries: [adjectives, animals, colors], separator: '-', style: 'capital' }; const name1 = uniqueNamesGenerator(config); // "Big-Donkey-Red" const name2 = uniqueNamesGenerator(config); // "Small-Cat-Blue" ``` -------------------------------- ### UniqueNamesGenerator Constructor Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Initializes the UniqueNamesGenerator with a configuration object. The configuration specifies dictionaries, separator, length, and style for name generation. ```APIDOC ## Constructor UniqueNamesGenerator ### Description Initializes the generator with the provided configuration, storing dictionaries, separator, length, and style. Seed is converted to a numeric value. Configuration validation occurs during `generate()`. ### Signature constructor(config: Config) ### Parameters #### Path Parameters - **config** (Config) - Required - Configuration object containing dictionaries and generation settings ### Example ```typescript import { UniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; const config = { dictionaries: [adjectives, animals, colors], separator: '-', length: 2, style: 'capital' }; const generator = new UniqueNamesGenerator(config); const name = generator.generate(); // Output: "Big-Donkey" ``` ``` -------------------------------- ### Main Export - uniqueNamesGenerator Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md Import and use the main function to generate unique names. Configure dictionaries, separator, length, style, and seed for custom generation. ```typescript import { uniqueNamesGenerator, Config } from 'unique-names-generator'; const name: string = uniqueNamesGenerator({ dictionaries: [/* string[][] */], separator?: string, // default: '_' length?: number, // default: dictionaries.length style?: 'lowerCase' | 'upperCase' | 'capital', // default: 'lowerCase' seed?: number | string // optional }); ``` -------------------------------- ### NumberDictionary Static Method Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/API-SUMMARY.md The NumberDictionary provides a static method to generate an array of numeric strings, which can be used as a dictionary in name generation. ```APIDOC ## Class: NumberDictionary ### Static Methods #### generate - **Signature:** `generate(config?: Partial)` - **Returns:** `string[]` - **Description:** Generates a numeric dictionary. Accepts an optional configuration object to specify the range or length of the numbers. ### Configuration ```typescript interface Config { min?: number; // default: 1 max?: number; // default: 999 length?: number; // overrides min/max } ``` ### Example ```typescript const numbers = NumberDictionary.generate({ length: 3 }); const name = uniqueNamesGenerator({ dictionaries: [['Agent'], numbers] }); // Returns: "Agent_427" ``` ``` -------------------------------- ### Generate Name with Countries Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Use the countries dictionary to generate a name. Ensure the 'countries' dictionary is imported. ```typescript import { uniqueNamesGenerator, Config, countries } from 'unique-names-generator'; const config: Config = { dictionaries: [countries] } const characterName: string = uniqueNamesGenerator(config); // United Arab Emirates ``` -------------------------------- ### Generate Number Dictionary with Seed for Deterministic Output Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/NumberDictionary.md Demonstrates how to use a seed with `uniqueNamesGenerator` to ensure reproducible names when a number dictionary is included. ```typescript import { uniqueNamesGenerator, NumberDictionary } from 'unique-names-generator'; const numberDict = NumberDictionary.generate({ min: 1, max: 100 }); const config = { dictionaries: [['Item'], numberDict], seed: 42 }; // Both calls will produce identical results const name1 = uniqueNamesGenerator(config); const name2 = uniqueNamesGenerator(config); // Both output: "Item_47" ``` -------------------------------- ### Generate a Basic Unique Name Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/START-HERE.md Import the generator and use it with default dictionaries to create a simple name. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [adjectives, animals], separator: '-', style: 'capital' }); // Output: "Big-Donkey" ``` -------------------------------- ### Lazy Loading Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md This pattern demonstrates how to lazily load dictionaries. The `adjectives` dictionary is only imported and assigned when `getDictionaries` is called for the first time. ```typescript import { adjectives, animals } from 'unique-names-generator'; let loadedDictionaries: typeof adjectives | null = null; function getDictionaries() { if (!loadedDictionaries) { loadedDictionaries = adjectives; } return loadedDictionaries; } ``` -------------------------------- ### Generate Name with Names Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Use the names dictionary to generate a name. Ensure the 'names' dictionary is imported. ```typescript import { uniqueNamesGenerator, Config, names } from 'unique-names-generator'; const config: Config = { dictionaries: [names] } const characterName: string = uniqueNamesGenerator(config); // Winona ``` -------------------------------- ### Dictionaries Reference Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/DOCUMENTATION-MANIFEST.txt Information about the built-in dictionaries available for use. ```APIDOC ## Dictionaries ### Description The library includes 7 built-in dictionaries that can be used to generate names. Each dictionary contains a list of words relevant to its category. ### Available Dictionaries - **adjectives**: Contains descriptive words. - **colors**: Contains names of colors. - **animals**: Contains names of animals. - **planets**: Contains names of planets. - **temperaments**: Contains words related to temperaments. - **verbs**: Contains action words. - **nouns**: Contains common nouns. ### Usage Specify the desired dictionary names in the `dictionaries` array within the configuration object when calling `uniqueNamesGenerator` or initializing `UniqueNamesGenerator`. ### Example ```javascript const name = uniqueNamesGenerator({ dictionaries: ['planets', 'verbs', 'nouns'], separator: '_' }); // Example output: "earth_run_chair" ``` ``` -------------------------------- ### Dynamically Configure Name Generation Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Conditionally include dictionaries or adjust settings like separator and style based on input options. This allows for flexible name generation tailored to specific needs. ```typescript import { uniqueNamesGenerator, Config, adjectives, animals, colors, names } from 'unique-names-generator'; interface NameOptions { includeColors?: boolean; useCapital?: boolean; separator?: string; deterministic?: boolean; seed?: number; } function generateCustomName(options: NameOptions = {}): string { const dictionaries = [adjectives, animals]; if (options.includeColors) { dictionaries.push(colors); } const config: Config = { dictionaries, separator: options.separator || '_', style: options.useCapital ? 'capital' : 'lowerCase' }; if (options.deterministic && options.seed !== undefined) { config.seed = options.seed; } return uniqueNamesGenerator(config); } const name1 = generateCustomName(); // Output: "big_donkey" const name2 = generateCustomName({ includeColors: true, useCapital: true, separator: '-' }); // Output: "Big-Donkey-Red" const name3 = generateCustomName({ deterministic: true, seed: 12345 }); // Output: (deterministic) "abashed_emu" ``` -------------------------------- ### Combine Static and Dynamic Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Integrate dynamically generated number sequences with static dictionaries. The NumberDictionary can be configured with a specified length. ```typescript import { uniqueNamesGenerator, adjectives, NumberDictionary } from 'unique-names-generator'; const numbers = NumberDictionary.generate({ length: 5 }); const name = uniqueNamesGenerator({ dictionaries: [adjectives, numbers], separator: '' }); // Output: "brightsome12847" ``` -------------------------------- ### NumberDictionary Utility Class Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/README.md Provides a static method `generate` to create an array of numbers based on specified configuration. Supports min, max, and length parameters. ```typescript class NumberDictionary { static generate(config?: { min?: number, max?: number, length?: number }): string[] } ``` -------------------------------- ### Generate a Random Name with TypeScript Source: https://github.com/andreasonny83/unique-names-generator/blob/main/README.md Utilize the package in a TypeScript project, including type definitions for configuration. ```typescript 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 ``` -------------------------------- ### Generate Multi-Component Name Using Names Dictionary Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/Dictionaries.md Create a name composed of multiple components by providing the 'names' dictionary twice and specifying the desired 'length' and 'separator'. ```typescript import { uniqueNamesGenerator, names } from 'unique-names-generator'; const name = uniqueNamesGenerator({ dictionaries: [names, names], separator: ' ', length: 2, style: 'capital' }); // Output: "James Michael" ``` -------------------------------- ### Implement Safe Defaults for Name Generation Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/errors.md Create a function that merges user-provided configuration with safe default values. This ensures that generation can proceed even with minimal or invalid user input, by adjusting parameters like 'length' to valid ranges. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; function safeGenerateName(customConfig = {}) { const defaultConfig = { dictionaries: [adjectives, animals, colors], separator: '_', length: 3, style: 'lowerCase' }; const config = { ...defaultConfig, ...customConfig }; // Validate length if (!config.length || config.length <= 0) { config.length = config.dictionaries.length; } else if (config.length > config.dictionaries.length) { config.length = config.dictionaries.length; } return uniqueNamesGenerator(config); } const name = safeGenerateName({ separator: '-' }); ``` -------------------------------- ### Custom Domain-Specific Dictionaries Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Create entirely new dictionaries for specific domains like space missions or game characters. You can also customize the separator and capitalization style. ```typescript import { uniqueNamesGenerator } from 'unique-names-generator'; // Space mission names const missions = ['Apollo', 'Gemini', 'Mercury', 'Artemis', 'Skylab']; const bodies = ['Moon', 'Mars', 'Venus', 'Jupiter', 'Saturn']; const spaceName = uniqueNamesGenerator({ dictionaries: [missions, bodies], separator: '-', style: 'capital' }); // Output: "Apollo-Mars" // Game character names const classes = ['Knight', 'Mage', 'Archer', 'Rogue', 'Paladin']; const races = ['Human', 'Elf', 'Dwarf', 'Orc', 'Dragonborn']; const traits = ['Brave', 'Cunning', 'Strong', 'Wise', 'Swift']; const characterName = uniqueNamesGenerator({ dictionaries: [traits, classes, races], separator: ' ', style: 'capital' }); // Output: "Brave Knight Human" ``` -------------------------------- ### Create Name Pools for Batch Operations Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Generate an array of unique names for batch processing. Optionally use a seed for reproducible pools, ensuring the same set of names is generated each time. ```typescript import { uniqueNamesGenerator, adjectives, animals, colors } from 'unique-names-generator'; function createNamePool(size: number, seed?: number): string[] { const pool: string[] = []; for (let i = 0; i < size; i++) { // Use index-based seed for reproducibility if seed is provided const finalSeed = seed !== undefined ? seed + i : undefined; pool.push(uniqueNamesGenerator({ dictionaries: [adjectives, animals, colors], seed: finalSeed })); } return pool; } const smallPool = createNamePool(5); const deterministicPool = createNamePool(5, 1000); // Reproducible // Both pools will be the same every time when using same seed ``` -------------------------------- ### Lint Code Source: https://github.com/andreasonny83/unique-names-generator/blob/main/CONTRIBUTING.md Check code styling and identify potential issues using the linter. This command helps ensure code quality. ```bash npm run lint ``` -------------------------------- ### Generate Name with Custom Dictionaries and Configuration Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/api-reference/UniqueNamesGenerator.md Generates a name using custom word lists, a specified separator, capital style, and a fixed length. The 'length' parameter must not exceed the number of dictionaries. ```typescript import { UniqueNamesGenerator } from 'unique-names-generator'; const customWords = ['Phoenix', 'Dragon', 'Tiger']; const customNumbers = ['001', '002', '003']; const generator = new UniqueNamesGenerator({ dictionaries: [customWords, customNumbers], separator: '-', style: 'capital', length: 2 }); const name = generator.generate(); // "Phoenix-001" ``` -------------------------------- ### Safe Name Generation with Fallback Source: https://github.com/andreasonny83/unique-names-generator/blob/main/_autodocs/usage-patterns.md Use this pattern to ensure a name is always returned, even if generation fails or configuration is invalid. It provides a fallback string when errors occur or essential configuration is missing. ```typescript import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'; function safeGenerateName(config: any, fallback: string = 'Unknown'): string { try { if (!config || !config.dictionaries || config.dictionaries.length === 0) { return fallback; } if (config.length && config.length > config.dictionaries.length) { config.length = config.dictionaries.length; } return uniqueNamesGenerator(config); } catch (error) { console.error('Name generation failed:', error); return fallback; } } const name = safeGenerateName({ dictionaries: [adjectives, animals] }); ```