diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index b232f6490d5..11ebdcd906c 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -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'; @@ -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'); @@ -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( diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 70a539d7c5e..b7000dc6d5f 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -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 { - return Array.from({ length }).map(() => - this.boolean() ? this.faker.string.sample() : this.faker.number.int() + array( + length: number | { min: number; max: number } = 10 + ): Array { + return this.faker.helpers.multiple( + () => + this.boolean() ? this.faker.string.sample() : this.faker.number.int(), + { count: length } ); } diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index a76b430f034..e8bdde283eb 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -331,13 +331,19 @@ 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. @@ -345,6 +351,8 @@ export class DateModule { * @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') @@ -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 */ @@ -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 @@ -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 @@ -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()); } /** diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index af6ae4b8e4b..37487247298 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -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( + 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); + } } diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index dc3eaa624f7..4f3132752be 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -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( + '.' + ); } /** @@ -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(':'); } /** diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index c73a92c8ff5..8e989ab60de 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -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(' '); } @@ -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); } @@ -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); } @@ -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'); } } diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index f8691198734..1f4b499b505 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(' '); } /** diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 4bf28affc58..ca78929e111 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -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}`; } diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index d326cd70976..22a22a0b65d 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -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(' '); } } diff --git a/test/__snapshots__/datatype.spec.ts.snap b/test/__snapshots__/datatype.spec.ts.snap index 5e2d13234b4..0a070497c7b 100644 --- a/test/__snapshots__/datatype.spec.ts.snap +++ b/test/__snapshots__/datatype.spec.ts.snap @@ -24,6 +24,15 @@ exports[`datatype > 42 > array > with length 1`] = ` ] `; +exports[`datatype > 42 > array > with length range 1`] = ` +[ + 95071, + "eiXX/J/*&K", + 33370, + 14286, +] +`; + exports[`datatype > 42 > bigInt > noArgs 1`] = `379177551410048n`; exports[`datatype > 42 > bigInt > with value 1`] = `37n`; @@ -144,6 +153,16 @@ exports[`datatype > 1211 > array > with length 1`] = ` ] `; +exports[`datatype > 1211 > array > with length range 1`] = ` +[ + "ti5-}$_/\`4", + 42075, + "0afl\\"h^]dn", + 43503, + "q|p|5KWu3/", +] +`; + exports[`datatype > 1211 > bigInt > noArgs 1`] = `948721906162743n`; exports[`datatype > 1211 > bigInt > with value 1`] = `8n`; @@ -264,6 +283,14 @@ exports[`datatype > 1337 > array > with length 1`] = ` ] `; +exports[`datatype > 1337 > array > with length range 1`] = ` +[ + 15868, + ":SK$>6QX9@", + 27048, +] +`; + exports[`datatype > 1337 > bigInt > noArgs 1`] = `251225403255239n`; exports[`datatype > 1337 > bigInt > with value 1`] = `25n`; diff --git a/test/__snapshots__/date.spec.ts.snap b/test/__snapshots__/date.spec.ts.snap index 8ac619a9306..54d7ae6dde3 100644 --- a/test/__snapshots__/date.spec.ts.snap +++ b/test/__snapshots__/date.spec.ts.snap @@ -24,6 +24,15 @@ exports[`date > 42 > betweens > with Date dates and count 1`] = ` ] `; +exports[`date > 42 > betweens > with Date dates and count range 1`] = ` +[ + 2021-03-04T12:54:15.263Z, + 2021-04-05T21:40:57.315Z, + 2021-04-09T17:05:10.406Z, + 2021-04-18T19:23:52.973Z, +] +`; + exports[`date > 42 > betweens > with mixed dates 1`] = ` [ 2021-03-15T19:30:57.091Z, @@ -136,6 +145,16 @@ exports[`date > 1211 > betweens > with Date dates and count 1`] = ` ] `; +exports[`date > 1211 > betweens > with Date dates and count range 1`] = ` +[ + 2021-03-01T09:04:55.612Z, + 2021-03-07T00:34:12.770Z, + 2021-03-20T19:08:07.621Z, + 2021-04-08T15:12:37.581Z, + 2021-04-15T10:20:25.794Z, +] +`; + exports[`date > 1211 > betweens > with mixed dates 1`] = ` [ 2021-03-20T19:08:07.621Z, @@ -248,6 +267,14 @@ exports[`date > 1337 > betweens > with Date dates and count 1`] = ` ] `; +exports[`date > 1337 > betweens > with Date dates and count range 1`] = ` +[ + 2021-03-03T01:51:22.512Z, + 2021-03-06T06:11:08.446Z, + 2021-03-26T18:53:00.564Z, +] +`; + exports[`date > 1337 > betweens > with mixed dates 1`] = ` [ 2021-03-03T01:51:22.512Z, diff --git a/test/__snapshots__/helpers.spec.ts.snap b/test/__snapshots__/helpers.spec.ts.snap index 3dc76f896a9..68fba3bcc08 100644 --- a/test/__snapshots__/helpers.spec.ts.snap +++ b/test/__snapshots__/helpers.spec.ts.snap @@ -37,6 +37,33 @@ exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`; exports[`helpers > 42 > maybe > with value and probability 1`] = `undefined`; +exports[`helpers > 42 > multiple > with method and count 1`] = ` +[ + 37454, + 79654, + 95071, + 18343, + 73199, +] +`; + +exports[`helpers > 42 > multiple > with method and count range 1`] = ` +[ + 79654, + 95071, + 18343, + 73199, +] +`; + +exports[`helpers > 42 > multiple > with only method 1`] = ` +[ + 37454, + 79654, + 95071, +] +`; + exports[`helpers > 42 > mustache > template with method 1`] = `"Hello John!"`; exports[`helpers > 42 > mustache > template with string 1`] = `"Hello John!"`; @@ -189,6 +216,39 @@ exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`; exports[`helpers > 1211 > maybe > with value and probability 1`] = `undefined`; +exports[`helpers > 1211 > multiple > with method and count 1`] = ` +[ + 92852, + 45901, + 89347, + 77826, + 22557, +] +`; + +exports[`helpers > 1211 > multiple > with method and count range 1`] = ` +[ + 45901, + 89347, + 77826, + 22557, + 12988, + 99725, + 3842, + 67199, + 15897, + 68481, +] +`; + +exports[`helpers > 1211 > multiple > with only method 1`] = ` +[ + 92852, + 45901, + 89347, +] +`; + exports[`helpers > 1211 > mustache > template with method 1`] = `"Hello John!"`; exports[`helpers > 1211 > mustache > template with string 1`] = `"Hello John!"`; @@ -331,6 +391,32 @@ exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`; exports[`helpers > 1337 > maybe > with value and probability 1`] = `undefined`; +exports[`helpers > 1337 > multiple > with method and count 1`] = ` +[ + 26202, + 56052, + 15868, + 21258, + 27812, +] +`; + +exports[`helpers > 1337 > multiple > with method and count range 1`] = ` +[ + 56052, + 15868, + 21258, +] +`; + +exports[`helpers > 1337 > multiple > with only method 1`] = ` +[ + 26202, + 56052, + 15868, +] +`; + exports[`helpers > 1337 > mustache > template with method 1`] = `"Hello John!"`; exports[`helpers > 1337 > mustache > template with string 1`] = `"Hello John!"`; diff --git a/test/__snapshots__/internet.spec.ts.snap b/test/__snapshots__/internet.spec.ts.snap index 16ea39d4241..206a15ee05c 100644 --- a/test/__snapshots__/internet.spec.ts.snap +++ b/test/__snapshots__/internet.spec.ts.snap @@ -54,7 +54,7 @@ exports[`internet > 42 > ip 1`] = `"203.243.46.187"`; exports[`internet > 42 > ipv4 1`] = `"95.203.243.46"`; -exports[`internet > 42 > ipv6 1`] = `"5cf2:bc99:2721:07d5:92ba:00fb:df30:2f29"`; +exports[`internet > 42 > ipv6 1`] = `"8be4:abdd:3932:1ad7:d3fe:01ff:ce40:4f4d"`; exports[`internet > 42 > mac > noArgs 1`] = `"5c:f2:bc:99:27:21"`; @@ -142,11 +142,11 @@ exports[`internet > 1211 > httpStatusCode > noArgs 1`] = `505`; exports[`internet > 1211 > httpStatusCode > with options 1`] = `429`; -exports[`internet > 1211 > ip 1`] = `"7ec3:2f0a:2a3c:652b:bd0c:aabd:e64d:fdf3"`; +exports[`internet > 1211 > ip 1`] = `"adb4:2f0e:3f4a:973f:ab0a:eefc:e96d:fcf4"`; exports[`internet > 1211 > ipv4 1`] = `"237.117.228.199"`; -exports[`internet > 1211 > ipv6 1`] = `"e7ec:32f0:a2a3:c652:bbd0:caab:de64:dfdf"`; +exports[`internet > 1211 > ipv6 1`] = `"eadb:42f0:e3f4:a973:fab0:aeef:ce96:dfcf"`; exports[`internet > 1211 > mac > noArgs 1`] = `"e7:ec:32:f0:a2:a3"`; @@ -238,7 +238,7 @@ exports[`internet > 1337 > ip 1`] = `"143.40.54.71"`; exports[`internet > 1337 > ipv4 1`] = `"67.143.40.54"`; -exports[`internet > 1337 > ipv6 1`] = `"4823:4870:5389:45f4:b41c:61a5:2bf2:7dcc"`; +exports[`internet > 1337 > ipv6 1`] = `"5c34:6ba0:75bd:57f5:a62b:82d7:2af3:9cbb"`; exports[`internet > 1337 > mac > noArgs 1`] = `"48:23:48:70:53:89"`; diff --git a/test/__snapshots__/random.spec.ts.snap b/test/__snapshots__/random.spec.ts.snap index c76519258b1..483ff05ebe4 100644 --- a/test/__snapshots__/random.spec.ts.snap +++ b/test/__snapshots__/random.spec.ts.snap @@ -2,60 +2,66 @@ exports[`random > 42 > alpha > noArgs 1`] = `"t"`; -exports[`random > 42 > alpha > withLength 1`] = `"tPXjM"`; +exports[`random > 42 > alpha > with length 1`] = `"tPXjM"`; exports[`random > 42 > alphaNumeric > noArgs 1`] = `"n"`; -exports[`random > 42 > alphaNumeric > withLength 1`] = `"nNWbJ"`; +exports[`random > 42 > alphaNumeric > with length 1`] = `"nNWbJ"`; exports[`random > 42 > locale 1`] = `"es"`; exports[`random > 42 > numeric > noArgs 1`] = `"3"`; -exports[`random > 42 > numeric > withLength 1`] = `"37917"`; +exports[`random > 42 > numeric > with length 1`] = `"37917"`; exports[`random > 42 > word 1`] = `"responsive"`; exports[`random > 42 > words > noArgs 1`] = `"lavender Shoes"`; -exports[`random > 42 > words > withLength 1`] = `"responsive comeback Neptunium Hip deposit"`; +exports[`random > 42 > words > with length 1`] = `"responsive comeback Neptunium Hip deposit"`; + +exports[`random > 42 > words > with length range 1`] = `"lavender Shoes"`; exports[`random > 1211 > alpha > noArgs 1`] = `"W"`; -exports[`random > 1211 > alpha > withLength 1`] = `"WxUOl"`; +exports[`random > 1211 > alpha > with length 1`] = `"WxUOl"`; exports[`random > 1211 > alphaNumeric > noArgs 1`] = `"V"`; -exports[`random > 1211 > alphaNumeric > withLength 1`] = `"VsTMd"`; +exports[`random > 1211 > alphaNumeric > with length 1`] = `"VsTMd"`; exports[`random > 1211 > locale 1`] = `"ur"`; exports[`random > 1211 > numeric > noArgs 1`] = `"9"`; -exports[`random > 1211 > numeric > withLength 1`] = `"94872"`; +exports[`random > 1211 > numeric > with length 1`] = `"94872"`; exports[`random > 1211 > word 1`] = `"gah"`; exports[`random > 1211 > words > noArgs 1`] = `"invoice Cyclocross assault"`; -exports[`random > 1211 > words > withLength 1`] = `"gah strictly Rustic assault Manager"`; +exports[`random > 1211 > words > with length 1`] = `"gah strictly Rustic assault Manager"`; + +exports[`random > 1211 > words > with length range 1`] = `"invoice Cyclocross assault Manager New"`; exports[`random > 1337 > alpha > noArgs 1`] = `"n"`; -exports[`random > 1337 > alpha > withLength 1`] = `"nDilo"`; +exports[`random > 1337 > alpha > with length 1`] = `"nDilo"`; exports[`random > 1337 > alphaNumeric > noArgs 1`] = `"g"`; -exports[`random > 1337 > alphaNumeric > withLength 1`] = `"gy9dh"`; +exports[`random > 1337 > alphaNumeric > with length 1`] = `"gy9dh"`; exports[`random > 1337 > locale 1`] = `"en_GH"`; exports[`random > 1337 > numeric > noArgs 1`] = `"2"`; -exports[`random > 1337 > numeric > withLength 1`] = `"25122"`; +exports[`random > 1337 > numeric > with length 1`] = `"25122"`; exports[`random > 1337 > word 1`] = `"Bespoke"`; exports[`random > 1337 > words > noArgs 1`] = `"quia"`; -exports[`random > 1337 > words > withLength 1`] = `"Bespoke connect Fort generating Ball"`; +exports[`random > 1337 > words > with length 1`] = `"Bespoke connect Fort generating Ball"`; + +exports[`random > 1337 > words > with length range 1`] = `"quia Ball"`; diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index 025ad494af7..308b7871ac1 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -28,7 +28,7 @@ exports[`system > 42 > fileName > noArgs 1`] = `"nonbeliever_stub.skt"`; exports[`system > 42 > fileName > with extensionCount 1`] = `"nonbeliever_stub.skt.latex"`; -exports[`system > 42 > fileName > with extensionCount range 1`] = `"crowded_fail_woefully.dra"`; +exports[`system > 42 > fileName > with extensionCount range 1`] = `"nonbeliever_stub.latex"`; exports[`system > 42 > filePath 1`] = `"/opt/bin/crowded_fail_woefully.dra"`; @@ -108,7 +108,7 @@ exports[`system > 1211 > fileName > noArgs 1`] = `"although_instantly_though.ust exports[`system > 1211 > fileName > with extensionCount 1`] = `"although_instantly_though.ustar.ecelp4800"`; -exports[`system > 1211 > fileName > with extensionCount range 1`] = `"outside_even.xhvml.ttc"`; +exports[`system > 1211 > fileName > with extensionCount range 1`] = `"although_instantly_though.ecelp4800"`; exports[`system > 1211 > filePath 1`] = `"/var/log/outside_even.xhvml"`; @@ -188,7 +188,7 @@ exports[`system > 1337 > fileName > noArgs 1`] = `"although.chrt"`; exports[`system > 1337 > fileName > with extensionCount 1`] = `"although.chrt.dpg"`; -exports[`system > 1337 > fileName > with extensionCount range 1`] = `"yum_fast"`; +exports[`system > 1337 > fileName > with extensionCount range 1`] = `"although"`; exports[`system > 1337 > filePath 1`] = `"/Library/yum_fast.aiff"`; diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts index 5571f3b9b02..9a8cb1bb513 100644 --- a/test/datatype.spec.ts +++ b/test/datatype.spec.ts @@ -77,7 +77,9 @@ describe('datatype', () => { t.it('json'); t.describe('array', (t) => { - t.it('noArgs').it('with length', 4); + t.it('noArgs') + .it('with length', 4) + .it('with length range', { min: 3, max: 5 }); }); t.describe('bigInt', (t) => { @@ -448,10 +450,21 @@ describe('datatype', () => { expect(generatedArray).toHaveLength(randomSize); }); + it('generates an array with 0 element', () => { + const generatedArray = faker.datatype.array(0); + expect(generatedArray).toHaveLength(0); + }); + it('generates an array with 1 element', () => { const generatedArray = faker.datatype.array(1); expect(generatedArray).toHaveLength(1); }); + + it('generates an array with length range', () => { + const generatedArray = faker.datatype.array({ min: 1, max: 5 }); + expect(generatedArray.length).toBeGreaterThanOrEqual(1); + expect(generatedArray.length).toBeLessThanOrEqual(5); + }); }); describe('bigInt', () => { diff --git a/test/date.spec.ts b/test/date.spec.ts index d4436951af8..75f203cca5f 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -91,6 +91,11 @@ describe('date', () => { from: new Date('2021-02-21T17:09:15.711Z'), to: new Date('2021-04-21T17:11:17.711Z'), count: 5, + }) + .it('with Date dates and count range', { + from: new Date('2021-02-21T17:09:15.711Z'), + to: new Date('2021-04-21T17:11:17.711Z'), + count: { min: 3, max: 5 }, }); }); @@ -280,7 +285,47 @@ describe('date', () => { describe('betweens()', () => { it.each(converterMap)( - 'should return an array of 3 dates ( by default ) of sorted randoms dates between the dates given', + 'should return an array of 3 ( by default ) sorted random dates between the dates given', + (converter) => { + const from = new Date(1990, 5, 7, 9, 11, 0, 0); + const to = new Date(2000, 6, 8, 10, 12, 0, 0); + + const dates = faker.date.betweens({ + from: converter(from), + to: converter(to), + }); + + expect(dates).toHaveLength(3); + + expect(dates[0]).greaterThan(from); + expect(dates[0]).lessThan(dates[1]); + expect(dates[1]).lessThan(dates[2]); + expect(dates[2]).lessThan(to); + } + ); + + it.each(converterMap)( + 'should return an array of 2 sorted random dates between the dates given', + (converter) => { + const from = new Date(1990, 5, 7, 9, 11, 0, 0); + const to = new Date(2000, 6, 8, 10, 12, 0, 0); + + const dates = faker.date.betweens( + converter(from), + converter(to), + 2 + ); + + expect(dates).toHaveLength(2); + + expect(dates[0]).greaterThan(from); + expect(dates[0]).lessThan(dates[1]); + expect(dates[1]).lessThan(to); + } + ); + + it.each(converterMap)( + 'should return an array of 3-5 sorted random dates between the dates given', (converter) => { const from = new Date(1990, 5, 7, 9, 11, 0, 0); const to = new Date(2000, 6, 8, 10, 12, 0, 0); @@ -288,12 +333,20 @@ describe('date', () => { const dates = faker.date.betweens({ from: converter(from), to: converter(to), + count: { + min: 3, + max: 5, + }, }); + expect(dates.length).greaterThanOrEqual(3); + expect(dates.length).lessThanOrEqual(5); + expect(dates[0]).greaterThan(from); - expect(dates[0]).lessThan(to); - expect(dates[1]).greaterThan(dates[0]); - expect(dates[2]).greaterThan(dates[1]); + for (let i = 1; i < dates.length; i++) { + expect(dates[i]).greaterThan(dates[i - 1]); + } + expect(dates[dates.length - 1]).lessThan(to); } ); }); diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 530c3b49a00..24a8d6dac48 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -112,6 +112,14 @@ describe('helpers', () => { .it('with () => number', faker.number.int) .it('with () => number and args', faker.number.int, [50]); }); + + t.describe('multiple', (t) => { + t.it('with only method', faker.datatype.number) + .it('with method and count', faker.datatype.number, { count: 5 }) + .it('with method and count range', faker.datatype.number, { + count: { min: 1, max: 10 }, + }); + }); }); describe(`random seeded tests for seed ${faker.seed()}`, () => { @@ -854,5 +862,33 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`) expect(store).toEqual({ 'with conflict: 0': 'with conflict: 0' }); }); }); + + describe('multiple()', () => { + it('should generate values from the function with a default length of 3', () => { + const result = faker.helpers.multiple(faker.person.firstName); + expect(result).toBeTypeOf('object'); + expect(Array.isArray(result)).toBe(true); + expect(result.length).toBe(3); + }); + + it('should generate the given amount of values from the function', () => { + const result = faker.helpers.multiple(faker.person.firstName, { + count: 5, + }); + expect(result).toBeTypeOf('object'); + expect(Array.isArray(result)).toBe(true); + expect(result.length).toBe(5); + }); + + it('should generate a ranged number of values from the function', () => { + const result = faker.helpers.multiple(faker.person.firstName, { + count: { min: 1, max: 10 }, + }); + expect(result).toBeTypeOf('object'); + expect(Array.isArray(result)).toBe(true); + expect(result.length).toBeGreaterThanOrEqual(1); + expect(result.length).toBeLessThanOrEqual(10); + }); + }); }); }); diff --git a/test/random.spec.ts b/test/random.spec.ts index 2ec6008da49..510009dbe14 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -12,10 +12,15 @@ describe('random', () => { t.describeEach( 'alpha', 'alphaNumeric', - 'numeric', - 'words' + 'numeric' )((t) => { - t.it('noArgs').it('withLength', 5); + t.it('noArgs').it('with length', 5); + }); + + t.describe('words', (t) => { + t.it('noArgs') + .it('with length', 5) + .it('with length range', { min: 1, max: 5 }); }); }); @@ -99,7 +104,7 @@ describe('random', () => { expect(words.length).toBeLessThanOrEqual(3); }); - it('should return random words', () => { + it('should return 5 random words', () => { const actual = faker.random.words(5); expect(actual).toBeTruthy(); @@ -108,6 +113,17 @@ describe('random', () => { const words = actual.split(' '); expect(words).toHaveLength(5); }); + + it('should return 3-5 random words', () => { + const actual = faker.random.words({ min: 3, max: 5 }); + + expect(actual).toBeTruthy(); + expect(actual).toBeTypeOf('string'); + + const words = actual.split(' '); + expect(words.length).toBeGreaterThanOrEqual(3); + expect(words.length).toBeLessThanOrEqual(5); + }); }); describe('locale', () => { diff --git a/test/system.spec.ts b/test/system.spec.ts index 7a566b2160e..a603afea6f7 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -416,7 +416,7 @@ describe('system', () => { describe('cron()', () => { const regex = - /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; + /^([0-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; const regexElements = regex.toString().replace(/\//g, '').split(' ');