diff options
| author | Leyla Jähnig <[email protected]> | 2022-08-29 13:02:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-29 11:02:02 +0000 |
| commit | c2108fa5db889bb1455a5735934776bcf91fabac (patch) | |
| tree | c56000d64e55e3f893fafc50642d041b6a614bc5 /src | |
| parent | df789f5cf0407416667173595009810071e96eab (diff) | |
| download | faker-c2108fa5db889bb1455a5735934776bcf91fabac.tar.xz faker-c2108fa5db889bb1455a5735934776bcf91fabac.zip | |
fix(random): retry on invalid word generation (#1307)
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/random/index.ts | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 94bdcda1..4550e298 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -116,36 +116,67 @@ export class Random { */ word(): string { const wordMethods = [ + this.faker.address.cardinalDirection, + this.faker.address.cityName, + this.faker.address.country, + this.faker.address.county, + this.faker.address.direction, + this.faker.address.ordinalDirection, + this.faker.address.state, + this.faker.address.street, + + this.faker.color.human, + this.faker.commerce.department, - this.faker.commerce.productName, + this.faker.commerce.product, this.faker.commerce.productAdjective, this.faker.commerce.productMaterial, - this.faker.commerce.product, - this.faker.color.human, + this.faker.commerce.productName, - this.faker.company.catchPhraseAdjective, - this.faker.company.catchPhraseDescriptor, - this.faker.company.catchPhraseNoun, this.faker.company.bsAdjective, this.faker.company.bsBuzz, this.faker.company.bsNoun, - this.faker.address.county, - this.faker.address.country, - this.faker.address.state, + this.faker.company.catchPhraseAdjective, + this.faker.company.catchPhraseDescriptor, + this.faker.company.catchPhraseNoun, this.faker.finance.accountName, - this.faker.finance.transactionType, this.faker.finance.currencyName, + this.faker.finance.transactionType, - this.faker.hacker.noun, - this.faker.hacker.verb, + this.faker.hacker.abbreviation, this.faker.hacker.adjective, this.faker.hacker.ingverb, - this.faker.hacker.abbreviation, + this.faker.hacker.noun, + this.faker.hacker.verb, - this.faker.name.jobDescriptor, + this.faker.lorem.word, + + this.faker.music.genre, + + this.faker.name.gender, this.faker.name.jobArea, + this.faker.name.jobDescriptor, + this.faker.name.jobTitle, this.faker.name.jobType, + this.faker.name.sex, + + () => this.faker.science.chemicalElement().name, + () => this.faker.science.unit().name, + + this.faker.vehicle.bicycle, + this.faker.vehicle.color, + this.faker.vehicle.fuel, + this.faker.vehicle.manufacturer, + this.faker.vehicle.type, + + this.faker.word.adjective, + this.faker.word.adverb, + this.faker.word.conjunction, + this.faker.word.interjection, + this.faker.word.noun, + this.faker.word.preposition, + this.faker.word.verb, ]; const bannedChars = [ @@ -178,7 +209,12 @@ export class Random { // randomly pick from the many faker methods that can generate words const randomWordMethod = this.faker.helpers.arrayElement(wordMethods); - result = randomWordMethod(); + try { + result = randomWordMethod(); + } catch { + // catch missing locale data potentially required by randomWordMethod + continue; + } } while (!result || bannedChars.some((char) => result.includes(char))); return this.faker.helpers.arrayElement(result.split(' ')); |
