From 51f23c4410ddd6cae3f7e956171e1218dcadf9cc Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 14:28:22 -0400 Subject: [PATCH 1/2] expand/ reverse size --- query/reverse.js | 2 +- sanitiser/_size.js | 6 ++++-- sanitiser/reverse.js | 8 ++++++-- test/unit/query/reverse.js | 10 ++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/query/reverse.js b/query/reverse.js index 21b755da..3ada51be 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -9,7 +9,7 @@ function generate( params ){ lon: params.lon }; - return queries.distance( centroid, { size: 1 } ); + return queries.distance( centroid, { size: params.size || 1 } ); } module.exports = generate; \ No newline at end of file diff --git a/sanitiser/_size.js b/sanitiser/_size.js index b3e1041e..4bc1ec8e 100644 --- a/sanitiser/_size.js +++ b/sanitiser/_size.js @@ -1,9 +1,11 @@ // validate inputs, convert types and apply defaults -function sanitize( req ){ +function sanitize( req, default_size){ var clean = req.clean || {}; var params= req.query; + var default_size = default_size || 10; + // ensure the input params are a valid object if( Object.prototype.toString.call( params ) !== '[object Object]' ){ params = {}; @@ -14,7 +16,7 @@ function sanitize( req ){ if( !isNaN( size ) ){ clean.size = Math.min( Math.max( size, 1 ), 40 ); // max } else { - clean.size = 10; // default + clean.size = default_size; // default } req.clean = clean; diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index ae7c1af6..cb3e141f 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -2,8 +2,12 @@ var logger = require('../src/logger'), _sanitize = require('../sanitiser/_sanitize'), sanitiser = { - latlonzoom: require('../sanitiser/_latlonzoom') - }; + latlonzoom: require('../sanitiser/_latlonzoom'), + size: function( req ) { + var size = require('../sanitiser/_size'); + return size(req, 1); + } + }; var sanitize = function(req, cb) { _sanitize(req, sanitiser, cb); } diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 4b54b646..85a0720c 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -58,6 +58,16 @@ module.exports.tests.query = function(test, common) { }; t.deepEqual(query, expected, 'valid reverse query'); + + // test different sizes + var sizes = [1,2,10,undefined,null]; + sizes.forEach( function(size) { + query = generate({ + lat: 29.49136, lon: -82.50622, size: size + }); + expected.size = size ? size : 1; + t.deepEqual(query, expected, 'valid reverse query for size: '+ size); + }); t.end(); }); }; From ceecf5837d33459f4fc0e3297baa874a65f1b7db Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 13 Oct 2014 14:35:04 -0400 Subject: [PATCH 2/2] fix indentation --- sanitiser/reverse.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index cb3e141f..b0e7b275 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -2,12 +2,12 @@ var logger = require('../src/logger'), _sanitize = require('../sanitiser/_sanitize'), sanitiser = { - latlonzoom: require('../sanitiser/_latlonzoom'), + latlonzoom: require('../sanitiser/_latlonzoom'), size: function( req ) { var size = require('../sanitiser/_size'); return size(req, 1); } - }; + }; var sanitize = function(req, cb) { _sanitize(req, sanitiser, cb); } @@ -24,4 +24,4 @@ module.exports.middleware = function( req, res, next ){ req.clean = clean; next(); }); -}; \ No newline at end of file +};