-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (63 loc) · 1.54 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
const fs = require('fs');
const path = require('path');
const data = JSON.parse(
fs.readFileSync(path.join(__dirname, 'data/serbia_zip_codes.json'), 'utf8')
);
/**
* Get all dataset
* @returns {Array.<{city: string, zip_code: string}>}
*/
function getAll() {
return data;
}
/**
* Search by city name
* @param {string} city
* @returns {Array.<{city: string, zip_code: string}>}
*/
function findByCitySync(city) {
return data.filter(function (d) {
return d.city.match(RegExp(city, 'i'));
});
}
/**
* Search by zip code
* @param {string | number} zip
* @returns {Array.<{city: string, zip_code: string}>}
*/
function findByZipSync(zip) {
return data.filter(function (d) {
return d.zip_code.match(RegExp(zip, 'i'));
});
}
/**
* Search by city name
* @param {string} city
* @returns {Promise<Array.<{city: string, zip_code: string}> | Error>}
*/
function findByCity(city) {
return new Promise((resolve, reject) => {
if (!city) reject(new Error('Function parameter missing!'));
const result = findByCitySync(city);
resolve(result);
});
}
/**
* Search by zip code
* @param {string | number} zip
* @returns {Promise<Array.<{city: string, zip_code: string}> | Error>}
*/
function findByZip(zip) {
return new Promise((resolve, reject) => {
if (!zip) reject(new Error('Function parameter missing!'));
const result = findByZipSync(zip);
resolve(result);
});
}
module.exports = {
getAll: getAll,
findByCitySync: findByCitySync,
findByZipSync: findByZipSync,
findByCity: findByCity,
findByZip: findByZip,
};