From f06126a1ba8515d6e0b7733999d5cd2f8849be7a Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Wed, 7 Dec 2022 20:18:08 +0100 Subject: feat(helpers): introduce `multiple` method (#1545) --- src/modules/random/index.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'src/modules/random') diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index f8691198..1f4b499b 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -137,28 +137,23 @@ export class RandomModule { } /** - * Returns string with set of random words. + * Returns a string with a given number of random words. * - * @param count Number of words. Defaults to a random value between `1` and `3`. + * @param count The number or range of words. Defaults to a random value between `1` and `3`. + * @param count.min The minimum number of words. Defaults to `1`. + * @param count.max The maximum number of words. Defaults to `3`. * * @example * faker.random.words() // 'neural' * faker.random.words(5) // 'copy Handcrafted bus client-server Point' + * faker.random.words({ min: 3, max: 5 }) // 'cool sticky Borders' * * @since 3.1.0 */ - words(count?: number): string { - const words: string[] = []; - - if (count == null) { - count = this.faker.number.int({ min: 1, max: 3 }); - } - - for (let i = 0; i < count; i++) { - words.push(this.word()); - } - - return words.join(' '); + words( + count: number | { min: number; max: number } = { min: 1, max: 3 } + ): string { + return this.faker.helpers.multiple(this.word, { count }).join(' '); } /** -- cgit v1.2.3