Skip to content

Commit bfbb98e

Browse files
committed
refactor: move logic for when to activate filter to function
This just helps separate the "when" from the "how"
1 parent 6037d95 commit bfbb98e

1 file changed

Lines changed: 47 additions & 44 deletions

File tree

sanitizer/_address_layer_filter.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,51 @@ const nonEmptyString = (v) => _.isString(v) && !_.isEmpty(v);
2525

2626
const ADDRESS_FILTER_WARNING = 'performance optimization: excluding \'address\' layer';
2727

28+
function can_remove_addresses(clean) {
29+
// default to using the full 'clean.text'
30+
// note: this should already have superfluous characters removed
31+
let input = clean.text;
32+
33+
// if a parser has removed tokens, use the parsed text instead, this
34+
// is the text which will be queried against the 'name.default' field.
35+
// @todo: this logic is duplicated from 'query/text_parser.js' and may
36+
// be subject to change.
37+
if (_.isObject(clean.parsed_text) && !_.isEmpty(clean.parsed_text)) {
38+
39+
var isStreetAddress = clean.parsed_text.hasOwnProperty('housenumber') && clean.parsed_text.hasOwnProperty('street');
40+
41+
// use $subject where available (pelias parser)
42+
if (_.has(clean, 'parsed_text.subject')) {
43+
input = clean.parsed_text.subject;
44+
}
45+
46+
// if 'pelias_parser' or 'libpostal' identified input as a street address
47+
else if (isStreetAddress) {
48+
input = clean.parsed_text.housenumber + ' ' + clean.parsed_text.street;
49+
}
50+
51+
// else if the 'naive parser' was used, input is equal to 'name'
52+
else if (nonEmptyString(clean.parsed_text.admin_parts) && nonEmptyString(clean.parsed_text.name)) {
53+
input = clean.parsed_text.name;
54+
}
55+
}
56+
57+
// count the number of words specified
58+
let totalWords = input.split(/\s+/).filter(nonEmptyString).length;
59+
60+
// check that at least one numeral was specified
61+
let hasNumeral = /\d/.test(input);
62+
63+
// do not consider numeric street names, such as '26 st' in numeric check.
64+
if( _.has(clean, 'parsed_text.street') ){
65+
hasNumeral = /\d/.test(input.replace(clean.parsed_text.street, ''));
66+
}
67+
68+
// if less than two words were specified /or no numeral is present
69+
// then it is safe to apply the layer filter
70+
return totalWords < 2 || !hasNumeral;
71+
}
72+
2873
function _setup(tm) {
2974

3075
return {
@@ -38,54 +83,12 @@ function _setup(tm) {
3883
return messages;
3984
}
4085

41-
// default to using the full 'clean.text'
42-
// note: this should already have superfluous characters removed
43-
let input = clean.text;
44-
4586
// do nothing if no input text specified in the request
46-
if (!nonEmptyString(input)) {
87+
if (!nonEmptyString(clean.text)) {
4788
return messages;
4889
}
4990

50-
// if a parser has removed tokens, use the parsed text instead, this
51-
// is the text which will be queried against the 'name.default' field.
52-
// @todo: this logic is duplicated from 'query/text_parser.js' and may
53-
// be subject to change.
54-
if (_.isObject(clean.parsed_text) && !_.isEmpty(clean.parsed_text)) {
55-
56-
var isStreetAddress = clean.parsed_text.hasOwnProperty('housenumber') && clean.parsed_text.hasOwnProperty('street');
57-
58-
// use $subject where available (pelias parser)
59-
if (_.has(clean, 'parsed_text.subject')) {
60-
input = clean.parsed_text.subject;
61-
}
62-
63-
// if 'pelias_parser' or 'libpostal' identified input as a street address
64-
else if (isStreetAddress) {
65-
input = clean.parsed_text.housenumber + ' ' + clean.parsed_text.street;
66-
}
67-
68-
// else if the 'naive parser' was used, input is equal to 'name'
69-
else if (nonEmptyString(clean.parsed_text.admin_parts) && nonEmptyString(clean.parsed_text.name)) {
70-
input = clean.parsed_text.name;
71-
}
72-
}
73-
74-
// count the number of words specified
75-
let totalWords = input.split(/\s+/).filter(nonEmptyString).length;
76-
77-
// check that at least one numeral was specified
78-
let hasNumeral = /\d/.test(input);
79-
80-
// do not consider numeric street names, such as '26 st' in numeric check.
81-
if( _.has(clean, 'parsed_text.street') ){
82-
hasNumeral = /\d/.test(input.replace(clean.parsed_text.street, ''));
83-
}
84-
85-
// if less than two words were specified /or no numeral is present
86-
// then it is safe to apply the layer filter
87-
if (totalWords < 2 || !hasNumeral) {
88-
91+
if (can_remove_addresses(clean)) {
8992
// handle the common case where neither sources nor (positive) layers were specified
9093
if (!_.isArray(clean.sources) || _.isEmpty(clean.sources)) {
9194
// if there are no layers already set, start with the list of all of them

0 commit comments

Comments
 (0)