2025-01-27 15:59:36 -05:00

30 lines
1.2 KiB
JavaScript

const { uniqueNamesGenerator, adjectives, animals, colors, names, languages, starWars, countries } = require('unique-names-generator');
function generateUniqueName() {
const dictionaries = [adjectives, animals, colors, names, languages, starWars, countries];
const randomIndex1 = Math.floor(Math.random() * dictionaries.length);
let randomIndex2 = Math.floor(Math.random() * dictionaries.length);
while (randomIndex2 === randomIndex1) {
randomIndex2 = Math.floor(Math.random() * dictionaries.length);
}
const randomName = uniqueNamesGenerator({
dictionaries: [dictionaries[randomIndex1], dictionaries[randomIndex2]],
separator: '-',
style: 'lowerCase'
});
const formattedName = randomName
.replace(/[^a-zA-Z0-9\s-]/g, '') // Remove special characters except spaces and hyphens
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
.replace(/^-+|-+$/g, ''); // Remove hyphens from start and end
const randomNumber = Math.floor(Math.random() * 999999) + 1;
return `${formattedName}${randomNumber}`;
}
console.log(generateUniqueName());