diff options
| author | Shinigami <[email protected]> | 2022-12-31 12:22:38 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-31 12:22:38 +0100 |
| commit | 2d93e6f14a5ba976f87b71202bc4e011e38ee823 (patch) | |
| tree | 09c569ac0aa920ba090667a938f67416134dca26 /src | |
| parent | e296ff2d4f6f1aa56fe7c80722257bd0b316b30a (diff) | |
| download | faker-2d93e6f14a5ba976f87b71202bc4e011e38ee823.tar.xz faker-2d93e6f14a5ba976f87b71202bc4e011e38ee823.zip | |
chore: turn on padding-line-between-statements (#1691)
Diffstat (limited to 'src')
32 files changed, 89 insertions, 0 deletions
diff --git a/src/faker.ts b/src/faker.ts index 7870d9c5..a5a6dbc2 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -62,6 +62,7 @@ export class Faker { `Locale ${locale} is not supported. You might want to add the requested locale first to \`faker.locales\`.` ); } + this._locale = locale; } @@ -75,6 +76,7 @@ export class Faker { `Locale ${localeFallback} is not supported. You might want to add the requested locale first to \`faker.locales\`.` ); } + this._localeFallback = localeFallback; } diff --git a/src/internal/mersenne/twister.ts b/src/internal/mersenne/twister.ts index f7757842..15189e67 100644 --- a/src/internal/mersenne/twister.ts +++ b/src/internal/mersenne/twister.ts @@ -129,6 +129,7 @@ export default class MersenneTwister19937 { sum = this.addition32(sum, this.unsigned32(n2 << i)); } } + return sum; } @@ -194,10 +195,12 @@ export default class MersenneTwister19937 { this.mt[0] = this.mt[this.N - 1]; i = 1; } + if (j >= keyLength) { j = 0; } } + for (k = this.N - 1; k; k--) { // mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941)) - i this.mt[i] = this.subtraction32( @@ -218,6 +221,7 @@ export default class MersenneTwister19937 { i = 1; } } + this.mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array } diff --git a/src/modules/animal/index.ts b/src/modules/animal/index.ts index 4a0af052..3cd9d71e 100644 --- a/src/modules/animal/index.ts +++ b/src/modules/animal/index.ts @@ -10,6 +10,7 @@ export class AnimalModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 167fc225..23e41694 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -58,9 +58,11 @@ function formatHexColor( hexColor = hexColor.toLowerCase(); break; } + if (options?.prefix) { hexColor = options.prefix + hexColor; } + return hexColor; } @@ -78,6 +80,7 @@ function toBinary(values: number[]): string { const bytes = new Uint8Array(buffer); return toBinary(Array.from(bytes)).split(' ').join(''); } + return (value >>> 0).toString(2).padStart(8, '0'); }); return binary.join(' '); @@ -161,6 +164,7 @@ export class ColorModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -320,11 +324,13 @@ export class ColorModule { color = formatHexColor(color, options); return color; } + color = Array.from({ length: 3 }, () => this.faker.number.int(255)); if (includeAlpha) { color.push(this.faker.number.float()); cssFunction = 'rgba'; } + return toColorFormat(color, format, cssFunction); } @@ -460,6 +466,7 @@ export class ColorModule { for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) { hsl.push(this.faker.number.float()); } + return toColorFormat( hsl, options?.format || 'decimal', @@ -537,6 +544,7 @@ export class ColorModule { for (let i = 0; i < 2; i++) { hsl.push(this.faker.number.float()); } + return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } @@ -598,6 +606,7 @@ export class ColorModule { this.faker.number.float({ min: -100, max: 100, precision: 0.0001 }) ); } + return toColorFormat(lab, options?.format || 'decimal', 'lab'); } @@ -669,6 +678,7 @@ export class ColorModule { for (let i = 0; i < 2; i++) { lch.push(this.faker.number.float({ max: 230, precision: 0.1 })); } + return toColorFormat(lch, options?.format || 'decimal', 'lch'); } @@ -742,6 +752,7 @@ export class ColorModule { if (options?.format === 'css' && !options?.space) { options = { ...options, space: 'sRGB' }; } + const color = Array.from({ length: 3 }, () => this.faker.number.float({ precision: 0.0001 }) ); diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index 99515463..4afa8819 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -10,6 +10,7 @@ export class CommerceModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/company/index.ts b/src/modules/company/index.ts index d4b181f6..70c40521 100644 --- a/src/modules/company/index.ts +++ b/src/modules/company/index.ts @@ -10,6 +10,7 @@ export class CompanyModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index d0985c53..922f4b0e 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -10,6 +10,7 @@ export class DatabaseModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index f9c2bac6..f62d3a68 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -11,6 +11,7 @@ export class DatatypeModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -211,14 +212,17 @@ export class DatatypeModule { probability: options, }; } + const { probability = 0.5 } = options; if (probability <= 0) { return false; } + if (probability >= 1) { // This check is required to avoid returning false when float() returns 1 return true; } + return this.faker.number.float() < probability; } diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index e8bdde28..c9c2eb5a 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -28,6 +28,7 @@ export class DateModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/finance/iban.ts b/src/modules/finance/iban.ts index 9b4780c5..2eb6fbba 100644 --- a/src/modules/finance/iban.ts +++ b/src/modules/finance/iban.ts @@ -1401,6 +1401,7 @@ const iban: Iban = { for (let i = 0; i < digitStr.length; i++) { m = (m * 10 + +digitStr[i]) % 97; } + return m; }, pattern10: ['01', '02', '03', '04', '05', '06', '07', '08', '09'], diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 9c34faa2..bc540afc 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -12,6 +12,7 @@ export class FinanceModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -34,6 +35,7 @@ export class FinanceModule { for (let i = 0; i < length; i++) { template += '#'; } + length = null; return this.faker.helpers.replaceSymbolWithNumber(template); } @@ -219,6 +221,7 @@ export class FinanceModule { this.faker.definitions.finance.currency )['symbol']; } + return symbol; } @@ -292,6 +295,7 @@ export class FinanceModule { const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats format = this.faker.helpers.arrayElement(formats); } + format = format.replace(/\//g, ''); return this.faker.helpers.replaceCreditCardSymbols(format); } @@ -338,6 +342,7 @@ export class FinanceModule { if (length < 1) { throw new FakerError('minimum length is 1'); } + return this.faker.string.numeric({ length, allowLeadingZeros: true }); } @@ -409,10 +414,13 @@ export class FinanceModule { s += this.faker.number.int(9); } } + c--; } + s = s.substring(0, count); } + let checksum: string | number = 98 - iban.mod97(iban.toDigitString(`${s}${ibanFormat.country}00`)); diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 31df2518..4d8e2cd0 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -28,6 +28,7 @@ export class GitModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/hacker/index.ts b/src/modules/hacker/index.ts index d842d74e..aadf839a 100644 --- a/src/modules/hacker/index.ts +++ b/src/modules/hacker/index.ts @@ -10,6 +10,7 @@ export class HackerModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index a1a845cd..3bdc2f68 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -14,6 +14,7 @@ export class HelpersModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -65,6 +66,7 @@ export class HelpersModule { str += string.charAt(i); } } + return str; } @@ -130,6 +132,7 @@ export class HelpersModule { str += string.charAt(i); } } + return str; } @@ -199,6 +202,7 @@ export class HelpersModule { max = min; min = tmp; } + repetitions = this.faker.number.int({ min, max }); string = string.slice(0, token.index) + @@ -206,6 +210,7 @@ export class HelpersModule { string.slice(token.index + token[0].length); token = string.match(RANGE_REP_REG); } + // Deal with repeat `{num}` token = string.match(REP_REG); while (token != null) { @@ -229,12 +234,14 @@ export class HelpersModule { max = min; min = tmp; } + string = string.slice(0, token.index) + this.faker.number.int({ min, max }).toString() + string.slice(token.index + token[0].length); token = string.match(RANGE_REG); } + return string; } @@ -320,6 +327,7 @@ export class HelpersModule { const array = Array.from(set); return this.shuffle(array).splice(0, length); } + const set = new Set<T>(); try { if (typeof source === 'function') { @@ -330,6 +338,7 @@ export class HelpersModule { } catch { // Ignore } + return Array.from(set); } @@ -356,6 +365,7 @@ export class HelpersModule { if (str == null) { return ''; } + for (const p in data) { const re = new RegExp(`{{${p}}}`, 'g'); const value = data[p]; @@ -365,6 +375,7 @@ export class HelpersModule { str = str.replace(re, value); } } + return str; } @@ -390,6 +401,7 @@ export class HelpersModule { if (this.faker.datatype.boolean(options)) { return callback(); } + return undefined; } @@ -740,6 +752,7 @@ export class HelpersModule { if (typeof numberOrRange === 'number') { return numberOrRange; } + return this.faker.number.int(numberOrRange); } diff --git a/src/modules/helpers/luhn-check.ts b/src/modules/helpers/luhn-check.ts index 9cfcc577..7d841878 100644 --- a/src/modules/helpers/luhn-check.ts +++ b/src/modules/helpers/luhn-check.ts @@ -35,8 +35,10 @@ function luhnChecksum(str: string): number { n = (n % 10) + 1; } } + sum += n; alternate = !alternate; } + return sum % 10; } diff --git a/src/modules/helpers/unique.ts b/src/modules/helpers/unique.ts index 31fddec8..fd240c2f 100644 --- a/src/modules/helpers/unique.ts +++ b/src/modules/helpers/unique.ts @@ -28,6 +28,7 @@ function defaultCompare( if (obj[key] === undefined) { return -1; } + return 0; } diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index cf59627d..6a2d6144 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -30,6 +30,7 @@ export class ImageModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index d2cc9f51..9691a9ff 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -13,6 +13,7 @@ export class Placeholder { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 2bac0668..8f5cc8bb 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -33,6 +33,7 @@ export class InternetModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -173,10 +174,12 @@ export class InternetModule { if (charMapping[char]) { return charMapping[char]; } + if (char.charCodeAt(0) < 0x80) { // Keep ASCII characters return char; } + // Final fallback return the Unicode char code value for Chinese, Japanese, Korean etc, base-36 encoded return char.charCodeAt(0).toString(36); }) @@ -224,6 +227,7 @@ export class InternetModule { ])}${lastName}${this.faker.number.int(99)}`; break; } + result = result.toString().replace(/'/g, ''); result = result.replace(/ /g, ''); return result; @@ -493,6 +497,7 @@ export class InternetModule { mac += validSep; } } + return mac; } @@ -536,6 +541,7 @@ export class InternetModule { if (prefix.length >= length) { return prefix; } + if (memorable) { if (prefix.match(consonant)) { pattern = vowel; @@ -543,16 +549,20 @@ export class InternetModule { pattern = consonant; } } + const n = this.faker.number.int(94) + 33; let char = String.fromCharCode(n); if (memorable) { char = char.toLowerCase(); } + if (!char.match(pattern)) { return _password(length, memorable, pattern, prefix); } + return _password(length, memorable, pattern, prefix + char); }; + return _password(len, memorable, pattern, prefix); } diff --git a/src/modules/internet/user-agent.ts b/src/modules/internet/user-agent.ts index 005e31e6..40bc025f 100644 --- a/src/modules/internet/user-agent.ts +++ b/src/modules/internet/user-agent.ts @@ -70,6 +70,7 @@ export function generate(faker: Faker): string { if (rand >= min && rand <= max) { break; } + min = min + obj[key]; } } @@ -219,6 +220,7 @@ export function generate(faker: Faker): string { for (let x = 0; x < dots; x++) { return_val += `.${faker.string.numeric({ allowLeadingZeros: true })}`; } + return return_val; }; diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index a53c11d7..4e25cbb9 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -10,6 +10,7 @@ export class LocationModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -39,6 +40,7 @@ export class LocationModule { format = this.faker.helpers.arrayElement(localeFormat); } } + return this.faker.helpers.replaceSymbols(format); } @@ -62,6 +64,7 @@ export class LocationModule { if (zipRange) { return String(this.faker.number.int(zipRange)); } + return this.zipCode(); } @@ -309,6 +312,7 @@ export class LocationModule { this.faker.definitions.location.direction ); } + return this.faker.helpers.arrayElement( this.faker.definitions.location.direction_abbr ); @@ -333,6 +337,7 @@ export class LocationModule { this.faker.definitions.location.direction.slice(0, 4) ); } + return this.faker.helpers.arrayElement( this.faker.definitions.location.direction_abbr.slice(0, 4) ); @@ -357,6 +362,7 @@ export class LocationModule { this.faker.definitions.location.direction.slice(4, 8) ); } + return this.faker.helpers.arrayElement( this.faker.definitions.location.direction_abbr.slice(4, 8) ); @@ -419,6 +425,7 @@ export class LocationModule { newCoordinate[0] = Math.sign(newCoordinate[0]) * 180 - newCoordinate[0]; newCoordinate[1] += 180; } + // Box longitude [-180°, 180°] newCoordinate[1] = (((newCoordinate[1] % 360) + 540) % 360) - 180; diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index 8e989ab6..37fb2cd7 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -11,6 +11,7 @@ export class LoremModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/music/index.ts b/src/modules/music/index.ts index 482ee2b0..e25ade68 100644 --- a/src/modules/music/index.ts +++ b/src/modules/music/index.ts @@ -10,6 +10,7 @@ export class MusicModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index d3f27453..54352fcd 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -12,6 +12,7 @@ export class NumberModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -56,6 +57,7 @@ export class NumberModule { `No integer value between ${min} and ${max} found.` ); } + throw new FakerError(`Max ${max} should be greater than min ${min}.`); } diff --git a/src/modules/person/index.ts b/src/modules/person/index.ts index af954d54..2e443c3e 100644 --- a/src/modules/person/index.ts +++ b/src/modules/person/index.ts @@ -65,6 +65,7 @@ export class PersonModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/phone/index.ts b/src/modules/phone/index.ts index 3e6d9815..437464bf 100644 --- a/src/modules/phone/index.ts +++ b/src/modules/phone/index.ts @@ -10,6 +10,7 @@ export class PhoneModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 1f4b499b..88f81c63 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -18,6 +18,7 @@ export class RandomModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -206,6 +207,7 @@ export class RandomModule { if (typeof options === 'number') { return this.faker.string.alpha(options); } + return this.faker.string.alpha({ length: options.count, casing: options.casing, diff --git a/src/modules/science/index.ts b/src/modules/science/index.ts index 51203a3a..7dba5d3d 100644 --- a/src/modules/science/index.ts +++ b/src/modules/science/index.ts @@ -39,6 +39,7 @@ export class ScienceModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 32f76b72..ab16dc34 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -91,6 +91,7 @@ export class StringModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -440,6 +441,7 @@ export class StringModule { const value = placeholder === 'x' ? random : (random & 0x3) | 0x8; return value.toString(16); }; + return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index ca78929e..202f9899 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -42,6 +42,7 @@ export class SystemModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index dd08b75f..9c9841b0 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -10,6 +10,7 @@ export class VehicleModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index 22a22a0b..414fe622 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -12,6 +12,7 @@ export class WordModule { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } + this[name] = this[name].bind(this); } } @@ -392,6 +393,7 @@ export class WordModule { if (typeof options === 'number') { options = { count: options }; } + const { count = { min: 1, max: 3 } } = options; return this.faker.helpers |
