
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
unique-username-generator
Advanced tools
A package to generate a unique username from email or randomly selected nouns and adjectives. User can add a separator between the username, define the maximum length of a username and adds up to six random digits.
The unique-username-generator npm package is designed to generate unique usernames. It provides customizable options to create usernames based on different criteria, ensuring that the generated usernames are unique and suitable for various applications.
Basic Username Generation
This feature allows you to generate a basic unique username with default settings. The code sample demonstrates how to import the package and generate a username, which is then printed to the console.
const { generateUsername } = require('unique-username-generator');
const username = generateUsername();
console.log(username);
Customizable Username Generation
This feature provides options to customize the username generation process. You can specify a separator, the number of words, and the maximum length of the username. The code sample shows how to generate a username with a hyphen separator, two words, and a maximum length of 10 characters.
const { generateUsername } = require('unique-username-generator');
const username = generateUsername('-', 2, 10);
console.log(username);
The unique-names-generator package is similar in functionality as it also generates unique names. It offers more customization options, such as dictionaries for adjectives, colors, and animals, allowing for more personalized and thematic username generation compared to unique-username-generator.
The random-username-generator package focuses on generating random usernames. It provides a simple interface for creating usernames, but may not offer the same level of customization as unique-username-generator, making it more suitable for straightforward use cases.
A tiny, flexible username generator for Node and browsers. Generate from email or dictionaries; control separator, style, max length, optional digits, profanity filtering, templates, deterministic seeds, and batch generation.
Security note: this library generates human-friendly display names. It is not intended for security-sensitive randomness (e.g., passwords, tokens). It prefers Web Crypto when available and falls back to a non-crypto PRNG only as a last resort.
npm install unique-username-generator --save
// Using Node.js `require()`
const { generateFromEmail, generateUsername } = require("unique-username-generator");
// Using ES6 imports
import { generateFromEmail, generateUsername } from "unique-username-generator";
It will generate username from email and add upto six random digits at the end of the name.
// Simple: add three random digits
generateFromEmail("lakshmi.narayan@example.com", 3); // "lakshminarayan234"
// Advanced: options
generateFromEmail("123john@example.com", { randomDigits: 2, stripLeadingDigits: true }); // "john12"
generateFromEmail("12345@example.com", { randomDigits: 0, leadingFallback: "member" }); // "member"
It will generate unique username from adjectives, nouns, random digits and separator. You can control these following parameters - separator, number of random digits and maximum length of a username.
// generaterUsername(separator, number of random digits, maximum length)
// Without any parameter
const username = generateUsername();
console.log(username); // blossomlogistical
// With any separator like "-, _"
const username = generateUsername("-");
console.log(username); // blossom-logistical
// With random digits and no separator
const username = generateUsername("", 3);
console.log(username); // blossomlogistical732
// With maximum length constraint and no separator, no random digits
const username = generateUsername("", 0, 15);
console.log(username); // blossomlogistic
// With maximum length constraint and separator but no random digits
const username = generateUsername("-", 0, 15);
console.log(username); // blossom-logisti
// With maximum length constraint and random digits but no separator
const username = generateUsername("", 2, 19);
console.log(username); // blossomlogistical73
// With all parameters
const username = generateUsername("-", 2, 20, "unique username");
console.log(username); // unique-username-73
By default, the unique username generator library comes with 2 dictionaries out of the box, so that you can use them straight away.
The new syntax for using the default dictionaries is the following:
import { uniqueUsernameGenerator, Config, adjectives, nouns } from 'unique-username-generator';
const config: Config = {
dictionaries: [adjectives, nouns]
}
const username: string = uniqueUsernameGenerator(config); // blossomlogistical
You might want to provide your custom dictionaries to use for generating your unique username, in order to meet your project requirements. You can easily do that using the dictionaries option.
import { uniqueUsernameGenerator } from 'unique-username-generator';
const marvelCharacters = [
'Iron Man',
'Doctor Strange',
'Hulk',
'Captain America',
'Thanos'
];
const config: Config = {
dictionaries: [marvelCharacters],
separator: '',
style: 'capital',
randomDigits: 3
}
const username: string = uniqueUsernameGenerator(config); // Hulk123
By default, the generator filters out common profane words from the built-in dictionaries to avoid unsafe names. You can extend or override the blocklist using exclude
and profanityList
.
import { uniqueUsernameGenerator, adjectives, nouns, DEFAULT_PROFANITY } from 'unique-username-generator';
const username = uniqueUsernameGenerator({
dictionaries: [adjectives, nouns],
exclude: ['beta', 'foo'], // custom exclusions
profanityList: DEFAULT_PROFANITY, // extend/override blocklist
randomDigits: 0
});
Additional styles are supported beyond lowerCase
, upperCase
, and capital
:
camelCase
pascalCase
kebabCase
snakeCase
titleCase
(capitalize each word)uniqueUsernameGenerator({ dictionaries: [["blue"],["whale"]], separator: "-", style: 'camelCase', randomDigits: 0 }); // blueWhale
// Template tokens: {adjective}, {noun}, positional {0},{1},... and {digits:n}
uniqueUsernameGenerator({
dictionaries: [adjectives, nouns],
template: "{adjective}-{noun}-{digits:2}",
seed: "v1", // make output deterministic
style: "lowerCase",
}); // e.g. "brave-otter-42"
import { generateMany, generateUniqueAsync } from 'unique-username-generator';
// Generate N (optionally unique) usernames in-memory
const many = generateMany({ dictionaries: [adjectives, nouns], count: 5, unique: true });
// Generate a username that is unique against an external store
const taken = new Set(["cool-fox"]);
const username = await generateUniqueAsync(
{ dictionaries: [adjectives, nouns], separator: "-" },
(candidate) => taken.has(candidate)
);
After installing, a simple CLI is available as usergen
(aliases: usernamegen
, unique-username
, uuname
).
Usage: usergen [options]
Options:
-s, --separator <sep> Separator between words (default: empty)
-d, --digits <n> Number of random digits to append (0-6)
-l, --length <n> Maximum username length (default: 15)
--style <style> Style: lowerCase | upperCase | capital | camelCase | pascalCase | kebabCase | snakeCase | titleCase
-U, --upper Shortcut for --style upperCase
--seed <seed> Deterministic seed for reproducible output
-t, --template <tpl> Template, e.g. "{adjective}-{noun}-{digits:2}"
-c, --count <n> Generate many
-u, --unique Ensure unique usernames within this run
-o, --out <file> Write results to a file (UTF-8)
--unsafe Disable profanity filtering
-U, --upper Shortcut for --style upperCase
-D, --dict <a,b,c> Provide a custom dictionary (can be used multiple times)
-x, --exclude <a,b,c> Extra words to exclude
-h, --help Show help
Returns a string
with a random generated username
Type: Config
The options
argument mostly corresponds to the properties defined for uniqueUsernameGenerator. Only dictionaries
is required.
Option | Type | Description | Default value | Example value |
---|---|---|---|---|
dictionaries | array | This is an array of dictionaries. Each dictionary is an array of strings containing the words to use for generating the string. The provided dictionaries can be imported from the library as a separate modules and provided in the desired order. | n/a | import { uniqueUsernameGenerator, adjectives, nouns } from 'unique-username-generator'; const username: string = uniqueUsernameGenerator({ dictionaries: [nouns, adjectives]}); // blossomlogistical |
separator | string | A string separator to be used for separate the words generated. The default separator is set to be empty string. | "" | - |
randomDigits | number | A number of random digits to add at the end of a username. | 0 | 3 |
length | number | A maximum length of a username | 15 | 12 |
style | lowerCase | upperCase | capital | titleCase | The default value is set to lowerCase and it will return a lower case username.By setting the value to upperCase , the words will be returned in upper case.The capital option will capitalize only the first character of the full username.titleCase will capitalize each word (token). | lowerCase | lowerCase |
Additional options:
Option | Type | Description | Default |
---|---|---|---|
exclude | string[] | Extra words to filter out | [] |
profanityList | string[] | Profanity blocklist to apply to dictionaries | built-in minimal list |
profanityOptions | { matchSubstrings?: boolean, wordBoundary?: string } | Fine-tune matching | word-boundary matching |
style | extend to: camelCase | pascalCase | kebabCase | snakeCase | titleCase | Additional output styles | lowerCase |
The MIT License.
If you'd like to say thanks, I'd really appreciate a coffee :)
FAQs
A package to generate a unique username from email or randomly selected nouns and adjectives. User can add a separator between the username, define the maximum length of a username and adds up to six random digits.
The npm package unique-username-generator receives a total of 131,655 weekly downloads. As such, unique-username-generator popularity was classified as popular.
We found that unique-username-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.