Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace deprecated arrayElement calls #903

Merged
merged 1 commit into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/random.ts
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
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
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