From d6c2e77caa0ff8c8048ec05ad67482ed84a60344 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:16:19 +0200 Subject: [PATCH 01/11] refactor: move some random methods to helpers --- src/helpers.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/random.ts | 44 ++++++++++++-------------------------- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index a04c577929f..5ce178fe18d 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -742,4 +742,62 @@ export class Helpers { const key = this.faker.helpers.objectKey(object); return object[key]; } + + /** + * Returns random element from the given array. + * + * @template T The type of the entries to pick from. + * @param array Array to pick the value from. + * + * @example + * faker.helpers.arrayElement(['cat', 'dog', 'mouse']) // 'dog' + */ + arrayElement(array: ReadonlyArray): T { + const index = + array.length > 1 + ? this.faker.datatype.number({ max: array.length - 1 }) + : 0; + + return array[index]; + } + + /** + * Returns a subset with random elements of the given array in random order. + * + * @template T The type of the entries to pick from. + * @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`. + * @param count Number of elements to pick. + * When not provided, random number of elements will be picked. + * When value exceeds array boundaries, it will be limited to stay inside. + * + * @example + * faker.helpers.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat'] + * faker.helpers.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2] + */ + arrayElements(array: ReadonlyArray, count?: number): T[] { + if (typeof count !== 'number') { + count = this.faker.datatype.number({ min: 1, max: array.length }); + } else if (count > array.length) { + count = array.length; + } else if (count < 0) { + count = 0; + } + + const arrayCopy = array.slice(0); + let i = array.length; + const min = i - count; + let temp: T; + let index: number; + + while (i-- > min) { + index = Math.floor( + (i + 1) * this.faker.datatype.float({ min: 0, max: 0.99 }) + ); + temp = arrayCopy[index]; + arrayCopy[index] = arrayCopy[i]; + arrayCopy[i] = temp; + } + + return arrayCopy.slice(min); + } } diff --git a/src/random.ts b/src/random.ts index 2b90db073f7..4a52554fef4 100644 --- a/src/random.ts +++ b/src/random.ts @@ -108,12 +108,13 @@ export class Random { arrayElement( array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray ): T { - const index = - array.length > 1 - ? this.faker.datatype.number({ max: array.length - 1 }) - : 0; - - return array[index]; + deprecated({ + deprecated: 'faker.random.arrayElement()', + proposed: 'faker.helpers.arrayElement()', + since: 'v6.3.0', + until: 'v7.0.0', + }); + return this.faker.helpers.arrayElement(array); } /** @@ -134,30 +135,13 @@ export class Random { array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray, count?: number ): T[] { - if (typeof count !== 'number') { - count = this.faker.datatype.number({ min: 1, max: array.length }); - } else if (count > array.length) { - count = array.length; - } else if (count < 0) { - count = 0; - } - - const arrayCopy = array.slice(0); - let i = array.length; - const min = i - count; - let temp: T; - let index: number; - - while (i-- > min) { - index = Math.floor( - (i + 1) * this.faker.datatype.float({ min: 0, max: 0.99 }) - ); - temp = arrayCopy[index]; - arrayCopy[index] = arrayCopy[i]; - arrayCopy[i] = temp; - } - - return arrayCopy.slice(min); + deprecated({ + deprecated: 'faker.random.arrayElements()', + proposed: 'faker.helpers.arrayElements()', + since: 'v6.3.0', + until: 'v7.0.0', + }); + return this.faker.helpers.arrayElements(array, count); } /** From 64637c6cdff843d31532e1cd878e9dd8f732163b Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:23:22 +0200 Subject: [PATCH 02/11] refactor: move some random methods to helpers --- src/helpers.ts | 4 ++-- src/random.ts | 4 ++++ test/helpers.spec.ts | 53 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 5ce178fe18d..df75bb27ef5 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -147,11 +147,11 @@ export class Helpers { ): T { deprecated({ deprecated: 'faker.helpers.randomize()', - proposed: 'faker.random.arrayElement()', + proposed: 'faker.helpers.arrayElement()', // since: 'v5.0.0', (?) until: 'v7.0.0', }); - return this.faker.random.arrayElement(array); + return this.faker.helpers.arrayElement(array); } /** diff --git a/src/random.ts b/src/random.ts index 4a52554fef4..3cf4c0aa899 100644 --- a/src/random.ts +++ b/src/random.ts @@ -104,6 +104,8 @@ export class Random { * @example * faker.random.arrayElement() // 'b' * faker.random.arrayElement(['cat', 'dog', 'mouse']) // 'dog' + * + * @deprecated */ arrayElement( array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray @@ -130,6 +132,8 @@ export class Random { * faker.random.arrayElements() // ['b', 'c'] * faker.random.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat'] * faker.random.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2] + * + * @deprecated */ arrayElements( array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray, diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 3f63a80728c..555ceea6dc1 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -497,9 +497,58 @@ describe('helpers', () => { faker.seedValue )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { - describe('randomize()', () => { - // Will be marked as deprecated soon + describe('arrayElement', () => { + it('should return a random element in the array', () => { + const testArray = ['hello', 'to', 'you', 'my', 'friend']; + const actual = faker.helpers.arrayElement(testArray); + + expect(testArray).toContain(actual); + }); + + it('should return a random element in the array when there is only 1', () => { + const testArray = ['hello']; + const actual = faker.helpers.arrayElement(testArray); + + expect(actual).toBe('hello'); + }); + }); + + describe('arrayElements', () => { + it('should return a subset with random elements in the array', () => { + const testArray = ['hello', 'to', 'you', 'my', 'friend']; + const subset = faker.helpers.arrayElements(testArray); + + // Check length + expect(subset.length).toBeGreaterThanOrEqual(1); + expect(subset.length).toBeLessThanOrEqual(testArray.length); + + // Check elements + subset.forEach((element) => { + expect(testArray).toContain(element); + }); + // Check uniqueness + expect(subset).toHaveLength(new Set(subset).size); + }); + + it('should return a subset of fixed length with random elements in the array', () => { + const testArray = ['hello', 'to', 'you', 'my', 'friend']; + const subset = faker.helpers.arrayElements(testArray, 3); + + // Check length + expect(subset).toHaveLength(3); + + // Check elements + subset.forEach((element) => { + expect(testArray).toContain(element); + }); + + // Check uniqueness + expect(subset).toHaveLength(new Set(subset).size); + }); + }); + + describe('randomize()', () => { it('returns a random element from an array', () => { const arr = ['a', 'b', 'c']; const elem = faker.helpers.randomize(arr); From f8cbe51908c3fbbd8562d5df9cfc95a9c7329af2 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:26:37 +0200 Subject: [PATCH 03/11] refactor: move some random methods to helpers --- src/helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.ts b/src/helpers.ts index df75bb27ef5..4a34f6f3f82 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -765,7 +765,7 @@ export class Helpers { * Returns a subset with random elements of the given array in random order. * * @template T The type of the entries to pick from. - * @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`. + * @param array Array to pick the value from. * @param count Number of elements to pick. * When not provided, random number of elements will be picked. * When value exceeds array boundaries, it will be limited to stay inside. From acf1f09d95122a80093dc413500519e6ec59152c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:32:27 +0200 Subject: [PATCH 04/11] chore: fix comments --- src/fake.ts | 2 +- src/helpers.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fake.ts b/src/fake.ts index 4a22e2ff149..4b6bc788b1d 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -54,7 +54,7 @@ export class Fake { * faker.fake('This is static test.') // 'This is static test.' * faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!' * faker.fake('You can call me at {{phone.phoneNumber(!## ### #####!)}}.') // 'You can call me at 202 555 973722.' - * faker.fake('I flipped the coin an got: {{random.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' + * faker.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' */ fake(str: string): string { // if incoming str parameter is not provided, return error message diff --git a/src/helpers.ts b/src/helpers.ts index 4a34f6f3f82..5acec3b6bc7 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -127,14 +127,14 @@ export class Helpers { } /** - * Backward-compatibility. Use `faker.random.arrayElement()` instead. + * Backward-compatibility. Use `faker.helpers.arrayElement()` instead. * * Takes an array and returns a random element of the array. * * @template T The type of the entries to pick from. * @param array The array to select an element from. * - * @see faker.random.arrayElement() + * @see faker.helpers.arrayElement() * * @example * faker.helpers.randomize() // 'c' From 58c3626df8d5f91cbca440e9f16e003cfd44b38e Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:39:02 +0200 Subject: [PATCH 05/11] chore: add TODO --- src/helpers.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 5acec3b6bc7..b5d99fcd36b 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -752,7 +752,11 @@ export class Helpers { * @example * faker.helpers.arrayElement(['cat', 'dog', 'mouse']) // 'dog' */ - arrayElement(array: ReadonlyArray): T { + arrayElement( + // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty + // See https://github.com/faker-js/faker/issues/*** + array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray + ): T { const index = array.length > 1 ? this.faker.datatype.number({ max: array.length - 1 }) @@ -774,7 +778,12 @@ export class Helpers { * faker.helpers.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat'] * faker.helpers.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2] */ - arrayElements(array: ReadonlyArray, count?: number): T[] { + arrayElements( + // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty + // See https://github.com/faker-js/faker/issues/*** + array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray, + count?: number + ): T[] { if (typeof count !== 'number') { count = this.faker.datatype.number({ min: 1, max: array.length }); } else if (count > array.length) { From 5df00785fe6c8b14316be3b8d78fc148ec023975 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:44:57 +0200 Subject: [PATCH 06/11] chore: use helpers.arrayElement --- src/address.ts | 42 ++++++++++++++++--------------- src/animal.ts | 32 ++++++++++++----------- src/commerce.ts | 12 ++++----- src/company.ts | 14 +++++------ src/database.ts | 10 +++++--- src/datatype.ts | 2 +- src/date.ts | 4 +-- src/finance.ts | 32 +++++++++++------------ src/git.ts | 4 +-- src/hacker.ts | 12 ++++----- src/helpers.ts | 8 +++--- src/image.ts | 2 +- src/image_providers/lorempixel.ts | 2 +- src/internet.ts | 24 +++++++++--------- src/lorem.ts | 4 +-- src/music.ts | 2 +- src/name.ts | 18 ++++++------- src/phone.ts | 2 +- src/system.ts | 14 +++++------ src/utils/user-agent.ts | 2 +- src/vehicle.ts | 12 +++++---- src/word.ts | 14 +++++------ 22 files changed, 139 insertions(+), 129 deletions(-) diff --git a/src/address.ts b/src/address.ts index cfac28067ef..e077acaa127 100644 --- a/src/address.ts +++ b/src/address.ts @@ -35,7 +35,7 @@ export class Address { if (typeof localeFormat === 'string') { format = localeFormat; } else { - format = this.faker.random.arrayElement(localeFormat); + format = this.faker.helpers.arrayElement(localeFormat); } } return this.faker.helpers.replaceSymbols(format); @@ -108,7 +108,7 @@ export class Address { * faker.address.cityPrefix() // 'East' */ cityPrefix(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.city_prefix ); } @@ -120,7 +120,7 @@ export class Address { * faker.address.citySuffix() // 'mouth' */ citySuffix(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.city_suffix ); } @@ -132,7 +132,7 @@ export class Address { * faker.address.cityName() // 'San Rafael' */ cityName(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.city_name ); } @@ -144,7 +144,7 @@ export class Address { * faker.address.buildingNumber() // '379' */ buildingNumber(): string { - const format = this.faker.random.arrayElement( + const format = this.faker.helpers.arrayElement( this.faker.definitions.address.building_number ); @@ -200,7 +200,7 @@ export class Address { * faker.address.streetSuffix() // 'Streets' */ streetSuffix(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.street_suffix ); } @@ -212,7 +212,7 @@ export class Address { * fakerGH.address.streetPrefix() // 'Boame' */ streetPrefix(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.street_prefix ); } @@ -226,7 +226,7 @@ export class Address { */ secondaryAddress(): string { return this.faker.helpers.replaceSymbolWithNumber( - this.faker.random.arrayElement( + this.faker.helpers.arrayElement( this.faker.definitions.address.secondary_address ) ); @@ -239,7 +239,7 @@ export class Address { * faker.address.county() // 'Cambridgeshire' */ county(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.county ); } @@ -251,7 +251,7 @@ export class Address { * faker.address.country() // 'Greece' */ country(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.country ); } @@ -271,7 +271,7 @@ export class Address { const key: keyof typeof this.faker.definitions.address = alphaCode === 'alpha-3' ? 'country_code_alpha_3' : 'country_code'; - return this.faker.random.arrayElement(this.faker.definitions.address[key]); + return this.faker.helpers.arrayElement(this.faker.definitions.address[key]); } /** @@ -281,7 +281,9 @@ export class Address { * faker.address.state() // 'Georgia' */ state(): string { - return this.faker.random.arrayElement(this.faker.definitions.address.state); + return this.faker.helpers.arrayElement( + this.faker.definitions.address.state + ); } /** @@ -291,7 +293,7 @@ export class Address { * faker.address.stateAbbr() // 'ND' */ stateAbbr(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.state_abbr ); } @@ -355,11 +357,11 @@ export class Address { */ direction(useAbbr: boolean = false): string { if (!useAbbr) { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction ); } - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction_abbr ); } @@ -377,11 +379,11 @@ export class Address { */ cardinalDirection(useAbbr: boolean = false): string { if (!useAbbr) { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction.slice(0, 4) ); } - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction_abbr.slice(0, 4) ); } @@ -399,11 +401,11 @@ export class Address { */ ordinalDirection(useAbbr: boolean = false): string { if (!useAbbr) { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction.slice(4, 8) ); } - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.direction_abbr.slice(4, 8) ); } @@ -479,7 +481,7 @@ export class Address { * faker.address.timeZone() // 'Pacific/Guam' */ timeZone(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.address.time_zone ); } diff --git a/src/animal.ts b/src/animal.ts index 11bfad52164..6a1d10a214d 100644 --- a/src/animal.ts +++ b/src/animal.ts @@ -21,7 +21,7 @@ export class Animal { * faker.animal.dog() // 'Irish Water Spaniel' */ dog(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.dog); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.dog); } /** @@ -31,7 +31,7 @@ export class Animal { * faker.animal.cat() // 'Singapura' */ cat(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.cat); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.cat); } /** @@ -41,7 +41,7 @@ export class Animal { * faker.animal.snake() // 'Eyelash viper' */ snake(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.snake); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.snake); } /** @@ -51,7 +51,7 @@ export class Animal { * faker.animal.bear() // 'Asian black bear' */ bear(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.bear); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.bear); } /** @@ -61,7 +61,7 @@ export class Animal { * faker.animal.lion() // 'Northeast Congo Lion' */ lion(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.lion); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.lion); } /** @@ -71,7 +71,7 @@ export class Animal { * faker.animal.cetacean() // 'Spinner Dolphin' */ cetacean(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.animal.cetacean ); } @@ -83,7 +83,7 @@ export class Animal { * faker.animal.horse() // 'Swedish Warmblood' */ horse(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.horse); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.horse); } /** @@ -93,7 +93,7 @@ export class Animal { * faker.animal.bird() // 'Buller's Shearwater' */ bird(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.bird); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.bird); } /** @@ -103,7 +103,7 @@ export class Animal { * faker.animal.cow() // 'Brava' */ cow(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.cow); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.cow); } /** @@ -113,7 +113,7 @@ export class Animal { * faker.animal.fish() // 'Mandarin fish' */ fish(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.fish); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.fish); } /** @@ -123,7 +123,7 @@ export class Animal { * faker.animal.crocodilia() // 'Philippine Crocodile' */ crocodilia(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.animal.crocodilia ); } @@ -135,7 +135,9 @@ export class Animal { * faker.animal.insect() // 'Pyramid ant' */ insect(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.insect); + return this.faker.helpers.arrayElement( + this.faker.definitions.animal.insect + ); } /** @@ -145,7 +147,9 @@ export class Animal { * faker.animal.rabbit() // 'Florida White' */ rabbit(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.rabbit); + return this.faker.helpers.arrayElement( + this.faker.definitions.animal.rabbit + ); } /** @@ -155,6 +159,6 @@ export class Animal { * faker.animal.type() // 'crocodilia' */ type(): string { - return this.faker.random.arrayElement(this.faker.definitions.animal.type); + return this.faker.helpers.arrayElement(this.faker.definitions.animal.type); } } diff --git a/src/commerce.ts b/src/commerce.ts index 9d0a57316bd..754fe73a510 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -21,7 +21,7 @@ export class Commerce { * faker.commerce.color() // 'red' */ color(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.color ); } @@ -33,7 +33,7 @@ export class Commerce { * faker.commerce.department() // 'Garden' */ department(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.department ); } @@ -90,7 +90,7 @@ export class Commerce { * faker.commerce.productAdjective() // 'Handcrafted' */ productAdjective(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.product_name.adjective ); } @@ -102,7 +102,7 @@ export class Commerce { * faker.commerce.productMaterial() // 'Rubber' */ productMaterial(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.product_name.material ); } @@ -114,7 +114,7 @@ export class Commerce { * faker.commerce.product() // 'Computer' */ product(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.product_name.product ); } @@ -126,7 +126,7 @@ export class Commerce { * faker.commerce.productDescription() // 'Andy shoes are designed to keeping...' */ productDescription(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.commerce.product_description ); } diff --git a/src/company.ts b/src/company.ts index 67d70199514..c2d72260d78 100644 --- a/src/company.ts +++ b/src/company.ts @@ -54,7 +54,7 @@ export class Company { * faker.company.companySuffix() // 'and Sons' */ companySuffix(): string { - return this.faker.random.arrayElement(this.suffixes()); + return this.faker.helpers.arrayElement(this.suffixes()); } /** @@ -88,7 +88,7 @@ export class Company { * faker.company.catchPhraseAdjective() // 'Multi-tiered' */ catchPhraseAdjective(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.company.adjective ); } @@ -100,7 +100,7 @@ export class Company { * faker.company.catchPhraseDescriptor() // 'composite' */ catchPhraseDescriptor(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.company.descriptor ); } @@ -112,7 +112,7 @@ export class Company { * faker.company.catchPhraseNoun() // 'leverage' */ catchPhraseNoun(): string { - return this.faker.random.arrayElement(this.faker.definitions.company.noun); + return this.faker.helpers.arrayElement(this.faker.definitions.company.noun); } /** @@ -122,7 +122,7 @@ export class Company { * faker.company.bsAdjective() // 'one-to-one' */ bsAdjective(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.company.bs_adjective ); } @@ -134,7 +134,7 @@ export class Company { * faker.company.bsBuzz() // 'empower' */ bsBuzz(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.company.bs_verb ); } @@ -146,7 +146,7 @@ export class Company { * faker.company.bsNoun() // 'paradigms' */ bsNoun(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.company.bs_noun ); } diff --git a/src/database.ts b/src/database.ts index 8b80c927808..770b4b04dd7 100644 --- a/src/database.ts +++ b/src/database.ts @@ -21,7 +21,7 @@ export class Database { * faker.database.column() // 'createdAt' */ column(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.database.column ); } @@ -33,7 +33,9 @@ export class Database { * faker.database.type() // 'timestamp' */ type(): string { - return this.faker.random.arrayElement(this.faker.definitions.database.type); + return this.faker.helpers.arrayElement( + this.faker.definitions.database.type + ); } /** @@ -43,7 +45,7 @@ export class Database { * faker.database.collation() // 'utf8_unicode_ci' */ collation(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.database.collation ); } @@ -55,7 +57,7 @@ export class Database { * faker.database.engine() // 'ARCHIVE' */ engine(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.database.engine ); } diff --git a/src/datatype.ts b/src/datatype.ts index 90211b69b65..6f277f54057 100644 --- a/src/datatype.ts +++ b/src/datatype.ts @@ -217,7 +217,7 @@ export class Datatype { let wholeString = ''; for (let i = 0; i < length; i++) { - wholeString += this.faker.random.arrayElement([ + wholeString += this.faker.helpers.arrayElement([ '0', '1', '2', diff --git a/src/date.ts b/src/date.ts index c10a80f8fa2..bb65ce4ee42 100644 --- a/src/date.ts +++ b/src/date.ts @@ -217,7 +217,7 @@ export class _Date { type = 'wide'; } - return this.faker.random.arrayElement(source[type]); + return this.faker.helpers.arrayElement(source[type]); } /** @@ -251,6 +251,6 @@ export class _Date { type = 'wide'; } - return this.faker.random.arrayElement(source[type]); + return this.faker.helpers.arrayElement(source[type]); } } diff --git a/src/finance.ts b/src/finance.ts index 7211de3b2f8..48ce550c9ec 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -44,7 +44,7 @@ export class Finance { */ accountName(): string { return [ - this.faker.random.arrayElement( + this.faker.helpers.arrayElement( this.faker.definitions.finance.account_type ), 'Account', @@ -158,7 +158,7 @@ export class Finance { * faker.finance.transactionType() // 'payment' */ transactionType(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.finance.transaction_type ); } @@ -213,10 +213,10 @@ export class Finance { bitcoinAddress(): string { const addressLength = this.faker.datatype.number({ min: 25, max: 34 }); - let address = this.faker.random.arrayElement(['1', '3']); + let address = this.faker.helpers.arrayElement(['1', '3']); for (let i = 0; i < addressLength - 1; i++) - address += this.faker.random.arrayElement( + address += this.faker.helpers.arrayElement( '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('') ); @@ -232,10 +232,10 @@ export class Finance { litecoinAddress(): string { const addressLength = this.faker.datatype.number({ min: 26, max: 33 }); - let address = this.faker.random.arrayElement(['L', 'M', '3']); + let address = this.faker.helpers.arrayElement(['L', 'M', '3']); for (let i = 0; i < addressLength - 1; i++) - address += this.faker.random.arrayElement( + address += this.faker.helpers.arrayElement( '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('') ); @@ -257,7 +257,7 @@ export class Finance { const localeFormat = this.faker.definitions.finance.credit_card; const normalizedIssuer = issuer.toLowerCase(); if (normalizedIssuer in localeFormat) { - format = this.faker.random.arrayElement(localeFormat[normalizedIssuer]); + format = this.faker.helpers.arrayElement(localeFormat[normalizedIssuer]); } else if (issuer.match(/#/)) { // The user chose an optional scheme format = issuer; @@ -265,7 +265,7 @@ export class Finance { // Choose a random issuer // Credit cards are in an object structure const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats - format = this.faker.random.arrayElement(formats); + format = this.faker.helpers.arrayElement(formats); } format = format.replace(/\//g, ''); return this.faker.helpers.replaceCreditCardSymbols(format); @@ -340,7 +340,7 @@ export class Finance { iban(formatted: boolean = false, countryCode?: string): string { const ibanFormat = countryCode ? iban.formats.find((f) => f.country === countryCode) - : this.faker.random.arrayElement(iban.formats); + : this.faker.helpers.arrayElement(iban.formats); if (!ibanFormat) { throw new FakerError(`Country code ${countryCode} not supported.`); @@ -353,20 +353,20 @@ export class Finance { count += bban.count; while (c > 0) { if (bban.type === 'a') { - s += this.faker.random.arrayElement(iban.alpha); + s += this.faker.helpers.arrayElement(iban.alpha); } else if (bban.type === 'c') { if (this.faker.datatype.number(100) < 80) { s += this.faker.datatype.number(9); } else { - s += this.faker.random.arrayElement(iban.alpha); + s += this.faker.helpers.arrayElement(iban.alpha); } } else { if (c >= 3 && this.faker.datatype.number(100) < 30) { if (this.faker.datatype.boolean()) { - s += this.faker.random.arrayElement(iban.pattern100); + s += this.faker.helpers.arrayElement(iban.pattern100); c -= 2; } else { - s += this.faker.random.arrayElement(iban.pattern10); + s += this.faker.helpers.arrayElement(iban.pattern10); c--; } } else { @@ -401,13 +401,13 @@ export class Finance { return [ this.faker.helpers.replaceSymbols('???'), - this.faker.random.arrayElement(vowels), - this.faker.random.arrayElement(iban.iso3166), + this.faker.helpers.arrayElement(vowels), + this.faker.helpers.arrayElement(iban.iso3166), this.faker.helpers.replaceSymbols('?'), '1', prob < 10 ? this.faker.helpers.replaceSymbols( - `?${this.faker.random.arrayElement(vowels)}?` + `?${this.faker.helpers.arrayElement(vowels)}?` ) : prob < 40 ? this.faker.helpers.replaceSymbols('###') diff --git a/src/git.ts b/src/git.ts index c1d02147202..c0c9806234e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -110,7 +110,7 @@ export class Git { let commit = ''; for (let i = 0; i < 40; i++) { - commit += this.faker.random.arrayElement(this.hexChars); + commit += this.faker.helpers.arrayElement(this.hexChars); } return commit; @@ -126,7 +126,7 @@ export class Git { let shortSha = ''; for (let i = 0; i < 7; i++) { - shortSha += this.faker.random.arrayElement(this.hexChars); + shortSha += this.faker.helpers.arrayElement(this.hexChars); } return shortSha; diff --git a/src/hacker.ts b/src/hacker.ts index 3ec91820042..49c636026c3 100644 --- a/src/hacker.ts +++ b/src/hacker.ts @@ -21,7 +21,7 @@ export class Hacker { * faker.hacker.abbreviation() // 'THX' */ abbreviation(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.hacker.abbreviation ); } @@ -33,7 +33,7 @@ export class Hacker { * faker.hacker.adjective() // 'cross-platform' */ adjective(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.hacker.adjective ); } @@ -45,7 +45,7 @@ export class Hacker { * faker.hacker.noun() // 'system' */ noun(): string { - return this.faker.random.arrayElement(this.faker.definitions.hacker.noun); + return this.faker.helpers.arrayElement(this.faker.definitions.hacker.noun); } /** @@ -55,7 +55,7 @@ export class Hacker { * faker.hacker.verb() // 'copy' */ verb(): string { - return this.faker.random.arrayElement(this.faker.definitions.hacker.verb); + return this.faker.helpers.arrayElement(this.faker.definitions.hacker.verb); } /** @@ -65,7 +65,7 @@ export class Hacker { * faker.hacker.ingverb() // 'navigating' */ ingverb(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.hacker.ingverb ); } @@ -86,7 +86,7 @@ export class Hacker { verb: this.verb, }; - const phrase = this.faker.random.arrayElement( + const phrase = this.faker.helpers.arrayElement( this.faker.definitions.hacker.phrase ); return this.faker.helpers.mustache(phrase, data); diff --git a/src/helpers.ts b/src/helpers.ts index b5d99fcd36b..3545e640a22 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -249,10 +249,10 @@ export class Helpers { if (string.charAt(i) === '#') { str += this.faker.datatype.number(9); } else if (string.charAt(i) === '?') { - str += this.faker.random.arrayElement(alpha); + str += this.faker.helpers.arrayElement(alpha); } else if (string.charAt(i) === '*') { str += this.faker.datatype.boolean() - ? this.faker.random.arrayElement(alpha) + ? this.faker.helpers.arrayElement(alpha) : this.faker.datatype.number(9); } else { str += string.charAt(i); @@ -686,7 +686,7 @@ export class Helpers { name: [this.faker.finance.accountName(), this.faker.finance.mask()].join( ' ' ), - type: this.faker.random.arrayElement( + type: this.faker.helpers.arrayElement( this.faker.definitions.finance.transaction_type ), account: this.faker.finance.account(), @@ -727,7 +727,7 @@ export class Helpers { */ objectKey>(object: T): keyof T { const array: Array = Object.keys(object); - return this.faker.random.arrayElement(array); + return this.faker.helpers.arrayElement(array); } /** diff --git a/src/image.ts b/src/image.ts index 9fe7f6039e0..fc6a2a96042 100644 --- a/src/image.ts +++ b/src/image.ts @@ -56,7 +56,7 @@ export class Image { 'technics', 'transport', ]; - return this[this.faker.random.arrayElement(categories)]( + return this[this.faker.helpers.arrayElement(categories)]( width, height, randomize diff --git a/src/image_providers/lorempixel.ts b/src/image_providers/lorempixel.ts index 50e48eded0b..17cbc2f59df 100644 --- a/src/image_providers/lorempixel.ts +++ b/src/image_providers/lorempixel.ts @@ -30,7 +30,7 @@ export class Lorempixel { 'technics', 'transport', ]; - return this[this.faker.random.arrayElement(categories)]( + return this[this.faker.helpers.arrayElement(categories)]( width, height, randomize diff --git a/src/internet.ts b/src/internet.ts index df830bca4ef..6d018dc3cca 100644 --- a/src/internet.ts +++ b/src/internet.ts @@ -64,7 +64,7 @@ export class Internet { ): string { provider = provider || - this.faker.random.arrayElement( + this.faker.helpers.arrayElement( this.faker.definitions.internet.free_email ); @@ -76,8 +76,8 @@ export class Internet { const usernameChars: string[] = '._-'.split(''); const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split(''); localPart = localPart.replace( - this.faker.random.arrayElement(usernameChars), - this.faker.random.arrayElement(specialChars) + this.faker.helpers.arrayElement(usernameChars), + this.faker.helpers.arrayElement(specialChars) ); } @@ -103,7 +103,7 @@ export class Internet { lastName?: string, options?: { allowSpecialCharacters?: boolean } ): string { - const provider = this.faker.random.arrayElement( + const provider = this.faker.helpers.arrayElement( this.faker.definitions.internet.example_email ); return this.email(firstName, lastName, provider, options); @@ -129,10 +129,10 @@ export class Internet { break; case 1: result = - firstName + this.faker.random.arrayElement(['.', '_']) + lastName; + firstName + this.faker.helpers.arrayElement(['.', '_']) + lastName; break; case 2: - result = `${firstName}${this.faker.random.arrayElement([ + result = `${firstName}${this.faker.helpers.arrayElement([ '.', '_', ])}${lastName}${this.faker.datatype.number(99)}`; @@ -152,7 +152,7 @@ export class Internet { */ protocol(): 'http' | 'https' { const protocols: ['http', 'https'] = ['http', 'https']; - return this.faker.random.arrayElement(protocols); + return this.faker.helpers.arrayElement(protocols); } /** @@ -177,7 +177,7 @@ export class Internet { 'DELETE', 'PATCH', ]; - return this.faker.random.arrayElement(httpMethods); + return this.faker.helpers.arrayElement(httpMethods); } /** @@ -208,7 +208,7 @@ export class Internet { * faker.internet.domainSuffix() // 'name' */ domainSuffix(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.internet.domain_suffix ); } @@ -268,7 +268,7 @@ export class Internet { const randHash = () => { let result = ''; for (let i = 0; i < 4; i++) { - result += this.faker.random.arrayElement([ + result += this.faker.helpers.arrayElement([ '0', '1', '2', @@ -450,8 +450,8 @@ export class Internet { this.faker.definitions.internet.emoji ) as Array, } = options; - const emojiType = this.faker.random.arrayElement(types); - return this.faker.random.arrayElement( + const emojiType = this.faker.helpers.arrayElement(types); + return this.faker.helpers.arrayElement( this.faker.definitions.internet.emoji[emojiType] ); } diff --git a/src/lorem.ts b/src/lorem.ts index 14cc2f6b1ce..a7a1b4d39f5 100644 --- a/src/lorem.ts +++ b/src/lorem.ts @@ -32,7 +32,7 @@ export class Lorem { properLengthWords = this.faker.definitions.lorem.words.filter(hasRightLength); } - return this.faker.random.arrayElement(properLengthWords); + return this.faker.helpers.arrayElement(properLengthWords); } /** @@ -175,7 +175,7 @@ export class Lorem { 'lines', ]; - const method = this.faker.random.arrayElement(methods); + const method = this.faker.helpers.arrayElement(methods); return `${this[method]()}`; } diff --git a/src/music.ts b/src/music.ts index 5c1945dc374..d0e0a1a7f4d 100644 --- a/src/music.ts +++ b/src/music.ts @@ -21,6 +21,6 @@ export class Music { * faker.music.genre() // 'Reggae' */ genre(): string { - return this.faker.random.arrayElement(this.faker.definitions.music.genre); + return this.faker.helpers.arrayElement(this.faker.definitions.music.genre); } } diff --git a/src/name.ts b/src/name.ts index 41a6075ac97..280e4b098a5 100644 --- a/src/name.ts +++ b/src/name.ts @@ -77,13 +77,13 @@ function selectDefinition( if (values == null) { if (female != null && male != null) { - values = faker.random.arrayElement([female, male]); + values = faker.helpers.arrayElement([female, male]); } else { values = generic; } } - return faker.random.arrayElement(values); + return faker.helpers.arrayElement(values); } /** @@ -203,7 +203,7 @@ export class Name { const normalizedGender: Exclude = normalizeGender(gender, 'findName') ?? - this.faker.random.arrayElement(['female', 'male']); + this.faker.helpers.arrayElement(['female', 'male']); firstName = firstName || this.firstName(normalizedGender); lastName = lastName || this.lastName(normalizedGender); @@ -238,12 +238,12 @@ export class Name { */ gender(binary?: boolean): string { if (binary) { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.name.binary_gender ); } - return this.faker.random.arrayElement(this.faker.definitions.name.gender); + return this.faker.helpers.arrayElement(this.faker.definitions.name.gender); } /** @@ -280,7 +280,7 @@ export class Name { */ suffix(): string { // TODO @Shinigami92 2022-03-21: Add female_suffix and male_suffix - return this.faker.random.arrayElement(this.faker.definitions.name.suffix); + return this.faker.helpers.arrayElement(this.faker.definitions.name.suffix); } /** @@ -319,7 +319,7 @@ export class Name { * faker.name.jobDescriptor() // 'Customer' */ jobDescriptor(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.name.title.descriptor ); } @@ -331,7 +331,7 @@ export class Name { * faker.name.jobArea() // 'Brand' */ jobArea(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.name.title.level ); } @@ -343,7 +343,7 @@ export class Name { * faker.name.jobType() // 'Assistant' */ jobType(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.name.title.job ); } diff --git a/src/phone.ts b/src/phone.ts index c922e341e14..c8a1902ddaa 100644 --- a/src/phone.ts +++ b/src/phone.ts @@ -56,7 +56,7 @@ export class Phone { */ // TODO @pkuczynski 2022-02-01: simplify name to `format()` phoneFormats(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.phone_number.formats ); } diff --git a/src/system.ts b/src/system.ts index 05d0bf441b1..90e56d55900 100644 --- a/src/system.ts +++ b/src/system.ts @@ -82,7 +82,7 @@ export class System { mimeType(): string { const mimeTypeKeys = Object.keys(this.faker.definitions.system.mimeTypes); - return this.faker.random.arrayElement(mimeTypeKeys); + return this.faker.helpers.arrayElement(mimeTypeKeys); } /** @@ -92,7 +92,7 @@ export class System { * faker.system.commonFileType() // 'audio' */ commonFileType(): string { - return this.faker.random.arrayElement(commonFileTypes); + return this.faker.helpers.arrayElement(commonFileTypes); } /** @@ -102,7 +102,7 @@ export class System { * faker.system.commonFileExt() // 'gif' */ commonFileExt(): string { - return this.fileExt(this.faker.random.arrayElement(commonMimeTypes)); + return this.fileExt(this.faker.helpers.arrayElement(commonMimeTypes)); } /** @@ -122,7 +122,7 @@ export class System { }); const types = setToArray(typeSet); - return this.faker.random.arrayElement(types); + return this.faker.helpers.arrayElement(types); } /** @@ -137,7 +137,7 @@ export class System { fileExt(mimeType?: string): string { if (typeof mimeType === 'string') { const mimes = this.faker.definitions.system.mimeTypes; - return this.faker.random.arrayElement(mimes[mimeType].extensions); + return this.faker.helpers.arrayElement(mimes[mimeType].extensions); } const mimeTypes = this.faker.definitions.system.mimeTypes; @@ -153,7 +153,7 @@ export class System { const extensions = setToArray(extensionSet); - return this.faker.random.arrayElement(extensions); + return this.faker.helpers.arrayElement(extensions); } /** @@ -164,7 +164,7 @@ export class System { */ directoryPath(): string { const paths = this.faker.definitions.system.directoryPaths; - return this.faker.random.arrayElement(paths); + return this.faker.helpers.arrayElement(paths); } /** diff --git a/src/utils/user-agent.ts b/src/utils/user-agent.ts index 5ff18c2a4aa..b3438cbd20f 100644 --- a/src/utils/user-agent.ts +++ b/src/utils/user-agent.ts @@ -62,7 +62,7 @@ export function generate(faker: Faker): string { if (Array.isArray(a)) { //returns a random element from array (a), even weighting - return faker.random.arrayElement(a); + return faker.helpers.arrayElement(a); } if (a && typeof a === 'object') { diff --git a/src/vehicle.ts b/src/vehicle.ts index 979631bdddc..ca0d11f2f46 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -31,7 +31,7 @@ export class Vehicle { * faker.vehicle.manufacturer() // 'Ford' */ manufacturer(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.vehicle.manufacturer ); } @@ -43,7 +43,9 @@ export class Vehicle { * faker.vehicle.model() // 'Explorer' */ model(): string { - return this.faker.random.arrayElement(this.faker.definitions.vehicle.model); + return this.faker.helpers.arrayElement( + this.faker.definitions.vehicle.model + ); } /** @@ -53,7 +55,7 @@ export class Vehicle { * faker.vehicle.type() // 'Coupe' */ type(): string { - return this.faker.random.arrayElement(this.faker.definitions.vehicle.type); + return this.faker.helpers.arrayElement(this.faker.definitions.vehicle.type); } /** @@ -63,7 +65,7 @@ export class Vehicle { * faker.vehicle.fuel() // 'Electric' */ fuel(): string { - return this.faker.random.arrayElement(this.faker.definitions.vehicle.fuel); + return this.faker.helpers.arrayElement(this.faker.definitions.vehicle.fuel); } /** @@ -122,7 +124,7 @@ export class Vehicle { * faker.vehicle.bicycle() // 'Adventure Road Bicycle' */ bicycle(): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( this.faker.definitions.vehicle.bicycle_type ); } diff --git a/src/word.ts b/src/word.ts index f02a9a05a59..b586c0d03a0 100644 --- a/src/word.ts +++ b/src/word.ts @@ -50,7 +50,7 @@ export class Word { * faker.word.adjective(100) // 'complete' */ adjective(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.adjective, length, @@ -69,7 +69,7 @@ export class Word { * faker.word.adverb(100) // 'sadly' */ adverb(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.adverb, length, @@ -88,7 +88,7 @@ export class Word { * faker.word.conjunction(100) // 'as long as' */ conjunction(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.conjunction, length, @@ -107,7 +107,7 @@ export class Word { * faker.word.interjection(100) // 'yowza' */ interjection(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.interjection, length, @@ -126,7 +126,7 @@ export class Word { * faker.word.noun(100) // 'care' */ noun(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.noun, length, @@ -145,7 +145,7 @@ export class Word { * faker.word.preposition(100) // 'an' */ preposition(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.preposition, length, @@ -164,7 +164,7 @@ export class Word { * faker.word.verb(100) // 'mess' */ verb(length?: number): string { - return this.faker.random.arrayElement( + return this.faker.helpers.arrayElement( filterWordListByLength({ wordList: this.faker.definitions.word.verb, length, From d12861ef1db9c9b10bb24ad10aad184f417b4417 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:46:15 +0200 Subject: [PATCH 07/11] test: update --- test/helpers.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 555ceea6dc1..994b68e5403 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -907,7 +907,7 @@ describe('helpers', () => { }); describe('deprecation warnings', () => { - it.each([['randomize', 'random.arrayElement']])( + it.each([['randomize', 'helpers.arrayElement']])( 'should warn user that function helpers.%s is deprecated', (functionName, newLocation) => { const spy = vi.spyOn(console, 'warn'); From 179cf9debc757ece60f6711e03818930e72c5c5d Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:57:36 +0200 Subject: [PATCH 08/11] chore: fix todo comment --- src/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 3545e640a22..8f1ff656b9b 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -754,7 +754,7 @@ export class Helpers { */ arrayElement( // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty - // See https://github.com/faker-js/faker/issues/*** + // See https://github.com/faker-js/faker/issues/893 array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray ): T { const index = @@ -780,7 +780,7 @@ export class Helpers { */ arrayElements( // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty - // See https://github.com/faker-js/faker/issues/*** + // See https://github.com/faker-js/faker/issues/893 array: ReadonlyArray = ['a', 'b', 'c'] as unknown as ReadonlyArray, count?: number ): T[] { From 006b3481267a2fe9545da484985df0a8ac2128d8 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 30 Apr 2022 11:58:34 +0200 Subject: [PATCH 09/11] chore: fix comment --- test/internet.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 1f870b27e6c..b9927489861 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -310,7 +310,7 @@ describe('internet', () => { expect(username).toBeTypeOf('string'); expect(username).toMatch(/\w/); expect(username).includes('Aiden'); - // FIXME christopher 2022-02-11: The lastName is sometimes not taken + // FIXME @Shinigami92 2022-02-11: The lastName is sometimes not taken }); }); From a4ae359d589821eda2a039b5d5e2e1dc8da19135 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Sun, 1 May 2022 14:15:42 +0200 Subject: [PATCH 10/11] Apply suggestions from code review Co-authored-by: ST-DDT --- src/helpers.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 8f1ff656b9b..a66b4bb3e64 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -151,7 +151,7 @@ export class Helpers { // since: 'v5.0.0', (?) until: 'v7.0.0', }); - return this.faker.helpers.arrayElement(array); + return this.arrayElement(array); } /** @@ -249,10 +249,10 @@ export class Helpers { if (string.charAt(i) === '#') { str += this.faker.datatype.number(9); } else if (string.charAt(i) === '?') { - str += this.faker.helpers.arrayElement(alpha); + str += this.arrayElement(alpha); } else if (string.charAt(i) === '*') { str += this.faker.datatype.boolean() - ? this.faker.helpers.arrayElement(alpha) + ? this.arrayElement(alpha) : this.faker.datatype.number(9); } else { str += string.charAt(i); @@ -686,7 +686,7 @@ export class Helpers { name: [this.faker.finance.accountName(), this.faker.finance.mask()].join( ' ' ), - type: this.faker.helpers.arrayElement( + type: this.arrayElement( this.faker.definitions.finance.transaction_type ), account: this.faker.finance.account(), @@ -727,7 +727,7 @@ export class Helpers { */ objectKey>(object: T): keyof T { const array: Array = Object.keys(object); - return this.faker.helpers.arrayElement(array); + return this.arrayElement(array); } /** From 7e632f96a2d24830c33ad5f23b1147b5a712d0aa Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 1 May 2022 14:31:53 +0200 Subject: [PATCH 11/11] chore: format --- src/helpers.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index a66b4bb3e64..29b62a51934 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -686,9 +686,7 @@ export class Helpers { name: [this.faker.finance.accountName(), this.faker.finance.mask()].join( ' ' ), - type: this.arrayElement( - this.faker.definitions.finance.transaction_type - ), + type: this.arrayElement(this.faker.definitions.finance.transaction_type), account: this.faker.finance.account(), }; }