feat: input page (wip)
This commit is contained in:
+29
-9
@@ -15,15 +15,19 @@ export class DomainFactory {
|
||||
this.words.english = this.initWords(englishWords);
|
||||
}
|
||||
|
||||
escapeWord(word: string): string {
|
||||
return word
|
||||
.normalize("NFD")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-]/g, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
initWords(raw: string): string[] {
|
||||
return raw
|
||||
.split("\n")
|
||||
.map((word) => {
|
||||
word = word
|
||||
.normalize("NFD")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-]/g, "")
|
||||
.trim();
|
||||
word = this.escapeWord(word);
|
||||
if (word.length == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -40,13 +44,12 @@ export class DomainFactory {
|
||||
return randomElement(this.words[lang]);
|
||||
}
|
||||
|
||||
randomDomain(lang: string): string | null {
|
||||
const word = this.randomWord(lang);
|
||||
randomDomain(word: string): string | null {
|
||||
for (const tld of shuffled(TLD_LIST.slice())) {
|
||||
const tldEscaped: string = tld.replace(".", "");
|
||||
if (word.endsWith(tldEscaped) && word.length > tldEscaped.length + 2) {
|
||||
const domain = `${word.substring(0, word.length - tldEscaped.length)}.${tld}`;
|
||||
if (!domain.includes("-.")) {
|
||||
if (!domain.includes("-.") && !this.generated.includes(domain)) {
|
||||
this.generated.push(domain);
|
||||
return domain;
|
||||
}
|
||||
@@ -60,7 +63,8 @@ export class DomainFactory {
|
||||
let k = 10000;
|
||||
while (k > 0) {
|
||||
k--;
|
||||
const domain = this.randomDomain(lang);
|
||||
const word = this.randomWord(lang);
|
||||
const domain = this.randomDomain(word);
|
||||
if (domain) {
|
||||
resolve(domain);
|
||||
return;
|
||||
@@ -70,4 +74,20 @@ export class DomainFactory {
|
||||
reject(new Error(this.error));
|
||||
});
|
||||
}
|
||||
|
||||
nextFromWord(word: string): Promise<string> {
|
||||
const escapedWord = this.escapeWord(word.replace(/ /gm, "-"));
|
||||
return new Promise((resolve, reject) => {
|
||||
let k = 10000;
|
||||
while (k > 0) {
|
||||
k--;
|
||||
const domain = this.randomDomain(escapedWord);
|
||||
if (domain) {
|
||||
resolve(domain);
|
||||
return;
|
||||
}
|
||||
}
|
||||
reject(new Error("Could not generate a valid domain"));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user