forked from kevinroberts/city-timezones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (36 loc) · 1.03 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
40
41
"use strict";
const _ = require('lodash')
const cityMapping = require('./data/cityMap.json')
const cityMappingDict = require('./data/cityMapDict.json')
function lookupViaCity(city) {
const cityLookup = cityMappingDict[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
};