Skip to content

Commit 837efa8

Browse files
committed
combined methos for sorted and unsorted queries by using sorted parameter
1 parent 439af5d commit 837efa8

File tree

3 files changed

+51
-102
lines changed

3 files changed

+51
-102
lines changed

integration/test/ParseGeoPointTest.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('Geo Point', () => {
7373
let query = new Parse.Query(TestObject);
7474
let point = new Parse.GeoPoint(24, 19);
7575
query.equalTo('construct', 'line');
76-
query.withinMiles('location', point, 10000);
76+
query.withinMiles('location', point, 10000, true);
7777
return query.find();
7878
}).then((results) => {
7979
assert.equal(results.length, 10);
@@ -97,7 +97,7 @@ describe('Geo Point', () => {
9797
let query = new Parse.Query(TestObject);
9898
let point = new Parse.GeoPoint(1, -1);
9999
query.equalTo('construct', 'large_dist');
100-
query.withinRadians('location', point, 3.14);
100+
query.withinRadians('location', point, 3.14, true);
101101
return query.find();
102102
}).then((results) => {
103103
assert.equal(results.length, 3);
@@ -119,7 +119,7 @@ describe('Geo Point', () => {
119119
let query = new Parse.Query(TestObject);
120120
let point = new Parse.GeoPoint(1, -1);
121121
query.equalTo('construct', 'medium_dist');
122-
query.withinRadians('location', point, 3.14 * 0.5);
122+
query.withinRadians('location', point, 3.14 * 0.5, true);
123123
return query.find();
124124
}).then((results) => {
125125
assert.equal(results.length, 2);
@@ -143,7 +143,7 @@ describe('Geo Point', () => {
143143
let query = new Parse.Query(TestObject);
144144
let point = new Parse.GeoPoint(1, -1);
145145
query.equalTo('construct', 'small_dist');
146-
query.withinRadians('location', point, 3.14 * 0.25);
146+
query.withinRadians('location', point, 3.14 * 0.25, true);
147147
return query.find();
148148
}).then((results) => {
149149
assert.equal(results.length, 1);
@@ -155,7 +155,7 @@ describe('Geo Point', () => {
155155
it('can measure distance within km - everywhere', (done) => {
156156
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
157157
let query = new Parse.Query(TestPoint);
158-
query.withinKilometers('location', sfo, 4000.0);
158+
query.withinKilometers('location', sfo, 4000.0, true);
159159
query.find().then((results) => {
160160
assert.equal(results.length, 3);
161161
done();
@@ -165,7 +165,7 @@ describe('Geo Point', () => {
165165
it('can measure distance within km - california', (done) => {
166166
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
167167
let query = new Parse.Query(TestPoint);
168-
query.withinKilometers('location', sfo, 3700.0);
168+
query.withinKilometers('location', sfo, 3700.0, true);
169169
query.find().then((results) => {
170170
assert.equal(results.length, 2);
171171
assert.equal(results[0].get('name'), 'San Francisco');
@@ -177,7 +177,7 @@ describe('Geo Point', () => {
177177
it('can measure distance within km - bay area', (done) => {
178178
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
179179
let query = new Parse.Query(TestPoint);
180-
query.withinKilometers('location', sfo, 100.0);
180+
query.withinKilometers('location', sfo, 100.0, true);
181181
query.find().then((results) => {
182182
assert.equal(results.length, 1);
183183
assert.equal(results[0].get('name'), 'San Francisco');
@@ -188,7 +188,7 @@ describe('Geo Point', () => {
188188
it('can measure distance within km - mid peninsula', (done) => {
189189
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
190190
let query = new Parse.Query(TestPoint);
191-
query.withinKilometers('location', sfo, 10.0);
191+
query.withinKilometers('location', sfo, 10.0, true);
192192
query.find().then((results) => {
193193
assert.equal(results.length, 0);
194194
done();
@@ -198,7 +198,7 @@ describe('Geo Point', () => {
198198
it('can measure distance within miles - everywhere', (done) => {
199199
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
200200
let query = new Parse.Query(TestPoint);
201-
query.withinMiles('location', sfo, 2500.0);
201+
query.withinMiles('location', sfo, 2500.0, true);
202202
query.find().then((results) => {
203203
assert.equal(results.length, 3);
204204
done();
@@ -208,7 +208,7 @@ describe('Geo Point', () => {
208208
it('can measure distance within miles - california', (done) => {
209209
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
210210
let query = new Parse.Query(TestPoint);
211-
query.withinMiles('location', sfo, 2200.0);
211+
query.withinMiles('location', sfo, 2200.0, true);
212212
query.find().then((results) => {
213213
assert.equal(results.length, 2);
214214
assert.equal(results[0].get('name'), 'San Francisco');
@@ -220,7 +220,7 @@ describe('Geo Point', () => {
220220
it('can measure distance within miles - bay area', (done) => {
221221
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
222222
let query = new Parse.Query(TestPoint);
223-
query.withinMiles('location', sfo, 75.0);
223+
query.withinMiles('location', sfo, 75.0, true);
224224
query.find().then((results) => {
225225
assert.equal(results.length, 1);
226226
assert.equal(results[0].get('name'), 'San Francisco');
@@ -231,7 +231,7 @@ describe('Geo Point', () => {
231231
it('can measure distance within km - mid peninsula', (done) => {
232232
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
233233
let query = new Parse.Query(TestPoint);
234-
query.withinMiles('location', sfo, 10.0);
234+
query.withinMiles('location', sfo, 10.0, true);
235235
query.find().then((results) => {
236236
assert.equal(results.length, 0);
237237
done();
@@ -252,7 +252,7 @@ describe('Geo Point', () => {
252252
let query = new Parse.Query(TestObject);
253253
let point = new Parse.GeoPoint(1, -1);
254254
query.equalTo('construct', 'large_dist');
255-
query.withinRadiansUnsorted('location', point, 3.14);
255+
query.withinRadians('location', point, 3.14, false);
256256
return query.find();
257257
}).then((results) => {
258258
assert.equal(results.length, 3);
@@ -274,7 +274,7 @@ describe('Geo Point', () => {
274274
let query = new Parse.Query(TestObject);
275275
let point = new Parse.GeoPoint(1, -1);
276276
query.equalTo('construct', 'medium_dist');
277-
query.withinRadiansUnsorted('location', point, 3.14 * 0.5);
277+
query.withinRadians('location', point, 3.14 * 0.5, false);
278278
return query.find();
279279
}).then((results) => {
280280
assert.equal(results.length, 2);
@@ -298,7 +298,7 @@ describe('Geo Point', () => {
298298
let query = new Parse.Query(TestObject);
299299
let point = new Parse.GeoPoint(1, -1);
300300
query.equalTo('construct', 'small_dist');
301-
query.withinRadiansUnsorted('location', point, 3.14 * 0.25);
301+
query.withinRadians('location', point, 3.14 * 0.25, false);
302302
return query.find();
303303
}).then((results) => {
304304
assert.equal(results.length, 1);
@@ -310,7 +310,7 @@ describe('Geo Point', () => {
310310
it('can measure distance within km unsorted - everywhere', (done) => {
311311
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
312312
let query = new Parse.Query(TestPoint);
313-
query.withinKilometersUnsorted('location', sfo, 4000.0);
313+
query.withinKilometers('location', sfo, 4000.0, false);
314314
query.find().then((results) => {
315315
assert.equal(results.length, 3);
316316
done();
@@ -320,7 +320,7 @@ describe('Geo Point', () => {
320320
it('can measure distance within km unsorted - california', (done) => {
321321
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
322322
let query = new Parse.Query(TestPoint);
323-
query.withinKilometersUnsorted('location', sfo, 3700.0);
323+
query.withinKilometers('location', sfo, 3700.0, false);
324324
query.find().then((results) => {
325325
assert.equal(results.length, 2);
326326
assert.equal(results[0].get('name'), 'San Francisco');
@@ -332,7 +332,7 @@ describe('Geo Point', () => {
332332
it('can measure distance within km unsorted - bay area', (done) => {
333333
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
334334
let query = new Parse.Query(TestPoint);
335-
query.withinKilometersUnsorted('location', sfo, 100.0);
335+
query.withinKilometers('location', sfo, 100.0, false);
336336
query.find().then((results) => {
337337
assert.equal(results.length, 1);
338338
assert.equal(results[0].get('name'), 'San Francisco');
@@ -343,7 +343,7 @@ describe('Geo Point', () => {
343343
it('can measure distance within km unsorted - mid peninsula', (done) => {
344344
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
345345
let query = new Parse.Query(TestPoint);
346-
query.withinKilometersUnsorted('location', sfo, 10.0);
346+
query.withinKilometers('location', sfo, 10.0, false);
347347
query.find().then((results) => {
348348
assert.equal(results.length, 0);
349349
done();
@@ -353,7 +353,7 @@ describe('Geo Point', () => {
353353
it('can measure distance within miles unsorted - everywhere', (done) => {
354354
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
355355
let query = new Parse.Query(TestPoint);
356-
query.withinMilesUnsorted('location', sfo, 2500.0);
356+
query.withinMiles('location', sfo, 2500.0, false);
357357
query.find().then((results) => {
358358
assert.equal(results.length, 3);
359359
done();
@@ -363,7 +363,7 @@ describe('Geo Point', () => {
363363
it('can measure distance within miles unsorted - california', (done) => {
364364
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
365365
let query = new Parse.Query(TestPoint);
366-
query.withinMilesUnsorted('location', sfo, 2200.0);
366+
query.withinMiles('location', sfo, 2200.0, false);
367367
query.find().then((results) => {
368368
assert.equal(results.length, 2);
369369
assert.equal(results[0].get('name'), 'San Francisco');
@@ -375,7 +375,7 @@ describe('Geo Point', () => {
375375
it('can measure distance within miles unsorted - bay area', (done) => {
376376
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
377377
let query = new Parse.Query(TestPoint);
378-
query.withinMilesUnsorted('location', sfo, 75.0);
378+
query.withinMiles('location', sfo, 75.0, false);
379379
query.find().then((results) => {
380380
assert.equal(results.length, 1);
381381
assert.equal(results[0].get('name'), 'San Francisco');
@@ -386,7 +386,7 @@ describe('Geo Point', () => {
386386
it('can measure distance within km unsorted - mid peninsula', (done) => {
387387
let sfo = new Parse.GeoPoint(37.6189722, -122.3748889);
388388
let query = new Parse.Query(TestPoint);
389-
query.withinMilesUnsorted('location', sfo, 10.0);
389+
query.withinMiles('location', sfo, 10.0, false);
390390
query.find().then((results) => {
391391
assert.equal(results.length, 0);
392392
done();

src/ParseQuery.js

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,88 +1100,49 @@ class ParseQuery {
11001100
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
11011101
* @param {Number} maxDistance Maximum distance (in radians) of results to
11021102
* return.
1103+
* @param {Boolean} sorted A Bool value that is true if results should be
1104+
* sorted by distance ascending, false is no sorting is required.
11031105
* @return {Parse.Query} Returns the query, so you can chain this call.
11041106
*/
1105-
withinRadians(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1106-
this.near(key, point);
1107-
return this._addCondition(key, '$maxDistance', distance);
1108-
}
1109-
1110-
/**
1111-
* Adds a proximity based constraint for finding objects with key point
1112-
* values near the point given and within the maximum distance given.
1113-
* Radius of earth used is 3958.8 miles.
1114-
* @param {String} key The key that the Parse.GeoPoint is stored in.
1115-
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
1116-
* @param {Number} maxDistance Maximum distance (in miles) of results to
1117-
* return.
1118-
* @return {Parse.Query} Returns the query, so you can chain this call.
1119-
*/
1120-
withinMiles(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1121-
return this.withinRadians(key, point, distance / 3958.8);
1122-
}
1123-
1124-
/**
1125-
* Adds a proximity based constraint for finding objects with key point
1126-
* values near the point given and within the maximum distance given.
1127-
* Radius of earth used is 6371.0 kilometers.
1128-
* @param {String} key The key that the Parse.GeoPoint is stored in.
1129-
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
1130-
* @param {Number} maxDistance Maximum distance (in kilometers) of results
1131-
* to return.
1132-
* @return {Parse.Query} Returns the query, so you can chain this call.
1133-
*/
1134-
withinKilometers(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1135-
return this.withinRadians(key, point, distance / 6371.0);
1136-
}
1137-
1138-
/**
1139-
* Adds a proximity based constraint for finding objects with key point
1140-
* values near the point given and within the maximum distance given.
1141-
* Results are unsorted for better performance compared to `withinRadians'.
1142-
* @param {String} key The key that the Parse.GeoPoint is stored in.
1143-
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
1144-
* @param {Number} maxDistance Maximum distance (in radians) of results to
1145-
* return.
1146-
* @return {Parse.Query} Returns the query, so you can chain this call.
1147-
*/
1148-
withinRadiansUnsorted(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1149-
1150-
if (!(point instanceof ParseGeoPoint)) {
1151-
// Try to cast it as a GeoPoint
1152-
point = new ParseGeoPoint(point);
1107+
withinRadians(key: string, point: ParseGeoPoint, distance: number, sorted: boolean): ParseQuery {
1108+
if (sorted) {
1109+
this.near(key, point);
1110+
return this._addCondition(key, '$maxDistance', distance);
1111+
} else {
1112+
return this._addCondition(key, '$geoWithin', { '$centerSphere': [[point.longitude, point.latitude], distance] });
11531113
}
1154-
return this._addCondition(key, '$geoWithin', { '$centerSphere': [point, distance] });
11551114
}
11561115

11571116
/**
11581117
* Adds a proximity based constraint for finding objects with key point
11591118
* values near the point given and within the maximum distance given.
11601119
* Radius of earth used is 3958.8 miles.
1161-
* Results are unsorted for better performance compared to `withinMiles'.
11621120
* @param {String} key The key that the Parse.GeoPoint is stored in.
11631121
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
11641122
* @param {Number} maxDistance Maximum distance (in miles) of results to
1165-
* return.
1123+
* return.
1124+
* @param {Boolean} sorted A Bool value that is true if results should be
1125+
* sorted by distance ascending, false is no sorting is required.
11661126
* @return {Parse.Query} Returns the query, so you can chain this call.
11671127
*/
1168-
withinMilesUnsorted(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1169-
return this.withinRadiansUnsorted(key, point, distance / 3958.8);
1128+
withinMiles(key: string, point: ParseGeoPoint, distance: number, sorted: boolean): ParseQuery {
1129+
return this.withinRadians(key, point, distance / 3958.8, sorted);
11701130
}
11711131

11721132
/**
11731133
* Adds a proximity based constraint for finding objects with key point
11741134
* values near the point given and within the maximum distance given.
11751135
* Radius of earth used is 6371.0 kilometers.
1176-
* Results are unsorted for better performance compared to `withinKilometers'.
11771136
* @param {String} key The key that the Parse.GeoPoint is stored in.
11781137
* @param {Parse.GeoPoint} point The reference Parse.GeoPoint that is used.
11791138
* @param {Number} maxDistance Maximum distance (in kilometers) of results
1180-
* to return.
1139+
* to return.
1140+
* @param {Boolean} sorted A Bool value that is true if results should be
1141+
* sorted by distance ascending, false is no sorting is required.
11811142
* @return {Parse.Query} Returns the query, so you can chain this call.
11821143
*/
1183-
withinKilometersUnsorted(key: string, point: ParseGeoPoint, distance: number): ParseQuery {
1184-
return this.withinRadiansUnsorted(key, point, distance / 6371.0);
1144+
withinKilometers(key: string, point: ParseGeoPoint, distance: number, sorted: boolean): ParseQuery {
1145+
return this.withinRadians(key, point, distance / 6371.0, sorted);
11851146
}
11861147

11871148
/**

0 commit comments

Comments
 (0)