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

fix: deterministic results for address.nearbyGPSCoordinate #737

Merged
merged 8 commits into from
Apr 1, 2022
8 changes: 7 additions & 1 deletion src/address.ts
Original file line number Diff line number Diff line change
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: 1,
precision: 1e-4,
}) * 360.0
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
),
radius,
isMetric
);
Expand Down
31 changes: 30 additions & 1 deletion test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const seededRuns = [
cardinalDirection: 'East',
cardinalDirectionAbbr: 'E',
timeZone: 'Europe/Amsterdam',
nearbyGpsCoordinates: ['-0.0393', '0.0396'],
},
inputs: {
nearbyGpsCoordinates: [0, 0],
} as {
nearbyGpsCoordinates: [number, number];
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand Down Expand Up @@ -58,6 +64,12 @@ const seededRuns = [
cardinalDirection: 'East',
cardinalDirectionAbbr: 'E',
timeZone: 'Africa/Casablanca',
nearbyGpsCoordinates: ['-0.0042', '0.0557'],
},
inputs: {
nearbyGpsCoordinates: [0, 0],
} as {
nearbyGpsCoordinates: [number, number];
},
},
{
Expand Down Expand Up @@ -87,6 +99,12 @@ const seededRuns = [
cardinalDirection: 'West',
cardinalDirectionAbbr: 'W',
timeZone: 'Asia/Magadan',
nearbyGpsCoordinates: ['0.0503', '-0.0242'],
},
inputs: {
nearbyGpsCoordinates: [0, 0],
} as {
nearbyGpsCoordinates: [number, number];
},
},
];
Expand All @@ -98,7 +116,7 @@ describe('address', () => {
faker.locale = 'en';
});

for (const { seed, expectations } of seededRuns) {
for (const { seed, expectations, inputs } of seededRuns) {
describe(`seed: ${seed}`, () => {
it('city()', () => {
faker.seed(seed);
Expand Down Expand Up @@ -333,6 +351,17 @@ describe('address', () => {
expect(timeZone).toEqual(expectations.timeZone);
});
});

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

const coords = faker.address.nearbyGPSCoordinate(
inputs.nearbyGpsCoordinates
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
);
expect(coords).toEqual(expectations.nearbyGpsCoordinates);
});
});
});
}

Expand Down