country-data-filter 1.0.15 → 1.0.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "country-data-filter",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "A package to filter country data",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -27,6 +27,7 @@ import { vavuniya } from "./districts/vavuniya";
27
27
  export const LK = {
28
28
  name: "Sri Lanka",
29
29
  code: "LK",
30
+ countryCode: "+94",
30
31
  provinces: {
31
32
  southern: {
32
33
  name: "Southern Province",
@@ -1,7 +1,7 @@
1
1
  import { countryData } from "../country-data";
2
2
 
3
3
  // Function to get a countryCode, provinceName, and districtName by cityName
4
- export function getCountryProvinceDistrictByCityFunction(cityName) {
4
+ export function getDataByCityFunction(cityName) {
5
5
  // Check if the input is valid
6
6
  if (typeof cityName !== "string" || cityName.trim() === "") {
7
7
  return {
@@ -9,9 +9,10 @@ export function getCountryProvinceDistrictByCityFunction(cityName) {
9
9
  };
10
10
  }
11
11
 
12
- let countryCode = "";
13
- let province = "";
14
- let district = "";
12
+ const countries = [];
13
+ const provinces = [];
14
+ const districts = [];
15
+ const cities = [];
15
16
 
16
17
  for (const countryKey in countryData) {
17
18
  const country = countryData[countryKey];
@@ -23,27 +24,40 @@ export function getCountryProvinceDistrictByCityFunction(cityName) {
23
24
  const selectedCity = selectedDistrict.cities[cityKey];
24
25
  if (selectedCity.name.toLowerCase() === cityName.toLowerCase()) {
25
26
  // Set the country code
26
- countryCode = countryKey;
27
+ countries.push({
28
+ code: country.code,
29
+ name: country.name,
30
+ countryCode: country.countryCode,
31
+ });
27
32
 
28
33
  // Set the province name
29
- province = selectedProvince.name;
34
+ provinces.push(selectedProvince.name);
30
35
 
31
36
  // Set the district name
32
- district = selectedDistrict.name;
37
+ districts.push(selectedDistrict.name);
38
+
39
+ // Set the city name
40
+ cities.push(selectedCity);
41
+
42
+ // Sort alphabetically
43
+ countries.sort();
44
+ provinces.sort();
45
+ districts.sort();
46
+ cities.sort();
33
47
 
34
48
  // Return the result
35
49
  return {
36
- countryCode,
37
- province,
38
- district,
50
+ countries,
51
+ provinces,
52
+ districts,
53
+ cities,
39
54
  };
40
55
  }
41
56
  }
42
57
  }
43
58
  }
44
59
  }
45
-
46
- // If no matching district was found
60
+ // // If no matching district was found
47
61
  return {
48
62
  error: `Provided city name "${cityName}" does not exist.`,
49
63
  };
@@ -1,17 +1,33 @@
1
1
  import { countryData } from "../country-data";
2
2
 
3
3
  // Function to get a list of province, district, and city names by country code
4
- export function getProvincesDistrictsCitiesByCountryFunction(code) {
5
- const country = countryData[code];
4
+ export function getDataByCountryFunction(countryCode) {
5
+ // Check if the input is valid
6
+ if (typeof countryCode !== "string" || countryCode.trim() === "") {
7
+ return {
8
+ error: "Invalid input: country code must be a non-empty string.",
9
+ };
10
+ }
11
+
12
+ const countries = [];
13
+ const districts = [];
14
+ const cities = [];
15
+
16
+ const country = countryData[countryCode];
17
+
6
18
  if (country) {
19
+ // Push the country name to the array
20
+ countries.push({
21
+ name: country.name,
22
+ code: country.code,
23
+ countryCode: country.countryCode,
24
+ });
25
+
7
26
  // Return an array of province names
8
27
  const provinces = Object.values(country.provinces)
9
28
  .map((province) => province.name)
10
29
  .sort();
11
30
 
12
- const districts = [];
13
- const cities = [];
14
-
15
31
  const districtArray = Object.values(country.provinces).flatMap(
16
32
  (province) => province.districts
17
33
  );
@@ -22,16 +38,17 @@ export function getProvincesDistrictsCitiesByCountryFunction(code) {
22
38
  const district = districtObject[key];
23
39
  // Push the district name to the array
24
40
  districts.push(district.name);
25
- district.cities.forEach((city) => cities.push(city.name));
41
+ district.cities.forEach((city) => cities.push(city));
26
42
  });
27
43
  });
28
44
 
29
- // Sort districts and cities alphabetically
45
+ // Sort alphabetically
46
+ countries.sort();
30
47
  districts.sort();
31
48
  cities.sort();
32
49
 
33
50
  // Return an array of provinces, districts, cities names by country code
34
- return { provinces, districts, cities };
51
+ return { countries, provinces, districts, cities };
35
52
  }
36
53
  return {
37
54
  error: `Provided country code "${code}" does not exist.`,
@@ -0,0 +1,80 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ export function getDataByCountryCityFunction(countryCode, cityName) {
4
+ // Check if the input is valid
5
+ if (
6
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
7
+ (typeof cityName !== "string" || cityName.trim() === "")
8
+ ) {
9
+ return {
10
+ error:
11
+ "Invalid input: country code & city name must be a non-empty string.",
12
+ };
13
+ } else if (typeof countryCode !== "string" || countryCode.trim() === "") {
14
+ return {
15
+ error: "Invalid input: country code must be a non-empty string.",
16
+ };
17
+ } else if (typeof cityName !== "string" || cityName.trim() === "") {
18
+ return {
19
+ error: "Invalid input: city name must be a non-empty string.",
20
+ };
21
+ }
22
+
23
+ const countries = [];
24
+ const provinces = [];
25
+ const districts = [];
26
+ const cities = [];
27
+
28
+ const country = countryData[countryCode];
29
+ if (country) {
30
+ // Push the country name to the array
31
+ countries.push({
32
+ name: country.name,
33
+ code: country.code,
34
+ countryCode: country.countryCode,
35
+ });
36
+
37
+ for (const provinceKey in country.provinces) {
38
+ const selectedProvince = country.provinces[provinceKey];
39
+ for (const districtKey in selectedProvince.districts) {
40
+ const district = selectedProvince.districts[districtKey];
41
+ for (const cityKey in district.cities) {
42
+ const selectedCity = district.cities[cityKey];
43
+ if (selectedCity.name.toLowerCase() === cityName.toLowerCase()) {
44
+ // Set the province name
45
+ provinces.push(selectedProvince.name);
46
+
47
+ // Set the district name
48
+ districts.push(district.name);
49
+
50
+ // Set the city name
51
+ cities.push(selectedCity);
52
+
53
+ // Sort alphabetically
54
+ countries.sort();
55
+ provinces.sort();
56
+ districts.sort();
57
+ cities.sort();
58
+
59
+ // Return the result
60
+ return {
61
+ countries,
62
+ provinces,
63
+ districts,
64
+ cities,
65
+ };
66
+ }
67
+ }
68
+ }
69
+ }
70
+ // If no matching city was found
71
+ return {
72
+ error: `Provided city name "${cityName}" does not exist.`,
73
+ };
74
+ }
75
+
76
+ // If no matching country was found
77
+ return {
78
+ error: `Provided country code "${countryCode}" does not exist.`,
79
+ };
80
+ }
@@ -0,0 +1,86 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ export function getDataByCountryDIstrictFunction(countryCode, districtName) {
4
+ // Check if the input is valid
5
+ if (
6
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
7
+ (typeof districtName !== "string" || districtName.trim() === "")
8
+ ) {
9
+ return {
10
+ error:
11
+ "Invalid input: country code & district name must be a non-empty string.",
12
+ };
13
+ } else if (typeof countryCode !== "string" || countryCode.trim() === "") {
14
+ return {
15
+ error: "Invalid input: country code must be a non-empty string.",
16
+ };
17
+ } else if (typeof districtName !== "string" || districtName.trim() === "") {
18
+ return {
19
+ error: "Invalid input: district name must be a non-empty string.",
20
+ };
21
+ }
22
+
23
+ const countries = [];
24
+ const provinces = [];
25
+ const districts = [];
26
+ const cities = [];
27
+
28
+ const country = countryData[countryCode];
29
+ if (country) {
30
+ // Push the country name to the array
31
+ countries.push({
32
+ name: country.name,
33
+ code: country.code,
34
+ countryCode: country.countryCode,
35
+ });
36
+
37
+ for (const provinceKey in country.provinces) {
38
+ const selectedProvince = country.provinces[provinceKey];
39
+ for (const districtKey in selectedProvince.districts) {
40
+ const district = selectedProvince.districts[districtKey];
41
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
42
+ // Add city names
43
+ district.cities.forEach((city) => {
44
+ cities.push(city);
45
+ });
46
+
47
+ // Set the country code
48
+ countries.push({
49
+ code: country.code,
50
+ name: country.name,
51
+ countryCode: country.countryCode,
52
+ });
53
+
54
+ // Set the province name
55
+ provinces.push(selectedProvince.name);
56
+
57
+ // Set the district name
58
+ districts.push(district.name);
59
+
60
+ // Sort alphabetically
61
+ countries.sort();
62
+ provinces.sort();
63
+ districts.sort();
64
+ cities.sort();
65
+
66
+ // Return the result
67
+ return {
68
+ countries,
69
+ provinces,
70
+ districts,
71
+ cities,
72
+ };
73
+ }
74
+ }
75
+ // If no matching district was found
76
+ return {
77
+ error: `Provided district name "${districtName}" does not exist.`,
78
+ };
79
+ }
80
+ }
81
+
82
+ // If no matching country was found
83
+ return {
84
+ error: `Provided country code "${countryCode}" does not exist.`,
85
+ };
86
+ }
@@ -0,0 +1,79 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a list of province, district, and city names by country code
4
+ export function getDataByCountryProvinceFunction(countryCode, provinceName) {
5
+ // Check if the input is valid
6
+ if (
7
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
8
+ (typeof provinceName !== "string" || provinceName.trim() === "")
9
+ ) {
10
+ return {
11
+ error:
12
+ "Invalid input: country code & province name must be a non-empty string.",
13
+ };
14
+ } else if (typeof countryCode !== "string" || countryCode.trim() === "") {
15
+ return {
16
+ error: "Invalid input: country code must be a non-empty string.",
17
+ };
18
+ } else if (typeof provinceName !== "string" || provinceName.trim() === "") {
19
+ return {
20
+ error: "Invalid input: province name must be a non-empty string.",
21
+ };
22
+ }
23
+
24
+ const countries = [];
25
+ const provinces = [];
26
+ const districts = [];
27
+ const cities = [];
28
+
29
+ const country = countryData[countryCode];
30
+
31
+ if (country) {
32
+ countries.push({
33
+ name: country.name,
34
+ code: country.code,
35
+ countryCode: country.countryCode,
36
+ });
37
+
38
+ for (const provinceKey in country.provinces) {
39
+ const province = country.provinces[provinceKey];
40
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
41
+ // Set the province name
42
+ provinces.push(province.name);
43
+ const districtsArray = province.districts;
44
+ for (const districtKey in districtsArray) {
45
+ const district = districtsArray[districtKey];
46
+ const citiesArray = district.cities.map((city) => ({
47
+ name: city,
48
+ }));
49
+
50
+ // Add district name
51
+ districts.push(district.name);
52
+
53
+ // Add city names
54
+ citiesArray.forEach((city) => cities.push(city));
55
+ }
56
+
57
+ // Sort alphabetically
58
+ countries.sort();
59
+ provinces.sort();
60
+ districts.sort();
61
+ cities.sort();
62
+
63
+ // Return result once a matching province is found
64
+ return {
65
+ countries,
66
+ provinces,
67
+ districts,
68
+ cities,
69
+ };
70
+ }
71
+ }
72
+ return {
73
+ error: `Provided province name "${provinceName}" does not exist.`,
74
+ };
75
+ }
76
+ return {
77
+ error: `Provided country code "${countryCode}" does not exist.`,
78
+ };
79
+ }
@@ -0,0 +1,92 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a list of province, district, and city names by country code
4
+ export function getDataByCountryProvinceDistrictFunction(
5
+ countryCode,
6
+ provinceName,
7
+ districtName
8
+ ) {
9
+ // Check if the input is valid
10
+ if (
11
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
12
+ (typeof provinceName !== "string" || provinceName.trim() === "") &&
13
+ (typeof districtName !== "string" || districtName.trim() === "")
14
+ ) {
15
+ return {
16
+ error:
17
+ "Invalid input: country code & province name & district name must be a non-empty string.",
18
+ };
19
+ } else if (typeof countryCode !== "string" || countryCode.trim() === "") {
20
+ return {
21
+ error: "Invalid input: country code must be a non-empty string.",
22
+ };
23
+ } else if (typeof provinceName !== "string" || provinceName.trim() === "") {
24
+ return {
25
+ error: "Invalid input: province name must be a non-empty string.",
26
+ };
27
+ } else if (typeof districtName !== "string" || districtName.trim() === "") {
28
+ return {
29
+ error: "Invalid input: district name must be a non-empty string.",
30
+ };
31
+ }
32
+
33
+ const countries = [];
34
+ const provinces = [];
35
+ const districts = [];
36
+ const cities = [];
37
+
38
+ const country = countryData[countryCode];
39
+
40
+ if (country) {
41
+ countries.push({
42
+ name: country.name,
43
+ code: country.code,
44
+ countryCode: country.countryCode,
45
+ });
46
+
47
+ for (const provinceKey in country.provinces) {
48
+ const province = country.provinces[provinceKey];
49
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
50
+ // Set the province name
51
+ provinces.push(province.name);
52
+ const districtsArray = province.districts;
53
+ for (const districtKey in districtsArray) {
54
+ const district = districtsArray[districtKey];
55
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
56
+ const citiesArray = district.cities.map((city) => ({
57
+ name: city,
58
+ }));
59
+
60
+ // Add district name
61
+ districts.push(district.name);
62
+
63
+ // Add city names
64
+ citiesArray.forEach((city) => cities.push(city));
65
+ // Sort districts and cities alphabetically
66
+ countries.sort();
67
+ provinces.sort();
68
+ districts.sort();
69
+ cities.sort();
70
+
71
+ // Return result once a matching district is found
72
+ return {
73
+ countries,
74
+ provinces,
75
+ districts,
76
+ cities,
77
+ };
78
+ }
79
+ }
80
+ return {
81
+ error: `Provided district name "${districtName}" does not exist.`,
82
+ };
83
+ }
84
+ }
85
+ return {
86
+ error: `Provided province name "${provinceName}" does not exist.`,
87
+ };
88
+ }
89
+ return {
90
+ error: `Provided country code "${countryCode}" does not exist.`,
91
+ };
92
+ }
@@ -0,0 +1,107 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a list of province, district, and city names by country code
4
+ export function getDataByCountryProvinceDistrictCityFunction(
5
+ countryCode,
6
+ provinceName,
7
+ districtName,
8
+ cityName
9
+ ) {
10
+ // Check if the input is valid
11
+ if (
12
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
13
+ (typeof provinceName !== "string" || provinceName.trim() === "") &&
14
+ (typeof districtName !== "string" || districtName.trim() === "") &&
15
+ (typeof cityName !== "string" || cityName.trim() === "")
16
+ ) {
17
+ return {
18
+ error:
19
+ "Invalid input: country code & province name & district name must be a non-empty string.",
20
+ };
21
+ } else if (typeof countryCode !== "string" || countryCode.trim() === "") {
22
+ return {
23
+ error: "Invalid input: country code must be a non-empty string.",
24
+ };
25
+ } else if (typeof provinceName !== "string" || provinceName.trim() === "") {
26
+ return {
27
+ error: "Invalid input: province name must be a non-empty string.",
28
+ };
29
+ } else if (typeof districtName !== "string" || districtName.trim() === "") {
30
+ return {
31
+ error: "Invalid input: district name must be a non-empty string.",
32
+ };
33
+ } else if (typeof cityName !== "string" || cityName.trim() === "") {
34
+ return {
35
+ error: "Invalid input: city name must be a non-empty string.",
36
+ };
37
+ }
38
+
39
+ const countries = [];
40
+ const provinces = [];
41
+ const districts = [];
42
+ const cities = [];
43
+
44
+ const country = countryData[countryCode];
45
+
46
+ if (country) {
47
+ countries.push({
48
+ name: country.name,
49
+ code: country.code,
50
+ countryCode: country.countryCode,
51
+ });
52
+
53
+ for (const provinceKey in country.provinces) {
54
+ const province = country.provinces[provinceKey];
55
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
56
+ // Set the province name
57
+ provinces.push(province.name);
58
+ const districtsArray = province.districts;
59
+ for (const districtKey in districtsArray) {
60
+ const district = districtsArray[districtKey];
61
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
62
+ const citiesArray = district.cities;
63
+
64
+ // Add district name
65
+ districts.push(district.name);
66
+
67
+ // Add city name
68
+ for (const cityKey in citiesArray) {
69
+ const city = citiesArray[cityKey];
70
+ if (
71
+ city.name.toLocaleLowerCase() === cityName.toLocaleLowerCase()
72
+ ) {
73
+ cities.push(city);
74
+
75
+ // Sort districts and cities alphabetically
76
+ countries.sort();
77
+ provinces.sort();
78
+ districts.sort();
79
+ cities.sort();
80
+
81
+ // Return result once a matching province is found
82
+ return {
83
+ countries,
84
+ provinces,
85
+ districts,
86
+ cities,
87
+ };
88
+ }
89
+ }
90
+ return {
91
+ error: `Provided city name "${cityName}" does not exist.`,
92
+ };
93
+ }
94
+ }
95
+ return {
96
+ error: `Provided district name "${districtName}" does not exist.`,
97
+ };
98
+ }
99
+ }
100
+ return {
101
+ error: `Provided province name "${provinceName}" does not exist.`,
102
+ };
103
+ }
104
+ return {
105
+ error: `Provided country code "${countryCode}" does not exist.`,
106
+ };
107
+ }
@@ -1,7 +1,7 @@
1
1
  import { countryData } from "../country-data";
