country-data-filter 1.0.10 → 1.0.12

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.10",
3
+ "version": "1.0.12",
4
4
  "description": "A package to filter country data",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -28,6 +28,7 @@
28
28
  "src/*.js",
29
29
  "src/index.js",
30
30
  "src/index.d.ts",
31
+ "src/functions/*.js",
31
32
  "src/countries/*.js"
32
33
  ]
33
34
  }
@@ -1,4 +1,4 @@
1
- import { LK } from "./countries/LK";
1
+ import { LK } from "./countries/LK/LK";
2
2
 
3
3
  export const countryData = {
4
4
  LK: LK,
@@ -0,0 +1,52 @@
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 getCountryDistrictsCitiesByProvinceFunction(provinceName) {
5
+ // Check if the input is valid
6
+ if (typeof provinceName !== "string" || provinceName.trim() === "") {
7
+ return {
8
+ error: "Invalid input: Province name must be a non-empty string.",
9
+ };
10
+ }
11
+
12
+ let countryCode = "";
13
+ const districts = [];
14
+ const cities = [];
15
+
16
+ for (const countryKey in countryData) {
17
+ const country = countryData[countryKey];
18
+ for (const provinceKey in country.provinces) {
19
+ const province = country.provinces[provinceKey];
20
+ if (province.name.toLowerCase() === provinceName.toLowerCase()) {
21
+ const districtsArray = province.districts;
22
+ for (const districtKey in districtsArray) {
23
+ const district = districtsArray[districtKey];
24
+ const citiesArray = district.cities.map((city) => ({
25
+ name: city.name,
26
+ }));
27
+
28
+ // Set the country code
29
+ countryCode = countryKey;
30
+
31
+ // Add district name
32
+ districts.push(district.name);
33
+
34
+ // Add city names
35
+ citiesArray.forEach((city) => cities.push(city.name));
36
+ }
37
+
38
+ // Return result once a matching province is found
39
+ return {
40
+ countryCode,
41
+ districts,
42
+ cities,
43
+ };
44
+ }
45
+ }
46
+ }
47
+
48
+ // If no matching province was found
49
+ return {
50
+ error: `Provided province name "${provinceName}" does not exist.`,
51
+ };
52
+ }
@@ -0,0 +1,49 @@
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 getCountryProvinceCityByDistrictFunction(districtName) {
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
+ let countryCode = "";
13
+ let province = "";
14
+ const cities = [];
15
+
16
+ for (const countryKey in countryData) {
17
+ const country = countryData[countryKey];
18
+ for (const provinceKey in country.provinces) {
19
+ const selectedProvince = country.provinces[provinceKey];
20
+ for (const districtKey in selectedProvince.districts) {
21
+ const district = selectedProvince.districts[districtKey];
22
+ if (district.name.toLowerCase() === districtName.toLowerCase()) {
23
+ // Add city names
24
+ district.cities.forEach((city) => {
25
+ cities.push(city.name);
26
+ });
27
+
28
+ // Set the country code
29
+ countryCode = countryKey;
30
+
31
+ // Set the province name
32
+ province = selectedProvince.name;
33
+
34
+ // Return the result
35
+ return {
36
+ countryCode,
37
+ province,
38
+ cities,
39
+ };
40
+ }
41
+ }
42
+ }
43
+ }
44
+
45
+ // If no matching district was found
46
+ return {
47
+ error: `Provided district name "${districtName}" does not exist.`,
48
+ };
49
+ }
@@ -0,0 +1,50 @@
1
+ import { countryData } from "../country-data";
2
+
3
+ // Function to get a countryCode, provinceName, and districtName by cityName
4
+ export function getCountryProvinceDistrictByCityFunction(cityName) {
5
+ // Check if the input is valid
6
+ if (typeof cityName !== "string" || cityName.trim() === "") {
7
+ return {
8
+ error: "Invalid input: city name must be a non-empty string.",
9
+ };
10
+ }
11
+
12
+ let countryCode = "";
13
+ let province = "";
14
+ let district = "";
15
+
16
+ for (const countryKey in countryData) {
17
+ const country = countryData[countryKey];
18
+ for (const provinceKey in country.provinces) {
19
+ const selectedProvince = country.provinces[provinceKey];
20
+ for (const districtKey in selectedProvince.districts) {
21
+ const selectedDistrict = selectedProvince.districts[districtKey];
22
+ for (const cityKey in selectedDistrict.cities) {
23
+ const selectedCity = selectedDistrict.cities[cityKey];
24
+ if (selectedCity.name.toLowerCase() === cityName.toLowerCase()) {
25
+ // Set the country code
26
+ countryCode = countryKey;
27
+
28
+ // Set the province name
29
+ province = selectedProvince.name;
30
+
31
+ // Set the district name
32
+ district = selectedDistrict.name;
33
+
34
+ // Return the result
35
+ return {
36
+ countryCode,
37
+ province,
38
+ district,
39
+ };
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ // If no matching district was found
47
+ return {
48
+ error: `Provided city name "${cityName}" does not exist.`,
49
+ };
50
+ }
@@ -0,0 +1,35 @@
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 getProvincesDistrictsCitiesByCountryFunction(code) {
5
+ const country = countryData[code];
6
+ if (country) {
7
+ // Return an array of province names
8
+ const provinces = Object.values(country.provinces).map(
9
+ (province) => province.name
10
+ );
11
+
12
+ const districts = [];
13
+ const cities = [];
14
+
15
+ const districtArray = Object.values(country.provinces).flatMap(
16
+ (province) => province.districts
17
+ );
18
+
19
+ districtArray.forEach((districtObject) => {
20
+ // Each item in the array is an object with district names as keys
21
+ Object.keys(districtObject).forEach((key) => {
22
+ const district = districtObject[key];
23
+ // Push the district name to the array
24
+ districts.push(district.name);
25
+ district.cities.forEach((city) => cities.push(city.name));
26
+ });
27
+ });
28
+
29
+ // Return an array of provinces, districts, cities names by country code
30
+ return { provinces, districts, cities };
31
+ }
32
+ return {
33
+ error: `Provided country code "${code}" does not exist.`,
34
+ };
35
+ }
package/src/index.d.ts CHANGED
@@ -1,22 +1,10 @@
1
- export function getProvincesByCountry(code: string): string[];
2
- export function getDistrictsByProvince(
3
- countryCode: string,
4
- provinceName: string
5
- ): string[];
6
- export function getCityByDistrict(
7
- countryCode: string,
8
- provinceName: string,
9
- districtName: string
10
- ): any;
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;
11
5
  export function listFunctions(): {
12
- getProvincesByCountry: (code: string) => string[];
13
- getDistrictsByProvince: (
14
- countryCode: string,
15
- provinceName: string
16
- ) => string[];
17
- getCityByDistrict: (
18
- countryCode: string,
19
- provinceName: string,
20
- districtName: string
21
- ) => any;
6
+ getProvincesDistrictsCitiesByCountry: (code: string) => any;
7
+ getCountryDistrictsCitiesByProvince: (provinceName: string) => any;
8
+ getCountryProvinceCityByDistrict: (districtName: string) => any;
9
+ getCountryProvinceDistrictByCity: (cityName: string) => any;
22
10
  };
package/src/index.js CHANGED
@@ -1,42 +1,33 @@
1
- import { countryData } from "./country-data.js";
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";
2
5
 
3
- // Function to get the list of province names by country code
4
- export function getProvincesByCountry(code) {
5
- const country = countryData[code];
6
- if (country) {
7
- // Return an array of province names
8
- return Object.values(country.province).map((province) => province.name);
9
- }
10
- return [];
6
+ // Function to get a list of province, district, and city names by country code
7
+ export function getProvincesDistrictsCitiesByCountry(code) {
8
+ return getProvincesDistrictsCitiesByCountryFunction(code);
11
9
  }
12
10
 
13
- // Function to get a list of district names by country code and province name
14
- export function getDistrictsByProvince(countryCode, provinceName) {
15
- const country = countryData[countryCode];
16
- if (country && country.province[provinceName.toLowerCase()]) {
17
- // Return an array of district names
18
- return Object.values(country.province[provinceName].districts).map(
19
- (district) => district.name
20
- );
21
- }
22
- return [];
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);
23
14
  }
24
15
 
25
- // Function to get city details by country code, province name, and district name
26
- export function getCityByDistrict(countryCode, provinceName, districtName) {
27
- const country = countryData[countryCode];
28
- const province = country.province[provinceName.toLowerCase()];
29
- if (province && province.districts[districtName.toLowerCase()]) {
30
- return province.districts[districtName];
31
- }
32
- return null;
16
+ // Function to get a countryCode, provinceName, and list of city names by district name
17
+ export function getCountryProvinceCityByDistrict(districtName) {
18
+ return getCountryProvinceCityByDistrictFunction(districtName);
19
+ }
20
+
21
+ export function getCountryProvinceDistrictByCity(cityName) {
22
+ return getCountryProvinceDistrictByCityFunction(cityName);
33
23
  }
34
24
 
35
25
  // Function to list all available functions
36
26
  export function listFunctions() {
37
27
  return {
38
- getProvincesByCountry,
39
- getDistrictsByProvince,
40
- getCityByDistrict,
28
+ getProvincesDistrictsCitiesByCountry,
29
+ getCountryDistrictsCitiesByProvince,
30
+ getCountryProvinceCityByDistrict,
31
+ getCountryProvinceDistrictByCity,
41
32
  };
42
33
  }
@@ -1,18 +0,0 @@
1
- export const LK = {
2
- name: "Sri Lanka",
3
- province: {
4
- southern: {
5
- name: "Southern",
6
- districts: {
7
- matara: {
8
- name: "Matara",
9
- cities: [{ name: "Weligama", postalCode: 85700 }],
10
- },
11
- galle: {
12
- name: "Galle",
13
- cities: [{ name: "Galle", postalCode: 80000 }],
14
- },
15
- },
16
- },
17
- },
18
- };