Skip to content

Commit c2108fa

Browse files
fix(random): retry on invalid word generation (#1307)
1 parent df789f5 commit c2108fa

File tree

3 files changed

+84
-48
lines changed

3 files changed

+84
-48
lines changed

src/modules/random/index.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,67 @@ export class Random {
116116
*/
117117
word(): string {
118118
const wordMethods = [
119+
this.faker.address.cardinalDirection,
120+
this.faker.address.cityName,
121+
this.faker.address.country,
122+
this.faker.address.county,
123+
this.faker.address.direction,
124+
this.faker.address.ordinalDirection,
125+
this.faker.address.state,
126+
this.faker.address.street,
127+
128+
this.faker.color.human,
129+
119130
this.faker.commerce.department,
120-
this.faker.commerce.productName,
131+
this.faker.commerce.product,
121132
this.faker.commerce.productAdjective,
122133
this.faker.commerce.productMaterial,
123-
this.faker.commerce.product,
124-
this.faker.color.human,
134+
this.faker.commerce.productName,
125135

126-
this.faker.company.catchPhraseAdjective,
127-
this.faker.company.catchPhraseDescriptor,
128-
this.faker.company.catchPhraseNoun,
129136
this.faker.company.bsAdjective,
130137
this.faker.company.bsBuzz,
131138
this.faker.company.bsNoun,
132-
this.faker.address.county,
133-
this.faker.address.country,
134-
this.faker.address.state,
139+
this.faker.company.catchPhraseAdjective,
140+
this.faker.company.catchPhraseDescriptor,
141+
this.faker.company.catchPhraseNoun,
135142

136143
this.faker.finance.accountName,
137-
this.faker.finance.transactionType,
138144
this.faker.finance.currencyName,
145+
this.faker.finance.transactionType,
139146

140-
this.faker.hacker.noun,
141-
this.faker.hacker.verb,
147+
this.faker.hacker.abbreviation,
142148
this.faker.hacker.adjective,
143149
this.faker.hacker.ingverb,
144-
this.faker.hacker.abbreviation,
150+
this.faker.hacker.noun,
151+
this.faker.hacker.verb,
145152

146-
this.faker.name.jobDescriptor,
153+
this.faker.lorem.word,
154+
155+
this.faker.music.genre,
156+
157+
this.faker.name.gender,
147158
this.faker.name.jobArea,
159+
this.faker.name.jobDescriptor,
160+
this.faker.name.jobTitle,
148161
this.faker.name.jobType,
162+
this.faker.name.sex,
163+
164+
() => this.faker.science.chemicalElement().name,
165+
() => this.faker.science.unit().name,
166+
167+
this.faker.vehicle.bicycle,
168+
this.faker.vehicle.color,
169+
this.faker.vehicle.fuel,
170+
this.faker.vehicle.manufacturer,
171+
this.faker.vehicle.type,
172+
173+
this.faker.word.adjective,
174+
this.faker.word.adverb,
175+
this.faker.word.conjunction,
176+
this.faker.word.interjection,
177+
this.faker.word.noun,
178+
this.faker.word.preposition,
179+
this.faker.word.verb,
149180
];
150181

151182
const bannedChars = [
@@ -178,7 +209,12 @@ export class Random {
178209
// randomly pick from the many faker methods that can generate words
179210
const randomWordMethod = this.faker.helpers.arrayElement(wordMethods);
180211

181-
result = randomWordMethod();
212+
try {
213+
result = randomWordMethod();
214+
} catch {
215+
// catch missing locale data potentially required by randomWordMethod
216+
continue;
217+
}
182218
} while (!result || bannedChars.some((char) => result.includes(char)));
183219

184220
return this.faker.helpers.arrayElement(result.split(' '));

test/__snapshots__/random.spec.ts.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ exports[`random > 42 > numeric > noArgs 1`] = `"4"`;
1414

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

17-
exports[`random > 42 > word 1`] = `"impactful"`;
17+
exports[`random > 42 > word 1`] = `"responsive"`;
1818

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

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

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

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

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

37-
exports[`random > 1211 > word 1`] = `"Division"`;
37+
exports[`random > 1211 > word 1`] = `"gah"`;
3838

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

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

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

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

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

57-
exports[`random > 1337 > word 1`] = `"Organic"`;
57+
exports[`random > 1337 > word 1`] = `"Bespoke"`;
5858

59-
exports[`random > 1337 > words > noArgs 1`] = `"Delaware"`;
59+
exports[`random > 1337 > words > noArgs 1`] = `"nesciunt"`;
6060

61-
exports[`random > 1337 > words > withLength 1`] = `"Organic Bike interactive core Montenegro"`;
61+
exports[`random > 1337 > words > withLength 1`] = `"Bespoke connect Folsom Sleek Folk"`;

test/__snapshots__/system.spec.ts.snap

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)