Skip to content

Commit 5898e75

Browse files
authored
Merge pull request pelias#1404 from pelias/remove_check_types
remove npm check-types module
2 parents ad5e0e3 + a7d908e commit 5898e75

23 files changed

+197
-209
lines changed

middleware/500.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var check = require('check-types'),
2-
logger = require( 'pelias-logger' ).get( 'api' );
1+
const _ = require('lodash');
2+
const logger = require('pelias-logger').get('api');
33

44
// handle application errors
5-
function middleware(err, req, res, next) {
5+
function middleware(err, req, res) {
66

77
logger.error( 'Error: `%s`. Stack trace: `%s`.', err, err.stack );
88

@@ -11,9 +11,16 @@ function middleware(err, req, res, next) {
1111
res.status(500);
1212
}
1313

14-
var error = ( err && err.message ) ? err.message : err;
14+
// set error message
15+
const error = (err && err.message) ? err.message : err;
16+
let msg = 'internal server error';
17+
if (_.isString(error) && !_.isEmpty(error)) {
18+
msg = error;
19+
}
20+
21+
// send response
1522
res.header('Cache-Control','public');
16-
res.json({ error: check.nonEmptyString( error ) ? error : 'internal server error' });
23+
res.json({ error: msg });
1724
}
1825

1926
module.exports = middleware;

middleware/accuracy.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
* Accuracy level should be set for each item in the results.
44
* The level can be any of the following:
55
* - point
6-
* - interpolated
6+
* - interpolated (not currently used)
77
* - centroid
88
*/
99

10-
var check = require('check-types');
11-
12-
var accuracyLevelPoint = 'point';
13-
var accuracyLevelInterpolated = 'interpolated';
14-
var accuracyLevelCentroid = 'centroid';
10+
const _ = require('lodash');
1511

12+
const accuracyLevelPoint = 'point';
13+
// const accuracyLevelInterpolated = 'interpolated';
14+
const accuracyLevelCentroid = 'centroid';
1615

1716
function setup() {
1817
return computeAccuracy;
1918
}
2019

2120
function computeAccuracy(req, res, next) {
2221
// do nothing if no result data set
23-
if (check.undefined(res) || check.undefined(res.data)) {
22+
if (_.isUndefined(res) || _.isUndefined(res.data)) {
2423
return next();
2524
}
2625

middleware/confidenceScore.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111
* - detection (or specification) of query type. i.e. an address shouldn't match an admin address.
1212
*/
1313

14+
const _ = require('lodash');
1415
const stats = require('stats-lite');
1516
const logger = require('pelias-logger').get('api');
16-
const check = require('check-types');
1717
const field = require('../helper/fieldValue');
1818

1919
var RELATIVE_SCORES = true;
2020

2121
function setup(peliasConfig) {
22-
if (check.assigned(peliasConfig)) {
22+
if (!_.isNil(peliasConfig)) {
2323
RELATIVE_SCORES = peliasConfig.hasOwnProperty('relativeScores') ? peliasConfig.relativeScores : true;
2424
}
2525
return computeScores;
2626
}
2727

2828
function computeScores(req, res, next) {
2929
// do nothing if no result data set or if query is not of the pelias_parser variety
30-
if (check.undefined(req.clean) || check.undefined(res) ||
31-
check.undefined(res.data) || check.undefined(res.meta) ||
30+
if (_.isUndefined(req.clean) || _.isUndefined(res) ||
31+
_.isUndefined(res.data) || _.isUndefined(res.meta) ||
3232
res.meta.query_type !== 'search_pelias_parser') {
3333
return next();
3434
}
@@ -91,17 +91,17 @@ function computeConfidenceScore(req, mean, stdev, hit) {
9191
* @returns {bool}
9292
*/
9393
function checkForDealBreakers(req, hit) {
94-
if (check.undefined(req.clean.parsed_text)) {
94+
if (_.isUndefined(req.clean.parsed_text)) {
9595
return false;
9696
}
9797

98-
if (check.assigned(req.clean.parsed_text.state) && check.assigned(hit.parent) &&
98+
if (!_.isNil(req.clean.parsed_text.state) && !_.isNil(hit.parent) &&
9999
hit.parent.region_a && req.clean.parsed_text.state !== hit.parent.region_a[0]) {
100100
logger.debug('[confidence][deal-breaker]: state !== region_a');
101101
return true;
102102
}
103103

104-
if (check.assigned(req.clean.parsed_text.postalcode) && check.assigned(hit.address_parts) &&
104+
if (!_.isNil(req.clean.parsed_text.postalcode) && !_.isNil(hit.address_parts) &&
105105
req.clean.parsed_text.postalcode !== hit.address_parts.zip) {
106106
return true;
107107
}
@@ -131,7 +131,7 @@ function checkDistanceFromMean(score, mean, stdev) {
131131
*/
132132
function checkName(text, parsed_text, hit) {
133133
// parsed_text name should take precedence if available since it's the cleaner name property
134-
if (check.assigned(parsed_text) && check.assigned(parsed_text.name) &&
134+
if (!_.isNil(parsed_text) && !_.isNil(parsed_text.name) &&
135135
field.getStringValue(hit.name.default).toLowerCase() === parsed_text.name.toLowerCase()) {
136136
return 1;
137137
}
@@ -154,9 +154,9 @@ function checkName(text, parsed_text, hit) {
154154
* @returns {number}
155155
*/
156156
function checkQueryType(text, hit) {
157-
if (check.assigned(text) && check.assigned(text.number) &&
158-
(check.undefined(hit.address_parts) ||
159-
(check.assigned(hit.address_parts) && check.undefined(hit.address_parts.number)))) {
157+
if (!_.isNil(text) && !_.isNil(text.number) &&
158+
(_.isUndefined(hit.address_parts) ||
159+
(!_.isNil(hit.address_parts) && _.isUndefined(hit.address_parts.number)))) {
160160
return 0;
161161
}
162162
return 1;
@@ -173,22 +173,22 @@ function checkQueryType(text, hit) {
173173
function propMatch(textProp, hitProp, expectEnriched) {
174174

175175
// both missing, but expect to have enriched value in result => BAD
176-
if (check.undefined(textProp) && check.undefined(hitProp) && check.assigned(expectEnriched)) { return 0; }
176+
if (_.isUndefined(textProp) && _.isUndefined(hitProp) && !_.isNil(expectEnriched)) { return 0; }
177177

178178
// both missing, and no enrichment expected => GOOD
179-
if (check.undefined(textProp) && check.undefined(hitProp)) { return 1; }
179+
if (_.isUndefined(textProp) && _.isUndefined(hitProp)) { return 1; }
180180

181181
// text has it, result doesn't => BAD
182-
if (check.assigned(textProp) && check.undefined(hitProp)) { return 0; }
182+
if (!_.isNil(textProp) && _.isUndefined(hitProp)) { return 0; }
183183

184184
// text missing, result has it, and enrichment is expected => GOOD
185-
if (check.undefined(textProp) && check.assigned(hitProp) && check.assigned(expectEnriched)) { return 1; }
185+
if (_.isUndefined(textProp) && !_.isNil(hitProp) && !_.isNil(expectEnriched)) { return 1; }
186186

187187
// text missing, result has it, enrichment not desired => 50/50
188-
if (check.undefined(textProp) && check.assigned(hitProp)) { return 0.5; }
188+
if (_.isUndefined(textProp) && !_.isNil(hitProp)) { return 0.5; }
189189

190190
// both present, values match => GREAT
191-
if (check.assigned(textProp) && check.assigned(hitProp) &&
191+
if (!_.isNil(textProp) && !_.isNil(hitProp) &&
192192
textProp.toString().toLowerCase() === hitProp.toString().toLowerCase()) { return 1; }
193193

194194
// ¯\_(ツ)_/¯
@@ -218,7 +218,7 @@ function checkAddress(text, hit) {
218218
var checkCount = 5;
219219
var res = 0;
220220

221-
if (check.assigned(text) && check.assigned(text.number) && check.assigned(text.street)) {
221+
if (!_.isNil(text) && !_.isNil(text.number) && !_.isNil(text.street)) {
222222
res += propMatch(text.number, (hit.address_parts ? hit.address_parts.number : null), false);
223223
res += propMatch(text.street, (hit.address_parts ? hit.address_parts.street : null), false);
224224
res += propMatch(text.postalcode, (hit.address_parts ? hit.address_parts.zip: null), true);

middleware/confidenceScoreFallback.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
* - fallback status (aka layer match between expected and actual)
1010
*/
1111

12-
var check = require('check-types');
13-
var logger = require('pelias-logger').get('api');
1412
const _ = require('lodash');
13+
const logger = require('pelias-logger').get('api');
1514

1615
function setup() {
1716
return computeScores;
@@ -20,8 +19,8 @@ function setup() {
2019
function computeScores(req, res, next) {
2120
// do nothing if no result data set or if the query is not of the fallback variety
2221
// later add disambiguation to this list
23-
if (check.undefined(req.clean) || check.undefined(res) ||
24-
check.undefined(res.data) || check.undefined(res.meta)) {
22+
if (_.isUndefined(req.clean) || _.isUndefined(res) ||
23+
_.isUndefined(res.data) || _.isUndefined(res.meta)) {
2524
return next();
2625
}
2726

@@ -78,7 +77,7 @@ function checkFallbackLevel(req, hit) {
7877
case 'street':
7978
return 0.8;
8079
case 'postalcode':
81-
return 0.8;
80+
return 0.8;
8281
case 'localadmin':
8382
case 'locality':
8483
case 'borough':

middleware/distance.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
var geolib = require('geolib');
2-
var check = require('check-types');
3-
1+
const _ = require('lodash');
2+
const geolib = require('geolib');
43

54
function setup(prefix) {
6-
75
return function (req, res, next) {
8-
var opts = {
6+
const opts = {
97
prefix: prefix || 'point.'
108
};
119
return computeDistances(req, res, next, opts);
@@ -19,8 +17,8 @@ function computeDistances(req, res, next, opts) {
1917
return next();
2018
}
2119

22-
if (!(check.number(req.clean[opts.prefix + 'lat']) &&
23-
check.number(req.clean[opts.prefix + 'lon']))) {
20+
if (!(_.isFinite(req.clean[opts.prefix + 'lat']) &&
21+
_.isFinite(req.clean[opts.prefix + 'lon']))) {
2422
return next();
2523
}
2624

middleware/localNamingConventions.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const check = require('check-types');
21
const _ = require('lodash');
32
const field = require('../helper/fieldValue');
43

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"@hapi/joi": "^16.0.1",
4040
"@mapbox/geojson-extent": "^0.3.1",
4141
"async": "^3.0.1",
42-
"check-types": "^10.0.0",
4342
"elasticsearch": "^16.0.0",
4443
"express": "^4.8.8",
4544
"geojson": "^0.5.0",

query/address_search_using_ids.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
const _ = require('lodash');
12
const peliasQuery = require('pelias-query');
23
const defaults = require('./search_defaults');
3-
const logger = require('pelias-logger').get('api');
4-
const _ = require('lodash');
5-
const check = require('check-types');
64

75
//------------------------------
86
// general-purpose search query
@@ -142,19 +140,19 @@ function generateQuery( clean, res ){
142140
}
143141

144142
// focus point
145-
if( check.number(clean['focus.point.lat']) &&
146-
check.number(clean['focus.point.lon']) ){
143+
if( _.isFinite(clean['focus.point.lat']) &&
144+
_.isFinite(clean['focus.point.lon']) ){
147145
vs.set({
148146
'focus:point:lat': clean['focus.point.lat'],
149147
'focus:point:lon': clean['focus.point.lon']
150148
});
151149
}
152150

153151
// boundary rect
154-
if( check.number(clean['boundary.rect.min_lat']) &&
155-
check.number(clean['boundary.rect.max_lat']) &&
156-
check.number(clean['boundary.rect.min_lon']) &&
157-
check.number(clean['boundary.rect.max_lon']) ){
152+
if( _.isFinite(clean['boundary.rect.min_lat']) &&
153+
_.isFinite(clean['boundary.rect.max_lat']) &&
154+
_.isFinite(clean['boundary.rect.min_lon']) &&
155+
_.isFinite(clean['boundary.rect.max_lon']) ){
158156
vs.set({
159157
'boundary:rect:top': clean['boundary.rect.max_lat'],
160158
'boundary:rect:right': clean['boundary.rect.max_lon'],
@@ -164,29 +162,29 @@ function generateQuery( clean, res ){
164162
}
165163

166164
// boundary circle
167-
if( check.number(clean['boundary.circle.lat']) &&
168-
check.number(clean['boundary.circle.lon']) ){
165+
if( _.isFinite(clean['boundary.circle.lat']) &&
166+
_.isFinite(clean['boundary.circle.lon']) ){
169167
vs.set({
170168
'boundary:circle:lat': clean['boundary.circle.lat'],
171169
'boundary:circle:lon': clean['boundary.circle.lon']
172170
});
173171

174-
if( check.number(clean['boundary.circle.radius']) ){
172+
if( _.isFinite(clean['boundary.circle.radius']) ){
175173
vs.set({
176174
'boundary:circle:radius': Math.round( clean['boundary.circle.radius'] ) + 'km'
177175
});
178176
}
179177
}
180178

181179
// boundary country
182-
if( check.nonEmptyArray(clean['boundary.country']) ){
180+
if( _.isArray(clean['boundary.country']) && !_.isEmpty(clean['boundary.country']) ){
183181
vs.set({
184182
'boundary:country': clean['boundary.country'].join(' ')
185183
});
186184
}
187185

188186
// boundary gid
189-
if ( check.string(clean['boundary.gid']) ){
187+
if ( _.isString(clean['boundary.gid']) ){
190188
vs.set({
191189
'boundary:gid': clean['boundary.gid']
192190
});

0 commit comments

Comments
 (0)