-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
135 lines (126 loc) · 3.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const axios = require('axios');
const fetch = (jsonPathName) => axios.get(`https://isaacdarcilla.github.io/philippine-addresses/${jsonPathName}.json`);
/**
* @param NULL
* @returns all regions
*/
const regions = async () => {
try {
const regions = await fetch('region');
return regions.data.map((region) => {
return {
id: region.id,
psgc_code: region.psgc_code,
region_name: region.region_name,
region_code: region.region_code
};
})
} catch (e) {
return e.message;
}
};
/**
* @param REGION_CODE
* @returns region base on region code parameter.
*/
const regionByCode = async (code) => {
try {
const regions = await fetch('region');
return regions.data.find( ({region_code}) => region_code === code)
} catch (e) {
return e.message;
}
};
/** Default
* @param REGION_CODE
* @returns all provinces base on region code parameter.
*/
const provinces = async (code) => {
try {
const provinces = await fetch('province');
return provinces.data.filter(province => province.region_code === code)
.map((filtered) => {
return {
psgc_code: filtered.psgc_code,
province_name: filtered.province_name,
province_code: filtered.province_code,
region_code: filtered.region_code
};
});
} catch (e) {
return e.message;
}
};
/**
* @param REGION_CODE
* @returns all provinces base on region code parameter.
*/
const provincesByCode = async (code) => {
try {
const provinces = await fetch('province');
return provinces.data.filter(province => province.region_code === code)
.map((filtered) => {
return {
psgc_code: filtered.psgc_code,
province_name: filtered.province_name,
province_code: filtered.province_code,
region_code: filtered.region_code
};
});
} catch (e) {
return e.message;
}
};
/**
* @param PROVINCE_NAME
* @returns province base on province name parameter.
*/
const provinceByName = async (name) => {
try {
const provinces = await fetch('province');
return provinces.data.find( ({province_name}) => province_name === name)
} catch (e) {
return e.message;
}
};
/**
* @param PROVINCE_CODE
* @returns all cities base on province code parameter.
*/
const cities = async (code) => {
try {
const cities = await fetch('city');
return cities.data.filter(city => city.province_code === code)
.map((filtered) => {
return {
city_name: filtered.city_name,
city_code: filtered.city_code,
province_code: filtered.province_code,
region_desc: filtered.region_desc,
};
});
} catch (e) {
return e.message;
}
};
/**
* @param CITY_CODE
* @returns all barangays base on city code parameter.
*/
const barangays = async (code) => {
try {
const barangays = await fetch('barangay')
return barangays.data.filter(barangay => barangay.city_code === code)
.map((filtered) => {
return {
brgy_name: filtered.brgy_name,
brgy_code: filtered.brgy_code,
province_code: filtered.province_code,
region_code: filtered.region_code,
};
});
} catch (e) {
return e.message;
}
};
module.exports = { regions, regionByCode, provinces, provincesByCode, provinceByName, cities, barangays };