Browse Source

add a precommit hook to prevent errors

pull/14/head
Peter Johnson 10 years ago
parent
commit
afcee7eb0c
  1. 1
      .jshintignore
  2. 21
      .jshintrc
  3. 8
      controller/suggest.js
  4. 12
      index.js
  5. 6
      middleware/search.js
  6. 6
      package.json
  7. 22
      query/reverse.js
  8. 48
      query/search.js
  9. 4
      sanitiser/_latlonzoom.js
  10. 2
      sanitiser/_layers.js
  11. 8
      sanitiser/reverse.js
  12. 19
      sanitiser/suggest.js
  13. 2
      test/unit/mock/backend.js
  14. 8
      test/unit/sanitiser/sanitise.js

1
.jshintignore

@ -0,0 +1 @@
node_modules

21
.jshintrc

@ -0,0 +1,21 @@
{
"node": true,
"curly": true,
"eqeqeq": true,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": false,
"newcap": true,
"noarg": true,
"noempty": true,
"nonbsp": true,
"nonew": true,
"plusplus": false,
"quotmark": "single",
"undef": true,
"unused": false,
"maxparams": 4,
"maxdepth": 4,
"maxlen": 140
}

8
controller/suggest.js

@ -25,7 +25,7 @@ function setup( backend, query ){
// map response to a valid FeatureCollection
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
docs = data.pelias[0].options || [];
}
// convert docs to geojson
@ -56,16 +56,16 @@ function geoJsonifyDocs( docs ){
var geodata = docs.map( function( doc ){
// something went very wrong
if( !doc || !doc.payload ) return warning();
if( !doc || !doc.payload ){ return warning(); }
// split payload id string in to geojson properties
if( 'string' !== typeof doc.payload.id ) return warning();
if( 'string' !== typeof doc.payload.id ){ return warning(); }
var idParts = doc.payload.id.split('/');
doc.type = idParts[0];
doc.id = idParts[1];
// split payload geo string in to geojson properties
if( 'string' !== typeof doc.payload.geo ) return warning();
if( 'string' !== typeof doc.payload.geo ){ return warning(); }
var geoParts = doc.payload.geo.split(',');
doc.lat = parseFloat( geoParts[1] );
doc.lng = parseFloat( geoParts[0] );

12
index.js

@ -5,13 +5,13 @@ var cluster = require('cluster'),
port = ( process.env.PORT || 3100 );
/** cluster webserver across all cores **/
if( multicore ){
// if( multicore ){
// @todo: not finished yet
// cluster(app)
// .use(cluster.stats())
// .listen( process.env.PORT || 3100 );
}
else {
console.log( 'listening on ' + port );
app.listen( process.env.PORT || 3100 );
}
// }
// else {
console.log( 'listening on ' + port );
app.listen( process.env.PORT || 3100 );
// }

6
middleware/search.js

@ -11,7 +11,7 @@ var WGS84_a = 6378137.0; // Major semiaxis [m]
var WGS84_b = 6356752.3; // Minor semiaxis [m]
// Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
function WGS84EarthRadius(lat){
function wGS84EarthRadius(lat){
// http://en.wikipedia.org/wiki/Earth_radius
var An = WGS84_a*WGS84_a * Math.cos(lat);
var Bn = WGS84_b*WGS84_b * Math.sin(lat);
@ -29,7 +29,7 @@ function boundingBox(latitudeInDegrees, longitudeInDegrees, halfSideInKm) {
var halfSide = 1000*halfSideInKm;
// Radius of Earth at given latitude
var radius = WGS84EarthRadius(lat);
var radius = wGS84EarthRadius(lat);
// Radius of the parallel at given latitude
var pradius = radius*Math.cos(lat);
@ -62,4 +62,4 @@ function middleware(req, res, next){
}
// middleware
module.exports.middleware = middleware
module.exports.middleware = middleware;

6
package.json

@ -40,8 +40,10 @@
},
"devDependencies": {
"ciao": "^0.3.4",
"tape": "^2.13.4",
"jshint": "^2.5.6",
"nsp": "^0.3.0",
"precommit-hook": "^1.0.7",
"tap-spec": "^0.2.0",
"nsp": "^0.3.0"
"tape": "^2.13.4"
}
}

22
query/reverse.js

@ -4,23 +4,23 @@ var logger = require('../src/logger');
function generate( params ){
var cmd = {
"query":{
"filtered" : {
"query" : {
"match_all" : {}
'query':{
'filtered' : {
'query' : {
'match_all' : {}
},
"filter" : {
"geo_distance" : {
"distance" : "1km",
"center_point" : {
"lat": params.lat,
"lon": params.lon
'filter' : {
'geo_distance' : {
'distance' : '1km',
'center_point' : {
'lat': params.lat,
'lon': params.lon
}
}
}
}
},
"size": 1
'size': 1
};
// logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) );

48
query/search.js

@ -4,38 +4,38 @@ var logger = require('../src/logger');
function generate( params ){
var cmd = {
"query":{
"query_string" : {
"query": params.input,
"fields": ['name.default'],
"default_operator": 'OR'
'query':{
'query_string' : {
'query': params.input,
'fields': ['name.default'],
'default_operator': 'OR'
}
},
"filter": {
"geo_bounding_box": {
"center_point": {
"bottom_left": {
"lat": params.bbox.bottom_left.lat,
"lon": params.bbox.bottom_left.lon
'filter': {
'geo_bounding_box': {
'center_point': {
'bottom_left': {
'lat': params.bbox.bottom_left.lat,
'lon': params.bbox.bottom_left.lon
},
"top_right": {
"lat": params.bbox.top_right.lat,
"lon": params.bbox.top_right.lon
'top_right': {
'lat': params.bbox.top_right.lat,
'lon': params.bbox.top_right.lon
}
}
}
},
"sort" : [{
"_geo_distance" : {
"center_point" : {
"lat": params.lat,
"lon": params.lon
},
"order": 'asc',
"unit": 'km'
}
'sort' : [{
'_geo_distance' : {
'center_point' : {
'lat': params.lat,
'lon': params.lon
},
'order': 'asc',
'unit': 'km'
}
}],
"size": params.size
'size': params.size
};
// logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) );

4
sanitiser/_latlonzoom.js

@ -15,7 +15,7 @@ function sanitize( req ){
return {
'error': true,
'message': 'invalid param \'lat\': must be >0 and <90'
}
};
}
clean.lat = lat;
@ -25,7 +25,7 @@ function sanitize( req ){
return {
'error': true,
'message': 'invalid param \'lon\': must be >-180 and <180'
}
};
}
clean.lon = lon;

2
sanitiser/_layers.js

@ -21,7 +21,7 @@ function sanitize( req ){
return {
'error': true,
'message': 'invalid param \'layer\': must be one or more of ' + indeces.join(',')
}
};
}
}
clean.layers = layers;

8
sanitiser/reverse.js

@ -2,10 +2,12 @@
var logger = require('../src/logger'),
_sanitize = require('../sanitiser/_sanitize'),
sanitiser = {
latlonzoom: require('../sanitiser/_latlonzoom')
};
latlonzoom: require('../sanitiser/_latlonzoom')
};
var sanitize = function(req, cb) { _sanitize(req, sanitiser, cb); }
var sanitize = function(req, cb) {
_sanitize(req, sanitiser, cb);
};
// export sanitize for testing
module.exports.sanitize = sanitize;

19
sanitiser/suggest.js

@ -1,14 +1,17 @@
var logger = require('../src/logger'),
_sanitize = require('../sanitiser/_sanitize'),
sanitizers = {
input: require('../sanitiser/_input'),
size: require('../sanitiser/_size'),
layers: require('../sanitiser/_layers'),
latlonzoom: require('../sanitiser/_latlonzoom')
};
_sanitize = require('../sanitiser/_sanitize');
var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }
var sanitizers = {
input: require('../sanitiser/_input'),
size: require('../sanitiser/_size'),
layers: require('../sanitiser/_layers'),
latlonzoom: require('../sanitiser/_latlonzoom')
};
var sanitize = function(req, cb) {
_sanitize(req, sanitizers, cb);
};
// export sanitize for testing
module.exports.sanitize = sanitize;

2
test/unit/mock/backend.js

@ -19,7 +19,7 @@ responses['client/search/fail/1'] = function( cmd, cb ){
};
function setup( key, cmdCb ){
function backend( a, b ){
function backend(){
return {
client: {
suggest: function( cmd, cb ){

8
test/unit/sanitiser/sanitise.js

@ -3,8 +3,12 @@ var suggest = require('../../../sanitiser/suggest'),
_sanitize = suggest.sanitize,
middleware = suggest.middleware,
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test', lat: 0, layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood' ], lon: 0, size: 10, zoom: 10 },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
var defaultClean = {
input: 'test', lat: 0, lon: 0, size: 10, zoom: 10,
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood' ]
};
module.exports.tests = {};

Loading…
Cancel
Save