Skip to content

Commit 17587a3

Browse files
paulovitinflovilmart
authored andcommitted
Validate if geopoint values is number (#671)
1 parent 361be2a commit 17587a3

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/ParseGeoPoint.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ParseGeoPoint {
5353
ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
5454
this._latitude = arg1.latitude;
5555
this._longitude = arg1.longitude;
56-
} else if (typeof arg1 === 'number' && typeof arg2 === 'number') {
56+
} else if (arg1 !== undefined && arg2 !== undefined) {
5757
ParseGeoPoint._validate(arg1, arg2);
5858
this._latitude = arg1;
5959
this._longitude = arg2;
@@ -163,7 +163,10 @@ class ParseGeoPoint {
163163
* Throws an exception if the given lat-long is out of bounds.
164164
*/
165165
static _validate(latitude: number, longitude: number) {
166-
if (latitude !== latitude || longitude !== longitude) {
166+
if (
167+
isNaN(latitude) || isNaN(longitude) ||
168+
typeof latitude !== 'number' || typeof longitude !== 'number'
169+
) {
167170
throw new TypeError(
168171
'GeoPoint latitude and longitude must be valid numbers'
169172
);

src/__tests__/ParseGeoPoint-test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,27 @@ describe('GeoPoint', () => {
3030
expect(point.longitude).toBe(88);
3131
});
3232

33-
it('throws when created with NaN values', () => {
34-
expect(function() {
35-
new ParseGeoPoint(NaN, NaN);
36-
}).toThrow('GeoPoint latitude and longitude must be valid numbers');
33+
it('throws when created with non numbers values', () => {
34+
[
35+
[NaN, NaN],
36+
[false, true],
37+
["29", "10"],
38+
[29, "10"],
39+
["29", 10],
40+
[["29", "10"]],
41+
[{ latitude: "29", longitude: "10" }],
42+
].forEach(case_test => {
43+
expect(function() {
44+
new ParseGeoPoint(...case_test);
45+
}).toThrow('GeoPoint latitude and longitude must be valid numbers');
46+
});
3747
});
3848

3949
it('can set latitude and longitude', () => {
4050
const point = new ParseGeoPoint();
51+
expect(point.latitude).toBe(0);
52+
expect(point.longitude).toBe(0);
53+
4154
point.latitude = 5.5;
4255
expect(point.latitude).toBe(5.5);
4356

0 commit comments

Comments
 (0)