Skip to content

Commit

Permalink
Merge branch 'main' into docs/nice-string-literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Apr 8, 2022
2 parents 28eeb06 + 753ab66 commit 30650ae
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 88 deletions.
6 changes: 3 additions & 3 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Address {
if (zipRange) {
return String(this.faker.datatype.number(zipRange));
}
return this.faker.address.zipCode();
return this.zipCode();
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export class Address {
*/
streetName(): string {
let result: string;
let suffix = this.faker.address.streetSuffix();
let suffix = this.streetSuffix();
if (suffix !== '') {
suffix = ' ' + suffix;
}
Expand Down Expand Up @@ -498,7 +498,7 @@ export class Address {
): [string, string] {
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.faker.address.latitude(), this.faker.address.longitude()];
return [this.latitude(), this.longitude()];
}

radius = radius || 10.0;
Expand Down
6 changes: 3 additions & 3 deletions src/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export class Commerce {
*/
productName(): string {
return (
this.faker.commerce.productAdjective() +
this.productAdjective() +
' ' +
this.faker.commerce.productMaterial() +
this.productMaterial() +
' ' +
this.faker.commerce.product()
this.product()
);
}

Expand Down
15 changes: 6 additions & 9 deletions src/company.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { Faker } from '.';
import type { Fake } from './fake';

let f: Fake['fake'];

/**
* Module to generate company related entries.
*/
export class Company {
constructor(private readonly faker: Faker) {
f = this.faker.fake;

// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Company.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
Expand Down Expand Up @@ -49,7 +44,7 @@ export class Company {
format = this.faker.datatype.number(formats.length - 1);
}

return f(formats[format]);
return this.faker.fake(formats[format]);
}

/**
Expand All @@ -59,7 +54,7 @@ export class Company {
* faker.company.companySuffix() // 'and Sons'
*/
companySuffix(): string {
return this.faker.random.arrayElement(this.faker.company.suffixes());
return this.faker.random.arrayElement(this.suffixes());
}

/**
Expand All @@ -69,7 +64,7 @@ export class Company {
* faker.company.catchPhrase() // 'Upgradable systematic flexibility'
*/
catchPhrase(): string {
return f(
return this.faker.fake(
'{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}'
);
}
Expand All @@ -81,7 +76,9 @@ export class Company {
* faker.company.bs() // 'cultivate synergistic e-markets'
*/
bs(): string {
return f('{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}');
return this.faker.fake(
'{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}'
);
}

/**
Expand Down
23 changes: 8 additions & 15 deletions src/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Datatype {
if (opts.precision == null) {
opts.precision = 0.01;
}
return this.faker.datatype.number(opts);
return this.number(opts);
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ export class Datatype {
max = Date.UTC(2100, 0);
}

return new Date(this.faker.datatype.number({ min, max }));
return new Date(this.number({ min, max }));
}

/**
Expand All @@ -147,9 +147,7 @@ export class Datatype {
let returnString = '';

for (let i = 0; i < length; i++) {
returnString += String.fromCharCode(
this.faker.datatype.number(charCodeOption)
);
returnString += String.fromCharCode(this.number(charCodeOption));
}

return returnString;
Expand All @@ -164,7 +162,7 @@ export class Datatype {
uuid(): string {
const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
const replacePlaceholders = (placeholder) => {
const random = this.faker.datatype.number({ min: 0, max: 15 });
const random = this.number({ min: 0, max: 15 });
const value = placeholder === 'x' ? random : (random & 0x3) | 0x8;
return value.toString(16);
};
Expand All @@ -178,7 +176,7 @@ export class Datatype {
* faker.datatype.boolean() // false
*/
boolean(): boolean {
return !!this.faker.datatype.number(1);
return !!this.number(1);
}

/**
Expand Down Expand Up @@ -258,9 +256,7 @@ export class Datatype {
const returnObject: Record<string, string | number> = {};

properties.forEach((prop) => {
returnObject[prop] = this.faker.datatype.boolean()
? this.faker.datatype.string()
: this.faker.datatype.number();
returnObject[prop] = this.boolean() ? this.string() : this.number();
});

return JSON.stringify(returnObject);
Expand All @@ -277,9 +273,7 @@ export class Datatype {
*/
array(length = 10): Array<string | number> {
return Array.from<string | number>({ length }).map(() =>
this.faker.datatype.boolean()
? this.faker.datatype.string()
: this.faker.datatype.number()
this.boolean() ? this.string() : this.number()
);
}

Expand All @@ -297,8 +291,7 @@ export class Datatype {
*/
bigInt(value?: string | number | bigint | boolean): bigint {
if (value === undefined) {
value =
Math.floor(this.faker.datatype.number() * 99999999999) + 10000000000;
value = Math.floor(this.number() * 99999999999) + 10000000000;
}

return BigInt(value);
Expand Down
4 changes: 2 additions & 2 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ export class Finance {
const amount = transaction.amount;
const transactionType = transaction.type;
const company = transaction.business;
const card = this.faker.finance.mask();
const currency = this.faker.finance.currencyCode();
const card = this.mask();
const currency = this.currencyCode();
return (
transactionType +
' transaction at ' +
Expand Down
16 changes: 8 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export class Helpers {
return sum % 10;
};

string = this.faker.helpers.regexpStyleStringParse(string); // replace [4-9] with a random number in range etc...
string = this.faker.helpers.replaceSymbolWithNumber(string, symbol); // replace ### with random numbers
string = this.regexpStyleStringParse(string); // replace [4-9] with a random number in range etc...
string = this.replaceSymbolWithNumber(string, symbol); // replace ### with random numbers

const numberList = string
.replace(/\D/g, '')
Expand Down Expand Up @@ -364,7 +364,7 @@ export class Helpers {
repetitions = this.faker.datatype.number({ min: min, max: max });
string =
string.slice(0, token.index) +
this.faker.helpers.repeatString(token[1], repetitions) +
this.repeatString(token[1], repetitions) +
string.slice(token.index + token[0].length);
token = string.match(RANGE_REP_REG);
}
Expand All @@ -374,7 +374,7 @@ export class Helpers {
repetitions = parseInt(token[2]);
string =
string.slice(0, token.index) +
this.faker.helpers.repeatString(token[1], repetitions) +
this.repeatString(token[1], repetitions) +
string.slice(token.index + token[0].length);
token = string.match(REP_REG);
}
Expand Down Expand Up @@ -444,7 +444,7 @@ export class Helpers {
if (Array.isArray(source)) {
const set = new Set<T>(source);
const array = Array.from(set);
return this.faker.helpers.shuffle(array).splice(0, length);
return this.shuffle(array).splice(0, length);
}
const set = new Set<T>();
try {
Expand Down Expand Up @@ -559,9 +559,9 @@ export class Helpers {
},
],
accountHistory: [
this.faker.helpers.createTransaction(),
this.faker.helpers.createTransaction(),
this.faker.helpers.createTransaction(),
this.createTransaction(),
this.createTransaction(),
this.createTransaction(),
],
};
}
Expand Down
26 changes: 13 additions & 13 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Image {
* faker.image.abstract(1234, 2345, true) // 'http://loremflickr.com/1234/2345/abstract?56789'
*/
abstract(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'abstract', randomize);
return this.imageUrl(width, height, 'abstract', randomize);
}

/**
Expand All @@ -143,7 +143,7 @@ export class Image {
* faker.image.animals(1234, 2345, true) // 'http://loremflickr.com/1234/2345/animals?56789'
*/
animals(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'animals', randomize);
return this.imageUrl(width, height, 'animals', randomize);
}

/**
Expand All @@ -159,7 +159,7 @@ export class Image {
* faker.image.business(1234, 2345, true) // 'http://loremflickr.com/1234/2345/business?56789'
*/
business(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'business', randomize);
return this.imageUrl(width, height, 'business', randomize);
}

/**
Expand All @@ -175,7 +175,7 @@ export class Image {
* faker.image.cats(1234, 2345, true) // 'http://loremflickr.com/1234/2345/cats?56789'
*/
cats(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'cats', randomize);
return this.imageUrl(width, height, 'cats', randomize);
}

/**
Expand All @@ -191,7 +191,7 @@ export class Image {
* faker.image.city(1234, 2345, true) // 'http://loremflickr.com/1234/2345/city?56789'
*/
city(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'city', randomize);
return this.imageUrl(width, height, 'city', randomize);
}

/**
Expand All @@ -207,7 +207,7 @@ export class Image {
* faker.image.food(1234, 2345, true) // 'http://loremflickr.com/1234/2345/food?56789'
*/
food(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'food', randomize);
return this.imageUrl(width, height, 'food', randomize);
}

/**
Expand All @@ -223,7 +223,7 @@ export class Image {
* faker.image.nightlife(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nightlife?56789'
*/
nightlife(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'nightlife', randomize);
return this.imageUrl(width, height, 'nightlife', randomize);
}

/**
Expand All @@ -239,7 +239,7 @@ export class Image {
* faker.image.fashion(1234, 2345, true) // 'http://loremflickr.com/1234/2345/fashion?56789'
*/
fashion(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'fashion', randomize);
return this.imageUrl(width, height, 'fashion', randomize);
}

/**
Expand All @@ -255,7 +255,7 @@ export class Image {
* faker.image.people(1234, 2345, true) // 'http://loremflickr.com/1234/2345/people?56789'
*/
people(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'people', randomize);
return this.imageUrl(width, height, 'people', randomize);
}

/**
Expand All @@ -271,7 +271,7 @@ export class Image {
* faker.image.nature(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nature?56789'
*/
nature(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'nature', randomize);
return this.imageUrl(width, height, 'nature', randomize);
}

