Skip to content

Commit f06126a

Browse files
authored
feat(helpers): introduce multiple method (#1545)
1 parent 50fb72c commit f06126a

20 files changed

+390
-121
lines changed

src/modules/color/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export class ColorModule {
320320
color = formatHexColor(color, options);
321321
return color;
322322
}
323-
color = Array.from({ length: 3 }).map(() => this.faker.number.int(255));
323+
color = Array.from({ length: 3 }, () => this.faker.number.int(255));
324324
if (includeAlpha) {
325325
color.push(this.faker.number.float({ max: 1, precision: 0.01 }));
326326
cssFunction = 'rgba';
@@ -380,7 +380,7 @@ export class ColorModule {
380380
*/
381381
cmyk(options?: { format?: ColorFormat }): string | number[];
382382
cmyk(options?: { format?: ColorFormat }): string | number[] {
383-
const color: string | number[] = Array.from({ length: 4 }).map(() =>
383+
const color: string | number[] = Array.from({ length: 4 }, () =>
384384
this.faker.number.float({ max: 1, precision: 0.01 })
385385
);
386386
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
@@ -742,7 +742,7 @@ export class ColorModule {
742742
if (options?.format === 'css' && !options?.space) {
743743
options = { ...options, space: 'sRGB' };
744744
}
745-
const color = Array.from({ length: 3 }).map(() =>
745+
const color = Array.from({ length: 3 }, () =>
746746
this.faker.number.float({ max: 1, precision: 0.0001 })
747747
);
748748
return toColorFormat(

src/modules/datatype/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,23 @@ export class DatatypeModule {
278278
* Returns an array with random strings and numbers.
279279
*
280280
* @param length Size of the returned array. Defaults to `10`.
281+
* @param length.min The minimum size of the array.
282+
* @param length.max The maximum size of the array.
281283
*
282284
* @example
283285
* faker.datatype.array() // [ 94099, 85352, 'Hz%T.C\\l;8', '|#gmtw3otS', '2>:rJ|3$&d', 56864, 'Ss2-p0RXSI', 51084, 2039, 'mNEU[.r0Vf' ]
284286
* faker.datatype.array(3) // [ 61845, 'SK7H$W3:d*', 'm[%7N8*GVK' ]
287+
* faker.datatype.array({ min: 3, max: 5 }) // [ 99403, 76924, 42281, "Q'|$&y\\G/9" ]
285288
*
286289
* @since 5.5.0
287290
*/
288-
array(length = 10): Array<string | number> {
289-
return Array.from<string | number>({ length }).map(() =>
290-
this.boolean() ? this.faker.string.sample() : this.faker.number.int()
291+
array(
292+
length: number | { min: number; max: number } = 10
293+
): Array<string | number> {
294+
return this.faker.helpers.multiple(
295+
() =>
296+
this.boolean() ? this.faker.string.sample() : this.faker.number.int(),
297+
{ count: length }
291298
);
292299
}
293300

src/modules/date/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,28 @@ export class DateModule {
331331
* // ]
332332
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
333333
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
334+
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: { min: 2, max: 5 }})
335+
* // [
336+
* // 2021-12-19T06:35:40.191Z,
337+
* // 2022-09-10T08:03:51.351Z,
338+
* // 2023-04-19T11:41:17.501Z
339+
* // ]
334340
*
335341
* @since 8.0.0
336342
*/
337343
betweens(options: {
338344
from: string | Date | number;
339345
to: string | Date | number;
340-
count?: number;
346+
count?: number | { min: number; max: number };
341347
}): Date[];
342348
/**
343349
* Generates random dates between the given boundaries.
344350
*
345351
* @param from The early date boundary.
346352
* @param to The late date boundary.
347353
* @param count The number of dates to generate. Defaults to `3`.
354+
* @param count.min The minimum number of dates to generate.
355+
* @param count.max The maximum number of dates to generate.
348356
*
349357
* @example
350358
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z')
@@ -384,6 +392,12 @@ export class DateModule {
384392
* // ]
385393
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
386394
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
395+
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: { min: 2, max: 5 }})
396+
* // [
397+
* // 2021-12-19T06:35:40.191Z,
398+
* // 2022-09-10T08:03:51.351Z,
399+
* // 2023-04-19T11:41:17.501Z
400+
* // ]
387401
*
388402
* @since 8.0.0
389403
*/
@@ -395,7 +409,7 @@ export class DateModule {
395409
| {
396410
from: string | Date | number;
397411
to: string | Date | number;
398-
count?: number;
412+
count?: number | { min: number; max: number };
399413
},
400414
legacyTo?: string | Date | number,
401415
legacyCount?: number
@@ -408,7 +422,7 @@ export class DateModule {
408422
| {
409423
from: string | Date | number;
410424
to: string | Date | number;
411-
count?: number;
425+
count?: number | { min: number; max: number };
412426
},
413427
legacyTo?: string | Date | number,
414428
legacyCount: number = 3
@@ -425,9 +439,9 @@ export class DateModule {
425439

426440
const { from, to, count = 3 } = options;
427441

428-
return Array.from({ length: count }, () => this.between({ from, to })).sort(
429-
(a, b) => a.getTime() - b.getTime()
430-
);
442+
return this.faker.helpers
443+
.multiple(() => this.between({ from, to }), { count })
444+
.sort((a, b) => a.getTime() - b.getTime());
431445
}
432446

433447
/**

src/modules/helpers/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,33 @@ export class HelpersModule {
690690
currentIterations: 0,
691691
});
692692
}
693+
694+
/**
695+
* Generates an array containing values returned by the given method.
696+
*
697+
* @param method The method used to generate the values.
698+
* @param options The optional options object.
699+
* @param options.count The number or range of elements to generate. Defaults to `3`.
700+
*
701+
* @example
702+
* faker.helpers.multiple(faker.person.firstName) // [ 'Aniya', 'Norval', 'Dallin' ]
703+
* faker.helpers.multiple(faker.person.firstName, { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
704+
*
705+
* @since 8.0.0
706+
*/
707+
multiple<T>(
708+
method: () => T,
709+
options: {
710+
count?: number | { min: number; max: number };
711+
} = {}
712+
): T[] {
713+
const count = this.rangeToNumber(options.count ?? 3);
714+
if (count <= 0) {
715+
return [];
716+
}
717+
718+
// TODO @ST-DDT 2022-11-21: Add support for unique option
719+
720+
return Array.from({ length: count }, method);
721+
}
693722
}

src/modules/internet/index.ts

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,9 @@ export class InternetModule {
387387
* @since 6.1.1
388388
*/
389389
ipv4(): string {
390-
const randNum = () => {
391-
return this.faker.number.int(255).toFixed(0);
392-
};
393-
394-
const result: string[] = [];
395-
for (let i = 0; i < 4; i++) {
396-
result[i] = randNum();
397-
}
398-
399-
return result.join('.');
390+
return Array.from({ length: 4 }, () => this.faker.number.int(255)).join(
391+
'.'
392+
);
400393
}
401394

402395
/**
@@ -408,36 +401,13 @@ export class InternetModule {
408401
* @since 4.0.0
409402
*/
410403
ipv6(): string {
411-
const randHash = () => {
412-
let result = '';
413-
for (let i = 0; i < 4; i++) {
414-
result += this.faker.helpers.arrayElement([
415-
'0',
416-
'1',
417-
'2',
418-
'3',
419-
'4',
420-
'5',
421-
'6',
422-
'7',
423-
'8',
424-
'9',
425-
'a',
426-
'b',
427-
'c',
428-
'd',
429-
'e',
430-
'f',
431-
]);
432-
}
433-
return result;
434-
};
435-
436-
const result: string[] = [];
437-
for (let i = 0; i < 8; i++) {
438-
result[i] = randHash();
439-
}
440-
return result.join(':');
404+
return Array.from({ length: 8 }, () =>
405+
this.faker.string.hexadecimal({
406+
length: 4,
407+
casing: 'lower',
408+
prefix: '',
409+
})
410+
).join(':');
441411
}
442412

443413
/**

src/modules/lorem/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ export class LoremModule {
7272
* @since 2.0.1
7373
*/
7474
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())
75+
return this.faker.helpers
76+
.multiple(() => this.word(), { count: wordCount })
7977
.join(' ');
8078
}
8179

@@ -141,10 +139,8 @@ export class LoremModule {
141139
sentenceCount: number | { min: number; max: number } = { min: 2, max: 6 },
142140
separator: string = ' '
143141
): string {
144-
sentenceCount = this.faker.helpers.rangeToNumber(sentenceCount);
145-
146-
return Array.from({ length: sentenceCount })
147-
.map(() => this.sentence())
142+
return this.faker.helpers
143+
.multiple(() => this.sentence(), { count: sentenceCount })
148144
.join(separator);
149145
}
150146

@@ -202,10 +198,8 @@ export class LoremModule {
202198
paragraphCount: number | { min: number; max: number } = 3,
203199
separator: string = '\n'
204200
): string {
205-
paragraphCount = this.faker.helpers.rangeToNumber(paragraphCount);
206-
207-
return Array.from({ length: paragraphCount })
208-
.map(() => this.paragraph())
201+
return this.faker.helpers
202+
.multiple(() => this.paragraph(), { count: paragraphCount })
209203
.join(separator);
210204
}
211205

@@ -267,8 +261,6 @@ export class LoremModule {
267261
lines(
268262
lineCount: number | { min: number; max: number } = { min: 1, max: 5 }
269263
): string {
270-
lineCount = this.faker.helpers.rangeToNumber(lineCount);
271-
272264
return this.sentences(lineCount, '\n');
273265
}
274266
}

src/modules/random/index.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,23 @@ export class RandomModule {
137137
}
138138

139139
/**
140-
* Returns string with set of random words.
140+
* Returns a string with a given number of random words.
141141
*
142-
* @param count Number of words. Defaults to a random value between `1` and `3`.
142+
* @param count The number or range of words. Defaults to a random value between `1` and `3`.
143+
* @param count.min The minimum number of words. Defaults to `1`.
144+
* @param count.max The maximum number of words. Defaults to `3`.
143145
*
144146
* @example
145147
* faker.random.words() // 'neural'
146148
* faker.random.words(5) // 'copy Handcrafted bus client-server Point'
149+
* faker.random.words({ min: 3, max: 5 }) // 'cool sticky Borders'
147150
*
148151
* @since 3.1.0
149152
*/
150-
words(count?: number): string {
151-
const words: string[] = [];
152-
153-
if (count == null) {
154-
count = this.faker.number.int({ min: 1, max: 3 });
155-
}
156-
157-
for (let i = 0; i < count; i++) {
158-
words.push(this.word());
159-
}
160-
161-
return words.join(' ');
153+
words(
154+
count: number | { min: number; max: number } = { min: 1, max: 3 }
155+
): string {
156+
return this.faker.helpers.multiple(this.word, { count }).join(' ');
162157
}
163158

164159
/**

src/modules/system/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,18 @@ export class SystemModule {
6767
extensionCount?: number | { min: number; max: number };
6868
} = {}
6969
): string {
70-
const extensionCount = this.faker.helpers.rangeToNumber(
71-
options.extensionCount ?? 1
72-
);
70+
const { extensionCount = 1 } = options;
7371

7472
const baseName = this.faker.word.words().toLowerCase().replace(/\W/g, '_');
7573

76-
if (extensionCount <= 0) {
74+
const extensionsStr = this.faker.helpers
75+
.multiple(() => this.fileExt(), { count: extensionCount })
76+
.join('.');
77+
78+
if (extensionsStr.length === 0) {
7779
return baseName;
7880
}
7981

80-
const extensionsStr = Array.from({ length: extensionCount })
81-
.map(() => this.fileExt())
82-
.join('.');
83-
8482
return `${baseName}.${extensionsStr}`;
8583
}
8684

src/modules/word/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@ export class WordModule {
392392
if (typeof options === 'number') {
393393
options = { count: options };
394394
}
395-
const count = this.faker.helpers.rangeToNumber(
396-
options.count ?? { min: 1, max: 3 }
397-
);
395+
const { count = { min: 1, max: 3 } } = options;
398396

399-
return Array.from({ length: count }, () => this.sample()).join(' ');
397+
return this.faker.helpers
398+
.multiple(() => this.sample(), { count })
399+
.join(' ');
400400
}
401401
}

0 commit comments

Comments
 (0)