Skip to content

Commit

Permalink
feat(helpers): introduce multiple method (#1545)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Dec 7, 2022
1 parent 50fb72c commit f06126a
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 121 deletions.
6 changes: 3 additions & 3 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class ColorModule {
color = formatHexColor(color, options);
return color;
}
color = Array.from({ length: 3 }).map(() => this.faker.number.int(255));
color = Array.from({ length: 3 }, () => this.faker.number.int(255));
if (includeAlpha) {
color.push(this.faker.number.float({ max: 1, precision: 0.01 }));
cssFunction = 'rgba';
Expand Down Expand Up @@ -380,7 +380,7 @@ export class ColorModule {
*/
cmyk(options?: { format?: ColorFormat }): string | number[];
cmyk(options?: { format?: ColorFormat }): string | number[] {
const color: string | number[] = Array.from({ length: 4 }).map(() =>
const color: string | number[] = Array.from({ length: 4 }, () =>
this.faker.number.float({ max: 1, precision: 0.01 })
);
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
Expand Down Expand Up @@ -742,7 +742,7 @@ export class ColorModule {
if (options?.format === 'css' && !options?.space) {
options = { ...options, space: 'sRGB' };
}
const color = Array.from({ length: 3 }).map(() =>
const color = Array.from({ length: 3 }, () =>
this.faker.number.float({ max: 1, precision: 0.0001 })
);
return toColorFormat(
Expand Down
13 changes: 10 additions & 3 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,23 @@ export class DatatypeModule {
* Returns an array with random strings and numbers.
*
* @param length Size of the returned array. Defaults to `10`.
* @param length.min The minimum size of the array.
* @param length.max The maximum size of the array.
*
* @example
* faker.datatype.array() // [ 94099, 85352, 'Hz%T.C\\l;8', '|#gmtw3otS', '2>:rJ|3$&d', 56864, 'Ss2-p0RXSI', 51084, 2039, 'mNEU[.r0Vf' ]
* faker.datatype.array(3) // [ 61845, 'SK7H$W3:d*', 'm[%7N8*GVK' ]
* faker.datatype.array({ min: 3, max: 5 }) // [ 99403, 76924, 42281, "Q'|$&y\\G/9" ]
*
* @since 5.5.0
*/
array(length = 10): Array<string | number> {
return Array.from<string | number>({ length }).map(() =>
this.boolean() ? this.faker.string.sample() : this.faker.number.int()
array(
length: number | { min: number; max: number } = 10
): Array<string | number> {
return this.faker.helpers.multiple(
() =>
this.boolean() ? this.faker.string.sample() : this.faker.number.int(),
{ count: length }
);
}

Expand Down
26 changes: 20 additions & 6 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,28 @@ export class DateModule {
* // ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: { min: 2, max: 5 }})
* // [
* // 2021-12-19T06:35:40.191Z,
* // 2022-09-10T08:03:51.351Z,
* // 2023-04-19T11:41:17.501Z
* // ]
*
* @since 8.0.0
*/
betweens(options: {
from: string | Date | number;
to: string | Date | number;
count?: number;
count?: number | { min: number; max: number };
}): Date[];
/**
* Generates random dates between the given boundaries.
*
* @param from The early date boundary.
* @param to The late date boundary.
* @param count The number of dates to generate. Defaults to `3`.
* @param count.min The minimum number of dates to generate.
* @param count.max The maximum number of dates to generate.
*
* @example
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z')
Expand Down Expand Up @@ -384,6 +392,12 @@ export class DateModule {
* // ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: { min: 2, max: 5 }})
* // [
* // 2021-12-19T06:35:40.191Z,
* // 2022-09-10T08:03:51.351Z,
* // 2023-04-19T11:41:17.501Z
* // ]
*
* @since 8.0.0
*/
Expand All @@ -395,7 +409,7 @@ export class DateModule {
| {
from: string | Date | number;
to: string | Date | number;
count?: number;
count?: number | { min: number; max: number };
},
legacyTo?: string | Date | number,
legacyCount?: number
Expand All @@ -408,7 +422,7 @@ export class DateModule {
| {
from: string | Date | number;
to: string | Date | number;
count?: number;
count?: number | { min: number; max: number };
},
legacyTo?: string | Date | number,
legacyCount: number = 3
Expand All @@ -425,9 +439,9 @@ export class DateModule {

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

return Array.from({ length: count }, () => this.between({ from, to })).sort(
(a, b) => a.getTime() - b.getTime()
);
return this.faker.helpers
.multiple(() => this.between({ from, to }), { count })
.sort((a, b) => a.getTime() - b.getTime());
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,33 @@ export class HelpersModule {
currentIterations: 0,
});
}

/**
* Generates an array containing values returned by the given method.
*
* @param method The method used to generate the values.
* @param options The optional options object.
* @param options.count The number or range of elements to generate. Defaults to `3`.
*
* @example
* faker.helpers.multiple(faker.person.firstName) // [ 'Aniya', 'Norval', 'Dallin' ]
* faker.helpers.multiple(faker.person.firstName, { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
*
* @since 8.0.0
*/
multiple<T>(
method: () => T,
options: {
count?: number | { min: number; max: number };
} = {}
): T[] {
const count = this.rangeToNumber(options.count ?? 3);
if (count <= 0) {
return [];
}

// TODO @ST-DDT 2022-11-21: Add support for unique option

return Array.from({ length: count }, method);
}
}
50 changes: 10 additions & 40 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,9 @@ export class InternetModule {
* @since 6.1.1
*/
ipv4(): string {
const randNum = () => {
return this.faker.number.int(255).toFixed(0);
};

const result: string[] = [];
for (let i = 0; i < 4; i++) {
result[i] = randNum();
}

return result.join('.');
return Array.from({ length: 4 }, () => this.faker.number.int(255)).join(
'.'
);
}

/**
Expand All @@ -408,36 +401,13 @@ export class InternetModule {
* @since 4.0.0
*/
ipv6(): string {
const randHash = () => {
let result = '';
for (let i = 0; i < 4; i++) {
result += this.faker.helpers.arrayElement([
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
]);
}
return result;
};

const result: string[] = [];
for (let i = 0; i < 8; i++) {
result[i] = randHash();
}
return result.join(':');
return Array.from({ length: 8 }, () =>
this.faker.string.hexadecimal({
length: 4,
casing: 'lower',
prefix: '',
})
).join(':');
}

/**
Expand Down
20 changes: 6 additions & 14 deletions src/modules/lorem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ export class LoremModule {
* @since 2.0.1
*/
words(wordCount: number | { min: number; max: number } = 3): string {
wordCount = this.faker.helpers.rangeToNumber(wordCount);

return Array.from({ length: wordCount })
.map(() => this.word())
return this.faker.helpers
.multiple(() => this.word(), { count: wordCount })
.join(' ');
}

Expand Down Expand Up @@ -141,10 +139,8 @@ export class LoremModule {
sentenceCount: number | { min: number; max: number } = { min: 2, max: 6 },
separator: string = ' '
): string {
sentenceCount = this.faker.helpers.rangeToNumber(sentenceCount);

return Array.from({ length: sentenceCount })
.map(() => this.sentence())
return this.faker.helpers
.multiple(() => this.sentence(), { count: sentenceCount })
.join(separator);
}

Expand Down Expand Up @@ -202,10 +198,8 @@ export class LoremModule {
paragraphCount: number | { min: number; max: number } = 3,
separator: string = '\n'
): string {
paragraphCount = this.faker.helpers.rangeToNumber(paragraphCount);

return Array.from({ length: paragraphCount })
.map(() => this.paragraph())
return this.faker.helpers
.multiple(() => this.paragraph(), { count: paragraphCount })
.join(separator);
}

Expand Down Expand Up @@ -267,8 +261,6 @@ export class LoremModule {
lines(
lineCount: number | { min: number; max: number } = { min: 1, max: 5 }
): string {
lineCount = this.faker.helpers.rangeToNumber(lineCount);

return this.sentences(lineCount, '\n');
}
}
23 changes: 9 additions & 14 deletions src/modules/random/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ export class SystemModule {
extensionCount?: number | { min: number; max: number };
} = {}
): string {
const extensionCount = this.faker.helpers.rangeToNumber(
options.extensionCount ?? 1
);
const { extensionCount = 1 } = options;

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

if (extensionCount <= 0) {
const extensionsStr = this.faker.helpers
.multiple(() => this.fileExt(), { count: extensionCount })
.join('.');

if (extensionsStr.length === 0) {
return baseName;
}

const extensionsStr = Array.from({ length: extensionCount })
.map(() => this.fileExt())
.join('.');

return `${baseName}.${extensionsStr}`;
}

Expand Down
8 changes: 4 additions & 4 deletions src/modules/word/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ export class WordModule {
if (typeof options === 'number') {
options = { count: options };
}
const count = this.faker.helpers.rangeToNumber(
options.count ?? { min: 1, max: 3 }
);
const { count = { min: 1, max: 3 } } = options;

return Array.from({ length: count }, () => this.sample()).join(' ');
return this.faker.helpers
.multiple(() => this.sample(), { count })
.join(' ');
}
}

0 comments on commit f06126a

Please sign in to comment.