aboutsummaryrefslogtreecommitdiff
path: root/src/modules/internet
diff options
context:
space:
mode:
authorMatt Mayer <[email protected]>2024-11-08 17:57:53 +0700
committerGitHub <[email protected]>2024-11-08 11:57:53 +0100
commit525fedc91bd02f53330cfb40fe228b148dcf562b (patch)
treeab36406c307c499ddbad4ddbd9c9ef78b81c92d1 /src/modules/internet
parent69173a36ed2854712cc27ddceed9a92bacf48336 (diff)
downloadfaker-525fedc91bd02f53330cfb40fe228b148dcf562b.tar.xz
faker-525fedc91bd02f53330cfb40fe228b148dcf562b.zip
fix(internet): ensure domainWord always returns a valid value in all locales (#3253)
Diffstat (limited to 'src/modules/internet')
-rw-r--r--src/modules/internet/index.ts29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts
index 58c6e586..115bd215 100644
--- a/src/modules/internet/index.ts
+++ b/src/modules/internet/index.ts
@@ -595,9 +595,32 @@ export class InternetModule extends ModuleBase {
* @since 2.0.1
*/
domainWord(): string {
- return this.faker.helpers
- .slugify(`${this.faker.word.adjective()}-${this.faker.word.noun()}`)
- .toLowerCase();
+ // Generate an ASCII "word" in the form `noun-adjective`
+ // For locales with non-ASCII characters, we fall back to lorem words, or a random string
+ const isValidSlug = (slug: string): boolean => {
+ return /^[a-z][a-z-]*[a-z]$/i.exec(slug) !== null;
+ };
+
+ const makeValidSlug = (word: string): string => {
+ const slug1 = this.faker.helpers.slugify(word);
+ if (isValidSlug(slug1)) {
+ return slug1;
+ }
+
+ const slug2 = this.faker.helpers.slugify(this.faker.lorem.word());
+ if (isValidSlug(slug2)) {
+ return slug2;
+ }
+
+ return this.faker.string.alpha({
+ casing: 'lower',
+ length: this.faker.number.int({ min: 4, max: 8 }),
+ });
+ };
+
+ const word1 = makeValidSlug(this.faker.word.adjective());
+ const word2 = makeValidSlug(this.faker.word.noun());
+ return `${word1}-${word2}`.toLowerCase();
}
/**