forked from kevinroberts/city-timezones
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (35 loc) · 1.02 KB
/
index.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
"use strict";
const _ = require('lodash')
const cityMapping = require('./data/cityMap.json')
function lookupViaCity(city) {
const cityLookup = _.filter(cityMapping, function (o) { return o.city?.toLowerCase() === city.toLowerCase() })
if (cityLookup && cityLookup.length) {
return cityLookup
} else {
return []
}
}
function findPartialMatch(itemsToSearch, searchString) {
const searchItems = searchString.split(" ");
const isPartialMatch = searchItems.every(function(i) {
return itemsToSearch.join().toLowerCase().indexOf(i.toLowerCase()) >= 0;
})
return isPartialMatch
}
function findFromCityStateProvince(searchString) {
if (searchString) {
const cityLookup = _.filter(cityMapping, function (o) { return findPartialMatch([o.city,o.state_ansi,o.province,o.country], searchString) })
if (cityLookup && cityLookup.length) {
return cityLookup
} else {
return []
}
} else {
return []
}
}
module.exports = {
lookupViaCity,
findFromCityStateProvince,
cityMapping
};