Skip to content

Commit

Permalink
fix: replace deprecated arrayElement calls (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored May 2, 2022
1 parent 4efaff9 commit 42d6795
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' '));
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/fake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}/);
});
Expand All @@ -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);
});
Expand Down
5 changes: 3 additions & 2 deletions test/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down Expand Up @@ -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'],
});
Expand Down

0 comments on commit 42d6795

Please sign in to comment.