Skip to content

Commit 42d6795

Browse files
authored
fix: replace deprecated arrayElement calls (#903)
1 parent 4efaff9 commit 42d6795

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/random.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ export class Random {
360360

361361
do {
362362
// randomly pick from the many faker methods that can generate words
363-
const randomWordMethod = this.arrayElement(wordMethods);
363+
const randomWordMethod = this.faker.helpers.arrayElement(wordMethods);
364364

365365
result = randomWordMethod();
366366
} while (!result || bannedChars.some((char) => result.includes(char)));
367367

368-
return this.arrayElement(result.split(' '));
368+
return this.faker.helpers.arrayElement(result.split(' '));
369369
}
370370

371371
/**
@@ -419,7 +419,7 @@ export class Random {
419419
* faker.random.locale() // 'el'
420420
*/
421421
locale(): string {
422-
return this.arrayElement(Object.keys(this.faker.locales));
422+
return this.faker.helpers.arrayElement(Object.keys(this.faker.locales));
423423
}
424424

425425
/**
@@ -485,7 +485,7 @@ export class Random {
485485

486486
let wholeString = '';
487487
for (let i = 0; i < count; i++) {
488-
wholeString += this.arrayElement(charsArray);
488+
wholeString += this.faker.helpers.arrayElement(charsArray);
489489
}
490490

491491
return upcase ? wholeString.toUpperCase() : wholeString;
@@ -558,7 +558,7 @@ export class Random {
558558

559559
let wholeString = '';
560560
for (let i = 0; i < count; i++) {
561-
wholeString += this.arrayElement(charsArray);
561+
wholeString += this.faker.helpers.arrayElement(charsArray);
562562
}
563563

564564
return wholeString;
@@ -610,13 +610,13 @@ export class Random {
610610
let result = '';
611611

612612
if (!allowLeadingZeros && !bannedDigits.includes('0')) {
613-
result += this.arrayElement(
613+
result += this.faker.helpers.arrayElement(
614614
allowedDigits.filter((digit) => digit !== '0')
615615
);
616616
}
617617

618618
while (result.length < length) {
619-
result += this.arrayElement(allowedDigits);
619+
result += this.faker.helpers.arrayElement(allowedDigits);
620620
}
621621

622622
return result;

test/fake.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('fake', () => {
1111

1212
it('replaces multiple tokens with random values for methods with no parameters', () => {
1313
const name = faker.fake(
14-
'{{random.arrayElement}}{{random.arrayElement}}{{random.arrayElement}}'
14+
'{{helpers.arrayElement}}{{helpers.arrayElement}}{{helpers.arrayElement}}'
1515
);
1616
expect(name).toMatch(/[abc]{3}/);
1717
});
@@ -24,7 +24,7 @@ describe('fake', () => {
2424
it('replaces a token with a random value for a method with an array parameter', () => {
2525
const arr = ['one', 'two', 'three'];
2626
const random = faker.fake(
27-
'{{random.arrayElement(["one", "two", "three"])}}'
27+
'{{helpers.arrayElement(["one", "two", "three"])}}'
2828
);
2929
expect(arr).toContain(random);
3030
});

test/unique.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const MOCK_ARRAY = Array.from(
3737
);
3838

3939
function customMethod(prefix: string = ''): string {
40-
const element = faker.random.arrayElement(MOCK_ARRAY);
40+
const element = faker.helpers.arrayElement(MOCK_ARRAY);
4141
return `${prefix}${element}`;
4242
}
4343

@@ -150,7 +150,8 @@ Try adjusting maxTime or maxRetries parameters for faker.unique().`)
150150
// This test can be only executed once, because the unique function has a global state.
151151
// See: https://github.com/faker-js/faker/issues/371
152152
it('should be possible to exclude results as array', () => {
153-
const internetProtocol = () => faker.random.arrayElement(['https', 'http']);
153+
const internetProtocol = () =>
154+
faker.helpers.arrayElement(['https', 'http']);
154155
const result = faker.unique(internetProtocol, [], {
155156
exclude: ['https'],
156157
});

0 commit comments

Comments
 (0)