nepali-location
v1.0.2
Published
Complete Nepal location data with provinces, districts, and municipalities. Perfect for address forms and location-based applications.
Maintainers
Readme
Nepali Location Package
A complete Nepal location data library with provinces, districts, and municipalities. Perfect for building location-based applications, address forms, or handling geographical data in Nepal.
Features
- Get provinces, districts, and municipalities in Nepal.
- Retrieve municipalities based on districts, and districts based on provinces.
- Location validation and search.
- Hierarchical location data with provinces, districts, and municipalities.
Installation
You can install the package using npm or yarn:
Using npm:
npm install nepali-location
Using yarn:
bash
Copy code
yarn add nepali-location
Usage
Once installed, you can easily access location data and perform operations with the following functions.
Example Code:
1. Get Districts by Province
To get all districts for a specific province:
typescript
Copy code
import { getDistrictsByProvince } from 'nepali-location';
const districts = getDistrictsByProvince('Bagmati');
console.log(districts);
2. Get Municipalities by District
To get all municipalities in a specific district:
typescript
Copy code
import { getMunicipalitiesByDistrict } from 'nepali-location';
const municipalities = getMunicipalitiesByDistrict('Kathmandu');
console.log(municipalities);
3. Get Location Hierarchy
To get the full hierarchy (province, district, municipality) for a specific municipality:
typescript
Copy code
import { getLocationHierarchy } from 'nepali-location';
const locationHierarchy = getLocationHierarchy('Kathmandu');
console.log(locationHierarchy);
// Output: { province: 'Bagmati', district: 'Kathmandu', municipality: 'Kathmandu' }
4. Check if a Location Exists
To check if a province, district, or municipality exists:
typescript
Copy code
import { locationExists } from 'nepali-location';
const exists = locationExists('district', 'Kathmandu');
console.log(exists); // Returns true or false
Data Structure Overview
Province-District Map: Mapping of provinces to their respective districts.
District-Municipality Map: Mapping of districts to their respective municipalities.
Example:
typescript
Copy code
const provinceDistrictMap = {
bagmati: ['Kathmandu', 'Bhaktapur', 'Dhading'],
gandaki: ['Gorkha', 'Kaski', 'Lamjung'],
lumbini: ['Kapilvastu', 'Rupandehi', 'Arghakhanchi']
};
const districtMunicipalityMap = {
kathmandu: ['Kathmandu', 'Kirtipur', 'Shankharapur'],
bhaktapur: ['Bhaktapur', 'Madhyapur Thimi', 'Changunarayan']
};
Contributing
Contributions are welcome! If you'd like to improve this library, feel free to fork the repository, submit issues, or open pull requests.
How to Contribute:
Fork the repository.
Create your branch (e.g., git checkout -b feature-name).
Make your changes and commit them (e.g., git commit -am 'Add new feature').
Push to your branch (e.g., git push origin feature-name).
Open a pull request.
License
This package is licensed under the MIT License.
React Integration Example
Here’s how you can integrate nepali-location in a React + TypeScript app:
1. Install Dependencies
bash
Copy code
npm install nepali-location
2. App Component Code Example
tsx
Copy code
import React, { useState } from "react";
import { getDistrictsByProvince, getMunicipalitiesByDistrict } from "nepali-location";
function App() {
const [province, setProvince] = useState("");
const [district, setDistrict] = useState("");
const [municipality, setMunicipality] = useState("");
const [districts, setDistricts] = useState<string[]>([]);
const [municipalities, setMunicipalities] = useState<string[]>([]);
const handleProvinceChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const selectedProvince = e.target.value;
setProvince(selectedProvince);
setDistricts(getDistrictsByProvince(selectedProvince));
setMunicipality(""); // Reset municipality when province changes
};
const handleDistrictChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const selectedDistrict = e.target.value;
setDistrict(selectedDistrict);
setMunicipalities(getMunicipalitiesByDistrict(selectedDistrict));
};
return (
<div className="App">
<h1>Select Your Location</h1>
{/* Province Dropdown */}
<select value={province} onChange={handleProvinceChange}>
<option value="">Select Province</option>
<option value="Bagmati">Bagmati</option>
<option value="Sudurpaschim">Sudurpaschim</option>
<option value="Gandaki">Gandaki</option>
<option value="Karnali">Karnali</option>
<option value="Lumbini">Lumbini</option>
<option value="Pradesh-1">Pradesh-1</option>
<option value="Madhesh">Madhesh</option>
</select>
{/* District Dropdown */}
{province && (
<select value={district} onChange={handleDistrictChange}>
<option value="">Select District</option>
{districts.map((district, index) => (
<option key={index} value={district}>{district}</option>
))}
</select>
)}
{/* Municipality Dropdown */}
{district && (
<select value={municipality} onChange={(e) => setMunicipality(e.target.value)}>
<option value="">Select Municipality</option>
{municipalities.map((municipality, index) => (
<option key={index} value={municipality}>{municipality}</option>
))}
</select>
)}
</div>
);
}
export default App;
This React component allows users to select a province, then a district, and finally a municipality. The nepali-location package is used to fetch the districts for a province and the municipalities for a district.
This README provides an easy-to-follow guide for users to install, use, and contribute to the library. It also includes a full example of integrating the package into a React application.