Skip to content

Commit dac6be3

Browse files
feat(location)!: latitude/longitude returns number (#1064)
1 parent 20f2236 commit dac6be3

File tree

3 files changed

+69
-91
lines changed

3 files changed

+69
-91
lines changed

src/modules/address/index.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,18 @@ export class AddressModule {
260260
* @param precision The number of decimal points of precision for the latitude. Defaults to `4`.
261261
*
262262
* @example
263-
* faker.address.latitude() // '-30.9501'
264-
* faker.address.latitude(10, -10, 5) // '2.68452'
263+
* faker.address.latitude() // -30.9501
264+
* faker.address.latitude(10, -10, 5) // 2.68452
265265
*
266266
* @since 2.0.1
267267
*/
268-
latitude(max: number = 90, min: number = -90, precision: number = 4): string {
269-
return this.faker.datatype
270-
.number({
271-
min,
272-
max,
273-
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
274-
})
275-
.toFixed(precision);
268+
// TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability
269+
latitude(max: number = 90, min: number = -90, precision: number = 4): number {
270+
return this.faker.datatype.number({
271+
min,
272+
max,
273+
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
274+
});
276275
}
277276

278277
/**
@@ -283,23 +282,22 @@ export class AddressModule {
283282
* @param precision The number of decimal points of precision for the longitude. Defaults to `4`.
284283
*
285284
* @example
286-
* faker.address.longitude() // '-154.0226'
287-
* faker.address.longitude(10, -10, 5) // '-4.03620'
285+
* faker.address.longitude() // -154.0226
286+
* faker.address.longitude(10, -10, 5) // -4.03620
288287
*
289288
* @since 2.0.1
290289
*/
290+
// TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability
291291
longitude(
292292
max: number = 180,
293293
min: number = -180,
294294
precision: number = 4
295-
): string {
296-
return this.faker.datatype
297-
.number({
298-
max: max,
299-
min: min,
300-
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
301-
})
302-
.toFixed(precision);
295+
): number {
296+
return this.faker.datatype.number({
297+
max: max,
298+
min: min,
299+
precision: parseFloat(`${(0.0).toPrecision(precision)}1`),
300+
});
303301
}
304302

305303
/**
@@ -396,7 +394,7 @@ export class AddressModule {
396394
): [latitude: number, longitude: number] {
397395
// If there is no coordinate, the best we can do is return a random GPS coordinate.
398396
if (coordinate === undefined) {
399-
return [parseFloat(this.latitude()), parseFloat(this.longitude())];
397+
return [this.latitude(), this.longitude()];
400398
}
401399

402400
const angleRadians = this.faker.datatype.float({

test/__snapshots__/address.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ exports[`address > 42 > direction > with abbr = false 1`] = `"South"`;
2828

2929
exports[`address > 42 > direction > with abbr = true 1`] = `"S"`;
3030

31-
exports[`address > 42 > latitude > noArgs 1`] = `"-22.5828"`;
31+
exports[`address > 42 > latitude > noArgs 1`] = `-22.5828`;
3232

33-
exports[`address > 42 > longitude > noArgs 1`] = `"-45.1656"`;
33+
exports[`address > 42 > longitude > noArgs 1`] = `-45.1656`;
3434

3535
exports[`address > 42 > nearbyGPSCoordinate > near origin 1`] = `
3636
[
@@ -106,9 +106,9 @@ exports[`address > 1211 > direction > with abbr = false 1`] = `"Southwest"`;
106106

107107
exports[`address > 1211 > direction > with abbr = true 1`] = `"SW"`;
108108

109-
exports[`address > 1211 > latitude > noArgs 1`] = `"77.1337"`;
109+
exports[`address > 1211 > latitude > noArgs 1`] = `77.1337`;
110110

111-
exports[`address > 1211 > longitude > noArgs 1`] = `"154.2673"`;
111+
exports[`address > 1211 > longitude > noArgs 1`] = `154.2673`;
112112

113113
exports[`address > 1211 > nearbyGPSCoordinate > near origin 1`] = `
114114
[
@@ -184,9 +184,9 @@ exports[`address > 1337 > direction > with abbr = false 1`] = `"South"`;
184184

185185
exports[`address > 1337 > direction > with abbr = true 1`] = `"S"`;
186186

187-
exports[`address > 1337 > latitude > noArgs 1`] = `"-42.8356"`;
187+
exports[`address > 1337 > latitude > noArgs 1`] = `-42.8356`;
188188

189-
exports[`address > 1337 > longitude > noArgs 1`] = `"-85.6711"`;
189+
exports[`address > 1337 > longitude > noArgs 1`] = `-85.6711`;
190190

191191
exports[`address > 1337 > nearbyGPSCoordinate > near origin 1`] = `
192192
[

test/address.spec.ts

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -153,100 +153,80 @@ describe('address', () => {
153153
});
154154

155155
describe('latitude()', () => {
156-
it('returns random latitude', () => {
157-
for (let i = 0; i < 100; i++) {
158-
const latitude = faker.address.latitude();
156+
it('returns a number', () => {
157+
const latitude = faker.address.latitude();
159158

160-
expect(latitude).toBeTypeOf('string');
159+
expect(latitude).toBeTypeOf('number');
160+
});
161161

162-
const latitude_float = parseFloat(latitude);
162+
it('returns random latitude', () => {
163+
const latitude = faker.address.latitude();
163164

164-
expect(latitude_float).toBeGreaterThanOrEqual(-90.0);
165-
expect(latitude_float).toBeLessThanOrEqual(90.0);
166-
}
165+
expect(latitude).toBeGreaterThanOrEqual(-90.0);
166+
expect(latitude).toBeLessThanOrEqual(90.0);
167167
});
168168

169169
it('returns latitude with min and max and default precision', () => {
170-
for (let i = 0; i < 100; i++) {
171-
const latitude = faker.address.latitude(5, -5);
172-
173-
expect(latitude).toBeTypeOf('string');
174-
expect(
175-
latitude.split('.')[1].length,
176-
'The precision of latitude should be 4 digits'
177-
).toBe(4);
170+
const latitude = faker.address.latitude(5, -5);
178171

179-
const latitude_float = parseFloat(latitude);
172+
expect(
173+
latitude.toString().split('.')[1].length,
174+
'The precision of latitude should be 4 digits'
175+
).lessThanOrEqual(4);
180176

181-
expect(latitude_float).toBeGreaterThanOrEqual(-5);
182-
expect(latitude_float).toBeLessThanOrEqual(5);
183-
}
177+
expect(latitude).toBeGreaterThanOrEqual(-5);
178+
expect(latitude).toBeLessThanOrEqual(5);
184179
});
185180

186181
it('returns random latitude with custom precision', () => {
187-
for (let i = 0; i < 100; i++) {
188-
const latitude = faker.address.latitude(undefined, undefined, 7);
189-
190-
expect(latitude).toBeTypeOf('string');
191-
expect(
192-
latitude.split('.')[1].length,
193-
'The precision of latitude should be 7 digits'
194-
).toBe(7);
182+
const latitude = faker.address.latitude(undefined, undefined, 7);
195183

196-
const latitude_float = parseFloat(latitude);
184+
expect(
185+
latitude.toString().split('.')[1].length,
186+
'The precision of latitude should be 7 digits'
187+
).lessThanOrEqual(7);
197188

198-
expect(latitude_float).toBeGreaterThanOrEqual(-180);
199-
expect(latitude_float).toBeLessThanOrEqual(180);
200-
}
189+
expect(latitude).toBeGreaterThanOrEqual(-180);
190+
expect(latitude).toBeLessThanOrEqual(180);
201191
});
202192
});
203193

204194
describe('longitude()', () => {
205-
it('returns random longitude', () => {
206-
for (let i = 0; i < 100; i++) {
207-
const longitude = faker.address.longitude();
195+
it('returns a number', () => {
196+
const longitude = faker.address.longitude();
208197

209-
expect(longitude).toBeTypeOf('string');
198+
expect(longitude).toBeTypeOf('number');
199+
});
210200

211-
const longitude_float = parseFloat(longitude);
201+
it('returns random longitude', () => {
202+
const longitude = faker.address.longitude();
212203

213-
expect(longitude_float).toBeGreaterThanOrEqual(-180);
214-
expect(longitude_float).toBeLessThanOrEqual(180);
215-
}
204+
expect(longitude).toBeGreaterThanOrEqual(-180);
205+
expect(longitude).toBeLessThanOrEqual(180);
216206
});
217207

218208
it('returns random longitude with min and max and default precision', () => {
219-
for (let i = 0; i < 100; i++) {
220-
const longitude = faker.address.longitude(100, -30);
221-
222-
expect(longitude).toBeTypeOf('string');
223-
expect(
224-
longitude.split('.')[1].length,
225-
'The precision of longitude should be 4 digits'
226-
).toBe(4);
209+
const longitude = faker.address.longitude(100, -30);
227210

228-
const longitude_float = parseFloat(longitude);
211+
expect(
212+
longitude.toString().split('.')[1].length,
213+
'The precision of longitude should be 4 digits'
214+
).lessThanOrEqual(4);
229215

230-
expect(longitude_float).toBeGreaterThanOrEqual(-30);
231-
expect(longitude_float).toBeLessThanOrEqual(100);
232-
}
216+
expect(longitude).toBeGreaterThanOrEqual(-30);
217+
expect(longitude).toBeLessThanOrEqual(100);
233218
});
234219

235220
it('returns random longitude with custom precision', () => {
236-
for (let i = 0; i < 100; i++) {
237-
const longitude = faker.address.longitude(undefined, undefined, 7);
238-
239-
expect(longitude).toBeTypeOf('string');
240-
expect(
241-
longitude.split('.')[1].length,
242-
'The precision of longitude should be 7 digits'
243-
).toBe(7);
221+
const longitude = faker.address.longitude(undefined, undefined, 7);
244222

245-
const longitude_float = parseFloat(longitude);
223+
expect(
224+
longitude.toString().split('.')[1].length,
225+
'The precision of longitude should be 7 digits'
226+
).lessThanOrEqual(7);
246227

247-
expect(longitude_float).toBeGreaterThanOrEqual(-180);
248-
expect(longitude_float).toBeLessThanOrEqual(180);
249-
}
228+
expect(longitude).toBeGreaterThanOrEqual(-180);
229+
expect(longitude).toBeLessThanOrEqual(180);
250230
});
251231
});
252232

0 commit comments

Comments
 (0)