Browse Source

Merge pull request #252 from pelias/lodash

lodash-ify some array functions; remove 'yes' and 'y' from thruthy
pull/256/head
Diana Shkolnikov 9 years ago
parent
commit
04fb595967
  1. 8
      helper/adminFields.js
  2. 9
      helper/geojsonify.js
  3. 6
      sanitiser/_details.js
  4. 12
      sanitiser/_id.js
  5. 5
      sanitiser/_source.js
  6. 4
      test/unit/sanitiser/reverse.js
  7. 4
      test/unit/sanitiser/search.js

8
helper/adminFields.js

@ -1,5 +1,7 @@
var peliasSchema = require('pelias-schema');
var peliasLogger = require( 'pelias-logger' ).get( 'api' ); var _ = require('lodash'),
peliasSchema = require('pelias-schema'),
peliasLogger = require( 'pelias-logger' ).get( 'api' );
var ADMIN_FIELDS = [ var ADMIN_FIELDS = [
'admin0', 'admin0',
@ -29,7 +31,7 @@ function getAvailableAdminFields(schema, expectedFields, logger) {
// check if expected fields are actually in current schema // check if expected fields are actually in current schema
var available = expectedFields.filter(function (field) { var available = expectedFields.filter(function (field) {
return (actualFields.indexOf(field) !== -1); return _.contains( actualFields, field );
}); });
if (available.length === 0) { if (available.length === 0) {

9
helper/geojsonify.js

@ -1,9 +1,8 @@
var GeoJSON = require('geojson'); var GeoJSON = require('geojson'),
var extent = require('geojson-extent'); extent = require('geojson-extent'),
var outputGenerator = require('./outputGenerator'); outputGenerator = require('./outputGenerator'),
var logger = require('pelias-logger').get('api'); logger = require('pelias-logger').get('api');
// Properties to be copied when details=true // Properties to be copied when details=true
var DETAILS_PROPS = [ var DETAILS_PROPS = [

6
sanitiser/_details.js

@ -1,5 +1,7 @@
var check = require('check-types'); var _ = require('lodash'),
check = require('check-types');
var DEFAULT_DETAILS_BOOL = true; var DEFAULT_DETAILS_BOOL = true;
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
@ -20,7 +22,7 @@ function sanitize( raw, clean ){
// be lenient with 'truthy' values // be lenient with 'truthy' values
function isTruthy(val) { function isTruthy(val) {
if( check.string( val ) ){ if( check.string( val ) ){
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1; return _.contains( ['true', '1'], val );
} }
return val === 1 || val === true; return val === 1 || val === true;

12
sanitiser/_id.js

@ -1,5 +1,6 @@
var check = require('check-types'), var _ = require('lodash'),
check = require('check-types'),
types = require('../query/types'); types = require('../query/types');
var ID_DELIM = ':'; var ID_DELIM = ':';
@ -18,12 +19,7 @@ function sanitize( raw, clean ){
var messages = { errors: [], warnings: [] }; var messages = { errors: [], warnings: [] };
// 'raw.id' can be an array!? // 'raw.id' can be an array!?
var rawIds = check.array( raw.id ) ? raw.id : [ raw.id ]; var rawIds = check.array( raw.id ) ? _.unique( raw.id ) : [ raw.id ];
// de-dupe ids
rawIds = rawIds.filter(function(item, pos) {
return rawIds.indexOf( item ) === pos;
});
// ensure all elements are valid non-empty strings // ensure all elements are valid non-empty strings
rawIds = rawIds.filter( function( uc ){ rawIds = rawIds.filter( function( uc ){
@ -60,7 +56,7 @@ function sanitize( raw, clean ){
messages.errors.push( errorMessage( rawId ) ); messages.errors.push( errorMessage( rawId ) );
} }
// type text must be one of the types // type text must be one of the types
if( types.indexOf( type ) === -1 ){ if( !_.contains( types, type ) ){
messages.errors.push( messages.errors.push(
errorMessage('type', type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']') errorMessage('type', type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']')
); );

5
sanitiser/_source.js

@ -1,5 +1,6 @@
var check = require('check-types'), var _ = require('lodash'),
check = require('check-types'),
sources_map = require( '../query/sources' ); sources_map = require( '../query/sources' );
var ALL_SOURCES = Object.keys(sources_map), var ALL_SOURCES = Object.keys(sources_map),
@ -20,7 +21,7 @@ function sanitize( raw, clean ) {
var sources = raw.source.split(','); var sources = raw.source.split(',');
var invalid_sources = sources.filter(function(source) { var invalid_sources = sources.filter(function(source) {
return ALL_SOURCES.indexOf(source) === -1; return !_.contains( ALL_SOURCES, source );
}); });
if( invalid_sources.length > 0 ){ if( invalid_sources.length > 0 ){

4
test/unit/sanitiser/reverse.js

@ -133,7 +133,7 @@ module.exports.tests.sanitize_details = function(test, common) {
}); });
}); });
var valid_values = [true, 'true', 1, '1', 'yes', 'y']; var valid_values = [true, 'true', 1, '1'];
valid_values.forEach(function(details) { valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) { test('valid details param ' + details, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){ sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){
@ -150,7 +150,7 @@ module.exports.tests.sanitize_details = function(test, common) {
}); });
}); });
var valid_false_values = ['false', false, 0, '0', 'no', 'n']; var valid_false_values = ['false', false, 0, '0'];
valid_false_values.forEach(function(details) { valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) { test('test setting false explicitly ' + details, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){ sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){

4
test/unit/sanitiser/search.js

@ -252,7 +252,7 @@ module.exports.tests.sanitize_details = function(test, common) {
}); });
}); });
var valid_values = ['true', true, 1, '1', 'yes', 'y']; var valid_values = ['true', true, 1, '1'];
valid_values.forEach(function(details) { valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) { test('valid details param ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){ sanitize({ text: 'test', details: details }, function( err, clean ){
@ -262,7 +262,7 @@ module.exports.tests.sanitize_details = function(test, common) {
}); });
}); });
var valid_false_values = ['false', false, 0, '0', 'no', 'n']; var valid_false_values = ['false', false, 0, '0'];
valid_false_values.forEach(function(details) { valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) { test('test setting false explicitly ' + details, function(t) {
sanitize({ text: 'test', details: details }, function( err, clean ){ sanitize({ text: 'test', details: details }, function( err, clean ){

Loading…
Cancel
Save