Skip to content

Commit 496c40b

Browse files
Shinigami92ST-DDT
andauthored
refactor(location): nearbyGPSCoordinate options (#1682)
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
1 parent 59824e6 commit 496c40b

3 files changed

Lines changed: 237 additions & 18 deletions

File tree

src/modules/location/index.ts

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { deprecated } from '../../internal/deprecated';
23

34
/**
45
* Module to generate addresses and locations.
@@ -368,6 +369,27 @@ export class LocationModule {
368369
);
369370
}
370371

372+
/**
373+
* Generates a random GPS coordinate within the specified radius from the given coordinate.
374+
*
375+
* @param options The options for generating a GPS coordinate.
376+
* @param options.origin The original coordinate to get a new coordinate close to.
377+
* If no coordinate is given, a random one will be chosen.
378+
* @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
379+
* @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
380+
*
381+
* @example
382+
* faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
383+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
384+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
385+
*
386+
* @since 8.0.0
387+
*/
388+
nearbyGPSCoordinate(options?: {
389+
origin?: [latitude: number, longitude: number];
390+
radius?: number;
391+
isMetric?: boolean;
392+
}): [latitude: number, longitude: number];
371393
/**
372394
* Generates a random GPS coordinate within the specified radius from the given coordinate.
373395
*
@@ -382,14 +404,74 @@ export class LocationModule {
382404
* faker.location.nearbyGPSCoordinate([33, -170], 1000, true) // [ 37.9163, -179.2408 ]
383405
*
384406
* @since 8.0.0
407+
*
408+
* @deprecated Use `faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })` instead.
385409
*/
386410
nearbyGPSCoordinate(
387411
coordinate?: [latitude: number, longitude: number],
388-
radius: number = 10,
389-
isMetric: boolean = false
412+
radius?: number,
413+
isMetric?: boolean
414+
): [latitude: number, longitude: number];
415+
/**
416+
* Generates a random GPS coordinate within the specified radius from the given coordinate.
417+
*
418+
* @param options The options for generating a GPS coordinate.
419+
* @param options.origin The original coordinate to get a new coordinate close to.
420+
* If no coordinate is given, a random one will be chosen.
421+
* @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
422+
* @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
423+
* @param legacyRadius Deprecated, use `options.radius` instead.
424+
* @param legacyIsMetric Deprecated, use `options.isMetric` instead.
425+
*
426+
* @example
427+
* faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
428+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170] }) // [ 33.0165, -170.0636 ]
429+
* faker.location.nearbyGPSCoordinate({ origin: [33, -170], radius: 1000, isMetric: true }) // [ 37.9163, -179.2408 ]
430+
*
431+
* @since 8.0.0
432+
*/
433+
nearbyGPSCoordinate(
434+
options?:
435+
| [latitude: number, longitude: number]
436+
| {
437+
origin?: [latitude: number, longitude: number];
438+
radius?: number;
439+
isMetric?: boolean;
440+
},
441+
legacyRadius?: number,
442+
legacyIsMetric?: boolean
443+
): [latitude: number, longitude: number];
444+
nearbyGPSCoordinate(
445+
options:
446+
| [latitude: number, longitude: number]
447+
| {
448+
origin?: [latitude: number, longitude: number];
449+
radius?: number;
450+
isMetric?: boolean;
451+
} = {},
452+
legacyRadius: number = 10,
453+
legacyIsMetric: boolean = false
390454
): [latitude: number, longitude: number] {
391-
// If there is no coordinate, the best we can do is return a random GPS coordinate.
392-
if (coordinate === undefined) {
455+
if (Array.isArray(options)) {
456+
deprecated({
457+
deprecated:
458+
'faker.location.nearbyGPSCoordinate(coordinate, radius, isMetric)',
459+
proposed:
460+
'faker.location.nearbyGPSCoordinate({ origin, radius, isMetric })',
461+
since: '8.0',
462+
until: '9.0',
463+
});
464+
options = { origin: options };
465+
}
466+
467+
const {
468+
origin,
469+
radius = legacyRadius,
470+
isMetric = legacyIsMetric,
471+
} = options;
472+
473+
// If there is no origin, the best we can do is return a random GPS coordinate.
474+
if (origin == null) {
393475
return [this.latitude(), this.longitude()];
394476
}
395477

@@ -414,22 +496,22 @@ export class LocationModule {
414496

415497
const distanceInDegree = distanceInKm / kmPerDegree; // in °
416498

417-
const newCoordinate: [latitude: number, longitude: number] = [
418-
coordinate[0] + Math.sin(angleRadians) * distanceInDegree,
419-
coordinate[1] + Math.cos(angleRadians) * distanceInDegree,
499+
const coordinate: [latitude: number, longitude: number] = [
500+
origin[0] + Math.sin(angleRadians) * distanceInDegree,
501+
origin[1] + Math.cos(angleRadians) * distanceInDegree,
420502
];
421503

422504
// Box latitude [-90°, 90°]
423-
newCoordinate[0] = newCoordinate[0] % 180;
424-
if (newCoordinate[0] < -90 || newCoordinate[0] > 90) {
425-
newCoordinate[0] = Math.sign(newCoordinate[0]) * 180 - newCoordinate[0];
426-
newCoordinate[1] += 180;
505+
coordinate[0] = coordinate[0] % 180;
506+
if (coordinate[0] < -90 || coordinate[0] > 90) {
507+
coordinate[0] = Math.sign(coordinate[0]) * 180 - coordinate[0];
508+
coordinate[1] += 180;
427509
}
428510

429511
// Box longitude [-180°, 180°]
430-
newCoordinate[1] = (((newCoordinate[1] % 360) + 540) % 360) - 180;
512+
coordinate[1] = (((coordinate[1] % 360) + 540) % 360) - 180;
431513

432-
return [newCoordinate[0], newCoordinate[1]];
514+
return [coordinate[0], coordinate[1]];
433515
}
434516

435517
/**

test/__snapshots__/location.spec.ts.snap

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@ exports[`location > 42 > nearbyGPSCoordinate > noArgs 1`] = `
4646
]
4747
`;
4848

49+
exports[`location > 42 > nearbyGPSCoordinate > only isMetric 1`] = `
50+
[
51+
-22.5828,
52+
106.7555,
53+
]
54+
`;
55+
56+
exports[`location > 42 > nearbyGPSCoordinate > only radius 1`] = `
57+
[
58+
-22.5828,
59+
106.7555,
60+
]
61+
`;
62+
63+
exports[`location > 42 > nearbyGPSCoordinate > with origin and isMetric 1`] = `
64+
[
65+
37.05058762889859,
66+
-13.05029562250138,
67+
]
68+
`;
69+
70+
exports[`location > 42 > nearbyGPSCoordinate > with origin and radius 1`] = `
71+
[
72+
37.122112668351875,
73+
-13.121407798779614,
74+
]
75+
`;
76+
77+
exports[`location > 42 > nearbyGPSCoordinate > with origin, radius and isMetric 1`] = `
78+
[
79+
37.075875092904894,
80+
-13.075437119965613,
81+
]
82+
`;
83+
84+
exports[`location > 42 > nearbyGPSCoordinate > with radius and isMetric 1`] = `
85+
[
86+
-22.5828,
87+
106.7555,
88+
]
89+
`;
90+
4991
exports[`location > 42 > ordinalDirection > noArgs 1`] = `"Northwest"`;
5092

5193
exports[`location > 42 > ordinalDirection > with abbr = false 1`] = `"Northwest"`;
@@ -124,6 +166,48 @@ exports[`location > 1211 > nearbyGPSCoordinate > noArgs 1`] = `
124166
]
125167
`;
126168

169+
exports[`location > 1211 > nearbyGPSCoordinate > only isMetric 1`] = `
170+
[
171+
77.1337,
172+
-14.7545,
173+
]
174+
`;
175+
176+
exports[`location > 1211 > nearbyGPSCoordinate > only radius 1`] = `
177+
[
178+
77.1337,
179+
-14.7545,
180+
]
181+
`;
182+
183+
exports[`location > 1211 > nearbyGPSCoordinate > with origin and isMetric 1`] = `
184+
[
185+
36.98215379643012,
186+
-12.962972893442156,
187+
]
188+
`;
189+
190+
exports[`location > 1211 > nearbyGPSCoordinate > with origin and radius 1`] = `
191+
[
192+
36.95691638741659,
193+
-12.910610595257708,
194+
]
195+
`;
196+
197+
exports[`location > 1211 > nearbyGPSCoordinate > with origin, radius and isMetric 1`] = `
198+
[
199+
36.97323069464518,
200+
-12.944459340163235,
201+
]
202+
`;
203+
204+
exports[`location > 1211 > nearbyGPSCoordinate > with radius and isMetric 1`] = `
205+
[
206+
77.1337,
207+
-14.7545,
208+
]
209+
`;
210+
127211
exports[`location > 1211 > ordinalDirection > noArgs 1`] = `"Southwest"`;
128212

129213
exports[`location > 1211 > ordinalDirection > with abbr = false 1`] = `"Southwest"`;
@@ -202,6 +286,48 @@ exports[`location > 1337 > nearbyGPSCoordinate > noArgs 1`] = `
202286
]
203287
`;
204288

289+
exports[`location > 1337 > nearbyGPSCoordinate > only isMetric 1`] = `
290+
[
291+
-42.8356,
292+
21.7907,
293+
]
294+
`;
295+
296+
exports[`location > 1337 > nearbyGPSCoordinate > only radius 1`] = `
297+
[
298+
-42.8356,
299+
21.7907,
300+
]
301+
`;
302+
303+
exports[`location > 1337 > nearbyGPSCoordinate > with origin and isMetric 1`] = `
304+
[
305+
37.05004958398222,
306+
-13.003788641630877,
307+
]
308+
`;
309+
310+
exports[`location > 1337 > nearbyGPSCoordinate > with origin and radius 1`] = `
311+
[
312+
37.12082442834317,
313+
-13.009146139144832,
314+
]
315+
`;
316+
317+
exports[`location > 1337 > nearbyGPSCoordinate > with origin, radius and isMetric 1`] = `
318+
[
319+
37.07507884069983,
320+
-13.00568330041608,
321+
]
322+
`;
323+
324+
exports[`location > 1337 > nearbyGPSCoordinate > with radius and isMetric 1`] = `
325+
[
326+
-42.8356,
327+
21.7907,
328+
]
329+
`;
330+
205331
exports[`location > 1337 > ordinalDirection > noArgs 1`] = `"Northwest"`;
206332

207333
exports[`location > 1337 > ordinalDirection > with abbr = false 1`] = `"Northwest"`;

test/location.spec.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ describe('location', () => {
7272
});
7373

7474
t.describe('nearbyGPSCoordinate', (t) => {
75-
t.it('noArgs').it('near origin', [0, 0]);
75+
t.it('noArgs')
76+
.it('near origin', { origin: [0, 0] })
77+
.it('with origin and radius', { origin: [37, -13], radius: 15 })
78+
.it('with origin, radius and isMetric', {
79+
origin: [37, -13],
80+
radius: 15,
81+
isMetric: true,
82+
})
83+
.it('with origin and isMetric', { origin: [37, -13], isMetric: true })
84+
.it('with radius and isMetric', { radius: 15, isMetric: true })
85+
.it('only radius', { radius: 12 })
86+
.it('only isMetric', { isMetric: true });
7687
});
7788
t.it('state').it('stateAbbr');
7889

@@ -286,11 +297,11 @@ describe('location', () => {
286297
const latitude1 = +faker.location.latitude();
287298
const longitude1 = +faker.location.longitude();
288299

289-
const coordinate = faker.location.nearbyGPSCoordinate(
290-
[latitude1, longitude1],
300+
const coordinate = faker.location.nearbyGPSCoordinate({
301+
origin: [latitude1, longitude1],
291302
radius,
292-
isMetric
293-
);
303+
isMetric,
304+
});
294305

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

0 commit comments

Comments
 (0)