/**
Expand All @@ -287,7 +287,7 @@ export class Image {
* faker.image.sports(1234, 2345, true) // 'http://loremflickr.com/1234/2345/sports?56789'
*/
sports(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'sports', randomize);
return this.imageUrl(width, height, 'sports', randomize);
}

/**
Expand All @@ -303,7 +303,7 @@ export class Image {
* faker.image.technics(1234, 2345, true) // 'http://loremflickr.com/1234/2345/technics?56789'
*/
technics(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'technics', randomize);
return this.imageUrl(width, height, 'technics', randomize);
}

/**
Expand All @@ -319,7 +319,7 @@ export class Image {
* faker.image.transport(1234, 2345, true) // 'http://loremflickr.com/1234/2345/transport?56789'
*/
transport(width?: number, height?: number, randomize?: boolean): string {
return this.faker.image.imageUrl(width, height, 'transport', randomize);
return this.imageUrl(width, height, 'transport', randomize);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Internet {
this.faker.definitions.internet.free_email
);
let localPart: string = this.faker.helpers.slugify(
this.faker.internet.userName(firstName, lastName)
this.userName(firstName, lastName)
);
if (options?.allowSpecialCharacters) {
const usernameChars: string[] = '._-'.split('');
Expand Down Expand Up @@ -172,9 +172,7 @@ export class Internet {
* faker.internet.url() // 'https://remarkable-hackwork.info'
*/
url(): string {
return (
this.faker.internet.protocol() + '://' + this.faker.internet.domainName()
);
return this.protocol() + '://' + this.domainName();
}

/**
Expand All @@ -184,11 +182,7 @@ export class Internet {
* faker.internet.domainName() // 'slow-timer.info'
*/
domainName(): string {
return (
this.faker.internet.domainWord() +
'.' +
this.faker.internet.domainSuffix()
);
return this.domainWord() + '.' + this.domainSuffix();
}

/**
Expand Down
Loading

0 comments on commit 30650ae

Please sign in to comment.