auto-account-machine/src/tools/account-register/generators/username-generator.js
dengqichen bf55bcee27 aaaaa
2025-11-16 19:46:13 +08:00

119 lines
2.9 KiB
JavaScript

/**
* Username Generator - 用户名生成器
*/
const { randomInt } = require('../../../shared/utils');
class UsernameGenerator {
constructor() {
this.adjectives = [
'quick', 'bright', 'swift', 'clever', 'smart',
'cool', 'happy', 'lucky', 'brave', 'wise',
'silent', 'dark', 'golden', 'silver', 'mystic'
];
this.nouns = [
'wolf', 'fox', 'eagle', 'tiger', 'dragon',
'phoenix', 'lion', 'bear', 'falcon', 'hawk',
'ninja', 'wizard', 'warrior', 'knight', 'hunter'
];
}
/**
* 生成用户名
* @param {Object} options - 选项
* @returns {string}
*/
generate(options = {}) {
const { pattern, minLength, maxLength } = options;
if (pattern) {
return this.generateFromPattern(pattern);
}
// 默认生成策略
const type = options.type || 'random';
switch (type) {
case 'word':
return this.generateWordBased();
case 'random':
return this.generateRandom(minLength, maxLength);
case 'numeric':
return this.generateNumeric();
default:
return this.generateWordBased();
}
}
/**
* 基于模式生成
* @param {string} pattern - 模式,如 "user_{random}", "player_{number}"
* @returns {string}
*/
generateFromPattern(pattern) {
return pattern
.replace('{random}', Math.random().toString(36).substring(2, 10))
.replace('{number}', randomInt(1000, 9999).toString())
.replace('{timestamp}', Date.now().toString().slice(-8));
}
/**
* 生成基于单词的用户名
* @returns {string}
*/
generateWordBased() {
const adj = this.adjectives[randomInt(0, this.adjectives.length - 1)];
const noun = this.nouns[randomInt(0, this.nouns.length - 1)];
const number = randomInt(10, 999);
return `${adj}${noun}${number}`;
}
/**
* 生成随机字符串用户名
* @param {number} minLength - 最小长度
* @param {number} maxLength - 最大长度
* @returns {string}
*/
generateRandom(minLength = 8, maxLength = 12) {
const length = randomInt(minLength, maxLength);
let username = '';
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
// 首字母必须是字母
username += chars.charAt(randomInt(0, 25));
// 其余字符
for (let i = 1; i < length; i++) {
username += chars.charAt(randomInt(0, chars.length - 1));
}
return username;
}
/**
* 生成数字用户名
* @returns {string}
*/
generateNumeric() {
return `user${randomInt(100000, 999999)}`;
}
/**
* 批量生成
* @param {number} count - 数量
* @param {Object} options - 选项
* @returns {Array}
*/
generateBatch(count, options = {}) {
const usernames = [];
for (let i = 0; i < count; i++) {
usernames.push(this.generate(options));
}
return usernames;
}
}
module.exports = UsernameGenerator;