-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (66 loc) · 2.55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
const _ = require('lodash');
const citiesRawData = require('./data/cities/latest.json');
const timezonesCountriesRawData = require('./data/timezones_countries/latest.json');
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 attachOffsetsWithCities(cities, timezones) {
return cities.map(city => {
const { utcOffset, offsetStr } = timezones[city.timezone];
city.utcOffset = utcOffset;
city.offsetStr = offsetStr;
return city;
});
};
const countriesAndTimezones = {
getAllCountries: function () {
return timezonesCountriesRawData.countries;
},
getAllTimezones: function () {
return timezonesCountriesRawData.timezones;
},
getTimezonesForCountry: function (countryId) {
const countries = this.getAllCountries();
const timezones = this.getAllTimezones();
const timezoneIds = (countries[countryId] || {}).timezones || [];
return timezoneIds.map(function (timezoneId) {
return timezones[timezoneId];
});
},
getCountriesForTimezone: function (timezoneId) {
const countries = this.getAllCountries();
const timezones = this.getAllTimezones();
const countryIds = (timezones[timezoneId] || {}).countries || [];
return countryIds.map(function (countryId) {
return countries[countryId];
});
},
lookupViaCity: function (city) {
let cityLookup = _.filter(citiesRawData, function (o) { return o.city.toLowerCase() === city.toLowerCase() })
if (cityLookup && cityLookup.length) {
cityLookup = attachOffsetsWithCities(cityLookup, this.getAllTimezones()[(city.timezone)]);
return cityLookup;
} else {
return [];
}
},
findFromCityStateProvince: function (searchString) {
if (searchString) {
let cityLookup = _.filter(citiesRawData, function (o) { return findPartialMatch([o.city, o.state_ansi, o.province, o.country], searchString) })
if (cityLookup && cityLookup.length) {
cityLookup = attachOffsetsWithCities(cityLookup, this.getAllTimezones());
return cityLookup;
} else {
return [];
};
} else {
return [];
};
}
};
module.exports = countriesAndTimezones;