Skip to content

Commit

Permalink
feat(location)!: latitude/longitude returns number (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Oct 16, 2022
1 parent 20f2236 commit dac6be3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 91 deletions.
40 changes: 19 additions & 21 deletions src/modules/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,18 @@ export class AddressModule {
* @param precision The number of decimal points of precision for the latitude. Defaults to `4`.
*
* @example
* faker.address.latitude() // '-30.9501'
* faker.address.latitude(10, -10, 5) // '2.68452'
* faker.address.latitude() // -30.9501
* faker.address.latitude(10, -10, 5) // 2.68452
*
* @since 2.0.1
*/
latitude(max: number = 90, min: number = -90, precision: number = 4): string {
return this.faker.datatype
.number({
min,
max,
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
})
.toFixed(precision);
// TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability
latitude(max: number = 90, min: number = -90, precision: number = 4): number {
return this.faker.datatype.number({
min,
max,
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
});
}

/**
Expand All @@ -283,23 +282,22 @@ export class AddressModule {
* @param precision The number of decimal points of precision for the longitude. Defaults to `4`.
*
* @example
* faker.address.longitude() // '-154.0226'
* faker.address.longitude(10, -10, 5) // '-4.03620'
* faker.address.longitude() // -154.0226
* faker.address.longitude(10, -10, 5) // -4.03620
*
* @since 2.0.1
*/
// TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability
longitude(
max: number = 180,
min: number = -180,
precision: number = 4
): string {
return this.faker.datatype
.number({
max: max,
min: min,
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
})
.toFixed(precision);
): number {
return this.faker.datatype.number({
max: max,
min: min,
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
});
}

/**
Expand Down Expand Up @@ -396,7 +394,7 @@ export class AddressModule {
): [latitude: number, longitude: number] {
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [parseFloat(this.latitude()), parseFloat(this.longitude())];
return [this.latitude(), this.longitude()];
}

const angleRadians = this.faker.datatype.float({
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/address.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ exports[`address > 42 > direction > with abbr = false 1`] = `"South"`;

exports[`address > 42 > direction > with abbr = true 1`] = `"S"`;

exports[`address > 42 > latitude > noArgs 1`] = `"-22.5828"`;
exports[`address > 42 > latitude > noArgs 1`] = `-22.5828`;

exports[`address > 42 > longitude > noArgs 1`] = `"-45.1656"`;
exports[`address > 42 > longitude > noArgs 1`] = `-45.1656`;

exports[`address > 42 > nearbyGPSCoordinate > near origin 1`] = `
[
Expand Down Expand Up @@ -106,9 +106,9 @@ exports[`address > 1211 > direction > with abbr = false 1`] = `"Southwest"`;

exports[`address > 1211 > direction > with abbr = true 1`] = `"SW"`;

exports[`address > 1211 > latitude > noArgs 1`] = `"77.1337"`;
exports[`address > 1211 > latitude > noArgs 1`] = `77.1337`;

exports[`address > 1211 > longitude > noArgs 1`] = `"154.2673"`;
exports[`address > 1211 > longitude > noArgs 1`] = `154.2673`;

exports[`address > 1211 > nearbyGPSCoordinate > near origin 1`] = `
[
Expand Down Expand Up @@ -184,9 +184,9 @@ exports[`address > 1337 > direction > with abbr = false 1`] = `"South"`;

exports[`address > 1337 > direction > with abbr = true 1`] = `"S"`;

exports[`address > 1337 > latitude > noArgs 1`] = `"-42.8356"`;
exports[`address > 1337 > latitude > noArgs 1`] = `-42.8356`;

exports[`address > 1337 > longitude > noArgs 1`] = `"-85.6711"`;
exports[`address > 1337 > longitude > noArgs 1`] = `-85.6711`;

exports[`address > 1337 > nearbyGPSCoordinate > near origin 1`] = `
[
Expand Down
108 changes: 44 additions & 64 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,100 +153,80 @@ describe('address', () => {
});

describe('latitude()', () => {
it('returns random latitude', () => {
for (let i = 0; i < 100; i++) {
const latitude = faker.address.latitude();
it('returns a number', () => {
const latitude = faker.address.latitude();

expect(latitude).toBeTypeOf('string');
expect(latitude).toBeTypeOf('number');
});

const latitude_float = parseFloat(latitude);
it('returns random latitude', () => {
const latitude = faker.address.latitude();

expect(latitude_float).toBeGreaterThanOrEqual(-90.0);
expect(latitude_float).toBeLessThanOrEqual(90.0);
}
expect(latitude).toBeGreaterThanOrEqual(-90.0);
expect(latitude).toBeLessThanOrEqual(90.0);
});

it('returns latitude with min and max and default precision', () => {
for (let i = 0; i < 100; i++) {
const latitude = faker.address.latitude(5, -5);

expect(latitude).toBeTypeOf('string');
expect(
latitude.split('.')[1].length,
'The precision of latitude should be 4 digits'
).toBe(4);
const latitude = faker.address.latitude(5, -5);

const latitude_float = parseFloat(latitude);
expect(
latitude.toString().split('.')[1].length,
'The precision of latitude should be 4 digits'
).lessThanOrEqual(4);

expect(latitude_float).toBeGreaterThanOrEqual(-5);
expect(latitude_float).toBeLessThanOrEqual(5);
}
expect(latitude).toBeGreaterThanOrEqual(-5);
expect(latitude).toBeLessThanOrEqual(5);
});

it('returns random latitude with custom precision', () => {
for (let i = 0; i < 100; i++) {
const latitude = faker.address.latitude(undefined, undefined, 7);

expect(latitude).toBeTypeOf('string');
expect(
latitude.split('.')[1].length,
'The precision of latitude should be 7 digits'
).toBe(7);
const latitude = faker.address.latitude(undefined, undefined, 7);

const latitude_float = parseFloat(latitude);
expect(
latitude.toString().split('.')[1].length,
'The precision of latitude should be 7 digits'
).lessThanOrEqual(7);

expect(latitude_float).toBeGreaterThanOrEqual(-180);
expect(latitude_float).toBeLessThanOrEqual(180);
}
expect(latitude).toBeGreaterThanOrEqual(-180);
expect(latitude).toBeLessThanOrEqual(180);
});
});

describe('longitude()', () => {
it('returns random longitude', () => {
for (let i = 0; i < 100; i++) {
const longitude = faker.address.longitude();
it('returns a number', () => {
const longitude = faker.address.longitude();

expect(longitude).toBeTypeOf('string');
expect(longitude).toBeTypeOf('number');
});

const longitude_float = parseFloat(longitude);
it('returns random longitude', () => {
const longitude = faker.address.longitude();

expect(longitude_float).toBeGreaterThanOrEqual(-180);
expect(longitude_float).toBeLessThanOrEqual(180);
}
expect(longitude).toBeGreaterThanOrEqual(-180);
expect(longitude).toBeLessThanOrEqual(180);
});

it('returns random longitude with min and max and default precision', () => {
for (let i = 0; i < 100; i++) {
const longitude = faker.address.longitude(100, -30);

expect(longitude).toBeTypeOf('string');
expect(
longitude.split('.')[1].length,
'The precision of longitude should be 4 digits'
).toBe(4);
const longitude = faker.address.longitude(100, -30);

const longitude_float = parseFloat(longitude);
expect(
longitude.toString().split('.')[1].length,
'The precision of longitude should be 4 digits'
).lessThanOrEqual(4);

expect(longitude_float).toBeGreaterThanOrEqual(-30);
expect(longitude_float).toBeLessThanOrEqual(100);
}
expect(longitude).toBeGreaterThanOrEqual(-30);
expect(longitude).toBeLessThanOrEqual(100);
});

it('returns random longitude with custom precision', () => {
for (let i = 0; i < 100; i++) {
const longitude = faker.address.longitude(undefined, undefined, 7);

expect(longitude).toBeTypeOf('string');
expect(
longitude.split('.')[1].length,
'The precision of longitude should be 7 digits'
).toBe(7);
const longitude = faker.address.longitude(undefined, undefined, 7);

const longitude_float = parseFloat(longitude);
expect(
longitude.toString().split('.')[1].length,
'The precision of longitude should be 7 digits'
).lessThanOrEqual(7);

expect(longitude_float).toBeGreaterThanOrEqual(-180);
expect(longitude_float).toBeLessThanOrEqual(180);
}
expect(longitude).toBeGreaterThanOrEqual(-180);
expect(longitude).toBeLessThanOrEqual(180);
});
});

Expand Down

0 comments on commit dac6be3

Please sign in to comment.