2
2
 
3
3
  // Function to get a countryCode, provinceName, and list of city names by district name
4
- export function getCountryProvinceCityByDistrictFunction(districtName) {
4
+ export function getDataByDistrictFunction(districtName) {
5
5
  // Check if the input is valid
6
6
  if (typeof districtName !== "string" || districtName.trim() === "") {
7
7
  return {
@@ -9,8 +9,9 @@ export function getCountryProvinceCityByDistrictFunction(districtName) {
9
9
  };
10
10
  }
11
11
 
12
- let countryCode = "";
13
- let province = "";
12
+ const countries = [];
13
+ const provinces = [];
14
+ const districts = [];
14
15
  const cities = [];
15
16
 
16
17
  for (const countryKey in countryData) {
@@ -20,31 +21,41 @@ export function getCountryProvinceCityByDistrictFunction(districtName) {
20
21
  for (const districtKey in selectedProvince.districts) {
21
22
  const district = selectedProvince.districts[districtKey];
22
23
  if (district.name.toLowerCase() === districtName.toLowerCase()) {
24
+ // Add district name
25
+ districts.push(district.name);
26
+
23
27
  // Add city names
24
28
  district.cities.forEach((city) => {
25
- cities.push(city.name);
29
+ cities.push(city);
26
30
  });
27
31
 
28
32
  // Set the country code
29
- countryCode = countryKey;
33
+ countries.push({
34
+ code: country.code,
35
+ name: country.name,
36
+ countryCode: country.countryCode,
37
+ });
30
38
 
31
39
  // Set the province name
32
- province = selectedProvince.name;
40
+ provinces.push(selectedProvince.name);
33
41
 
34
42
  // Sort districts and cities alphabetically
43
+ countries.sort();
44
+ provinces.sort();
45
+ districts.sort();
35
46
  cities.sort();
36
47
 
37
48
  // Return the result
38
49
  return {
39
- countryCode,
40
- province,
50
+ countries,
51
+ provinces,
52
+ districts,
41
53
  cities,
42
54
  };
43
55
  }
44
56
  }
45
57
  }
46
58
  }
47
-
48
59
  // If no matching district was found
49
60
  return {
50
61
  error: `Provided district name "${districtName}" does not exist.`,
@@ -0,0 +1,70 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a countryCode, provinceName, and list of city names by district name
4
+ export function getDataByDistrictCityFunction(districtName, cityName) {
5
+ // Check if the input is valid
6
+ if (typeof districtName !== "string" || districtName.trim() === "") {
7
+ return {
8
+ error: "Invalid input: District name must be a non-empty string.",
9
+ };
10
+ }
11
+
12
+ const countries = [];
13
+ const provinces = [];
14
+ const districts = [];
15
+ const cities = [];
16
+
17
+ for (const countryKey in countryData) {
18
+ const country = countryData[countryKey];
19
+ for (const provinceKey in country.provinces) {
20
+ const selectedProvince = country.provinces[provinceKey];
21
+ for (const districtKey in selectedProvince.districts) {
22
+ const district = selectedProvince.districts[districtKey];
23
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
24
+ // Add country name
25
+ countries.push({
26
+ code: country.code,
27
+ name: country.name,
28
+ countryCode: country.countryCode,
29
+ });
30
+
31
+ // Add province name
32
+ provinces.push(selectedProvince.name);
33
+
34
+ // Add district name
35
+ districts.push(district.name);
36
+
37
+ // Add city name
38
+ for (const cityKey in district.cities) {
39
+ const city = district.cities[cityKey];
40
+
41
+ if (city.name.toLowerCase() === cityName.toLowerCase()) {
42
+ cities.push(city);
43
+
44
+ // Sort districts and cities alphabetically
45
+ countries.sort();
46
+ provinces.sort();
47
+ districts.sort();
48
+ cities.sort();
49
+
50
+ // Return result once a matching province is found
51
+ return {
52
+ countries,
53
+ provinces,
54
+ districts,
55
+ cities,
56
+ };
57
+ }
58
+ }
59
+ return {
60
+ error: `Provided city name "${cityName}" does not exist.`,
61
+ };
62
+ }
63
+ }
64
+ }
65
+ }
66
+ // If no matching district was found
67
+ return {
68
+ error: `Provided district name "${districtName}" does not exist.`,
69
+ };
70
+ }
@@ -0,0 +1,90 @@
1
+ import { countryData } from "../country-data";
2
+ import { getDataByCityFunction } from "./getDataByCity";
3
+ import { getDataByCountryFunction } from "./getDataByCountry";
4
+ import { getDataByCountryCityFunction } from "./getDataByCountryCity";
5
+ import { getDataByCountryDIstrictFunction } from "./getDataByCountryDIstrict";
6
+ import { getDataByCountryProvinceFunction } from "./getDataByCountryProvince";
7
+ import { getDataByCountryProvinceDistrictFunction } from "./getDataByCountryProvinceDistrict";
8
+ import { getDataByCountryProvinceDistrictCityFunction } from "./getDataByCountryProvinceDistrictCity";
9
+ import { getDataByDistrictFunction } from "./getDataByDistrict";
10
+ import { getDataByDistrictCityFunction } from "./getDataByDistrictCity";
11
+ import { getDataByProvinceFunction } from "./getDataByProvince";
12
+ import { getDataByProvinceCityFunction } from "./getDataByProvinceCity";
13
+ import { getDataByProvinceDistrictFunction } from "./getDataByProvinceDistrict";
14
+
15
+ export function getDataByEtherCountryProvinceDistrictCityFunction(
16
+ countryCode,
17
+ province,
18
+ district,
19
+ city
20
+ ) {
21
+ // Check if the input is valid
22
+ if (
23
+ (typeof countryCode !== "string" || countryCode.trim() === "") &&
24
+ (typeof province !== "string" || province.trim() === "") &&
25
+ (typeof district !== "string" || district.trim() === "") &&
26
+ (typeof city !== "string" || city.trim() === "")
27
+ ) {
28
+ return {
29
+ error:
30
+ "Invalid input: country code & province name & district name must be a non-empty string.",
31
+ };
32
+ }
33
+
34
+ if (countryCode && !province && !district && !city) {
35
+ return getDataByCountryFunction(countryCode);
36
+ }
37
+
38
+ if (!countryCode && province && !district && !city) {
39
+ return getDataByProvinceFunction(province);
40
+ }
41
+
42
+ if (!countryCode && !province && district && !city) {
43
+ return getDataByDistrictFunction(district);
44
+ }
45
+
46
+ if (!countryCode && !province && !district && city) {
47
+ return getDataByCityFunction(city);
48
+ }
49
+
50
+ if (countryCode && province && !district && !city) {
51
+ return getDataByCountryProvinceFunction(countryCode, province);
52
+ }
53
+
54
+ if (countryCode && province && district && !city) {
55
+ return getDataByCountryProvinceDistrictFunction(
56
+ countryCode,
57
+ province,
58
+ district
59
+ );
60
+ }
61
+
62
+ if (countryCode && province && district && city) {
63
+ return getDataByCountryProvinceDistrictCityFunction(
64
+ countryCode,
65
+ province,
66
+ district,
67
+ city
68
+ );
69
+ }
70
+
71
+ if (countryCode && !province && district && !city) {
72
+ return getDataByCountryDIstrictFunction(countryCode, district);
73
+ }
74
+
75
+ if (countryCode && !province && !district && city) {
76
+ return getDataByCountryCityFunction(countryCode, city);
77
+ }
78
+
79
+ if (!countryCode && province && district && !city) {
80
+ return getDataByProvinceDistrictFunction(province, district);
81
+ }
82
+
83
+ if (!countryCode && province && !district && city) {
84
+ return getDataByProvinceCityFunction(province, city);
85
+ }
86
+
87
+ if (!countryCode && !province && district && city) {
88
+ return getDataByDistrictCityFunction(district, city);
89
+ }
90
+ }
@@ -1,7 +1,7 @@
1
1
  import { countryData } from "../country-data";
2
2
 
3
3
  // Function to get a countryCode and list of districts and city names by province name
4
- export function getCountryDistrictsCitiesByProvinceFunction(provinceName) {
4
+ export function getDataByProvinceFunction(provinceName) {
5
5
  // Check if the input is valid
6
6
  if (typeof provinceName !== "string" || provinceName.trim() === "") {
7
7
  return {
@@ -9,7 +9,8 @@ export function getCountryDistrictsCitiesByProvinceFunction(provinceName) {
9
9
  };
10
10
  }
11
11
 
12
- let countryCode = "";
12
+ const countries = [];
13
+ const provinces = [];
13
14
  const districts = [];
14
15
  const cities = [];
15
16
 
@@ -19,36 +20,46 @@ export function getCountryDistrictsCitiesByProvinceFunction(provinceName) {
19
20
  const province = country.provinces[provinceKey];
20
21
  if (province.name.toLowerCase() === provinceName.toLowerCase()) {
21
22
  const districtsArray = province.districts;
23
+
24
+ // Set the country code
25
+ countries.push({
26
+ code: country.code,
27
+ name: country.name,
28
+ countryCode: country.countryCode,
29
+ });
30
+
31
+ // Add province name
32
+ provinces.push(province.name);
33
+
22
34
  for (const districtKey in districtsArray) {
23
35
  const district = districtsArray[districtKey];
24
36
  const citiesArray = district.cities.map((city) => ({
25
- name: city.name,
37
+ name: city,
26
38
  }));
27
39
 
28
- // Set the country code
29
- countryCode = countryKey;
30
-
31
40
  // Add district name
32
41
  districts.push(district.name);
33
42
 
34
43
  // Add city names
35
- citiesArray.forEach((city) => cities.push(city.name));
44
+ citiesArray.forEach((city) => cities.push(city));
36
45
  }
37
46
 
38
47
  // Sort districts and cities alphabetically
48
+ countries.sort();
49
+ provinces.sort();
39
50
  districts.sort();
40
51
  cities.sort();
41
52
 
42
53
  // Return result once a matching province is found
43
54
  return {
44
- countryCode,
55
+ countries,
56
+ provinces,
45
57
  districts,
46
58
  cities,
47
59
  };
48
60
  }
49
61
  }
50
62
  }
51
-
52
63
  // If no matching province was found
53
64
  return {
54
65
  error: `Provided province name "${provinceName}" does not exist.`,
@@ -0,0 +1,84 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a countryCode and list of districts and city names by province name
4
+ export function getDataByProvinceCityFunction(provinceName, cityName) {
5
+ // Check if the input is valid
6
+ if (
7
+ (typeof provinceName !== "string" || provinceName.trim() === "") &&
8
+ (typeof cityName !== "string" || cityName.trim() === "")
9
+ ) {
10
+ return {
11
+ error:
12
+ "Invalid input: Province name & city name must be a non-empty string.",
13
+ };
14
+ } else if (typeof provinceName !== "string" || provinceName.trim() === "") {
15
+ return {
16
+ error: "Invalid input: Province name must be a non-empty string.",
17
+ };
18
+ } else if (typeof cityName !== "string" || cityName.trim() === "") {
19
+ return {
20
+ error: "Invalid input: city name must be a non-empty string.",
21
+ };
22
+ }
23
+
24
+ const countries = [];
25
+ const provinces = [];
26
+ const districts = [];
27
+ const cities = [];
28
+
29
+ for (const countryKey in countryData) {
30
+ const country = countryData[countryKey];
31
+ for (const provinceKey in country.provinces) {
32
+ const province = country.provinces[provinceKey];
33
+
34
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
35
+ // Set the country code
36
+ countries.push({
37
+ code: country.code,
38
+ name: country.name,
39
+ countryCode: country.countryCode,
40
+ });
41
+
42
+ // Add province name
43
+ provinces.push(province.name);
44
+ const districtsArray = province.districts;
45
+ for (const districtKey in districtsArray) {
46
+ const district = districtsArray[districtKey];
47
+ for (const cityKey in district.cities) {
48
+ const city = district.cities[cityKey];
49
+ if (city.name.toLowerCase() === cityName.toLowerCase()) {
50
+ // Add district name
51
+ districts.push(district.name);
52
+
53
+ // Add city names
54
+ cities.push(city.name);
55
+
56
+ // Sort districts and cities alphabetically
57
+ countries.sort();
58
+ provinces.sort();
59
+ districts.sort();
60
+ cities.sort();
61
+
62
+ // Return result once a matching province is found
63
+ return {
64
+ countries,
65
+ provinces,
66
+ districts,
67
+ cities,
68
+ };
69
+ }
70
+ }
71
+ }
72
+
73
+ // If no matching district was found
74
+ return {
75
+ error: `Provided district name "${cityName}" does not exist.`,
76
+ };
77
+ }
78
+ }
79
+ // If no matching province was found
80
+ return {
81
+ error: `Provided province name "${provinceName}" does not exist.`,
82
+ };
83
+ }
84
+ }
@@ -0,0 +1,81 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a countryCode and list of districts and city names by province name
4
+ export function getDataByProvinceDistrictFunction(provinceName, districtName) {
5
+ // Check if the input is valid
6
+ if (
7
+ (typeof provinceName !== "string" || provinceName.trim() === "") &&
8
+ (typeof districtName !== "string" || districtName.trim() === "")
9
+ ) {
10
+ return {
11
+ error:
12
+ "Invalid input: Province name & district name must be a non-empty string.",
13
+ };
14
+ } else if (typeof provinceName !== "string" || provinceName.trim() === "") {
15
+ return {
16
+ error: "Invalid input: Province name must be a non-empty string.",
17
+ };
18
+ } else if (typeof districtName !== "string" || districtName.trim() === "") {
19
+ return {
20
+ error: "Invalid input: district name must be a non-empty string.",
21
+ };
22
+ }
23
+
24
+ const countries = [];
25
+ const provinces = [];
26
+ const districts = [];
27
+ const cities = [];
28
+
29
+ for (const countryKey in countryData) {
30
+ const country = countryData[countryKey];
31
+ for (const provinceKey in country.provinces) {
32
+ const province = country.provinces[provinceKey];
33
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
34
+ // Set the country code
35
+ countries.push({
36
+ code: country.code,
37
+ name: country.name,
38
+ countryCode: country.countryCode,
39
+ });
40
+
41
+ // Set the province name
42
+ provinces.push(province.name);
43
+
44
+ const districtsArray = province.districts;
45
+ for (const districtKey in districtsArray) {
46
+ const district = districtsArray[districtKey];
47
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
48
+ // Add city names
49
+ district.cities.forEach((city) => cities.push(city));
50
+
51
+ // Set the district name
52
+ districts.push(district.name);
53
+
54
+ // Sort alphabetically
55
+ countries.sort();
56
+ provinces.sort();
57
+ districts.sort();
58
+ cities.sort();
59
+
60
+ // Return the result
61
+ return {
62
+ countries,
63
+ provinces,
64
+ districts,
65
+ cities,
66
+ };
67
+ }
68
+ }
69
+
70
+ // If no matching district was found
71
+ return {
72
+ error: `Provided district name "${districtName}" does not exist.`,
73
+ };
74
+ }
75
+ }
76
+ // If no matching province was found
77
+ return {
78
+ error: `Provided province name "${provinceName}" does not exist.`,
79
+ };
80
+ }
81
+ }
package/src/index.d.ts CHANGED
@@ -1,10 +1,68 @@
1
- export function getProvincesDistrictsCitiesByCountry(code: string): any;
2
- export function getCountryDistrictsCitiesByProvince(provinceName: string): any;
3
- export function getCountryProvinceCityByDistrict(districtName: string): any;
4
- export function getCountryProvinceDistrictByCity(cityName: string): any;
1
+ export function getDataByCountry(country: string): any;
2
+ export function getDataByProvince(provinceName: string): any;
3
+ export function getDataByDistrict(districtName: string): any;
4
+ export function getDataByCity(cityName: string): any;
5
+
6
+ export function getDataByCountryProvince(
7
+ country: string,
8
+ province: string
9
+ ): any;
10
+ export function getDataByCountryProvinceDistrict(
11
+ country: string,
12
+ province: string,
13
+ district: string
14
+ ): any;
15
+ export function getDataByCountryProvinceDistrictCity(
16
+ country: string,
17
+ province: string,
18
+ district: string,
19
+ cityName: string
20
+ ): any;
21
+ export function getDataByEtherCountryProvinceDistrictCity(
22
+ country: string | null | undefined,
23
+ province: string | null | undefined,
24
+ district: string | null | undefined,
25
+ cityName: string | null | undefined
26
+ ): any;
27
+ export function getDataByCountryDistrict(
28
+ country: string,
29
+ district: string
30
+ ): any;
31
+ export function getDataByCountryCity(country: string, city: string): any;
32
+ export function getDataByProvinceDistrict(
33
+ province: string,
34
+ district: string
35
+ ): any;
36
+ export function getDataByProvinceCity(province: string, city: string): any;
37
+ export function getDataByDistrictCity(district: string, city: string): any;
38
+
5
39
  export function listFunctions(): {
6
- getProvincesDistrictsCitiesByCountry: (code: string) => any;
7
- getCountryDistrictsCitiesByProvince: (provinceName: string) => any;
8
- getCountryProvinceCityByDistrict: (districtName: string) => any;
9
- getCountryProvinceDistrictByCity: (cityName: string) => any;
40
+ getDataByCountry: (country: string) => any;
41
+ getDataByProvince: (provinceName: string) => any;
42
+ getDataByDistrict: (districtName: string) => any;
43
+ getDataByCity: (cityName: string) => any;
44
+
45
+ getDataByCountryProvince: (country: string, province: string) => any;
46
+ getDataByCountryProvinceDistrict: (
47
+ country: string,
48
+ province: string,
49
+ district: string
50
+ ) => any;
51
+ getDataByCountryProvinceDistrictCity: (
52
+ country: string,
53
+ province: string,
54
+ district: string,
55
+ cityName: string
56
+ ) => any;
57
+ getDataByEtherCountryProvinceDistrictCity: (
58
+ country: string | null | undefined,
59
+ province: string | null | undefined,
60
+ district: string | null | undefined,
61
+ cityName: string | null | undefined
62
+ ) => any;
63
+ getDataByCountryDistrict: (country: string, district: string) => any;
64
+ getDataByCountryCity: (country: string, city: string) => any;
65
+ getDataByProvinceDistrict: (province: string, district: string) => any;
66
+ getDataByProvinceCity: (province: string, city: string) => any;
67
+ getDataByDistrictCity: (district: string, city: string) => any;
10
68
  };
package/src/index.js CHANGED
@@ -1,33 +1,112 @@
1
- import { getCountryDistrictsCitiesByProvinceFunction } from "./functions/getCountryDistrictsCitiesByProvince.js";
2
- import { getCountryProvinceCityByDistrictFunction } from "./functions/getCountryProvinceCityByDistrict.js";
3
- import { getCountryProvinceDistrictByCityFunction } from "./functions/getCountryProvinceDistrictByCity.js";
4
- import { getProvincesDistrictsCitiesByCountryFunction } from "./functions/getProvincesDistrictsCitiesByCountry.js";
1
+ import { getDataByCityFunction } from "./functions/getDataByCity";
2
+ import { getDataByCountryFunction } from "./functions/getDataByCountry";
3
+ import { getDataByCountryCityFunction } from "./functions/getDataByCountryCity";
4
+ import { getDataByCountryDIstrictFunction } from "./functions/getDataByCountryDIstrict";
5
+ import { getDataByCountryProvinceFunction } from "./functions/getDataByCountryProvince";
6
+ import { getDataByCountryProvinceDistrictFunction } from "./functions/getDataByCountryProvinceDistrict";
7
+ import { getDataByCountryProvinceDistrictCityFunction } from "./functions/getDataByCountryProvinceDistrictCity";
8
+ import { getDataByDistrictFunction } from "./functions/getDataByDistrict";
9
+ import { getDataByDistrictCityFunction } from "./functions/getDataByDistrictCity";
10
+ import { getDataByEtherCountryProvinceDistrictCityFunction } from "./functions/getDataByEtherCountryProvinceDistrictCity";
11
+ import { getDataByProvinceFunction } from "./functions/getDataByProvince";
12
+ import { getDataByProvinceCityFunction } from "./functions/getDataByProvinceCity";
13
+ import { getDataByProvinceDistrictFunction } from "./functions/getDataByProvinceDistrict";
5
14
 
6
- // Function to get a list of province, district, and city names by country code
7
- export function getProvincesDistrictsCitiesByCountry(code) {
8
- return getProvincesDistrictsCitiesByCountryFunction(code);
15
+ // Function to get a list of provinces, districts, and city names by country code
16
+ export function getDataByCountry(code) {
17
+ return getDataByCountryFunction(code);
9
18
  }
10
19
 
11
- // Function to get a countryCode and list of districts and city names by province name
12
- export function getCountryDistrictsCitiesByProvince(provinceName) {
13
- return getCountryDistrictsCitiesByProvinceFunction(provinceName);
20
+ // Function to get a countries and list of districts and city names by province name
21
+ export function getDataByProvince(provinceName) {
22
+ return getDataByProvinceFunction(provinceName);
14
23
  }
15
24
 
16
- // Function to get a countryCode, provinceName, and list of city names by district name
17
- export function getCountryProvinceCityByDistrict(districtName) {
18
- return getCountryProvinceCityByDistrictFunction(districtName);
25
+ // Function to get a countries, provinces, and list of city names by district name
26
+ export function getDataByDistrict(districtName) {
27
+ return getDataByDistrictFunction(districtName);
19
28
  }
20
29
 
21
- export function getCountryProvinceDistrictByCity(cityName) {
22
- return getCountryProvinceDistrictByCityFunction(cityName);
30
+ // Function to get a countries, provinces, and districts by city name
31
+ export function getDataByCity(cityName) {
32
+ return getDataByCityFunction(cityName);
33
+ }
34
+
35
+ // Function to get a list of district, and city names by country code, province
36
+ export function getDataByCountryProvince(country, province) {
37
+ return getDataByCountryProvinceFunction(country, province);
38
+ }
39
+
40
+ // Function to get a list of city names by country code, province, and district
41
+ export function getDataByCountryProvinceDistrict(country, province, district) {
42
+ return getDataByCountryProvinceDistrictFunction(country, province, district);
43
+ }
44
+
45
+ // Function to get a selected city data by country code, province, district, and city
46
+ export function getDataByCountryProvinceDistrictCity(
47
+ country,
48
+ province,
49
+ district,
50
+ city
51
+ ) {
52
+ return getDataByCountryProvinceDistrictCityFunction(
53
+ country,
54
+ province,
55
+ district,
56
+ city
57
+ );
58
+ }
59
+
60
+ // Function to get a selected data by ether country code, province, district, and city
61
+ export function getDataByEtherCountryProvinceDistrictCity(
62
+ country,
63
+ province,
64
+ district,
65
+ city
66
+ ) {
67
+ return getDataByEtherCountryProvinceDistrictCityFunction(
68
+ country,
69
+ province,
70
+ district,
71
+ city
72
+ );
73
+ }
74
+
75
+ export function getDataByCountryDistrict(country, district) {
76
+ return getDataByCountryDIstrictFunction(country, district);
77
+ }
78
+
79
+ export function getDataByCountryCity(country, city) {
80
+ return getDataByCountryCityFunction(country, city);
81
+ }
82
+
83
+ export function getDataByProvinceDistrict(province, district) {
84
+ return getDataByProvinceDistrictFunction(province, district);
85
+ }
86
+
87
+ export function getDataByProvinceCity(province, city) {
88
+ return getDataByProvinceCityFunction(province, city);
89
+ }
90
+
91
+ export function getDataByDistrictCity(district, city) {
92
+ return getDataByDistrictCityFunction(district, city);
23
93
  }
24
94
 
25
95
  // Function to list all available functions
26
96
  export function listFunctions() {
27
97
  return {
28
- getProvincesDistrictsCitiesByCountry,
29
- getCountryDistrictsCitiesByProvince,
30
- getCountryProvinceCityByDistrict,
31
- getCountryProvinceDistrictByCity,
98
+ getDataByCountry,
99
+ getDataByProvince,
100
+ getDataByDistrict,
101
+ getDataByCity,
102
+ getDataByCountryProvince,
103
+ getDataByCountryProvinceDistrict,
104
+ getDataByCountryProvinceDistrictCity,
105
+ getDataByEtherCountryProvinceDistrictCity,
106
+ getDataByCountryDistrict,
107
+ getDataByCountryCity,
108
+ getDataByProvinceDistrict,
109
+ getDataByProvinceCity,
110
+ getDataByDistrictCity,
32
111
  };
33
112
  }