Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(location)!: nearbyGPSCoordinate returns number tuple #1061

Merged
merged 8 commits into from
Oct 14, 2022
12 changes: 6 additions & 6 deletions src/modules/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,20 @@ export class AddressModule {
* @param isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
*
* @example
* faker.address.nearbyGPSCoordinate() // [ '33.8475', '-170.5953' ]
* faker.address.nearbyGPSCoordinate([33, -170]) // [ '33.0165', '-170.0636' ]
* faker.address.nearbyGPSCoordinate([33, -170], 1000, true) // [ '37.9163', '-179.2408' ]
* faker.address.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
* faker.address.nearbyGPSCoordinate([33, -170]) // [ 33.0165, -170.0636 ]
* faker.address.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
*
* @since 5.0.0
*/
nearbyGPSCoordinate(
coordinate?: [latitude: number, longitude: number],
radius: number = 10,
isMetric: boolean = false
): [latitude: string, longitude: string] {
): [latitude: number, longitude: number] {
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.latitude(), this.longitude()];
return [parseFloat(this.latitude()), parseFloat(this.longitude())];
}

const angleRadians = this.faker.datatype.float({
Expand Down Expand Up @@ -560,7 +560,7 @@ export class AddressModule {
// Box longitude [-180°, 180°]
newCoordinate[1] = (((newCoordinate[1] % 360) + 540) % 360) - 180;

return [newCoordinate[0].toFixed(4), newCoordinate[1].toFixed(4)];
return [newCoordinate[0], newCoordinate[1]];
}

/**
Expand Down
24 changes: 12 additions & 12 deletions test/__snapshots__/address.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ exports[`address > 42 > longitude > noArgs 1`] = `"-45.1656"`;

exports[`address > 42 > nearbyGPSCoordinate > near origin 1`] = `
[
"0.0814",
"-0.0809",
0.08140632875358443,
-0.08093642792425726,
]
`;

exports[`address > 42 > nearbyGPSCoordinate > noArgs 1`] = `
[
"-22.5828",
"106.7555",
-22.5828,
106.7555,
]
`;

Expand Down Expand Up @@ -128,15 +128,15 @@ exports[`address > 1211 > longitude > noArgs 1`] = `"154.2673"`;

exports[`address > 1211 > nearbyGPSCoordinate > near origin 1`] = `
[
"-0.0287",
"0.0596",
-0.02872051646443488,
0.05959053473372933,
]
`;

exports[`address > 1211 > nearbyGPSCoordinate > noArgs 1`] = `
[
"77.1337",
"-14.7545",
77.1337,
-14.7545,
]
`;

Expand Down Expand Up @@ -216,15 +216,15 @@ exports[`address > 1337 > longitude > noArgs 1`] = `"-85.6711"`;

exports[`address > 1337 > nearbyGPSCoordinate > near origin 1`] = `
[
"0.0806",
"-0.0061",
0.08055259537977688,
-0.006097651409731952,
]
`;

exports[`address > 1337 > nearbyGPSCoordinate > noArgs 1`] = `
[
"-42.8356",
"21.7907",
-42.8356,
21.7907,
]
`;

Expand Down
69 changes: 33 additions & 36 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,42 +305,39 @@ describe('address', () => {
describe('nearbyGPSCoordinate()', () => {
for (const isMetric of [true, false]) {
for (const radius of times(100)) {
it.each(times(5))(
`should return random gps coordinate within a distance of another one (${JSON.stringify(
{ isMetric, radius }
)}) (iter: %s)`,
() => {
const latitude1 = +faker.address.latitude();
const longitude1 = +faker.address.longitude();

const coordinate = faker.address.nearbyGPSCoordinate(
[latitude1, longitude1],
radius,
isMetric
);

expect(coordinate.length).toBe(2);
expect(coordinate[0]).toBeTypeOf('string');
expect(coordinate[1]).toBeTypeOf('string');

const latitude2 = +coordinate[0];
expect(latitude2).toBeGreaterThanOrEqual(-90.0);
expect(latitude2).toBeLessThanOrEqual(90.0);

const longitude2 = +coordinate[1];
expect(longitude2).toBeGreaterThanOrEqual(-180.0);
expect(longitude2).toBeLessThanOrEqual(180.0);

const actualDistance = haversine(
latitude1,
longitude1,
latitude2,
longitude2,
isMetric
);
expect(actualDistance).toBeLessThanOrEqual(radius);
}
);
it(`should return random gps coordinate within a distance of another one (${JSON.stringify(
{ isMetric, radius }
)})`, () => {
const latitude1 = +faker.address.latitude();
const longitude1 = +faker.address.longitude();

const coordinate = faker.address.nearbyGPSCoordinate(
[latitude1, longitude1],
radius,
isMetric
);

expect(coordinate.length).toBe(2);
expect(coordinate[0]).toBeTypeOf('number');
expect(coordinate[1]).toBeTypeOf('number');

const latitude2 = coordinate[0];
expect(latitude2).toBeGreaterThanOrEqual(-90.0);
expect(latitude2).toBeLessThanOrEqual(90.0);

const longitude2 = coordinate[1];
expect(longitude2).toBeGreaterThanOrEqual(-180.0);
expect(longitude2).toBeLessThanOrEqual(180.0);

const actualDistance = haversine(
latitude1,
longitude1,
latitude2,
longitude2,
isMetric
);
expect(actualDistance).toBeLessThanOrEqual(radius);
});
}
}
});
Expand Down