Skip to content

Commit

Permalink
fix(random): retry on invalid word generation (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Aug 29, 2022
1 parent df789f5 commit c2108fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 48 deletions.
66 changes: 51 additions & 15 deletions src/modules/random/index.ts
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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(' '));
Expand Down
18 changes: 9 additions & 9 deletions test/__snapshots__/random.spec.ts.snap
Expand Up @@ -14,11 +14,11 @@ exports[`random > 42 > numeric > noArgs 1`] = `"4"`;

exports[`random > 42 > numeric > withLength 1`] = `"47917"`;

exports[`random > 42 > word 1`] = `"impactful"`;
exports[`random > 42 > word 1`] = `"responsive"`;

exports[`random > 42 > words > noArgs 1`] = `"mobile Fish"`;
exports[`random > 42 > words > noArgs 1`] = `"lavender Shoes"`;

exports[`random > 42 > words > withLength 1`] = `"impactful Directives transmit Home initiatives"`;
exports[`random > 42 > words > withLength 1`] = `"responsive comeback Neptunium Hip deposit"`;

exports[`random > 1211 > alpha > noArgs 1`] = `"y"`;

Expand All @@ -34,11 +34,11 @@ exports[`random > 1211 > numeric > noArgs 1`] = `"9"`;

exports[`random > 1211 > numeric > withLength 1`] = `"94872"`;

exports[`random > 1211 > word 1`] = `"Division"`;
exports[`random > 1211 > word 1`] = `"gah"`;

exports[`random > 1211 > words > noArgs 1`] = `"functionalities Frozen Baby"`;
exports[`random > 1211 > words > noArgs 1`] = `"invoice Cyclocross assault"`;

exports[`random > 1211 > words > withLength 1`] = `"Division Forward purple Supervisor Peso"`;
exports[`random > 1211 > words > withLength 1`] = `"gah strictly Rustic assault Manager"`;

exports[`random > 1337 > alpha > noArgs 1`] = `"g"`;

Expand All @@ -54,8 +54,8 @@ exports[`random > 1337 > numeric > noArgs 1`] = `"3"`;

exports[`random > 1337 > numeric > withLength 1`] = `"35122"`;

exports[`random > 1337 > word 1`] = `"Organic"`;
exports[`random > 1337 > word 1`] = `"Bespoke"`;

exports[`random > 1337 > words > noArgs 1`] = `"Delaware"`;
exports[`random > 1337 > words > noArgs 1`] = `"nesciunt"`;

exports[`random > 1337 > words > withLength 1`] = `"Organic Bike interactive core Montenegro"`;
exports[`random > 1337 > words > withLength 1`] = `"Bespoke connect Folsom Sleek Folk"`;
48 changes: 24 additions & 24 deletions test/__snapshots__/system.spec.ts.snap
Expand Up @@ -2,9 +2,9 @@

exports[`system > 42 > commonFileExt 1`] = `"png"`;

exports[`system > 42 > commonFileName > noArgs 1`] = `"mobile_fish.mpe"`;
exports[`system > 42 > commonFileName > noArgs 1`] = `"lavender_shoes.mpe"`;

exports[`system > 42 > commonFileName > with extension 1`] = `"mobile_fish.ext"`;
exports[`system > 42 > commonFileName > with extension 1`] = `"lavender_shoes.ext"`;

exports[`system > 42 > commonFileType 1`] = `"audio"`;

Expand All @@ -14,11 +14,11 @@ exports[`system > 42 > fileExt > noArgs 1`] = `"lrm"`;

exports[`system > 42 > fileExt > with mimeType 1`] = `"json"`;

exports[`system > 42 > fileName > noArgs 1`] = `"mobile_fish.jxsc"`;
exports[`system > 42 > fileName > noArgs 1`] = `"lavender_shoes.jxsc"`;

exports[`system > 42 > fileName > with extensionCount 1`] = `"mobile_fish.jxsc.jardiff"`;
exports[`system > 42 > fileName > with extensionCount 1`] = `"lavender_shoes.jxsc.jardiff"`;

