-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
54 lines (44 loc) · 1.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var locations = require('./locations');
var haversine = require('haversine');
var _ = require('lodash');
var find = exports.find = _.partial(_.find, locations);
var filter = exports.filter = _.partial(_.filter, locations);
exports.zip_lookup = exports.zipLookup = function(zipcode) {
zipcode = _.padStart(zipcode, 5, 0);
return find({
zipcode: zipcode
});
};
exports.gps_lookup = exports.gpsLookup = function(latitude, longitude) {
var minDistance = Infinity; // simulate infinity
var minLocation = {};
var start = {
latitude: latitude,
longitude: longitude
}
for (var i = 0; i < locations.length; i++) {
var distance = haversine(start, locations[i]);
if (distance < minDistance) {
minLocation = locations[i];
minDistance = distance;
}
}
minLocation.distance = minDistance;
return minLocation;
};
exports.findByState = function(state) {
state = state.toUpperCase()
return filter({
state_abbr: state
})
}
exports.findByCityAndState = function(city, state) {
state = state.toUpperCase()
city = city.toUpperCase()
var cities = filter({
state_abbr: state
})
return _.find(cities, function(c) {
return c.city.toUpperCase() === city
})
}