diff options
| author | Shinigami <[email protected]> | 2022-03-24 12:39:37 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-03-24 11:39:37 +0000 |
| commit | b7b2e4f8dbd40b6bb2678fb60ee95e198838d08a (patch) | |
| tree | 2fa52212f1520d5d0610ff1ddd92784489c4b270 /src | |
| parent | 0924e852d300e012c80d74fb94b57168965827bf (diff) | |
| download | faker-b7b2e4f8dbd40b6bb2678fb60ee95e198838d08a.tar.xz faker-b7b2e4f8dbd40b6bb2678fb60ee95e198838d08a.zip | |
fix: only return word with desirable alpha characters (#654)
Diffstat (limited to 'src')
| -rw-r--r-- | src/random.ts | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/random.ts b/src/random.ts index b84a2ece..0658cdd5 100644 --- a/src/random.ts +++ b/src/random.ts @@ -242,7 +242,6 @@ export class Random { * @example * faker.random.word() // 'Seamless' */ - // TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc word(): string { const wordMethods = [ 'commerce.department', @@ -278,9 +277,36 @@ export class Random { 'name.jobType', ]; - // randomly pick from the many faker methods that can generate words - const randomWordMethod = this.faker.random.arrayElement(wordMethods); - const result = this.faker.fake('{{' + randomWordMethod + '}}'); + const bannedChars = [ + '!', + '#', + '%', + '&', + '*', + ')', + '(', + '+', + '=', + '.', + '<', + '>', + '{', + '}', + '[', + ']', + ':', + ';', + "'", + '"', + '_', + '-', + ]; + let result: string; + do { + // randomly pick from the many faker methods that can generate words + const randomWordMethod = this.faker.random.arrayElement(wordMethods); + result = this.faker.fake('{{' + randomWordMethod + '}}'); + } while (bannedChars.some((char) => result.includes(char))); return this.faker.random.arrayElement(result.split(' ')); } |
