Skip to content

Commit

Permalink
Add max, min for latitude and longitude
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilvagomez committed Jan 26, 2017
1 parent 7e96b93 commit 484e63a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,22 @@ function Address (faker) {
* latitude
*
* @method faker.address.latitude
* @param {Double} max default is 90
* @param {Double} min default is -90
*/
this.latitude = function () {
return (faker.random.number(180 * 10000) / 10000.0 - 90.0).toFixed(4);
this.latitude = function (max = 90, min = -90) {
return faker.random.number({max: max, min:min, precision:0.0001}).toFixed(4);
}

/**
* longitude
*
* @method faker.address.longitude
* @param {Double} max default is 180
* @param {Double} min default is -180
*/
this.longitude = function () {
return (faker.random.number(360 * 10000) / 10000.0 - 180.0).toFixed(4);
this.longitude = function (max = 180, min = -180) {
return faker.random.number({max: max, min:min, precision:0.0001}).toFixed(4);
}

return this;
Expand Down
26 changes: 26 additions & 0 deletions test/address.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ describe("address.js", function () {
faker.random.number.restore();
}
});

it("returns latitude with min and max", function () {
for (var i = 0; i < 100; i++) {
sinon.spy(faker.random, 'number');
var latitude = faker.address.latitude(-5, 5);
assert.ok(typeof latitude === 'string');
var latitude_float = parseFloat(latitude);
assert.ok(latitude_float >= -5);
assert.ok(latitude_float <= 5);
assert.ok(faker.random.number.called);
faker.random.number.restore();
}
});
});

describe("longitude()", function () {
Expand All @@ -280,6 +293,19 @@ describe("address.js", function () {
faker.random.number.restore();
}
});

it("returns random longitude with min and max", function () {
for (var i = 0; i < 100; i++) {
sinon.spy(faker.random, 'number');
var longitude = faker.address.longitude(100, -30);
assert.ok(typeof longitude === 'string');
var longitude_float = parseFloat(longitude);
assert.ok(longitude_float >= -30);
assert.ok(longitude_float <= 100);
assert.ok(faker.random.number.called);
faker.random.number.restore();
}
});
});

});

0 comments on commit 484e63a

Please sign in to comment.