country-data-filter 1.0.3 → 1.0.5
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/package.json +3 -3
- package/src/index.js +50 -0
- package/country-data.json +0 -18
- package/index.js +0 -31
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "country-data-filter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "A package to filter country data",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"author": "K N Nadeera",
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"files": [
|
|
26
|
-
"index.js",
|
|
27
|
-
"country-data.json"
|
|
26
|
+
"./src/index.js",
|
|
27
|
+
"./src/country-data.json"
|
|
28
28
|
]
|
|
29
29
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { countryData } = require("./country-data");
|
|
2
|
+
|
|
3
|
+
// Function to get the list of province names by country code
|
|
4
|
+
function getProvincesByCountry(code) {
|
|
5
|
+
const country = countryData.countries[code];
|
|
6
|
+
if (country) {
|
|
7
|
+
// Return an array of province names
|
|
8
|
+
return Object.values(country.province).map((province) => province.name);
|
|
9
|
+
}
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Function to get a list of district names by country code and province name
|
|
14
|
+
function getDistrictsByProvince(countryCode, provinceName) {
|
|
15
|
+
const country = countryData.countries[countryCode];
|
|
16
|
+
if (country && country.province[provinceName]) {
|
|
17
|
+
// Return an array of district names
|
|
18
|
+
return Object.values(country.province[provinceName].districts).map(
|
|
19
|
+
(district) => district.name
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Function to get city details by country code, province name, and district name
|
|
26
|
+
function getCityByDistrict(countryCode, provinceName, districtName) {
|
|
27
|
+
const country = countryData.countries[countryCode];
|
|
28
|
+
const province = country.province[provinceName];
|
|
29
|
+
if (province && province.districts[districtName]) {
|
|
30
|
+
return province.districts[districtName];
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Function to list all available functions
|
|
36
|
+
function listFunctions() {
|
|
37
|
+
return {
|
|
38
|
+
getProvincesByCountry,
|
|
39
|
+
getDistrictsByProvince,
|
|
40
|
+
getCityByDistrict,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Export all functions
|
|
45
|
+
module.exports = {
|
|
46
|
+
getProvincesByCountry,
|
|
47
|
+
getDistrictsByProvince,
|
|
48
|
+
getCityByDistrict,
|
|
49
|
+
listFunctions,
|
|
50
|
+
};
|
package/country-data.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"countries": {
|
|
3
|
-
"LK": {
|
|
4
|
-
"name": "Sri Lanka",
|
|
5
|
-
"province": {
|
|
6
|
-
"southern": {
|
|
7
|
-
"name": "Southern",
|
|
8
|
-
"districts": {
|
|
9
|
-
"matara": {
|
|
10
|
-
"name": "Matara",
|
|
11
|
-
"cities": [{ "name": "Weligama", "postalCode": 85700 }]
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
package/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const data = require("./country-data.json");
|
|
2
|
-
|
|
3
|
-
// Function to get country by code
|
|
4
|
-
function getProvinceByCountry(code) {
|
|
5
|
-
return data.countries[code];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
// Function to get province by country code and province name
|
|
9
|
-
// function getProvinceByCountryAndName(countryCode, provinceName) {
|
|
10
|
-
function getDistrictByProvince(countryCode, provinceName) {
|
|
11
|
-
const country = getProvinceByCountry(countryCode);
|
|
12
|
-
if (country) {
|
|
13
|
-
return country.province[provinceName];
|
|
14
|
-
}
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Function to get districts by country code, province name, and district name
|
|
19
|
-
function getCityByDistrict(countryCode, provinceName, districtName) {
|
|
20
|
-
const province = getDistrictByProvince(countryCode, provinceName);
|
|
21
|
-
if (province) {
|
|
22
|
-
return province.districts[districtName];
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = {
|
|
28
|
-
getProvinceByCountry,
|
|
29
|
-
getDistrictByProvince,
|
|
30
|
-
getCityByDistrict,
|
|
31
|
-
};
|