-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.js
54 lines (49 loc) · 1.43 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
42
43
44
45
46
47
48
49
50
51
52
53
54
"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 []
}
}
function findFromIsoCode(isoCodeValue){
if (isoCodeValue) {
const cityLookup = _.filter(cityMapping, function (o) {
return o.iso2.toString().toLowerCase() === isoCodeValue.toLowerCase() || o.iso3.toString().toLowerCase() === isoCodeValue.toLowerCase() })
if (cityLookup && cityLookup.length) {
return cityLookup
} else {
return []
}
} else {
return []
}
}
module.exports = {
lookupViaCity,
findFromCityStateProvince,
findFromIsoCode,
cityMapping
};