country-data-filter 1.0.14 → 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/README.md ADDED
@@ -0,0 +1,105 @@
1
+ Country Data Filter
2
+ country-data-filter is an npm package designed to provide detailed country data, including provinces, districts, cities, and postal codes. This package is useful for applications that need to access and filter geographical information.
3
+
4
+ Installation
5
+ You can install the package via npm:
6
+
7
+ bash
8
+ Copy code
9
+ npm install country-data-filter
10
+ Usage
11
+ Importing the Package
12
+ In your JavaScript or TypeScript project, you can import the functions provided by the package.
13
+
14
+ JavaScript
15
+ javascript
16
+ Copy code
17
+ const {
18
+ getProvincesDistrictsCitiesByCountryFunction,
19
+ getCountryProvinceCityByDistrict,
20
+ getCountryDistrictsCitiesByProvince,
21
+ listFunctions
22
+ } = require('country-data-filter');
23
+ TypeScript
24
+ typescript
25
+ Copy code
26
+ import {
27
+ getProvincesDistrictsCitiesByCountryFunction,
28
+ getCountryProvinceCityByDistrict,
29
+ getCountryDistrictsCitiesByProvince,
30
+ listFunctions
31
+ } from 'country-data-filter';
32
+ Functions
33
+ getProvincesDistrictsCitiesByCountryFunction(code)
34
+ Returns a list of provinces, districts, and cities for a given country code.
35
+
36
+ Parameters:
37
+
38
+ code (string): The country code (e.g., 'LK' for Sri Lanka).
39
+ Returns:
40
+
41
+ An object containing sorted arrays of provinces, districts, and cities, or an error message if the country code is invalid.
42
+ Example:
43
+
44
+ javascript
45
+ Copy code
46
+ const result = getProvincesDistrictsCitiesByCountryFunction('LK');
47
+ console.log(result);
48
+ getCountryProvinceCityByDistrict(districtName)
49
+ Returns the country code, province, and cities for a given district name.
50
+
51
+ Parameters:
52
+
53
+ districtName (string): The name of the district.
54
+ Returns:
55
+
56
+ An object containing the country code, province name, and list of cities, or an error message if the district name is invalid.
57
+ Example:
58
+
59
+ javascript
60
+ Copy code
61
+ const result = getCountryProvinceCityByDistrict('Matara');
62
+ console.log(result);
63
+ getCountryDistrictsCitiesByProvince(provinceName)
64
+ Returns the list of districts and cities for a given province name.
65
+
66
+ Parameters:
67
+
68
+ provinceName (string): The name of the province.
69
+ Returns:
70
+
71
+ An object containing the list of districts and cities, or an error message if the province name is invalid.
72
+ Example:
73
+
74
+ javascript
75
+ Copy code
76
+ const result = getCountryDistrictsCitiesByProvince('Southern');
77
+ console.log(result);
78
+ listFunctions()
79
+ Returns a list of all available functions in the package.
80
+
81
+ Returns:
82
+
83
+ An object containing the function names and their references.
84
+ Example:
85
+
86
+ javascript
87
+ Copy code
88
+ const functions = listFunctions();
89
+ console.log(functions);
90
+ Data Structure
91
+ The data for countries, provinces, districts, and cities is stored in JavaScript objects. Each country object contains provinces, and each province object contains districts and cities.
92
+
93
+ Contributing
94
+ If you want to contribute to this project, please follow these steps:
95
+
96
+ Fork the repository.
97
+ Create a new branch for your changes.
98
+ Make your changes and add tests if applicable.
99
+ Submit a pull request with a clear description of your changes.
100
+ License
101
+ This project is licensed under the MIT License. See the LICENSE file for details.
102
+
103
+ Contact
104
+ For any questions or support, please contact knnadeera1@gmail.com.
105
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "country-data-filter",
3
- "version": "1.0.14",
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
+ }