exports[`system > 42 > filePath 1`] = `"/opt/bin/directives_transmit_home.ptid"`;
exports[`system > 42 > filePath 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`;

exports[`system > 42 > fileType 1`] = `"image"`;

Expand Down Expand Up @@ -70,9 +70,9 @@ exports[`system > 42 > semver 1`] = `"3.7.9"`;

exports[`system > 1211 > commonFileExt 1`] = `"htm"`;

exports[`system > 1211 > commonFileName > noArgs 1`] = `"functionalities_frozen_baby.m2a"`;
exports[`system > 1211 > commonFileName > noArgs 1`] = `"invoice_cyclocross_assault.mp4"`;

exports[`system > 1211 > commonFileName > with extension 1`] = `"functionalities_frozen_baby.ext"`;
exports[`system > 1211 > commonFileName > with extension 1`] = `"invoice_cyclocross_assault.ext"`;

exports[`system > 1211 > commonFileType 1`] = `"application"`;

Expand All @@ -82,11 +82,11 @@ exports[`system > 1211 > fileExt > noArgs 1`] = `"dic"`;

exports[`system > 1211 > fileExt > with mimeType 1`] = `"map"`;

exports[`system > 1211 > fileName > noArgs 1`] = `"functionalities_frozen_baby.tsd"`;
exports[`system > 1211 > fileName > noArgs 1`] = `"invoice_cyclocross_assault.z2"`;

exports[`system > 1211 > fileName > with extensionCount 1`] = `"functionalities_frozen_baby.tsd.xml"`;
exports[`system > 1211 > fileName > with extensionCount 1`] = `"invoice_cyclocross_assault.z2.tsd"`;

exports[`system > 1211 > filePath 1`] = `"/var/log/forward_purple.avi"`;
exports[`system > 1211 > filePath 1`] = `"/var/log/strictly_rustic.avi"`;

exports[`system > 1211 > fileType 1`] = `"x-shader"`;

Expand Down Expand Up @@ -138,9 +138,9 @@ exports[`system > 1211 > semver 1`] = `"9.4.8"`;

exports[`system > 1337 > commonFileExt 1`] = `"wav"`;

exports[`system > 1337 > commonFileName > noArgs 1`] = `"delaware.mp2"`;
exports[`system > 1337 > commonFileName > noArgs 1`] = `"nesciunt.mp2"`;

exports[`system > 1337 > commonFileName > with extension 1`] = `"delaware.ext"`;
exports[`system > 1337 > commonFileName > with extension 1`] = `"nesciunt.ext"`;

exports[`system > 1337 > commonFileType 1`] = `"audio"`;

Expand All @@ -150,11 +150,11 @@ exports[`system > 1337 > fileExt > noArgs 1`] = `"oa3"`;

exports[`system > 1337 > fileExt > with mimeType 1`] = `"json"`;

exports[`system > 1337 > fileName > noArgs 1`] = `"delaware.cmc"`;
exports[`system > 1337 > fileName > noArgs 1`] = `"nesciunt.cmc"`;

exports[`system > 1337 > fileName > with extensionCount 1`] = `"delaware.cmc.kml"`;
exports[`system > 1337 > fileName > with extensionCount 1`] = `"nesciunt.cmc.kml"`;

exports[`system > 1337 > filePath 1`] = `"/Library/bike_interactive.link66"`;
exports[`system > 1337 > filePath 1`] = `"/Library/duane_folsom.dpg"`;

exports[`system > 1337 > fileType 1`] = `"font"`;

Expand Down Expand Up @@ -206,17 +206,17 @@ exports[`system > 1337 > semver 1`] = `"2.5.1"`;

exports[`system > seed: 42 > commonFileExt() 1`] = `"png"`;

exports[`system > seed: 42 > commonFileName() 1`] = `"mobile_fish.mpe"`;
exports[`system > seed: 42 > commonFileName() 1`] = `"lavender_shoes.mpe"`;

exports[`system > seed: 42 > commonFileType() 1`] = `"audio"`;

exports[`system > seed: 42 > directoryPath() 1`] = `"/opt/bin"`;

exports[`system > seed: 42 > fileExt() 1`] = `"lrm"`;

exports[`system > seed: 42 > fileName() 1`] = `"mobile_fish.jxsc"`;
exports[`system > seed: 42 > fileName() 1`] = `"lavender_shoes.jxsc"`;

exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/directives_transmit_home.ptid"`;
exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`;

exports[`system > seed: 42 > fileType() 1`] = `"image"`;

Expand All @@ -228,17 +228,17 @@ exports[`system > seed: 42 > semver() 1`] = `"3.7.9"`;

exports[`system > seed: 1211 > commonFileExt() 1`] = `"htm"`;

exports[`system > seed: 1211 > commonFileName() 1`] = `"functionalities_frozen_baby.m2a"`;
exports[`system > seed: 1211 > commonFileName() 1`] = `"invoice_cyclocross_assault.mp4"`;

exports[`system > seed: 1211 > commonFileType() 1`] = `"application"`;

exports[`system > seed: 1211 > directoryPath() 1`] = `"/var/log"`;

exports[`system > seed: 1211 > fileExt() 1`] = `"dic"`;

exports[`system > seed: 1211 > fileName() 1`] = `"functionalities_frozen_baby.tsd"`;
exports[`system > seed: 1211 > fileName() 1`] = `"invoice_cyclocross_assault.z2"`;

exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/forward_purple.avi"`;
exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/strictly_rustic.avi"`;

exports[`system > seed: 1211 > fileType() 1`] = `"x-shader"`;

Expand All @@ -250,17 +250,17 @@ exports[`system > seed: 1211 > semver() 1`] = `"9.4.8"`;

exports[`system > seed: 1337 > commonFileExt() 1`] = `"wav"`;

exports[`system > seed: 1337 > commonFileName() 1`] = `"delaware.mp2"`;
exports[`system > seed: 1337 > commonFileName() 1`] = `"nesciunt.mp2"`;

exports[`system > seed: 1337 > commonFileType() 1`] = `"audio"`;

exports[`system > seed: 1337 > directoryPath() 1`] = `"/Library"`;

exports[`system > seed: 1337 > fileExt() 1`] = `"oa3"`;

exports[`system > seed: 1337 > fileName() 1`] = `"delaware.cmc"`;
exports[`system > seed: 1337 > fileName() 1`] = `"nesciunt.cmc"`;

exports[`system > seed: 1337 > filePath() 1`] = `"/Library/bike_interactive.link66"`;
exports[`system > seed: 1337 > filePath() 1`] = `"/Library/duane_folsom.dpg"`;

exports[`system > seed: 1337 > fileType() 1`] = `"font"`;

Expand Down

0 comments on commit c2108fa

Please sign in to comment.