From 15d772fc0ab557c9480afa7d7f770920f2b91477 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 13 Jan 2022 21:04:28 +0100 Subject: [PATCH 1/5] feat: migrate address --- src/address.ts | 476 +++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 +- 2 files changed, 478 insertions(+), 1 deletion(-) create mode 100644 src/address.ts diff --git a/src/address.ts b/src/address.ts new file mode 100644 index 00000000000..d1276b66596 --- /dev/null +++ b/src/address.ts @@ -0,0 +1,476 @@ +import type { Faker } from '.'; + +export class Address { + readonly f; + readonly Helpers; + + constructor(private readonly faker: Faker) { + this.f = this.faker.fake; + this.Helpers = this.faker.helpers; + + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Address.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + + // TODO @Shinigami92 2022-01-13: Need better strategy + // @ts-expect-error + this.direction.schema = { + description: + 'Generates a direction. Use optional useAbbr bool to return abbreviation', + sampleResults: ['Northwest', 'South', 'SW', 'E'], + }; + // @ts-expect-error + this.cardinalDirection.schema = { + description: + 'Generates a cardinal direction. Use optional useAbbr boolean to return abbreviation', + sampleResults: ['North', 'South', 'E', 'W'], + }; + // @ts-expect-error + this.ordinalDirection.schema = { + description: + 'Generates an ordinal direction. Use optional useAbbr boolean to return abbreviation', + sampleResults: ['Northwest', 'Southeast', 'SW', 'NE'], + }; + } + + /** + * Generates random zipcode from format. If format is not specified, the + * locale's zip format is used. + * + * @method faker.address.zipCode + * @param {String} format + */ + zipCode(format) { + // if zip format is not specified, use the zip format defined for the locale + if (typeof format === 'undefined') { + var localeFormat = this.faker.definitions.address.postcode; + if (typeof localeFormat === 'string') { + format = localeFormat; + } else { + format = this.faker.random.arrayElement(localeFormat); + } + } + return this.Helpers.replaceSymbols(format); + } + + /** + * Generates random zipcode from state abbreviation. If state abbreviation is + * not specified, a random zip code is generated according to the locale's zip format. + * Only works for locales with postcode_by_state definition. If a locale does not + * have a postcode_by_state definition, a random zip code is generated according + * to the locale's zip format. + * + * @method faker.address.zipCodeByState + * @param {String} state + */ + zipCodeByState(state) { + var zipRange = this.faker.definitions.address.postcode_by_state[state]; + if (zipRange) { + return this.faker.datatype.number(zipRange); + } + return this.faker.address.zipCode(); + } + + /** + * Generates a random localized city name. The format string can contain any + * method provided by faker wrapped in `{{}}`, e.g. `{{name.firstName}}` in + * order to build the city name. + * + * If no format string is provided one of the following is randomly used: + * + * * `{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}` + * * `{{address.cityPrefix}} {{name.firstName}}` + * * `{{name.firstName}}{{address.citySuffix}}` + * * `{{name.lastName}}{{address.citySuffix}}` + * * `{{address.cityName}}` when city name is available + * + * @method faker.address.city + * @param {String} format + */ + city(format) { + var formats = [ + '{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}', + '{{address.cityPrefix}} {{name.firstName}}', + '{{name.firstName}}{{address.citySuffix}}', + '{{name.lastName}}{{address.citySuffix}}', + ]; + + if (!format && this.faker.definitions.address.city_name) { + formats.push('{{address.cityName}}'); + } + + if (typeof format !== 'number') { + format = this.faker.datatype.number(formats.length - 1); + } + + return this.f(formats[format]); + } + + /** + * Return a random localized city prefix + * + * @method faker.address.cityPrefix + */ + cityPrefix() { + return this.faker.random.arrayElement( + this.faker.definitions.address.city_prefix + ); + } + + /** + * Return a random localized city suffix + * + * @method faker.address.citySuffix + */ + citySuffix() { + return this.faker.random.arrayElement( + this.faker.definitions.address.city_suffix + ); + } + + /** + * Returns a random city name + * + * @method faker.address.cityName + */ + cityName() { + return this.faker.random.arrayElement( + this.faker.definitions.address.city_name + ); + } + + /** + * Returns a random localized street name + * + * @method faker.address.streetName + */ + streetName() { + var result; + var suffix = this.faker.address.streetSuffix(); + if (suffix !== '') { + suffix = ' ' + suffix; + } + + switch (this.faker.datatype.number(1)) { + case 0: + result = this.faker.name.lastName() + suffix; + break; + case 1: + result = this.faker.name.firstName() + suffix; + break; + } + return result; + } + + // + // TODO: change all these methods that accept a boolean to instead accept an options hash. + // + + /** + * Returns a random localized street address + * + * @method faker.address.streetAddress + * @param {Boolean} useFullAddress + */ + streetAddress(useFullAddress) { + if (useFullAddress === undefined) { + useFullAddress = false; + } + var address = ''; + switch (this.faker.datatype.number(2)) { + case 0: + address = + this.Helpers.replaceSymbolWithNumber('#####') + + ' ' + + this.faker.address.streetName(); + break; + case 1: + address = + this.Helpers.replaceSymbolWithNumber('####') + + ' ' + + this.faker.address.streetName(); + break; + case 2: + address = + this.Helpers.replaceSymbolWithNumber('###') + + ' ' + + this.faker.address.streetName(); + break; + } + return useFullAddress + ? address + ' ' + this.faker.address.secondaryAddress() + : address; + } + + /** + * streetSuffix + * + * @method faker.address.streetSuffix + */ + streetSuffix() { + return this.faker.random.arrayElement( + this.faker.definitions.address.street_suffix + ); + } + + /** + * streetPrefix + * + * @method faker.address.streetPrefix + */ + streetPrefix() { + return this.faker.random.arrayElement( + this.faker.definitions.address.street_prefix + ); + } + + /** + * secondaryAddress + * + * @method faker.address.secondaryAddress + */ + secondaryAddress() { + return this.Helpers.replaceSymbolWithNumber( + this.faker.random.arrayElement(['Apt. ###', 'Suite ###']) + ); + } + + /** + * county + * + * @method faker.address.county + */ + county() { + return this.faker.random.arrayElement( + this.faker.definitions.address.county + ); + } + + /** + * country + * + * @method faker.address.country + */ + country() { + return this.faker.random.arrayElement( + this.faker.definitions.address.country + ); + } + + /** + * countryCode + * + * @method faker.address.countryCode + * @param {string} alphaCode default alpha-2 + */ + countryCode(alphaCode) { + if (typeof alphaCode === 'undefined' || alphaCode === 'alpha-2') { + return this.faker.random.arrayElement( + this.faker.definitions.address.country_code + ); + } + + if (alphaCode === 'alpha-3') { + return this.faker.random.arrayElement( + this.faker.definitions.address.country_code_alpha_3 + ); + } + + return this.faker.random.arrayElement( + this.faker.definitions.address.country_code + ); + } + + /** + * state + * + * @method faker.address.state + * @param {Boolean} useAbbr + */ + state(useAbbr) { + return this.faker.random.arrayElement(this.faker.definitions.address.state); + } + + /** + * stateAbbr + * + * @method faker.address.stateAbbr + */ + stateAbbr() { + return this.faker.random.arrayElement( + this.faker.definitions.address.state_abbr + ); + } + + /** + * latitude + * + * @method faker.address.latitude + * @param {Double} max default is 90 + * @param {Double} min default is -90 + * @param {number} precision default is 4 + */ + latitude(max, min, precision) { + max = max || 90; + min = min || -90; + precision = precision || 4; + + return this.faker.datatype + .number({ + max: max, + min: min, + precision: parseFloat((0.0).toPrecision(precision) + '1'), + }) + .toFixed(precision); + } + + /** + * longitude + * + * @method faker.address.longitude + * @param {Double} max default is 180 + * @param {Double} min default is -180 + * @param {number} precision default is 4 + */ + longitude(max, min, precision) { + max = max || 180; + min = min || -180; + precision = precision || 4; + + return this.faker.datatype + .number({ + max: max, + min: min, + precision: parseFloat((0.0).toPrecision(precision) + '1'), + }) + .toFixed(precision); + } + + /** + * direction + * + * @method faker.address.direction + * @param {Boolean} useAbbr return direction abbreviation. defaults to false + */ + direction(useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return this.faker.random.arrayElement( + this.faker.definitions.address.direction + ); + } + return this.faker.random.arrayElement( + this.faker.definitions.address.direction_abbr + ); + } + + /** + * cardinal direction + * + * @method faker.address.cardinalDirection + * @param {Boolean} useAbbr return direction abbreviation. defaults to false + */ + cardinalDirection(useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return this.faker.random.arrayElement( + this.faker.definitions.address.direction.slice(0, 4) + ); + } + return this.faker.random.arrayElement( + this.faker.definitions.address.direction_abbr.slice(0, 4) + ); + } + + /** + * ordinal direction + * + * @method faker.address.ordinalDirection + * @param {Boolean} useAbbr return direction abbreviation. defaults to false + */ + ordinalDirection(useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return this.faker.random.arrayElement( + this.faker.definitions.address.direction.slice(4, 8) + ); + } + return this.faker.random.arrayElement( + this.faker.definitions.address.direction_abbr.slice(4, 8) + ); + } + + nearbyGPSCoordinate(coordinate, radius, isMetric) { + function randomFloat(min, max) { + return Math.random() * (max - min) + min; + } + function degreesToRadians(degrees) { + return degrees * (Math.PI / 180.0); + } + function radiansToDegrees(radians) { + return radians * (180.0 / Math.PI); + } + function kilometersToMiles(miles) { + return miles * 0.621371; + } + function coordinateWithOffset(coordinate, bearing, distance, isMetric) { + var R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html) + var d = isMetric ? distance : kilometersToMiles(distance); // Distance in km + + var lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians + var lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians + + var lat2 = Math.asin( + Math.sin(lat1) * Math.cos(d / R) + + Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing) + ); + + var lon2 = + lon1 + + Math.atan2( + Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1), + Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2) + ); + + // Keep longitude in range [-180, 180] + if (lon2 > degreesToRadians(180)) { + lon2 = lon2 - degreesToRadians(360); + } else if (lon2 < degreesToRadians(-180)) { + lon2 = lon2 + degreesToRadians(360); + } + + return [radiansToDegrees(lat2), radiansToDegrees(lon2)]; + } + + // If there is no coordinate, the best we can do is return a random GPS coordinate. + if (coordinate === undefined) { + return [faker.address.latitude(), faker.address.longitude()]; + } + radius = radius || 10.0; + isMetric = isMetric || false; + + // TODO: implement either a gaussian/uniform distribution of points in circular region. + // Possibly include param to function that allows user to choose between distributions. + + // This approach will likely result in a higher density of points near the center. + var randomCoord = coordinateWithOffset( + coordinate, + degreesToRadians(Math.random() * 360.0), + radius, + isMetric + ); + return [randomCoord[0].toFixed(4), randomCoord[1].toFixed(4)]; + } + + /** + * Return a random time zone + * + * @method faker.address.timeZone + */ + timeZone() { + return this.faker.random.arrayElement( + this.faker.definitions.address.time_zone + ); + } +} diff --git a/src/index.ts b/src/index.ts index 65dc62257b9..16a3b3475b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { Address } from './address'; import { Animal } from './animal'; import { Commerce } from './commerce'; import { Database } from './database'; @@ -180,7 +181,7 @@ export class Faker { datatype: Datatype = new Datatype(this); - readonly address = new (require('./address'))(this); + readonly address: Address = new Address(this); readonly animal: Animal = new Animal(this); readonly commerce: Commerce = new Commerce(this); readonly company = new (require('./company'))(this); From c9614b2c8604e7ddf9e9b3e6fe0802a175759085 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 13 Jan 2022 21:17:07 +0100 Subject: [PATCH 2/5] chore: add types --- src/address.ts | 153 +++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/src/address.ts b/src/address.ts index d1276b66596..c14a3472803 100644 --- a/src/address.ts +++ b/src/address.ts @@ -42,12 +42,12 @@ export class Address { * locale's zip format is used. * * @method faker.address.zipCode - * @param {String} format + * @param format */ - zipCode(format) { + zipCode(format?: string) { // if zip format is not specified, use the zip format defined for the locale if (typeof format === 'undefined') { - var localeFormat = this.faker.definitions.address.postcode; + const localeFormat = this.faker.definitions.address.postcode; if (typeof localeFormat === 'string') { format = localeFormat; } else { @@ -65,10 +65,10 @@ export class Address { * to the locale's zip format. * * @method faker.address.zipCodeByState - * @param {String} state + * @param state */ - zipCodeByState(state) { - var zipRange = this.faker.definitions.address.postcode_by_state[state]; + zipCodeByState(state: string) { + const zipRange = this.faker.definitions.address.postcode_by_state[state]; if (zipRange) { return this.faker.datatype.number(zipRange); } @@ -89,10 +89,10 @@ export class Address { * * `{{address.cityName}}` when city name is available * * @method faker.address.city - * @param {String} format + * @param format */ - city(format) { - var formats = [ + city(format?: string | number) { + const formats = [ '{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}', '{{address.cityPrefix}} {{name.firstName}}', '{{name.firstName}}{{address.citySuffix}}', @@ -115,7 +115,7 @@ export class Address { * * @method faker.address.cityPrefix */ - cityPrefix() { + cityPrefix(): string { return this.faker.random.arrayElement( this.faker.definitions.address.city_prefix ); @@ -126,7 +126,7 @@ export class Address { * * @method faker.address.citySuffix */ - citySuffix() { + citySuffix(): string { return this.faker.random.arrayElement( this.faker.definitions.address.city_suffix ); @@ -137,7 +137,7 @@ export class Address { * * @method faker.address.cityName */ - cityName() { + cityName(): string { return this.faker.random.arrayElement( this.faker.definitions.address.city_name ); @@ -148,9 +148,9 @@ export class Address { * * @method faker.address.streetName */ - streetName() { - var result; - var suffix = this.faker.address.streetSuffix(); + streetName(): string { + let result: string; + let suffix = this.faker.address.streetSuffix(); if (suffix !== '') { suffix = ' ' + suffix; } @@ -174,13 +174,10 @@ export class Address { * Returns a random localized street address * * @method faker.address.streetAddress - * @param {Boolean} useFullAddress + * @param useFullAddress */ - streetAddress(useFullAddress) { - if (useFullAddress === undefined) { - useFullAddress = false; - } - var address = ''; + streetAddress(useFullAddress: boolean = false): string { + let address = ''; switch (this.faker.datatype.number(2)) { case 0: address = @@ -211,7 +208,7 @@ export class Address { * * @method faker.address.streetSuffix */ - streetSuffix() { + streetSuffix(): string { return this.faker.random.arrayElement( this.faker.definitions.address.street_suffix ); @@ -222,7 +219,7 @@ export class Address { * * @method faker.address.streetPrefix */ - streetPrefix() { + streetPrefix(): string { return this.faker.random.arrayElement( this.faker.definitions.address.street_prefix ); @@ -244,7 +241,7 @@ export class Address { * * @method faker.address.county */ - county() { + county(): string { return this.faker.random.arrayElement( this.faker.definitions.address.county ); @@ -255,7 +252,7 @@ export class Address { * * @method faker.address.country */ - country() { + country(): string { return this.faker.random.arrayElement( this.faker.definitions.address.country ); @@ -265,10 +262,10 @@ export class Address { * countryCode * * @method faker.address.countryCode - * @param {string} alphaCode default alpha-2 + * @param alphaCode default alpha-2 */ - countryCode(alphaCode) { - if (typeof alphaCode === 'undefined' || alphaCode === 'alpha-2') { + countryCode(alphaCode: string = 'alpha-2'): string { + if (alphaCode === 'alpha-2') { return this.faker.random.arrayElement( this.faker.definitions.address.country_code ); @@ -289,9 +286,10 @@ export class Address { * state * * @method faker.address.state - * @param {Boolean} useAbbr + * @param useAbbr */ - state(useAbbr) { + // TODO christopher 2022-01-13: useAbbr not in use + state(useAbbr: boolean): string { return this.faker.random.arrayElement(this.faker.definitions.address.state); } @@ -300,7 +298,7 @@ export class Address { * * @method faker.address.stateAbbr */ - stateAbbr() { + stateAbbr(): string { return this.faker.random.arrayElement( this.faker.definitions.address.state_abbr ); @@ -310,15 +308,11 @@ export class Address { * latitude * * @method faker.address.latitude - * @param {Double} max default is 90 - * @param {Double} min default is -90 - * @param {number} precision default is 4 + * @param max default is 90 + * @param min default is -90 + * @param precision default is 4 */ - latitude(max, min, precision) { - max = max || 90; - min = min || -90; - precision = precision || 4; - + latitude(max: number = 90, min: number = -90, precision: number = 4): string { return this.faker.datatype .number({ max: max, @@ -332,15 +326,15 @@ export class Address { * longitude * * @method faker.address.longitude - * @param {Double} max default is 180 - * @param {Double} min default is -180 - * @param {number} precision default is 4 + * @param max default is 180 + * @param min default is -180 + * @param precision default is 4 */ - longitude(max, min, precision) { - max = max || 180; - min = min || -180; - precision = precision || 4; - + longitude( + max: number = 180, + min: number = -180, + precision: number = 4 + ): string { return this.faker.datatype .number({ max: max, @@ -354,10 +348,10 @@ export class Address { * direction * * @method faker.address.direction - * @param {Boolean} useAbbr return direction abbreviation. defaults to false + * @param useAbbr return direction abbreviation. defaults to false */ - direction(useAbbr) { - if (typeof useAbbr === 'undefined' || useAbbr === false) { + direction(useAbbr: boolean = false) { + if (!useAbbr) { return this.faker.random.arrayElement( this.faker.definitions.address.direction ); @@ -371,10 +365,10 @@ export class Address { * cardinal direction * * @method faker.address.cardinalDirection - * @param {Boolean} useAbbr return direction abbreviation. defaults to false + * @param useAbbr return direction abbreviation. defaults to false */ - cardinalDirection(useAbbr) { - if (typeof useAbbr === 'undefined' || useAbbr === false) { + cardinalDirection(useAbbr: boolean = false): string { + if (!useAbbr) { return this.faker.random.arrayElement( this.faker.definitions.address.direction.slice(0, 4) ); @@ -388,10 +382,10 @@ export class Address { * ordinal direction * * @method faker.address.ordinalDirection - * @param {Boolean} useAbbr return direction abbreviation. defaults to false + * @param useAbbr return direction abbreviation. defaults to false */ - ordinalDirection(useAbbr) { - if (typeof useAbbr === 'undefined' || useAbbr === false) { + ordinalDirection(useAbbr: boolean = false): string { + if (!useAbbr) { return this.faker.random.arrayElement( this.faker.definitions.address.direction.slice(4, 8) ); @@ -401,32 +395,41 @@ export class Address { ); } - nearbyGPSCoordinate(coordinate, radius, isMetric) { - function randomFloat(min, max) { + nearbyGPSCoordinate( + coordinate?: number[], + radius?: number, + isMetric?: boolean + ): string[] { + function randomFloat(min: number, max: number): number { return Math.random() * (max - min) + min; } - function degreesToRadians(degrees) { + function degreesToRadians(degrees: number): number { return degrees * (Math.PI / 180.0); } - function radiansToDegrees(radians) { + function radiansToDegrees(radians: number): number { return radians * (180.0 / Math.PI); } - function kilometersToMiles(miles) { + function kilometersToMiles(miles: number): number { return miles * 0.621371; } - function coordinateWithOffset(coordinate, bearing, distance, isMetric) { - var R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html) - var d = isMetric ? distance : kilometersToMiles(distance); // Distance in km - - var lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians - var lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians - - var lat2 = Math.asin( + function coordinateWithOffset( + coordinate: number[], + bearing: number, + distance: number, + isMetric: boolean + ): number[] { + const R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html) + const d = isMetric ? distance : kilometersToMiles(distance); // Distance in km + + const lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians + const lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians + + const lat2 = Math.asin( Math.sin(lat1) * Math.cos(d / R) + Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing) ); - var lon2 = + let lon2 = lon1 + Math.atan2( Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1), @@ -445,16 +448,16 @@ export class Address { // If there is no coordinate, the best we can do is return a random GPS coordinate. if (coordinate === undefined) { - return [faker.address.latitude(), faker.address.longitude()]; + return [this.faker.address.latitude(), this.faker.address.longitude()]; } - radius = radius || 10.0; - isMetric = isMetric || false; + radius ||= 10.0; + isMetric ||= false; // TODO: implement either a gaussian/uniform distribution of points in circular region. // Possibly include param to function that allows user to choose between distributions. // This approach will likely result in a higher density of points near the center. - var randomCoord = coordinateWithOffset( + const randomCoord = coordinateWithOffset( coordinate, degreesToRadians(Math.random() * 360.0), radius, @@ -468,7 +471,7 @@ export class Address { * * @method faker.address.timeZone */ - timeZone() { + timeZone(): string { return this.faker.random.arrayElement( this.faker.definitions.address.time_zone ); From 4064cfd23354f14cebe9f868d5a1c1f85f3af7c6 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 14 Jan 2022 17:21:20 +0100 Subject: [PATCH 3/5] chore: add member types --- src/address.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/address.ts b/src/address.ts index c14a3472803..3cdc0df9b28 100644 --- a/src/address.ts +++ b/src/address.ts @@ -1,8 +1,10 @@ import type { Faker } from '.'; +import type { Fake } from './fake'; +import type { Helpers } from './helpers'; export class Address { - readonly f; - readonly Helpers; + readonly f: Fake['fake']; + readonly Helpers: Helpers; constructor(private readonly faker: Faker) { this.f = this.faker.fake; From b9541c8e323771882a23e6d8166d2d0ced3e9b0b Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 14 Jan 2022 17:23:27 +0100 Subject: [PATCH 4/5] chore: fix types --- src/address.ts | 2 +- src/helpers.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/address.ts b/src/address.ts index 3cdc0df9b28..9a8f44c5218 100644 --- a/src/address.ts +++ b/src/address.ts @@ -291,7 +291,7 @@ export class Address { * @param useAbbr */ // TODO christopher 2022-01-13: useAbbr not in use - state(useAbbr: boolean): string { + state(useAbbr?: boolean): string { return this.faker.random.arrayElement(this.faker.definitions.address.state); } diff --git a/src/helpers.ts b/src/helpers.ts index 6d6aa62fcd4..f8219b3544d 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -394,7 +394,7 @@ export class Helpers { ), phone: this.faker.phone.phoneNumber(), address: { - street: this.faker.address.streetName(true), + street: this.faker.address.streetName(), suite: this.faker.address.secondaryAddress(), city: this.faker.address.city(), zipcode: this.faker.address.zipCode(), @@ -423,7 +423,7 @@ export class Helpers { username: this.faker.internet.userName(), email: this.faker.internet.email(), address: { - street: this.faker.address.streetName(true), + street: this.faker.address.streetName(), suite: this.faker.address.secondaryAddress(), city: this.faker.address.city(), zipcode: this.faker.address.zipCode(), From 1442e0ccc87a9766c82bcc568f5d906b91468d6f Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 14 Jan 2022 18:40:02 +0100 Subject: [PATCH 5/5] chore: move fake function out of class --- src/address.ts | 7 ++++--- src/fake.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/address.ts b/src/address.ts index 9a8f44c5218..540da2060fa 100644 --- a/src/address.ts +++ b/src/address.ts @@ -2,12 +2,13 @@ import type { Faker } from '.'; import type { Fake } from './fake'; import type { Helpers } from './helpers'; +let f: Fake['fake']; + export class Address { - readonly f: Fake['fake']; readonly Helpers: Helpers; constructor(private readonly faker: Faker) { - this.f = this.faker.fake; + f = this.faker.fake; this.Helpers = this.faker.helpers; // Bind `this` so namespaced is working correctly @@ -109,7 +110,7 @@ export class Address { format = this.faker.datatype.number(formats.length - 1); } - return this.f(formats[format]); + return f(formats[format]); } /** diff --git a/src/fake.ts b/src/fake.ts index e435ce8966e..1845dbc1070 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -79,7 +79,7 @@ export class Fake { } // assign the function from the module.function namespace - const fn = this.faker[parts[0]][parts[1]]; + const fn: Function = this.faker[parts[0]][parts[1]]; // If parameters are populated here, they are always going to be of string type // since we might actually be dealing with an object or array,