Browse Source

Get rid of ugly for-loops using lodash

pull/453/head
Vesa Meskanen 9 years ago
parent
commit
0d0651f8ac
  1. 21
      middleware/localNamingConventions.js

21
middleware/localNamingConventions.js

@ -1,4 +1,5 @@
var check = require('check-types'); var check = require('check-types');
var _ = require('lodash');
var flipNumberAndStreetCountries = ['DEU', 'FIN', 'SWE', 'NOR', 'DNK', 'ISL']; var flipNumberAndStreetCountries = ['DEU', 'FIN', 'SWE', 'NOR', 'DNK', 'ISL'];
@ -7,12 +8,7 @@ function setup() {
var settings = api.localization; var settings = api.localization;
if (settings && settings.flipNumberAndStreetCountries) { if (settings && settings.flipNumberAndStreetCountries) {
var countries = settings.flipNumberAndStreetCountries; var countries = settings.flipNumberAndStreetCountries;
for (var i=0; i<countries.length; i++) { flipNumberAndStreetCountries = _.uniq(flipNumberAndStreetCountries.concat(countries));
var country = countries[i];
if ( flipNumberAndStreetCountries.indexOf(country===-1) ) {
flipNumberAndStreetCountries.push(country);
}
}
} }
return applyLocalNamingConventions; return applyLocalNamingConventions;
@ -28,17 +24,10 @@ function applyLocalNamingConventions(req, res, next) {
// loop through data items and flip relevant number/street // loop through data items and flip relevant number/street
res.data.filter(function(place){ res.data.filter(function(place){
// relevant for some countries // relevant for some countries
var flip = false; var flip = place.parent.country_a.some(function(country) {
for (var i=0; i<place.parent.country_a.length; i++) { return _.includes(flipNumberAndStreetCountries, country);
var country_a = place.parent.country_a[i]; });
if( flipNumberAndStreetCountries.indexOf(country_a) !== -1 ) {
flip = true;
break;
}
}
if (!flip){ return false; } if (!flip){ return false; }
if( place.parent.country_a.indexOf('DEU') === -1 ){ return false; }
if( !place.hasOwnProperty('address_parts') ){ return false; } if( !place.hasOwnProperty('address_parts') ){ return false; }
if( !place.address_parts.hasOwnProperty('number') ){ return false; } if( !place.address_parts.hasOwnProperty('number') ){ return false; }
if( !place.address_parts.hasOwnProperty('street') ){ return false; } if( !place.address_parts.hasOwnProperty('street') ){ return false; }

Loading…
Cancel
Save