Skip to content

Commit 4765336

Browse files
feat(location)!: nearbyGPSCoordinate returns number tuple (#1061)
1 parent a90f2fe commit 4765336

3 files changed

Lines changed: 51 additions & 54 deletions

File tree

src/modules/address/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,20 @@ export class AddressModule {
383383
* @param isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
384384
*
385385
* @example
386-
* faker.address.nearbyGPSCoordinate() // [ '33.8475', '-170.5953' ]
387-
* faker.address.nearbyGPSCoordinate([33, -170]) // [ '33.0165', '-170.0636' ]
388-
* faker.address.nearbyGPSCoordinate([33, -170], 1000, true) // [ '37.9163', '-179.2408' ]
386+
* faker.address.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
387+
* faker.address.nearbyGPSCoordinate([33, -170]) // [ 33.0165, -170.0636 ]
388+
* faker.address.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
389389
*
390390
* @since 5.0.0
391391
*/
392392
nearbyGPSCoordinate(
393393
coordinate?: [latitude: number, longitude: number],
394394
radius: number = 10,
395395
isMetric: boolean = false
396-
): [latitude: string, longitude: string] {
396+
): [latitude: number, longitude: number] {
397397
// If there is no coordinate, the best we can do is return a random GPS coordinate.
398398
if (coordinate === undefined) {
399-
return [this.latitude(), this.longitude()];
399+
return [parseFloat(this.latitude()), parseFloat(this.longitude())];
400400
}
401401

402402
const angleRadians = this.faker.datatype.float({
@@ -436,7 +436,7 @@ export class AddressModule {
436436
// Box longitude [-180°, 180°]
437437
newCoordinate[1] = (((newCoordinate[1] % 360) + 540) % 360) - 180;
438438

439-
return [newCoordinate[0].toFixed(4), newCoordinate[1].toFixed(4)];
439+
return [newCoordinate[0], newCoordinate[1]];
440440
}
441441

442442
/**

test/__snapshots__/address.spec.ts.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ exports[`address > 42 > longitude > noArgs 1`] = `"-45.1656"`;
3434

3535
exports[`address > 42 > nearbyGPSCoordinate > near origin 1`] = `
3636
[
37-
"0.0814",
38-
"-0.0809",
37+
0.08140632875358443,
38+
-0.08093642792425726,
3939
]
4040
`;
4141

4242
exports[`address > 42 > nearbyGPSCoordinate > noArgs 1`] = `
4343
[
44-
"-22.5828",
45-
"106.7555",
44+
-22.5828,
45+
106.7555,
4646
]
4747
`;
4848

@@ -112,15 +112,15 @@ exports[`address > 1211 > longitude > noArgs 1`] = `"154.2673"`;
112112

113113
exports[`address > 1211 > nearbyGPSCoordinate > near origin 1`] = `
114114
[
115-
"-0.0287",
116-
"0.0596",
115+
-0.02872051646443488,
116+
0.05959053473372933,
117117
]
118118
`;
119119

120120
exports[`address > 1211 > nearbyGPSCoordinate > noArgs 1`] = `
121121
[
122-
"77.1337",
123-
"-14.7545",
122+
77.1337,
123+
-14.7545,
124124
]
125125
`;
126126

@@ -190,15 +190,15 @@ exports[`address > 1337 > longitude > noArgs 1`] = `"-85.6711"`;
190190

191191
exports[`address > 1337 > nearbyGPSCoordinate > near origin 1`] = `
192192
[
193-
"0.0806",
194-
"-0.0061",
193+
0.08055259537977688,
194+
-0.006097651409731952,
195195
]
196196
`;
197197

198198
exports[`address > 1337 > nearbyGPSCoordinate > noArgs 1`] = `
199199
[
200-
"-42.8356",
201-
"21.7907",
200+
-42.8356,
201+
21.7907,
202202
]
203203
`;
204204

test/address.spec.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -300,42 +300,39 @@ describe('address', () => {
300300
describe('nearbyGPSCoordinate()', () => {
301301
for (const isMetric of [true, false]) {
302302
for (const radius of times(100)) {
303-
it.each(times(5))(
304-
`should return random gps coordinate within a distance of another one (${JSON.stringify(
305-
{ isMetric, radius }
306-
)}) (iter: %s)`,
307-
() => {
308-
const latitude1 = +faker.address.latitude();
309-
const longitude1 = +faker.address.longitude();
310-
311-
const coordinate = faker.address.nearbyGPSCoordinate(
312-
[latitude1, longitude1],
313-
radius,
314-
isMetric
315-
);
316-
317-
expect(coordinate.length).toBe(2);
318-
expect(coordinate[0]).toBeTypeOf('string');
319-
expect(coordinate[1]).toBeTypeOf('string');
320-
321-
const latitude2 = +coordinate[0];
322-
expect(latitude2).toBeGreaterThanOrEqual(-90.0);
323-
expect(latitude2).toBeLessThanOrEqual(90.0);
324-
325-
const longitude2 = +coordinate[1];
326-
expect(longitude2).toBeGreaterThanOrEqual(-180.0);
327-
expect(longitude2).toBeLessThanOrEqual(180.0);
328-
329-
const actualDistance = haversine(
330-
latitude1,
331-
longitude1,
332-
latitude2,
333-
longitude2,
334-
isMetric
335-
);
336-
expect(actualDistance).toBeLessThanOrEqual(radius);
337-
}
338-
);
303+
it(`should return random gps coordinate within a distance of another one (${JSON.stringify(
304+
{ isMetric, radius }
305+
)})`, () => {
306+
const latitude1 = +faker.address.latitude();
307+
const longitude1 = +faker.address.longitude();
308+
309+
const coordinate = faker.address.nearbyGPSCoordinate(
310+
[latitude1, longitude1],
311+
radius,
312+
isMetric
313+
);
314+
315+
expect(coordinate.length).toBe(2);
316+
expect(coordinate[0]).toBeTypeOf('number');
317+
expect(coordinate[1]).toBeTypeOf('number');
318+
319+
const latitude2 = coordinate[0];
320+
expect(latitude2).toBeGreaterThanOrEqual(-90.0);
321+
expect(latitude2).toBeLessThanOrEqual(90.0);
322+
323+
const longitude2 = coordinate[1];
324+
expect(longitude2).toBeGreaterThanOrEqual(-180.0);
325+
expect(longitude2).toBeLessThanOrEqual(180.0);
326+
327+
const actualDistance = haversine(
328+
latitude1,
329+
longitude1,
330+
latitude2,
331+
longitude2,
332+
isMetric
333+
);
334+
expect(actualDistance).toBeLessThanOrEqual(radius);
335+
});
339336
}
340337
}
341338
});

0 commit comments

Comments
 (0)