From 42d679566624aaedd01eb5c0d9fa54104008016c Mon Sep 17 00:00:00 2001 From: Shinigami Date: Mon, 2 May 2022 20:17:48 +0200 Subject: [PATCH] fix: replace deprecated arrayElement calls (#903) --- src/random.ts | 14 +++++++------- test/fake.spec.ts | 4 ++-- test/unique.spec.ts | 5 +++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/random.ts b/src/random.ts index 3cf4c0aa899..879768281ab 100644 --- a/src/random.ts +++ b/src/random.ts @@ -360,12 +360,12 @@ export class Random { do { // randomly pick from the many faker methods that can generate words - const randomWordMethod = this.arrayElement(wordMethods); + const randomWordMethod = this.faker.helpers.arrayElement(wordMethods); result = randomWordMethod(); } while (!result || bannedChars.some((char) => result.includes(char))); - return this.arrayElement(result.split(' ')); + return this.faker.helpers.arrayElement(result.split(' ')); } /** @@ -419,7 +419,7 @@ export class Random { * faker.random.locale() // 'el' */ locale(): string { - return this.arrayElement(Object.keys(this.faker.locales)); + return this.faker.helpers.arrayElement(Object.keys(this.faker.locales)); } /** @@ -485,7 +485,7 @@ export class Random { let wholeString = ''; for (let i = 0; i < count; i++) { - wholeString += this.arrayElement(charsArray); + wholeString += this.faker.helpers.arrayElement(charsArray); } return upcase ? wholeString.toUpperCase() : wholeString; @@ -558,7 +558,7 @@ export class Random { let wholeString = ''; for (let i = 0; i < count; i++) { - wholeString += this.arrayElement(charsArray); + wholeString += this.faker.helpers.arrayElement(charsArray); } return wholeString; @@ -610,13 +610,13 @@ export class Random { let result = ''; if (!allowLeadingZeros && !bannedDigits.includes('0')) { - result += this.arrayElement( + result += this.faker.helpers.arrayElement( allowedDigits.filter((digit) => digit !== '0') ); } while (result.length < length) { - result += this.arrayElement(allowedDigits); + result += this.faker.helpers.arrayElement(allowedDigits); } return result; diff --git a/test/fake.spec.ts b/test/fake.spec.ts index 16193cf72f9..2846b9ee9c0 100644 --- a/test/fake.spec.ts +++ b/test/fake.spec.ts @@ -11,7 +11,7 @@ describe('fake', () => { it('replaces multiple tokens with random values for methods with no parameters', () => { const name = faker.fake( - '{{random.arrayElement}}{{random.arrayElement}}{{random.arrayElement}}' + '{{helpers.arrayElement}}{{helpers.arrayElement}}{{helpers.arrayElement}}' ); expect(name).toMatch(/[abc]{3}/); }); @@ -24,7 +24,7 @@ describe('fake', () => { it('replaces a token with a random value for a method with an array parameter', () => { const arr = ['one', 'two', 'three']; const random = faker.fake( - '{{random.arrayElement(["one", "two", "three"])}}' + '{{helpers.arrayElement(["one", "two", "three"])}}' ); expect(arr).toContain(random); }); diff --git a/test/unique.spec.ts b/test/unique.spec.ts index e304092d5ba..909b26a4329 100644 --- a/test/unique.spec.ts +++ b/test/unique.spec.ts @@ -37,7 +37,7 @@ const MOCK_ARRAY = Array.from( ); function customMethod(prefix: string = ''): string { - const element = faker.random.arrayElement(MOCK_ARRAY); + const element = faker.helpers.arrayElement(MOCK_ARRAY); return `${prefix}${element}`; } @@ -150,7 +150,8 @@ Try adjusting maxTime or maxRetries parameters for faker.unique().`) // This test can be only executed once, because the unique function has a global state. // See: https://github.com/faker-js/faker/issues/371 it('should be possible to exclude results as array', () => { - const internetProtocol = () => faker.random.arrayElement(['https', 'http']); + const internetProtocol = () => + faker.helpers.arrayElement(['https', 'http']); const result = faker.unique(internetProtocol, [], { exclude: ['https'], });