Skip to content

Commit 3777c44

Browse files
pkuczynskiShinigami92ST-DDT
authored
feat(word): add sample method (#714)
Co-authored-by: Shinigami <[email protected]> Co-authored-by: ST-DDT <[email protected]>
1 parent 666ff02 commit 3777c44

File tree

7 files changed

+157
-32
lines changed

7 files changed

+157
-32
lines changed

src/modules/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class HelpersModule {
307307
* @param length The number of elements to generate.
308308
*
309309
* @example
310-
* faker.helpers.uniqueArray(faker.random.word, 50)
310+
* faker.helpers.uniqueArray(faker.word.sample, 50)
311311
* faker.helpers.uniqueArray(faker.definitions.person.first_name, 6)
312312
* faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2)
313313
*

src/modules/system/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export class SystemModule {
5353
* @param options.extensionCount Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
5454
*
5555
* @example
56-
* faker.system.fileName() // 'self_enabling_accountability_toys.kpt'
57-
* faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs'
56+
* faker.system.fileName() // 'faithfully_calculating.u8mdn'
57+
* faker.system.fileName({ extensionCount: 2 }) // 'times_after.swf.ntf'
5858
*
5959
* @since 3.1.0
6060
*/
@@ -68,10 +68,7 @@ export class SystemModule {
6868
): string {
6969
const { extensionCount = 1 } = options;
7070

71-
const baseName = this.faker.random
72-
.words()
73-
.toLowerCase()
74-
.replace(/\W/g, '_');
71+
const baseName = this.faker.word.words().toLowerCase().replace(/\W/g, '_');
7572

7673
if (extensionCount <= 0) {
7774
return baseName;

src/modules/word/index.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { FakerError } from '../../errors/faker-error';
23
import { filterWordListByLength } from './filterWordListByLength';
34

45
/**
@@ -315,4 +316,78 @@ export class WordModule {
315316
})
316317
);
317318
}
319+
320+
/**
321+
* Returns a random sample of random or optionally specified length.
322+
*
323+
* @param options The expected length of the word or the options to use.
324+
* @param options.length The expected length of the word.
325+
* @param options.strategy The strategy to apply when no words with a matching length are found.
326+
*
327+
* Available error handling strategies:
328+
*
329+
* - `fail`: Throws an error if no words with the given length are found.
330+
* - `shortest`: Returns any of the shortest words.
331+
* - `closest`: Returns any of the words closest to the given length.
332+
* - `longest`: Returns any of the longest words.
333+
* - `any-length`: Returns a word with any length.
334+
*
335+
* Defaults to `'any-length'`.
336+
*
337+
* @example
338+
* faker.word.sample() // 'incidentally'
339+
* faker.word.sample(5) // 'fruit'
340+
*
341+
* @since 8.0.0
342+
*/
343+
sample(
344+
options:
345+
| number
346+
| {
347+
length?: number | { min: number; max: number };
348+
strategy?: 'fail' | 'closest' | 'shortest' | 'longest' | 'any-length';
349+
} = {}
350+
): string {
351+
const wordMethods = this.faker.helpers.shuffle([
352+
this.adjective,
353+
this.adverb,
354+
this.conjunction,
355+
this.interjection,
356+
this.noun,
357+
this.preposition,
358+
this.verb,
359+
]);
360+
361+
for (const randomWordMethod of wordMethods) {
362+
try {
363+
return randomWordMethod(options);
364+
} catch {
365+
// catch missing locale data potentially required by randomWordMethod
366+
continue;
367+
}
368+
}
369+
370+
throw new FakerError(
371+
'No matching word data available for the current locale'
372+
);
373+
}
374+
375+
/**
376+
* Returns a string containing a number of space separated random words.
377+
*
378+
* @param count Number of words. Defaults to a random value between `1` and `3`.
379+
*
380+
* @example
381+
* faker.word.words() // 'almost'
382+
* faker.word.words(5) // 'before hourly patiently dribble equal'
383+
*
384+
* @since 8.0.0
385+
*/
386+
words(count?: number): string {
387+
if (count == null) {
388+
count = this.faker.datatype.number({ min: 1, max: 3 });
389+
}
390+
391+
return Array.from({ length: count }, () => this.sample()).join(' ');
392+
}
318393
}

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`] = `"lavender_shoes.mpe"`;
5+
exports[`system > 42 > commonFileName > noArgs 1`] = `"nonbeliever_stub.png"`;
66

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

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

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

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

27-
exports[`system > 42 > fileName > noArgs 1`] = `"lavender_shoes.jxsc"`;
27+
exports[`system > 42 > fileName > noArgs 1`] = `"nonbeliever_stub.skt"`;
2828

29-
exports[`system > 42 > fileName > with extensionCount 1`] = `"lavender_shoes.jxsc.jardiff"`;
29+
exports[`system > 42 > fileName > with extensionCount 1`] = `"nonbeliever_stub.skt.latex"`;
3030

31-
exports[`system > 42 > filePath 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`;
31+
exports[`system > 42 > filePath 1`] = `"/opt/bin/crowded_fail_woefully.dra"`;
3232

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

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

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

83-
exports[`system > 1211 > commonFileName > noArgs 1`] = `"invoice_cyclocross_assault.mp4"`;
83+
exports[`system > 1211 > commonFileName > noArgs 1`] = `"although_instantly_though.gif"`;
8484

85-
exports[`system > 1211 > commonFileName > with extension 1`] = `"invoice_cyclocross_assault.ext"`;
85+
exports[`system > 1211 > commonFileName > with extension 1`] = `"although_instantly_though.ext"`;
8686

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

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

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

105-
exports[`system > 1211 > fileName > noArgs 1`] = `"invoice_cyclocross_assault.z2"`;
105+
exports[`system > 1211 > fileName > noArgs 1`] = `"although_instantly_though.ustar"`;
106106

107-
exports[`system > 1211 > fileName > with extensionCount 1`] = `"invoice_cyclocross_assault.z2.tsd"`;
107+
exports[`system > 1211 > fileName > with extensionCount 1`] = `"although_instantly_though.ustar.ecelp4800"`;
108108

109-
exports[`system > 1211 > filePath 1`] = `"/var/log/strictly_rustic.avi"`;
109+
exports[`system > 1211 > filePath 1`] = `"/var/log/outside_even.xhvml"`;
110110

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

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

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

161-
exports[`system > 1337 > commonFileName > noArgs 1`] = `"quia.mp2"`;
161+
exports[`system > 1337 > commonFileName > noArgs 1`] = `"although.wav"`;
162162

163-
exports[`system > 1337 > commonFileName > with extension 1`] = `"quia.ext"`;
163+
exports[`system > 1337 > commonFileName > with extension 1`] = `"although.ext"`;
164164

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

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

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

183-
exports[`system > 1337 > fileName > noArgs 1`] = `"quia.cmc"`;
183+
exports[`system > 1337 > fileName > noArgs 1`] = `"although.chrt"`;
184184

185-
exports[`system > 1337 > fileName > with extensionCount 1`] = `"quia.cmc.kml"`;
185+
exports[`system > 1337 > fileName > with extensionCount 1`] = `"although.chrt.dpg"`;
186186

187-
exports[`system > 1337 > filePath 1`] = `"/Library/duane_fort.wmlc"`;
187+
exports[`system > 1337 > filePath 1`] = `"/Library/yum_fast.aiff"`;
188188

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

@@ -236,7 +236,7 @@ exports[`system > 1337 > semver 1`] = `"2.5.1"`;
236236

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

239-
exports[`system > seed: 42 > commonFileName() 1`] = `"lavender_shoes.mpe"`;
239+
exports[`system > seed: 42 > commonFileName() 1`] = `"nonbeliever_stub.png"`;
240240

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

@@ -246,9 +246,9 @@ exports[`system > seed: 42 > directoryPath() 1`] = `"/opt/bin"`;
246246

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

249-
exports[`system > seed: 42 > fileName() 1`] = `"lavender_shoes.jxsc"`;
249+
exports[`system > seed: 42 > fileName() 1`] = `"nonbeliever_stub.skt"`;
250250

251-
exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/comeback_neptunium_hip.ptid"`;
251+
exports[`system > seed: 42 > filePath() 1`] = `"/opt/bin/crowded_fail_woefully.dra"`;
252252

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

@@ -260,7 +260,7 @@ exports[`system > seed: 42 > semver() 1`] = `"3.7.9"`;
260260

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

263-
exports[`system > seed: 1211 > commonFileName() 1`] = `"invoice_cyclocross_assault.mp4"`;
263+
exports[`system > seed: 1211 > commonFileName() 1`] = `"although_instantly_though.gif"`;
264264

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

@@ -270,9 +270,9 @@ exports[`system > seed: 1211 > directoryPath() 1`] = `"/var/log"`;
270270

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

273-
exports[`system > seed: 1211 > fileName() 1`] = `"invoice_cyclocross_assault.z2"`;
273+
exports[`system > seed: 1211 > fileName() 1`] = `"although_instantly_though.ustar"`;
274274

275-
exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/strictly_rustic.avi"`;
275+
exports[`system > seed: 1211 > filePath() 1`] = `"/var/log/outside_even.xhvml"`;
276276

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

@@ -284,7 +284,7 @@ exports[`system > seed: 1211 > semver() 1`] = `"9.4.8"`;
284284

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

287-
exports[`system > seed: 1337 > commonFileName() 1`] = `"quia.mp2"`;
287+
exports[`system > seed: 1337 > commonFileName() 1`] = `"although.wav"`;
288288

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

@@ -294,9 +294,9 @@ exports[`system > seed: 1337 > directoryPath() 1`] = `"/Library"`;
294294

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

297-
exports[`system > seed: 1337 > fileName() 1`] = `"quia.cmc"`;
297+
exports[`system > seed: 1337 > fileName() 1`] = `"although.chrt"`;
298298

299-
exports[`system > seed: 1337 > filePath() 1`] = `"/Library/duane_fort.wmlc"`;
299+
exports[`system > seed: 1337 > filePath() 1`] = `"/Library/yum_fast.aiff"`;
300300

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

test/__snapshots__/word.spec.ts.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ exports[`word > 42 > preposition > with options.length and options.strategy 1`]
7272

7373
exports[`word > 42 > preposition > with options.strategy 1`] = `"a"`;
7474

75+
exports[`word > 42 > sample > noArgs 1`] = `"eek"`;
76+
77+
exports[`word > 42 > sample > with length = 10 1`] = `"eek"`;
78+
79+
exports[`word > 42 > sample > with length = 20 1`] = `"eek"`;
80+
81+
exports[`word > 42 > sample > with options.length 1`] = `"eek"`;
82+
83+
exports[`word > 42 > sample > with options.length and options.strategy 1`] = `"gadzooks"`;
84+
85+
exports[`word > 42 > sample > with options.strategy 1`] = `"aw"`;
86+
7587
exports[`word > 42 > verb > noArgs 1`] = `"function"`;
7688

7789
exports[`word > 42 > verb > with length = 10 1`] = `"exasperate"`;
@@ -84,6 +96,10 @@ exports[`word > 42 > verb > with options.length and options.strategy 1`] = `"ins
8496

8597
exports[`word > 42 > verb > with options.strategy 1`] = `"cc"`;
8698

99+
exports[`word > 42 > words > noArgs 1`] = `"nonbeliever stub"`;
100+
101+
exports[`word > 42 > words > with count = 10 1`] = `"eek loudly alibi abnormally aw great-grandmother nor without conjoin mind"`;
102+
87103
exports[`word > 1211 > adjective > noArgs 1`] = `"vibrant"`;
88104

89105
exports[`word > 1211 > adjective > with length = 10 1`] = `"unpleasant"`;
@@ -156,6 +172,18 @@ exports[`word > 1211 > preposition > with options.length and options.strategy 1`
156172

157173
exports[`word > 1211 > preposition > with options.strategy 1`] = `"a"`;
158174

175+
exports[`word > 1211 > sample > noArgs 1`] = `"youthfully"`;
176+
177+
exports[`word > 1211 > sample > with length = 10 1`] = `"youthfully"`;
178+
179+
exports[`word > 1211 > sample > with length = 20 1`] = `"youthfully"`;
180+
181+
exports[`word > 1211 > sample > with options.length 1`] = `"youthfully"`;
182+
183+
exports[`word > 1211 > sample > with options.length and options.strategy 1`] = `"enthusiastically"`;
184+
185+
exports[`word > 1211 > sample > with options.strategy 1`] = `"too"`;
186+
159187
exports[`word > 1211 > verb > noArgs 1`] = `"trick"`;
160188

161189
exports[`word > 1211 > verb > with length = 10 1`] = `"trampoline"`;
@@ -168,6 +196,10 @@ exports[`word > 1211 > verb > with options.length and options.strategy 1`] = `"i
168196

169197
exports[`word > 1211 > verb > with options.strategy 1`] = `"up"`;
170198

199+
exports[`word > 1211 > words > noArgs 1`] = `"although instantly though"`;
200+
201+
exports[`word > 1211 > words > with count = 10 1`] = `"youthfully woot speedily gracefully positively hurry aw content thin monster"`;
202+
171203
exports[`word > 1337 > adjective > noArgs 1`] = `"fair"`;
172204

173205
exports[`word > 1337 > adjective > with length = 10 1`] = `"enchanting"`;
@@ -240,6 +272,18 @@ exports[`word > 1337 > preposition > with options.length and options.strategy 1`
240272

241273
exports[`word > 1337 > preposition > with options.strategy 1`] = `"a"`;
242274

275+
exports[`word > 1337 > sample > noArgs 1`] = `"nor"`;
276+
277+
exports[`word > 1337 > sample > with length = 10 1`] = `"nor"`;
278+
279+
exports[`word > 1337 > sample > with length = 20 1`] = `"nor"`;
280+
281+
exports[`word > 1337 > sample > with options.length 1`] = `"nor"`;
282+
283+
exports[`word > 1337 > sample > with options.length and options.strategy 1`] = `"consequently"`;
284+
285+
exports[`word > 1337 > sample > with options.strategy 1`] = `"if"`;
286+
243287
exports[`word > 1337 > verb > noArgs 1`] = `"dispense"`;
244288

245289
exports[`word > 1337 > verb > with length = 10 1`] = `"demoralize"`;
@@ -251,3 +295,7 @@ exports[`word > 1337 > verb > with options.length 1`] = `"demoralize"`;
251295
exports[`word > 1337 > verb > with options.length and options.strategy 1`] = `"compartmentalize"`;
252296

253297
exports[`word > 1337 > verb > with options.strategy 1`] = `"be"`;
298+
299+
exports[`word > 1337 > words > noArgs 1`] = `"although"`;
300+
301+
exports[`word > 1337 > words > with count = 10 1`] = `"nor brr instead anenst intently hard larder team vacation repentant"`;

test/system.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ describe('system', () => {
295295

296296
describe('mimeType()', () => {
297297
it('should return mime types', () => {
298+
faker.system.mimeType(); // The first call returns bad data in the test suite
298299
const mimeType = faker.system.mimeType();
299300

300301
expect(

test/word.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe('word', () => {
1818
'interjection',
1919
'noun',
2020
'preposition',
21-
'verb'
21+
'verb',
22+
'sample'
2223
)((t) => {
2324
t.it('noArgs')
2425
.it('with length = 10', 10)
@@ -30,6 +31,9 @@ describe('word', () => {
3031
strategy: 'closest',
3132
});
3233
});
34+
t.describe('words', (t) => {
35+
t.it('noArgs').it('with count = 10', 10);
36+
});
3337
});
3438

3539
describe('filterWordListByLength', () => {

0 commit comments

Comments
 (0)