Skip to content

Commit 9cd716e

Browse files
authored
feat(helpers): add rangeToNumber method and add range parameters (#1486)
1 parent 7cbeda6 commit 9cd716e

15 files changed

Lines changed: 609 additions & 128 deletions

File tree

src/modules/helpers/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,26 @@ export class HelpersModule {
627627
return this.fake(res);
628628
}
629629

630+
/**
631+
* Helper method that converts the given number or range to a number.
632+
*
633+
* @param numberOrRange The number or range to convert.
634+
* @param numberOrRange.min The minimum value for the range.
635+
* @param numberOrRange.max The maximum value for the range.
636+
*
637+
* @example
638+
* faker.helpers.rangeToNumber(1) // 1
639+
* faker.helpers.rangeToNumber({ min: 1, max: 10 }) // 5
640+
*
641+
* @since 8.0.0
642+
*/
643+
rangeToNumber(numberOrRange: number | { min: number; max: number }): number {
644+
if (typeof numberOrRange === 'number') {
645+
return numberOrRange;
646+
}
647+
return this.faker.datatype.number(numberOrRange);
648+
}
649+
630650
/**
631651
* Generates a unique result using the results of the given method.
632652
* Used unique entries will be stored internally and filtered from subsequent calls.

src/modules/lorem/index.ts

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class LoremModule {
3636
* faker.lorem.word() // 'temporibus'
3737
* faker.lorem.word(5) // 'velit'
3838
* faker.lorem.word({ strategy: 'shortest' }) // 'a'
39-
* faker.lorem.word({ length: { min: 5, max: 7 }, strategy: "fail" }) // 'quaerat'
39+
* faker.lorem.word({ length: { min: 5, max: 7 }, strategy: 'fail' }) // 'quaerat'
4040
*
4141
* @since 3.1.0
4242
*/
@@ -60,62 +60,71 @@ export class LoremModule {
6060
/**
6161
* Generates a space separated list of words.
6262
*
63-
* @param num The number of words to generate. Defaults to `3`.
63+
* @param wordCount The number of words to generate. Defaults to `3`.
64+
* @param wordCount.min The minimum number of words to generate.
65+
* @param wordCount.max The maximum number of words to generate.
6466
*
6567
* @example
6668
* faker.lorem.words() // 'qui praesentium pariatur'
6769
* faker.lorem.words(10) // 'debitis consectetur voluptatem non doloremque ipsum autem totam eum ratione'
70+
* faker.lorem.words({ min: 1, max: 3 }) // 'tenetur error cum'
6871
*
6972
* @since 2.0.1
7073
*/
71-
words(num: number = 3): string {
72-
const words: string[] = [];
73-
for (let i = 0; i < num; i++) {
74-
words.push(this.word());
75-
}
76-
return words.join(' ');
74+
words(wordCount: number | { min: number; max: number } = 3): string {
75+
wordCount = this.faker.helpers.rangeToNumber(wordCount);
76+
77+
return Array.from({ length: wordCount })
78+
.map(() => this.word())
79+
.join(' ');
7780
}
7881

7982
/**
80-
* Generates a space separated list of words beginning a capital letter and ending with a dot.
83+
* Generates a space separated list of words beginning with a capital letter and ending with a period.
8184
*
8285
* @param wordCount The number of words, that should be in the sentence. Defaults to a random number between `3` and `10`.
86+
* @param wordCount.min The minimum number of words to generate. Defaults to `3`.
87+
* @param wordCount.max The maximum number of words to generate. Defaults to `10`.
8388
*
8489
* @example
8590
* faker.lorem.sentence() // 'Voluptatum cupiditate suscipit autem eveniet aut dolorem aut officiis distinctio.'
8691
* faker.lorem.sentence(5) // 'Laborum voluptatem officiis est et.'
92+
* faker.lorem.sentence({ min: 3, max: 5 }) // 'Fugiat repellendus nisi.'
8793
*
8894
* @since 2.0.1
8995
*/
90-
sentence(wordCount?: number): string {
91-
if (wordCount == null) {
92-
wordCount = this.faker.datatype.number({ min: 3, max: 10 });
93-
}
94-
96+
sentence(
97+
wordCount: number | { min: number; max: number } = { min: 3, max: 10 }
98+
): string {
9599
const sentence = this.words(wordCount);
96-
return `${sentence.charAt(0).toUpperCase() + sentence.slice(1)}.`;
100+
return `${sentence.charAt(0).toUpperCase() + sentence.substring(1)}.`;
97101
}
98102

99103
/**
100104
* Generates a slugified text consisting of the given number of hyphen separated words.
101105
*
102106
* @param wordCount The number of words to generate. Defaults to `3`.
107+
* @param wordCount.min The minimum number of words to generate.
108+
* @param wordCount.max The maximum number of words to generate.
103109
*
104110
* @example
105111
* faker.lorem.slug() // 'dolores-illo-est'
112+
* faker.lorem.slug(5) // 'delectus-totam-iusto-itaque-placeat'
113+
* faker.lorem.slug({ min: 1, max: 3 }) // 'illo-ratione'
106114
*
107115
* @since 4.0.0
108116
*/
109-
slug(wordCount?: number): string {
117+
slug(wordCount: number | { min: number; max: number } = 3): string {
110118
const words = this.words(wordCount);
111-
112119
return this.faker.helpers.slugify(words);
113120
}
114121

115122
/**
116123
* Generates the given number of sentences.
117124
*
118125
* @param sentenceCount The number of sentences to generate. Defaults to a random number between `2` and `6`.
126+
* @param sentenceCount.min The minimum number of sentences to generate. Defaults to `2`.
127+
* @param sentenceCount.max The maximum number of sentences to generate. Defaults to `6`.
119128
* @param separator The separator to add between sentences. Defaults to `' '`.
120129
*
121130
* @example
@@ -124,39 +133,45 @@ export class LoremModule {
124133
* faker.lorem.sentences(2, '\n')
125134
* // 'Et rerum a unde tempora magnam sit nisi.
126135
* // Et perspiciatis ipsam omnis.'
136+
* faker.lorem.sentences({ min: 1, max: 3 }) // 'Placeat ex natus tenetur repellendus repellendus iste. Optio nostrum veritatis.'
127137
*
128138
* @since 2.0.1
129139
*/
130-
sentences(sentenceCount?: number, separator: string = ' '): string {
131-
if (sentenceCount == null) {
132-
sentenceCount = this.faker.datatype.number({ min: 2, max: 6 });
133-
}
134-
const sentences: string[] = [];
135-
for (sentenceCount; sentenceCount > 0; sentenceCount--) {
136-
sentences.push(this.sentence());
137-
}
138-
return sentences.join(separator);
140+
sentences(
141+
sentenceCount: number | { min: number; max: number } = { min: 2, max: 6 },
142+
separator: string = ' '
143+
): string {
144+
sentenceCount = this.faker.helpers.rangeToNumber(sentenceCount);
145+
146+
return Array.from({ length: sentenceCount })
147+
.map(() => this.sentence())
148+
.join(separator);
139149
}
140150

141151
/**
142-
* Generates a paragraph with at least the given number of sentences.
152+
* Generates a paragraph with the given number of sentences.
143153
*
144-
* @param sentenceCount The minim number of sentences to generate. Defaults to `3`.
154+
* @param sentenceCount The number of sentences to generate. Defaults to `3`.
155+
* @param sentenceCount.min The minimum number of sentences to generate.
156+
* @param sentenceCount.max The maximum number of sentences to generate.
145157
*
146158
* @example
147159
* faker.lorem.paragraph() // 'Non architecto nam unde sint. Ex tenetur dolor facere optio aut consequatur. Ea laudantium reiciendis repellendus.'
148-
* faker.lorem.paragraph() // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.'
160+
* faker.lorem.paragraph(2) // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.'
161+
* faker.lorem.paragraph({ min: 1, max: 3 }) // 'Quis doloribus necessitatibus sint. Rerum accusamus impedit corporis porro.'
149162
*
150163
* @since 2.0.1
151164
*/
152-
paragraph(sentenceCount: number = 3): string {
153-
return this.sentences(sentenceCount + this.faker.datatype.number(3));
165+
paragraph(sentenceCount: number | { min: number; max: number } = 3): string {
166+
return this.sentences(sentenceCount);
154167
}
155168

156169
/**
157170
* Generates the given number of paragraphs.
158171
*
159172
* @param paragraphCount The number of paragraphs to generate. Defaults to `3`.
173+
* @param paragraphCount.min The minimum number of paragraphs to generate.
174+
* @param paragraphCount.max The maximum number of paragraphs to generate.
160175
* @param separator The separator to use. Defaults to `'\n'`.
161176
*
162177
* @example
@@ -176,14 +191,22 @@ export class LoremModule {
176191
* // 'Eos magnam aut qui accusamus. Sapiente quas culpa totam excepturi. Blanditiis totam distinctio occaecati dignissimos cumque atque qui officiis.<br/>
177192
* // Nihil quis vel consequatur. Blanditiis commodi deserunt sunt animi dolorum. A optio porro hic dolorum fugit aut et sint voluptas. Minima ad sed ipsa est non dolores.'
178193
*
194+
* faker.lorem.paragraphs({ min: 1, max: 3 })
195+
* // 'Eum nam fugiat laudantium.
196+
* // Dignissimos tempore porro necessitatibus commodi nam.
197+
* // Veniam at commodi iste perferendis totam dolorum corporis ipsam.'
198+
*
179199
* @since 2.0.1
180200
*/
181-
paragraphs(paragraphCount: number = 3, separator: string = '\n'): string {
182-
const paragraphs: string[] = [];
183-
for (paragraphCount; paragraphCount > 0; paragraphCount--) {
184-
paragraphs.push(this.paragraph());
185-
}
186-
return paragraphs.join(separator);
201+
paragraphs(
202+
paragraphCount: number | { min: number; max: number } = 3,
203+
separator: string = '\n'
204+
): string {
205+
paragraphCount = this.faker.helpers.rangeToNumber(paragraphCount);
206+
207+
return Array.from({ length: paragraphCount })
208+
.map(() => this.paragraph())
209+
.join(separator);
187210
}
188211

189212
/**
@@ -202,8 +225,6 @@ export class LoremModule {
202225
*/
203226
text(): string {
204227
const methods: Array<keyof LoremModule> = [
205-
'word',
206-
'words',
207228
'sentence',
208229
'sentences',
209230
'paragraph',
@@ -220,6 +241,8 @@ export class LoremModule {
220241
* Generates the given number lines of lorem separated by `'\n'`.
221242
*
222243
* @param lineCount The number of lines to generate. Defaults to a random number between `1` and `5`.
244+
* @param lineCount.min The minimum number of lines to generate. Defaults to `1`.
245+
* @param lineCount.max The maximum number of lines to generate. Defaults to `5`.
223246
*
224247
* @example
225248
* faker.lorem.lines()
@@ -232,12 +255,20 @@ export class LoremModule {
232255
* // 'Soluta deserunt eos quam reiciendis libero autem enim nam ut.
233256
* // Voluptate aut aut.'
234257
*
258+
* faker.lorem.lines(2)
259+
* // 'Quod quas nam quis impedit aut consequuntur.
260+
* // Animi dolores aspernatur.'
261+
*
262+
* faker.lorem.lines({ min: 1, max: 3 })
263+
* // 'Error dolorem natus quos eum consequatur necessitatibus.'
264+
*
235265
* @since 3.1.0
236266
*/
237-
lines(lineCount?: number): string {
238-
if (lineCount == null) {
239-
lineCount = this.faker.datatype.number({ min: 1, max: 5 });
240-
}
267+
lines(
268+
lineCount: number | { min: number; max: number } = { min: 1, max: 5 }
269+
): string {
270+
lineCount = this.faker.helpers.rangeToNumber(lineCount);
271+
241272
return this.sentences(lineCount, '\n');
242273
}
243274
}

0 commit comments

Comments
 (0)