Skip to content

Commit

Permalink
chore: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Jan 13, 2022
1 parent c1645b6 commit e15c081
Showing 1 changed file with 78 additions and 75 deletions.
153 changes: 78 additions & 75 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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}}',
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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;
}
Expand All @@ -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 =
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -244,7 +241,7 @@ export class Address {
*
* @method faker.address.county
*/
county() {
county(): string {
return this.faker.random.arrayElement(
this.faker.definitions.address.county
);
Expand All @@ -255,7 +252,7 @@ export class Address {
*
* @method faker.address.country
*/
country() {
country(): string {
return this.faker.random.arrayElement(
this.faker.definitions.address.country
);
Expand All @@ -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
);
Expand All @@ -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);
}

Expand All @@ -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
);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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
);
Expand Down

0 comments on commit e15c081

Please sign in to comment.