Skip to content

Commit c2009f0

Browse files
committed
chore: add types
1 parent 9ac656b commit c2009f0

1 file changed

Lines changed: 78 additions & 75 deletions

File tree

src/address.ts

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export class Address {
4242
* locale's zip format is used.
4343
*
4444
* @method faker.address.zipCode
45-
* @param {String} format
45+
* @param format
4646
*/
47-
zipCode(format) {
47+
zipCode(format?: string) {
4848
// if zip format is not specified, use the zip format defined for the locale
4949
if (typeof format === 'undefined') {
50-
var localeFormat = this.faker.definitions.address.postcode;
50+
const localeFormat = this.faker.definitions.address.postcode;
5151
if (typeof localeFormat === 'string') {
5252
format = localeFormat;
5353
} else {
@@ -65,10 +65,10 @@ export class Address {
6565
* to the locale's zip format.
6666
*
6767
* @method faker.address.zipCodeByState
68-
* @param {String} state
68+
* @param state
6969
*/
70-
zipCodeByState(state) {
71-
var zipRange = this.faker.definitions.address.postcode_by_state[state];
70+
zipCodeByState(state: string) {
71+
const zipRange = this.faker.definitions.address.postcode_by_state[state];
7272
if (zipRange) {
7373
return this.faker.datatype.number(zipRange);
7474
}
@@ -89,10 +89,10 @@ export class Address {
8989
* * `{{address.cityName}}` when city name is available
9090
*
9191
* @method faker.address.city
92-
* @param {String} format
92+
* @param format
9393
*/
94-
city(format) {
95-
var formats = [
94+
city(format?: string | number) {
95+
const formats = [
9696
'{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}',
9797
'{{address.cityPrefix}} {{name.firstName}}',
9898
'{{name.firstName}}{{address.citySuffix}}',
@@ -115,7 +115,7 @@ export class Address {
115115
*
116116
* @method faker.address.cityPrefix
117117
*/
118-
cityPrefix() {
118+
cityPrefix(): string {
119119
return this.faker.random.arrayElement(
120120
this.faker.definitions.address.city_prefix
121121
);
@@ -126,7 +126,7 @@ export class Address {
126126
*
127127
* @method faker.address.citySuffix
128128
*/
129-
citySuffix() {
129+
citySuffix(): string {
130130
return this.faker.random.arrayElement(
131131
this.faker.definitions.address.city_suffix
132132
);
@@ -137,7 +137,7 @@ export class Address {
137137
*
138138
* @method faker.address.cityName
139139
*/
140-
cityName() {
140+
cityName(): string {
141141
return this.faker.random.arrayElement(
142142
this.faker.definitions.address.city_name
143143
);
@@ -148,9 +148,9 @@ export class Address {
148148
*
149149
* @method faker.address.streetName
150150
*/
151-
streetName() {
152-
var result;
153-
var suffix = this.faker.address.streetSuffix();
151+
streetName(): string {
152+
let result: string;
153+
let suffix = this.faker.address.streetSuffix();
154154
if (suffix !== '') {
155155
suffix = ' ' + suffix;
156156
}
@@ -174,13 +174,10 @@ export class Address {
174174
* Returns a random localized street address
175175
*
176176
* @method faker.address.streetAddress
177-
* @param {Boolean} useFullAddress
177+
* @param useFullAddress
178178
*/
179-
streetAddress(useFullAddress) {
180-
if (useFullAddress === undefined) {
181-
useFullAddress = false;
182-
}
183-
var address = '';
179+
streetAddress(useFullAddress: boolean = false): string {
180+
let address = '';
184181
switch (this.faker.datatype.number(2)) {
185182
case 0:
186183
address =
@@ -211,7 +208,7 @@ export class Address {
211208
*
212209
* @method faker.address.streetSuffix
213210
*/
214-
streetSuffix() {
211+
streetSuffix(): string {
215212
return this.faker.random.arrayElement(
216213
this.faker.definitions.address.street_suffix
217214
);
@@ -222,7 +219,7 @@ export class Address {
222219
*
223220
* @method faker.address.streetPrefix
224221
*/
225-
streetPrefix() {
222+
streetPrefix(): string {
226223
return this.faker.random.arrayElement(
227224
this.faker.definitions.address.street_prefix
228225
);
@@ -244,7 +241,7 @@ export class Address {
244241
*
245242
* @method faker.address.county
246243
*/
247-
county() {
244+
county(): string {
248245
return this.faker.random.arrayElement(
249246
this.faker.definitions.address.county
250247
);
@@ -255,7 +252,7 @@ export class Address {
255252
*
256253
* @method faker.address.country
257254
*/
258-
country() {
255+
country(): string {
259256
return this.faker.random.arrayElement(
260257
this.faker.definitions.address.country
261258
);
@@ -265,10 +262,10 @@ export class Address {
265262
* countryCode
266263
*
267264
* @method faker.address.countryCode
268-
* @param {string} alphaCode default alpha-2
265+
* @param alphaCode default alpha-2
269266
*/
270-
countryCode(alphaCode) {
271-
if (typeof alphaCode === 'undefined' || alphaCode === 'alpha-2') {
267+
countryCode(alphaCode: string = 'alpha-2'): string {
268+
if (alphaCode === 'alpha-2') {
272269
return this.faker.random.arrayElement(
273270
this.faker.definitions.address.country_code
274271
);
@@ -289,9 +286,10 @@ export class Address {
289286
* state
290287
*
291288
* @method faker.address.state
292-
* @param {Boolean} useAbbr
289+
* @param useAbbr
293290
*/
294-
state(useAbbr) {
291+
// TODO christopher 2022-01-13: useAbbr not in use
292+
state(useAbbr: boolean): string {
295293
return this.faker.random.arrayElement(this.faker.definitions.address.state);
296294
}
297295

@@ -300,7 +298,7 @@ export class Address {
300298
*
301299
* @method faker.address.stateAbbr
302300
*/
303-
stateAbbr() {
301+
stateAbbr(): string {
304302
return this.faker.random.arrayElement(
305303
this.faker.definitions.address.state_abbr
306304
);
@@ -310,15 +308,11 @@ export class Address {
310308
* latitude
311309
*
312310
* @method faker.address.latitude
313-
* @param {Double} max default is 90
314-
* @param {Double} min default is -90
315-
* @param {number} precision default is 4
311+
* @param max default is 90
312+
* @param min default is -90
313+
* @param precision default is 4
316314
*/
317-
latitude(max, min, precision) {
318-
max = max || 90;
319-
min = min || -90;
320-
precision = precision || 4;
321-
315+
latitude(max: number = 90, min: number = -90, precision: number = 4): string {
322316
return this.faker.datatype
323317
.number({
324318
max: max,
@@ -332,15 +326,15 @@ export class Address {
332326
* longitude
333327
*
334328
* @method faker.address.longitude
335-
* @param {Double} max default is 180
336-
* @param {Double} min default is -180
337-
* @param {number} precision default is 4
329+
* @param max default is 180
330+
* @param min default is -180
331+
* @param precision default is 4
338332
*/
339-
longitude(max, min, precision) {
340-
max = max || 180;
341-
min = min || -180;
342-
precision = precision || 4;
343-
333+
longitude(
334+
max: number = 180,
335+
min: number = -180,
336+
precision: number = 4
337+
): string {
344338
return this.faker.datatype
345339
.number({
346340
max: max,
@@ -354,10 +348,10 @@ export class Address {
354348
* direction
355349
*
356350
* @method faker.address.direction
357-
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
351+
* @param useAbbr return direction abbreviation. defaults to false
358352
*/
359-
direction(useAbbr) {
360-
if (typeof useAbbr === 'undefined' || useAbbr === false) {
353+
direction(useAbbr: boolean = false) {
354+
if (!useAbbr) {
361355
return this.faker.random.arrayElement(
362356
this.faker.definitions.address.direction
363357
);
@@ -371,10 +365,10 @@ export class Address {
371365
* cardinal direction
372366
*
373367
* @method faker.address.cardinalDirection
374-
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
368+
* @param useAbbr return direction abbreviation. defaults to false
375369
*/
376-
cardinalDirection(useAbbr) {
377-
if (typeof useAbbr === 'undefined' || useAbbr === false) {
370+
cardinalDirection(useAbbr: boolean = false): string {
371+
if (!useAbbr) {
378372
return this.faker.random.arrayElement(
379373
this.faker.definitions.address.direction.slice(0, 4)
380374
);
@@ -388,10 +382,10 @@ export class Address {
388382
* ordinal direction
389383
*
390384
* @method faker.address.ordinalDirection
391-
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
385+
* @param useAbbr return direction abbreviation. defaults to false
392386
*/
393-
ordinalDirection(useAbbr) {
394-
if (typeof useAbbr === 'undefined' || useAbbr === false) {
387+
ordinalDirection(useAbbr: boolean = false): string {
388+
if (!useAbbr) {
395389
return this.faker.random.arrayElement(
396390
this.faker.definitions.address.direction.slice(4, 8)
397391
);
@@ -401,32 +395,41 @@ export class Address {
401395
);
402396
}
403397

404-
nearbyGPSCoordinate(coordinate, radius, isMetric) {
405-
function randomFloat(min, max) {
398+
nearbyGPSCoordinate(
399+
coordinate?: number[],
400+
radius?: number,
401+
isMetric?: boolean
402+
): string[] {
403+
function randomFloat(min: number, max: number): number {
406404
return Math.random() * (max - min) + min;
407405
}
408-
function degreesToRadians(degrees) {
406+
function degreesToRadians(degrees: number): number {
409407
return degrees * (Math.PI / 180.0);
410408
}
411-
function radiansToDegrees(radians) {
409+
function radiansToDegrees(radians: number): number {
412410
return radians * (180.0 / Math.PI);
413411
}
414-
function kilometersToMiles(miles) {
412+
function kilometersToMiles(miles: number): number {
415413
return miles * 0.621371;
416414
}
417-
function coordinateWithOffset(coordinate, bearing, distance, isMetric) {
418-
var R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
419-
var d = isMetric ? distance : kilometersToMiles(distance); // Distance in km
420-
421-
var lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
422-
var lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians
423-
424-
var lat2 = Math.asin(
415+
function coordinateWithOffset(
416+
coordinate: number[],
417+
bearing: number,
418+
distance: number,
419+
isMetric: boolean
420+
): number[] {
421+
const R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
422+
const d = isMetric ? distance : kilometersToMiles(distance); // Distance in km
423+
424+
const lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
425+
const lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians
426+
427+
const lat2 = Math.asin(
425428
Math.sin(lat1) * Math.cos(d / R) +
426429
Math.cos(lat1) * Math.sin(d / R) * Math.cos(bearing)
427430
);
428431

429-
var lon2 =
432+
let lon2 =
430433
lon1 +
431434
Math.atan2(
432435
Math.sin(bearing) * Math.sin(d / R) * Math.cos(lat1),
@@ -445,16 +448,16 @@ export class Address {
445448

446449
// If there is no coordinate, the best we can do is return a random GPS coordinate.
447450
if (coordinate === undefined) {
448-
return [faker.address.latitude(), faker.address.longitude()];
451+
return [this.faker.address.latitude(), this.faker.address.longitude()];
449452
}
450-
radius = radius || 10.0;
451-
isMetric = isMetric || false;
453+
radius ||= 10.0;
454+
isMetric ||= false;
452455

453456
// TODO: implement either a gaussian/uniform distribution of points in circular region.
454457
// Possibly include param to function that allows user to choose between distributions.
455458

456459
// This approach will likely result in a higher density of points near the center.
457-
var randomCoord = coordinateWithOffset(
460+
const randomCoord = coordinateWithOffset(
458461
coordinate,
459462
degreesToRadians(Math.random() * 360.0),
460463
radius,
@@ -468,7 +471,7 @@ export class Address {
468471
*
469472
* @method faker.address.timeZone
470473
*/
471-
timeZone() {
474+
timeZone(): string {
472475
return this.faker.random.arrayElement(
473476
this.faker.definitions.address.time_zone
474477
);

0 commit comments

Comments
 (0)