diff options
| author | Piotr Kuczynski <[email protected]> | 2022-04-19 22:38:17 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-04-19 22:38:17 +0200 |
| commit | 00b9d4be4bff3b3f64edf163768af71c99bceed1 (patch) | |
| tree | 0fa0e76b036c11c38eea251a8c4d371875810eed /src | |
| parent | cb746cb466743a219c0e3845edb29527a06b0a35 (diff) | |
| download | faker-00b9d4be4bff3b3f64edf163768af71c99bceed1.tar.xz faker-00b9d4be4bff3b3f64edf163768af71c99bceed1.zip | |
chore: prefer string templates over string concatenation (#732)
Diffstat (limited to 'src')
| -rw-r--r-- | src/address.ts | 6 | ||||
| -rw-r--r-- | src/commerce.ts | 8 | ||||
| -rw-r--r-- | src/datatype.ts | 2 | ||||
| -rw-r--r-- | src/fake.ts | 4 | ||||
| -rw-r--r-- | src/finance.ts | 40 | ||||
| -rw-r--r-- | src/git.ts | 2 | ||||
| -rw-r--r-- | src/helpers.ts | 2 | ||||
| -rw-r--r-- | src/image.ts | 2 | ||||
| -rw-r--r-- | src/image_providers/lorempicsum.ts | 4 | ||||
| -rw-r--r-- | src/image_providers/lorempixel.ts | 2 | ||||
| -rw-r--r-- | src/image_providers/unsplash.ts | 4 | ||||
| -rw-r--r-- | src/internal/deprecated.ts | 2 | ||||
| -rw-r--r-- | src/internet.ts | 56 | ||||
| -rw-r--r-- | src/lorem.ts | 2 | ||||
| -rw-r--r-- | src/mersenne.ts | 4 | ||||
| -rw-r--r-- | src/name.ts | 8 | ||||
| -rw-r--r-- | src/system.ts | 13 | ||||
| -rw-r--r-- | src/utils/user-agent.ts | 51 |
18 files changed, 82 insertions, 130 deletions
diff --git a/src/address.ts b/src/address.ts index 83fa675b..e0fb6bf2 100644 --- a/src/address.ts +++ b/src/address.ts @@ -230,7 +230,7 @@ export class Address { let result: string; let suffix = this.streetSuffix(); if (suffix !== '') { - suffix = ' ' + suffix; + suffix = ` ${suffix}`; } switch (this.faker.datatype.number(1)) { @@ -381,7 +381,7 @@ export class Address { .number({ min, max, - precision: parseFloat((0.0).toPrecision(precision) + '1'), + precision: parseFloat(`${(0.0).toPrecision(precision)}1`), }) .toFixed(precision); } @@ -406,7 +406,7 @@ export class Address { .number({ max: max, min: min, - precision: parseFloat((0.0).toPrecision(precision) + '1'), + precision: parseFloat(`${(0.0).toPrecision(precision)}1`), }) .toFixed(precision); } diff --git a/src/commerce.ts b/src/commerce.ts index a94d7b85..9d0a5731 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -45,13 +45,7 @@ export class Commerce { * faker.commerce.productName() // 'Incredible Soft Gloves' */ productName(): string { - return ( - this.productAdjective() + - ' ' + - this.productMaterial() + - ' ' + - this.product() - ); + return `${this.productAdjective()} ${this.productMaterial()} ${this.product()}`; } /** diff --git a/src/datatype.ts b/src/datatype.ts index 29a4950b..90211b69 100644 --- a/src/datatype.ts +++ b/src/datatype.ts @@ -243,7 +243,7 @@ export class Datatype { ]); } - return '0x' + wholeString; + return `0x${wholeString}`; } /** diff --git a/src/fake.ts b/src/fake.ts index d478d01e..4a22e2ff 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -89,11 +89,11 @@ export class Fake { const parts = method.split('.'); if (this.faker[parts[0]] == null) { - throw new FakerError('Invalid module: ' + parts[0]); + throw new FakerError(`Invalid module: ${parts[0]}`); } if (this.faker[parts[0]][parts[1]] == null) { - throw new FakerError('Invalid method: ' + parts[0] + '.' + parts[1]); + throw new FakerError(`Invalid method: ${parts[0]}.${parts[1]}`); } // assign the function from the module.function namespace diff --git a/src/finance.ts b/src/finance.ts index ea4174d6..2214088d 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -96,7 +96,7 @@ export class Finance { let template = ''; for (let i = 0; i < length; i++) { - template = template + '#'; + template = `${template}#`; } //prefix with ellipsis @@ -332,7 +332,7 @@ export class Finance { : this.faker.random.arrayElement(iban.formats); if (!ibanFormat) { - throw new FakerError('Country code ' + countryCode + ' not supported.'); + throw new FakerError(`Country code ${countryCode} not supported.`); } let s = ''; @@ -387,20 +387,21 @@ export class Finance { bic(): string { const vowels = ['A', 'E', 'I', 'O', 'U']; const prob = this.faker.datatype.number(100); - return ( - this.faker.helpers.replaceSymbols('???') + - this.faker.random.arrayElement(vowels) + - this.faker.random.arrayElement(iban.iso3166) + - this.faker.helpers.replaceSymbols('?') + - '1' + - (prob < 10 + + return [ + this.faker.helpers.replaceSymbols('???'), + this.faker.random.arrayElement(vowels), + this.faker.random.arrayElement(iban.iso3166), + this.faker.helpers.replaceSymbols('?'), + '1', + prob < 10 ? this.faker.helpers.replaceSymbols( - '?' + this.faker.random.arrayElement(vowels) + '?' + `?${this.faker.random.arrayElement(vowels)}?` ) : prob < 40 ? this.faker.helpers.replaceSymbols('###') - : '') - ); + : '', + ].join(''); } /** @@ -418,18 +419,7 @@ export class Finance { const company = transaction.business; const card = this.mask(); const currency = this.currencyCode(); - return ( - transactionType + - ' transaction at ' + - company + - ' using card ending with ***' + - card + - ' for ' + - currency + - ' ' + - amount + - ' in account ***' + - account - ); + + return `${transactionType} transaction at ${company} using card ending with ***${card} for ${currency} ${amount} in account ***${account}`; } } @@ -42,7 +42,7 @@ export class Git { branch(): string { const noun = this.faker.hacker.noun().replace(' ', '-'); const verb = this.faker.hacker.verb().replace(' ', '-'); - return noun + '-' + verb; + return `${noun}-${verb}`; } /** diff --git a/src/helpers.ts b/src/helpers.ts index 5b26bd9d..be3ce8f5 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -481,7 +481,7 @@ export class Helpers { return ''; } for (const p in data) { - const re = new RegExp('{{' + p + '}}', 'g'); + const re = new RegExp(`{{${p}}}`, 'g'); const value = data[p]; if (typeof value === 'string') { str = str.replace(re, value); diff --git a/src/image.ts b/src/image.ts index 2f4444ab..8aeab602 100644 --- a/src/image.ts +++ b/src/image.ts @@ -104,7 +104,7 @@ export class Image { } let url = `${protocol}loremflickr.com/${width}/${height}`; if (category != null) { - url += '/' + category; + url += `/${category}`; } if (randomize) { diff --git a/src/image_providers/lorempicsum.ts b/src/image_providers/lorempicsum.ts index 9fed741e..f28b1e8f 100644 --- a/src/image_providers/lorempicsum.ts +++ b/src/image_providers/lorempicsum.ts @@ -104,7 +104,7 @@ export class LoremPicsum { let url = 'https://picsum.photos'; if (seed) { - url += '/seed/' + seed; + url += `/seed/${seed}`; } url += `/${width}/${height}`; @@ -114,7 +114,7 @@ export class LoremPicsum { } if (grayscale) { - return url + '?grayscale'; + return `${url}?grayscale`; } if (blur) { diff --git a/src/image_providers/lorempixel.ts b/src/image_providers/lorempixel.ts index 310ba45a..914f1066 100644 --- a/src/image_providers/lorempixel.ts +++ b/src/image_providers/lorempixel.ts @@ -67,7 +67,7 @@ export class Lorempixel { let url = `https://lorempixel.com/${width}/${height}`; if (category != null) { - url += '/' + category; + url += `/${category}`; } if (randomize) { diff --git a/src/image_providers/unsplash.ts b/src/image_providers/unsplash.ts index dd8abd37..994b2a04 100644 --- a/src/image_providers/unsplash.ts +++ b/src/image_providers/unsplash.ts @@ -59,7 +59,7 @@ export class Unsplash { let url = 'https://source.unsplash.com'; if (category != null) { - url += '/category/' + category; + url += `/category/${category}`; } url += `/${width}x${height}`; @@ -67,7 +67,7 @@ export class Unsplash { if (keyword != null) { const keywordFormat = /^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$/; if (keywordFormat.test(keyword)) { - url += '?' + keyword; + url += `?${keyword}`; } } diff --git a/src/internal/deprecated.ts b/src/internal/deprecated.ts index 7f20c24f..ab67b60a 100644 --- a/src/internal/deprecated.ts +++ b/src/internal/deprecated.ts @@ -25,5 +25,5 @@ export function deprecated(opts: DeprecatedOptions): void { message += `. Please use ${opts.proposed} instead`; } - console.warn(message + '.'); + console.warn(`${message}.`); } diff --git a/src/internet.ts b/src/internet.ts index 7e9b72a4..fa4ae6e8 100644 --- a/src/internet.ts +++ b/src/internet.ts @@ -67,9 +67,11 @@ export class Internet { this.faker.random.arrayElement( this.faker.definitions.internet.free_email ); + let localPart: string = this.faker.helpers.slugify( this.userName(firstName, lastName) ); + if (options?.allowSpecialCharacters) { const usernameChars: string[] = '._-'.split(''); const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split(''); @@ -78,6 +80,7 @@ export class Internet { this.faker.random.arrayElement(specialChars) ); } + return `${localPart}@${provider}`; } @@ -184,7 +187,7 @@ export class Internet { * faker.internet.url() // 'https://remarkable-hackwork.info' */ url(): string { - return this.protocol() + '://' + this.domainName(); + return `${this.protocol()}://${this.domainName()}`; } /** @@ -194,7 +197,7 @@ export class Internet { * faker.internet.domainName() // 'slow-timer.info' */ domainName(): string { - return this.domainWord() + '.' + this.domainSuffix(); + return `${this.domainWord()}.${this.domainSuffix()}`; } /** @@ -218,7 +221,7 @@ export class Internet { * faker.internet.domainWord() // 'weird-cytoplasm' */ domainWord(): string { - return (this.faker.word.adjective() + '-' + this.faker.word.noun()) + return `${this.faker.word.adjective()}-${this.faker.word.noun()}` .replace(/([\\~#&*{}/:<>?|\"'])/gi, '') .replace(/\s/g, '-') .replace(/-{2,}/g, '-') @@ -316,41 +319,34 @@ export class Internet { } /** - * Generates a random css hex color code. + * Generates a random css hex color code in aesthetically pleasing color palette. * - * @param baseRed255 The optional base red. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`. - * @param baseGreen255 The optional base green. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`. - * @param baseBlue255 The optional base blue. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`. + * Based on + * http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette + * + * @param redBase The optional base red in range between `0` and `255`. Defaults to `0`. + * @param greenBase The optional base green in range between `0` and `255`. Defaults to `0`. + * @param blueBase The optional base blue in range between `0` and `255`. Defaults to `0`. * * @example * faker.internet.color() // '#30686e' * faker.internet.color(100, 100, 100) // '#4e5f8b' */ color( - baseRed255: number = 0, - baseGreen255: number = 0, - baseBlue255: number = 0 + redBase: number = 0, + greenBase: number = 0, + blueBase: number = 0 ): string { - // based on awesome response : http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette - const red = Math.floor((this.faker.datatype.number(256) + baseRed255) / 2); - const green = Math.floor( - (this.faker.datatype.number(256) + baseGreen255) / 2 - ); - const blue = Math.floor( - (this.faker.datatype.number(256) + baseBlue255) / 2 - ); - const redStr = red.toString(16); - const greenStr = green.toString(16); - const blueStr = blue.toString(16); - return ( - '#' + - (redStr.length === 1 ? '0' : '') + - redStr + - (greenStr.length === 1 ? '0' : '') + - greenStr + - (blueStr.length === 1 ? '0' : '') + - blueStr - ); + const colorFromBase = (base: number): string => + Math.floor((this.faker.datatype.number(256) + base) / 2) + .toString(16) + .padStart(2, '0'); + + const red = colorFromBase(redBase); + const green = colorFromBase(greenBase); + const blue = colorFromBase(blueBase); + + return `#${red}${green}${blue}`; } /** diff --git a/src/lorem.ts b/src/lorem.ts index 103dd6a7..14cc2f6b 100644 --- a/src/lorem.ts +++ b/src/lorem.ts @@ -67,7 +67,7 @@ export class Lorem { } const sentence = this.words(wordCount); - return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.'; + return `${sentence.charAt(0).toUpperCase() + sentence.slice(1)}.`; } /** diff --git a/src/mersenne.ts b/src/mersenne.ts index 21f52f6c..c8f531f2 100644 --- a/src/mersenne.ts +++ b/src/mersenne.ts @@ -48,7 +48,7 @@ export class Mersenne { seed(S: number): void { if (typeof S !== 'number') { throw new FakerError( - 'seed(S) must take numeric argument; is ' + typeof S + `seed(S) must take numeric argument; is ${typeof S}` ); } @@ -64,7 +64,7 @@ export class Mersenne { seed_array(A: number[]): void { if (typeof A !== 'object') { throw new FakerError( - 'seed_array(A) must take array of numbers; is ' + typeof A + `seed_array(A) must take array of numbers; is ${typeof A}` ); } diff --git a/src/name.ts b/src/name.ts index b96eb8db..41a6075a 100644 --- a/src/name.ts +++ b/src/name.ts @@ -213,18 +213,18 @@ export class Name { case 0: prefix = this.prefix(gender); if (prefix) { - return prefix + ' ' + firstName + ' ' + lastName; + return `${prefix} ${firstName} ${lastName}`; } // TODO @Shinigami92 2022-01-21: Not sure if this fallthrough is wanted // eslint-disable-next-line no-fallthrough case 1: suffix = this.suffix(); if (suffix) { - return firstName + ' ' + lastName + ' ' + suffix; + return `${firstName} ${lastName} ${suffix}`; } } - return firstName + ' ' + lastName; + return `${firstName} ${lastName}`; } /** @@ -309,7 +309,7 @@ export class Name { * faker.name.jobTitle() // 'Global Accounts Engineer' */ jobTitle(): string { - return this.jobDescriptor() + ' ' + this.jobArea() + ' ' + this.jobType(); + return `${this.jobDescriptor()} ${this.jobArea()} ${this.jobType()}`; } /** diff --git a/src/system.ts b/src/system.ts index 16ddd876..05d0bf44 100644 --- a/src/system.ts +++ b/src/system.ts @@ -54,9 +54,9 @@ export class System { * faker.system.fileName() // 'self_enabling_accountability_toys.kpt' */ fileName(): string { - let str = this.faker.random.words(); - str = str.toLowerCase().replace(/\W/g, '_') + '.' + this.fileExt(); - return str; + const str = this.faker.random.words().toLowerCase().replace(/\W/g, '_'); + + return `${str}.${this.fileExt()}`; } /** @@ -68,10 +68,9 @@ export class System { * faker.system.commonFileName('txt') // 'global_borders_wyoming.txt' */ commonFileName(ext?: string): string { - let str = this.faker.random.words(); - str = str.toLowerCase().replace(/\W/g, '_'); - str += '.' + (ext || this.commonFileExt()); - return str; + const str = this.faker.random.words().toLowerCase().replace(/\W/g, '_'); + + return `${str}.${ext || this.commonFileExt()}`; } /** diff --git a/src/utils/user-agent.ts b/src/utils/user-agent.ts index 7f7e8cbb..5ff18c2a 100644 --- a/src/utils/user-agent.ts +++ b/src/utils/user-agent.ts @@ -267,23 +267,19 @@ export function generate(faker: Faker): string { firefox(arch: Arch): string { //https://developer.mozilla.org/en-US/docs/Gecko_user_agent_string_reference const firefox_ver = `${rnd(5, 15)}${randomRevision(2)}`, - gecko_ver = 'Gecko/20100101 Firefox/' + firefox_ver, + gecko_ver = `Gecko/20100101 Firefox/${firefox_ver}`, proc = randomProc(arch), os_ver = arch === 'win' - ? '(Windows NT ' + version_string.nt() + (proc ? `; ${proc}` : '') + ? `(Windows NT ${version_string.nt()}${proc ? `; ${proc}` : ''}` : arch === 'mac' ? `(Macintosh; ${proc} Mac OS X ${version_string.osx()}` : `(X11; Linux ${proc}`; - return ( - 'Mozilla/5.0 ' + - os_ver + - '; rv:' + - firefox_ver.slice(0, -2) + - ') ' + - gecko_ver - ); + return `Mozilla/5.0 ${os_ver}; rv:${firefox_ver.slice( + 0, + -2 + )}) ${gecko_ver}`; }, iexplorer(): string { @@ -299,18 +295,13 @@ export function generate(faker: Faker): string { //http://msdn.microsoft.com/en-us/library/ie/ms537503(v=vs.85).aspx return `Mozilla/5.0 (compatible; MSIE ${ver}.0; Windows NT ${version_string.nt()}; Trident/${version_string.trident()}${ - rnd(0, 1) === 1 ? '; .NET CLR ' + version_string.net() : '' + rnd(0, 1) === 1 ? `; .NET CLR ${version_string.net()}` : '' })`; }, opera(arch: Arch): string { //http://www.opera.com/docs/history/ - const presto_ver = - ' Presto/' + - version_string.presto() + - ' Version/' + - version_string.presto2() + - ')', + const presto_ver = ` Presto/${version_string.presto()} Version/${version_string.presto2()})`, os_ver = arch === 'win' ? `(Windows NT ${version_string.nt()}; U; ${randomLang()}${presto_ver}` @@ -329,18 +320,9 @@ export function generate(faker: Faker): string { ? `(Macintosh; ${randomProc('mac')} Mac OS X ${version_string.osx( '_' )} rv:${rnd(2, 6)}.0; ${randomLang()}) ` - : '(Windows; U; Windows NT ' + version_string.nt() + ')'; + : `(Windows; U; Windows NT ${version_string.nt()})`; - return ( - 'Mozilla/5.0 ' + - os_ver + - 'AppleWebKit/' + - safari + - ' (KHTML, like Gecko) Version/' + - ver + - ' Safari/' + - safari - ); + return `Mozilla/5.0 ${os_ver}AppleWebKit/${safari} (KHTML, like Gecko) Version/${ver} Safari/${safari}`; }, chrome(arch: Arch): string { @@ -351,19 +333,10 @@ export function generate(faker: Faker): string { '_' )}) ` : arch === 'win' - ? '(Windows; U; Windows NT ' + version_string.nt() + ')' + ? `(Windows; U; Windows NT ${version_string.nt()})` : `(X11; Linux ${randomProc(arch)}`; - return ( - 'Mozilla/5.0 ' + - os_ver + - ' AppleWebKit/' + - safari + - ' (KHTML, like Gecko) Chrome/' + - version_string.chrome() + - ' Safari/' + - safari - ); + return `Mozilla/5.0 ${os_ver} AppleWebKit/${safari} (KHTML, like Gecko) Chrome/${version_string.chrome()} Safari/${safari}`; }, }; |
