Skip to content

Gurleen-kansray/treebio

 
 

Repository files navigation

Username Suggestion Function Breakdown

Function Declaration

export async function getAvailableUsernameSuggestions(base: string, count = 3, maxTries = 10) {

What it does:

  • Declares an async function (can wait for database operations)
  • Takes 3 parameters:
    • base: the starting username (like "john")
    • count: how many suggestions to return (default: 3)
    • maxTries: maximum attempts to find usernames (default: 10)

Initialize Storage

const suggestions: string[] = [];

What it does:

  • Creates an empty array to store the available usernames we find
  • TypeScript notation : string[] means it only holds strings

Main Loop

for (let i = 1; suggestions.length < count && i < maxTries; i++) {

What it does:

  • Starts a loop with counter i = 1
  • Continues while BOTH conditions are true:
    • We haven't found enough suggestions yet (suggestions.length < count)
    • We haven't exceeded our attempt limit (i < maxTries)
  • Increments i after each attempt

Generate Username Candidate

const candidate = `${base}${i}`;

What it does:

  • Creates a potential username by combining the base with the current number
  • Example: if base = "john" and i = 1, then candidate = "john1"

Check Database

const exists = await db.user.findUnique({ where: { username: candidate } });

What it does:

  • Searches the database to see if this username already exists
  • await pauses the function until the database responds
  • Returns the user record if found, or null if not found

Add to Suggestions

if (!exists) suggestions.push(candidate);

What it does:

  • If the username doesn't exist in the database (!exists means "not exists")
  • Adds the available username to our suggestions array

Return Results

return suggestions;

What it does:

  • Returns the array of available usernames we found

Example Usage

If you call getAvailableUsernameSuggestions("sarah", 3):

  1. Try "sarah1" → Check database → If available, add to suggestions
  2. Try "sarah2" → Check database → If available, add to suggestions
  3. Try "sarah3" → Check database → If available, add to suggestions
  4. Continue until we have 3 suggestions OR we've tried 10 times

Possible result: ["sarah1", "sarah3", "sarah7"] (if "sarah2" was taken)

Key Features

  • Smart stopping: Stops when it finds enough suggestions, doesn't waste time
  • Prevents infinite loops: Won't try forever if most usernames are taken
  • Efficient: Only checks database for usernames that might work
  • Flexible: You can customize how many suggestions and how many attempts

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.3%
  • CSS 1.5%
  • JavaScript 0.2%