Skip to content

Commit

Permalink
fix: deterministic results for address.nearbyGPSCoordinate (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Apr 1, 2022
1 parent caa5f16 commit bc7bd57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/address.ts
Expand Up @@ -538,7 +538,13 @@ export class Address {
// This approach will likely result in a higher density of points near the center.
const randomCoord = coordinateWithOffset(
coordinate,
degreesToRadians(Math.random() * 360.0),
degreesToRadians(
this.faker.datatype.number({
min: 0,
max: 360,
precision: 1e-4,
})
),
radius,
isMetric
);
Expand Down
14 changes: 14 additions & 0 deletions test/address.spec.ts
Expand Up @@ -29,6 +29,7 @@ const seededRuns = [
cardinalDirection: 'East',
cardinalDirectionAbbr: 'E',
timeZone: 'Europe/Amsterdam',
nearbyGpsCoordinates: ['-0.0394', '0.0396'],
},
},
{
Expand Down Expand Up @@ -58,6 +59,7 @@ const seededRuns = [
cardinalDirection: 'East',
cardinalDirectionAbbr: 'E',
timeZone: 'Africa/Casablanca',
nearbyGpsCoordinates: ['-0.0042', '0.0557'],
},
},
{
Expand Down Expand Up @@ -87,6 +89,7 @@ const seededRuns = [
cardinalDirection: 'West',
cardinalDirectionAbbr: 'W',
timeZone: 'Asia/Magadan',
nearbyGpsCoordinates: ['0.0503', '-0.0242'],
},
},
];
Expand Down Expand Up @@ -333,6 +336,17 @@ describe('address', () => {
expect(timeZone).toEqual(expectations.timeZone);
});
});

describe('nearbyGPSCoordinate()', () => {
it('returns expected coordinates', () => {
faker.seed(seed);

// this input is required for all expected results for this function
const coordsInput: [number, number] = [0, 0];
const coords = faker.address.nearbyGPSCoordinate(coordsInput);
expect(coords).toEqual(expectations.nearbyGpsCoordinates);
});
});
});
}

Expand Down

0 comments on commit bc7bd57

Please sign in to comment.