aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-05-01 15:22:39 +0200
committerGitHub <[email protected]>2022-05-01 13:22:39 +0000
commitc093e4c300a5566faafc85289d2588742db25c5d (patch)
tree4e30ceff4ae4008004652dbf1c24e19b16f1d9c5 /src
parent1851ecab1e33b6266bb4b4614c814a7674099d01 (diff)
downloadfaker-c093e4c300a5566faafc85289d2588742db25c5d.tar.xz
faker-c093e4c300a5566faafc85289d2588742db25c5d.zip
refactor: move some random methods to helpers (#892)
Co-authored-by: ST-DDT <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/address.ts42
-rw-r--r--src/animal.ts32
-rw-r--r--src/commerce.ts12
-rw-r--r--src/company.ts14
-rw-r--r--src/database.ts10
-rw-r--r--src/datatype.ts2
-rw-r--r--src/date.ts4
-rw-r--r--src/fake.ts2
-rw-r--r--src/finance.ts32
-rw-r--r--src/git.ts4
-rw-r--r--src/hacker.ts12
-rw-r--r--src/helpers.ts85
-rw-r--r--src/image.ts2
-rw-r--r--src/image_providers/lorempixel.ts2
-rw-r--r--src/internet.ts24
-rw-r--r--src/lorem.ts4
-rw-r--r--src/music.ts2
-rw-r--r--src/name.ts18
-rw-r--r--src/phone.ts2
-rw-r--r--src/random.ts48
-rw-r--r--src/system.ts14
-rw-r--r--src/utils/user-agent.ts2
-rw-r--r--src/vehicle.ts12
-rw-r--r--src/word.ts14
24 files changed, 229 insertions, 166 deletions
diff --git a/src/address.ts b/src/address.ts
index cfac2806..e077acaa 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 11bfad52..6a1d10a2 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 9d0a5731..754fe73a 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 67d70199..c2d72260 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 8b80c927..770b4b04 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 90211b69..6f277f54 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 c10a80f8..bb65ce4e 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/fake.ts b/src/fake.ts
index 4a22e2ff..4b6bc788 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/finance.ts b/src/finance.ts
index 7211de3b..48ce550c 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 c1d02147..c0c98062 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 3ec91820..49c63602 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 a04c5779..29b62a51 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'
@@ -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.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.random.arrayElement(alpha);
+ str += this.arrayElement(alpha);
} else if (string.charAt(i) === '*') {
str += this.faker.datatype.boolean()
- ? this.faker.random.arrayElement(alpha)
+ ? this.arrayElement(alpha)
: this.faker.datatype.number(9);
} else {
str += string.charAt(i);
@@ -686,9 +686,7 @@ export class Helpers {
name: [this.faker.finance.accountName(), this.faker.finance.mask()].join(
' '
),
- type: this.faker.random.arrayElement(
- this.faker.definitions.finance.transaction_type
- ),
+ type: this.arrayElement(this.faker.definitions.finance.transaction_type),
account: this.faker.finance.account(),
};
}
@@ -727,7 +725,7 @@ export class Helpers {
*/
objectKey<T extends Record<string, unknown>>(object: T): keyof T {
const array: Array<keyof T> = Object.keys(object);
- return this.faker.random.arrayElement(array);
+ return this.arrayElement(array);
}
/**
@@ -742,4 +740,71 @@ 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<T = string>(
+ // 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/893
+ array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
+ ): 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.
+ * @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<T>(
+ // 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/893
+ array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>,
+ 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/image.ts b/src/image.ts
index 9fe7f603..fc6a2a96 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 50e48ede..17cbc2f5 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 df830bca..6d018dc3 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<EmojiType>,
} = 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 14cc2f6b..a7a1b4d3 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 5c1945dc..d0e0a1a7 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 41a6075a..280e4b09 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<GenderType, number> =
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 c922e341..c8a1902d 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/random.ts b/src/random.ts
index 2b90db07..3cf4c0aa 100644
--- a/src/random.ts
+++ b/src/random.ts
@@ -104,16 +104,19 @@ export class Random {
* @example
* faker.random.arrayElement() // 'b'
* faker.random.arrayElement(['cat', 'dog', 'mouse']) // 'dog'
+ *
+ * @deprecated
*/
arrayElement<T = string>(
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
): 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);
}
/**
@@ -129,35 +132,20 @@ 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<T>(
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>,
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);
}
/**
diff --git a/src/system.ts b/src/system.ts
index 05d0bf44..90e56d55 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 5ff18c2a..b3438cbd 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 979631bd..ca0d11f2 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 f02a9a05..b586c0d0 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,