From 8c93710963e71ccfab5c4fd662a8d3a2d8f0e01d Mon Sep 17 00:00:00 2001 From: Vincent Wen Date: Sun, 26 Mar 2017 22:04:16 +0800 Subject: [PATCH] make geo nearFilter support minDistance (#987) --- lib/geo.js | 5 ++++ test/geo.test.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/lib/geo.js b/lib/geo.js index 022b4942..9bef1222 100644 --- a/lib/geo.js +++ b/lib/geo.js @@ -22,6 +22,7 @@ exports.nearFilter = function nearFilter(where) { result = { near: ex.near, maxDistance: ex.maxDistance, + minDistance: ex.minDistance, unit: ex.unit, key: key, }; @@ -39,6 +40,7 @@ exports.nearFilter = function nearFilter(where) { exports.filter = function(arr, filter) { var origin = filter.near; var max = filter.maxDistance > 0 ? filter.maxDistance : false; + var min = filter.minDistance > 0 ? filter.minDistance : false; var unit = filter.unit; var key = filter.key; @@ -61,6 +63,9 @@ exports.filter = function(arr, filter) { var d = GeoPoint.distanceBetween(origin, loc, {type: unit}); + if (min && d < min) { + return; + } if (max && d > max) { // dont add } else { diff --git a/test/geo.test.js b/test/geo.test.js index 319ede13..20fc2dd2 100644 --- a/test/geo.test.js +++ b/test/geo.test.js @@ -11,6 +11,8 @@ require('should'); var GeoPoint = require('../lib/geo').GeoPoint; +var nearFilter = require('../lib/geo').nearFilter; +var geoFilter = require('../lib/geo').filter; var DELTA = 0.0000001; describe('GeoPoint', function() { @@ -132,4 +134,70 @@ describe('GeoPoint', function() { distance.should.be.approximately(0.0004483676593058972, DELTA); }); }); + + describe('nearFilter()', function() { + it('should return a filter includes minDistance if where contains minDistance option', function() { + var where = { + location: { + near: { + lat: 40.77492964101182, + lng: -73.90950187151662, + }, + minDistance: 100, + }, + }; + var filter = nearFilter(where); + filter.key.should.equal('location'); + filter.should.have.properties({ + key: 'location', + near: { + lat: 40.77492964101182, + lng: -73.90950187151662, + }, + minDistance: 100, + }); + }); + }); + + describe('filter()', function() { + it('should be able to filter geo points via minDistance', function() { + var points = [{ + location: { + lat: 30.283552, + lng: 120.126048, + }, + }, { + location: { + lat: 30.380307, + lng: 119.979445, + }, + }, { + location: { + lat: 30.229896, + lng: 119.744592, + }, + }, { + location: { + lat: 30.250863, + lng: 120.129498, + }, + }, { + location: { + lat: 31.244209, + lng: 121.483687, + }, + }]; + var filter = { + key: 'location', + near: { + lat: 30.278562, + lng: 120.139846, + }, + unit: 'meters', + minDistance: 10000, + }; + var results = geoFilter(points, filter); + results.length.should.be.equal(3); + }); + }); });