Skip to main content
Home
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
82%
Published
a year ago (2.1.1)

Username Generator

Description

usernameGenerator is a flexible JavaScript library designed to integrate seamlessly with Express.js controllers. It efficiently generates unique usernames based on user-provided criteria without blocking the event loop, ideal for handling HTTP requests within an Express application.

Installation

Install the package using npm:

npm install username-generator

Features

  • Asynchronous Username Generation: Perfect for real-time web applications.
  • Customizable: Supports custom lengths, inclusion of symbols, and various separators.
  • Express Compatibility: Optimized for use within Express.js server environments.

API Reference

usernameGenerator(options: UsernameGeneratorInput): Promise<string>

Generates a unique username based on provided options.

Parameters

  • fullName (string): The user's full name from which the username will be generated.
  • usedUsernames (string[]): An array of usernames already in use to ensure uniqueness.
  • minLength (number): Optional. Minimum length of the username, default is 8 characters.
  • allowSymbols (boolean): Optional. Whether to include symbols, default is false.
  • separator (string): Optional. Character to replace spaces in names, default is an empty string.

Returns

  • Promise: A promise that resolves to the generated username.

Example Usage

Below is an example of how usernameGenerator can be used:

import { usernameGenerator, UsernameGeneratorInput } from 'username-generator';

async function generateAndLogUsername() {
  const options: UsernameGeneratorInput = {
    fullName: 'John Doe', // Full name
    usedUsernames: ['johndoe', 'john_doe', 'johndoe1'], // List of used usernames
    allowSymbols: false, // Symbols allowed in username
    minLength: 6, // Minimum length of the username
    separator: '_', // Separator between first and last name
  };

  try {
    const username = await usernameGenerator(options); // Generate username
    console.log('Generated Username:', username); // Log the generated username
  } catch (error) {
    console.error('Error during username generation:', error); // Log error if username generation fails
  }
}

generateAndLogUsername(); // Call the function to generate and log a username

This example demonstrates how the usernameGenerator function can be used to generate a username with customized options.

Support

For support, issues, or further documentation, please visit our GitHub repository.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";
or

Import directly with a jsr specifier

import * as username_generator from "jsr:@liu/username-generator";

Add Package

pnpm i jsr:@liu/username-generator
or (using pnpm 10.8 or older)
pnpm dlx jsr add @liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";

Add Package

yarn add jsr:@liu/username-generator
or (using Yarn 4.8 or older)
yarn dlx jsr add @liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";

Add Package

vlt install jsr:@liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";

Add Package

npx jsr add @liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";

Add Package

bunx jsr add @liu/username-generator

Import symbol

import * as username_generator from "@liu/username-generator";