latest-ph-address-thanks-to-anehan 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +569 -0
- package/data/addresses.json +350154 -0
- package/data/by-level.json +350036 -0
- package/data/by-psgc.json +350154 -0
- package/data/hierarchy.json +3458 -0
- package/index.js +178 -0
- package/package.json +40 -0
package/index.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
const addressesData = require('./data/addresses.json');
|
|
2
|
+
const byPsgcData = require('./data/by-psgc.json');
|
|
3
|
+
const byLevelData = require('./data/by-level.json');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get all regions
|
|
7
|
+
* @returns {Array} Array of all regions (sorted A-Z)
|
|
8
|
+
*/
|
|
9
|
+
function getRegions() {
|
|
10
|
+
return [...byLevelData.regions].sort((a, b) => a.name.localeCompare(b.name));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get all provinces in a selected region
|
|
15
|
+
* If no region is provided, returns all provinces plus "-NO PROVINCE-" option
|
|
16
|
+
* Returns "-NO PROVINCE-" if the selected region is NCR
|
|
17
|
+
* @param {string} regionPsgc - Optional: Region PSGC code
|
|
18
|
+
* @returns {Array|string} Array of provinces (sorted A-Z) or "-NO PROVINCE-" for NCR
|
|
19
|
+
*/
|
|
20
|
+
function getProvincesByRegion(regionPsgc = null) {
|
|
21
|
+
// If no region provided, return all provinces plus "-NO PROVINCE-" option
|
|
22
|
+
if (!regionPsgc) {
|
|
23
|
+
const allProvinces = [...byLevelData.provinces].sort((a, b) => a.name.localeCompare(b.name));
|
|
24
|
+
return [{ psgc: '-NO PROVINCE-', name: '-NO PROVINCE-' }, ...allProvinces];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// NCR has no provinces
|
|
28
|
+
if (regionPsgc === '1300000000') {
|
|
29
|
+
return '-NO PROVINCE-';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Filter provinces by region (first 2 digits of PSGC)
|
|
33
|
+
const regionPrefix = regionPsgc.substring(0, 2);
|
|
34
|
+
return byLevelData.provinces
|
|
35
|
+
.filter(prov => prov.psgc.substring(0, 2) === regionPrefix)
|
|
36
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get all cities and municipalities geographically located in selected province
|
|
41
|
+
* If "-NO PROVINCE-" is provided, returns cities and municipalities in NCR Region
|
|
42
|
+
* @param {string|string} provincePsgc - Province PSGC code or "-NO PROVINCE-"
|
|
43
|
+
* @param {string} regionPsgc - Optional: Region PSGC code (required when provincePsgc is "-NO PROVINCE-")
|
|
44
|
+
* @returns {Array} Array of cities and municipalities (sorted A-Z)
|
|
45
|
+
*/
|
|
46
|
+
function getCitiesAndMunsByProvince(provincePsgc, regionPsgc = null) {
|
|
47
|
+
// Handle "-NO PROVINCE-" case (NCR) - must be explicitly "-NO PROVINCE-"
|
|
48
|
+
if (provincePsgc === '-NO PROVINCE-') {
|
|
49
|
+
if (!regionPsgc) {
|
|
50
|
+
// Default to NCR if no region provided
|
|
51
|
+
regionPsgc = '1300000000';
|
|
52
|
+
}
|
|
53
|
+
const regionPrefix = regionPsgc.substring(0, 2);
|
|
54
|
+
const cities = byLevelData.cities
|
|
55
|
+
.filter(city => city.psgc.substring(0, 2) === regionPrefix)
|
|
56
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
57
|
+
const municipalities = byLevelData.municipalities
|
|
58
|
+
.filter(mun => mun.psgc.substring(0, 2) === regionPrefix)
|
|
59
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
60
|
+
return [...cities, ...municipalities].sort((a, b) => a.name.localeCompare(b.name));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// If no province provided (undefined/null), return empty array
|
|
64
|
+
if (!provincePsgc) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Get regular cities and municipalities (administratively under the province)
|
|
69
|
+
const provincePrefix = provincePsgc.substring(0, 5);
|
|
70
|
+
const regularCities = byLevelData.cities
|
|
71
|
+
.filter(city => city.psgc.substring(0, 5) === provincePrefix)
|
|
72
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
73
|
+
|
|
74
|
+
const municipalities = byLevelData.municipalities
|
|
75
|
+
.filter(mun => mun.psgc.substring(0, 5) === provincePrefix)
|
|
76
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
77
|
+
|
|
78
|
+
// Get HUCs that are geographically located in this province
|
|
79
|
+
const hucsInProvince = byLevelData.cities
|
|
80
|
+
.filter(city => {
|
|
81
|
+
if (city.cityClass === 'HUC') {
|
|
82
|
+
const geoProv = getGeographicProvince(city.psgc);
|
|
83
|
+
return geoProv && geoProv.psgc === provincePsgc;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
})
|
|
87
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
88
|
+
|
|
89
|
+
// Combine all cities (regular + HUCs) and municipalities
|
|
90
|
+
const allCities = [...regularCities, ...hucsInProvince].sort((a, b) => a.name.localeCompare(b.name));
|
|
91
|
+
|
|
92
|
+
return [...allCities, ...municipalities].sort((a, b) => a.name.localeCompare(b.name));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get all barangays located in selected city or municipality
|
|
97
|
+
* Works for all cities and municipalities including HUCs (Manila, Baguio, etc.)
|
|
98
|
+
* @param {string} cityMunPsgc - City/Municipality PSGC code
|
|
99
|
+
* @returns {Array} Array of barangays (sorted A-Z)
|
|
100
|
+
*/
|
|
101
|
+
function getBarangaysByCityOrMun(cityMunPsgc) {
|
|
102
|
+
// PSGC Revision 1: City/Municipality = first 7 digits
|
|
103
|
+
// Filter barangays by matching first 7 digits
|
|
104
|
+
const cityMunPrefix7 = cityMunPsgc.substring(0, 7);
|
|
105
|
+
let barangays = byLevelData.barangays.filter(
|
|
106
|
+
bgy => bgy.psgc.substring(0, 7) === cityMunPrefix7
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// Fallback: Some cities (like Manila) have barangays with different 7th digit
|
|
110
|
+
// Try matching first 6 digits if no matches found with 7 digits
|
|
111
|
+
if (barangays.length === 0) {
|
|
112
|
+
const cityMunPrefix6 = cityMunPsgc.substring(0, 6);
|
|
113
|
+
barangays = byLevelData.barangays.filter(
|
|
114
|
+
bgy => bgy.psgc.substring(0, 6) === cityMunPrefix6
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return barangays.sort((a, b) => a.name.localeCompare(b.name));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Get region for a given province PSGC
|
|
123
|
+
* Useful when user selects province first (province-first flow)
|
|
124
|
+
* @param {string} provincePsgc - Province PSGC code
|
|
125
|
+
* @returns {Object|null} Region object or null if not found
|
|
126
|
+
*/
|
|
127
|
+
function getRegionByProvince(provincePsgc) {
|
|
128
|
+
// Get province to verify it exists
|
|
129
|
+
const province = byPsgcData[provincePsgc];
|
|
130
|
+
if (!province || province.geographicLevel !== 'Prov') {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Get region PSGC (first 2 digits + 00000000)
|
|
135
|
+
const regionPsgc = provincePsgc.substring(0, 2) + '00000000';
|
|
136
|
+
return byPsgcData[regionPsgc] || null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Helper: Get geographic province for HUC (Highly Urbanized City)
|
|
141
|
+
* HUCs are administratively independent but geographically located within a province.
|
|
142
|
+
* @param {string} cityPsgc - City PSGC code
|
|
143
|
+
* @returns {Object|null} Geographic province or null if not found
|
|
144
|
+
*/
|
|
145
|
+
function getGeographicProvince(cityPsgc) {
|
|
146
|
+
const city = byPsgcData[cityPsgc];
|
|
147
|
+
if (!city || city.cityClass !== 'HUC' || !city.correspondenceCode) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Use correspondence code to find geographic province
|
|
152
|
+
const corrPrefix = city.correspondenceCode.substring(0, 5);
|
|
153
|
+
|
|
154
|
+
// Find province with matching correspondence code prefix
|
|
155
|
+
let geographicProvince = byLevelData.provinces.find(prov => {
|
|
156
|
+
if (!prov.correspondenceCode) return false;
|
|
157
|
+
return prov.correspondenceCode.substring(0, 5) === corrPrefix;
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// If not found with 5 digits, try 4 digits (for some edge cases)
|
|
161
|
+
if (!geographicProvince) {
|
|
162
|
+
const corrPrefix4 = city.correspondenceCode.substring(0, 4);
|
|
163
|
+
geographicProvince = byLevelData.provinces.find(prov => {
|
|
164
|
+
if (!prov.correspondenceCode) return false;
|
|
165
|
+
return prov.correspondenceCode.substring(0, 4) === corrPrefix4;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return geographicProvince || null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
module.exports = {
|
|
173
|
+
getRegions,
|
|
174
|
+
getProvincesByRegion,
|
|
175
|
+
getCitiesAndMunsByProvince,
|
|
176
|
+
getBarangaysByCityOrMun,
|
|
177
|
+
getRegionByProvince
|
|
178
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "latest-ph-address-thanks-to-anehan",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Latest Philippine addresses database with complete coverage of all Regions, Provinces, Cities, Municipalities, and Barangays. Perfect for address forms and location-based applications.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "node scripts/build.js",
|
|
8
|
+
"example": "node example.js",
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/aldrinPA/latest-ph-address.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"philippines",
|
|
17
|
+
"ph-address",
|
|
18
|
+
"philippine-address",
|
|
19
|
+
"psgc",
|
|
20
|
+
"address",
|
|
21
|
+
"location",
|
|
22
|
+
"philippines-address",
|
|
23
|
+
"philippine-standard-geographic-code",
|
|
24
|
+
"dropdown",
|
|
25
|
+
"cascading-dropdown",
|
|
26
|
+
"region",
|
|
27
|
+
"province",
|
|
28
|
+
"city",
|
|
29
|
+
"municipality",
|
|
30
|
+
"barangay",
|
|
31
|
+
"ncr",
|
|
32
|
+
"huc"
|
|
33
|
+
],
|
|
34
|
+
"author": "Anehan Tech Team by Aldrin",
|
|
35
|
+
"license": "ISC",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/aldrinPA/latest-ph-address/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/aldrinPA/latest-ph-address#readme"
|
|
40
|
+
}
|