
Security News
Feross on TBPN: Socket's Series C and the State of Software Supply Chain Security
Feross Aboukhadijeh joins TBPN to discuss Socket's $60M Series C, 500%+ ARR growth, AI's impact on open source, and the rise in supply chain attacks.
github.com/surajaswal29/india-state-district
Advanced tools
Note: This is an educational project designed to demonstrate Indian geographical data handling. Feel free to use it for learning purposes.
A lightweight, type-safe TypeScript utility package for handling Indian states and districts data.
India State District is a zero-dependency solution that provides comprehensive data and utility functions for handling Indian geographical data (states and districts). Built with TypeScript, it offers a type-safe and developer-friendly way to work with Indian geographical data in your applications.
# Using npm
npm install india-state-district
# Using yarn
yarn add india-state-district
# Using pnpm
pnpm add india-state-district
import india, {
getAllStates,
getDistricts,
getAllStateCodes,
getCurrentState,
getAllStatesWithDistricts,
detectStateFromLocation,
isGeolocationSupported,
} from "india-state-district"
// Get all states
const states = getAllStates()
// Get districts for a specific state code
const districts = getDistricts("MH")
// Get all state codes
const codes = getAllStateCodes()
// Get current state (from default instance)
const currentState = getCurrentState()
// Get all states with their districts
const allStatesWithDistricts = getAllStatesWithDistricts()
import {
detectStateFromLocation,
isGeolocationSupported,
} from "india-state-district"
// Check if geolocation is supported
if (isGeolocationSupported()) {
try {
const result = await detectStateFromLocation({
timeout: 5000,
enableHighAccuracy: true,
})
console.log(result.state) // "Karnataka"
console.log(result.stateCode) // "KA"
console.log(result.latitude) // 12.9716
console.log(result.longitude) // 77.5946
} catch (error) {
if (error.code === "PERMISSION_DENIED") {
console.log("User denied location access")
} else if (error.code === "STATE_NOT_FOUND") {
console.log("Location is outside India")
}
}
}
import {
createIndiaStateDistrict,
IndiaStateDistrict,
} from "india-state-district"
const customIndia = createIndiaStateDistrict()
// Use methods on the custom instance
const states = customIndia.getAllStates()
const districts = customIndia.getDistrictsForState("MH")
const india = require("india-state-district").default
const { getAllStates, getDistricts } = require("india-state-district")
console.log(getAllStates())
console.log(getDistricts("MH"))
import React, { useState } from "react"
import { getAllStates, getDistricts } from "india-state-district"
const LocationSelector: React.FC = () => {
const [selectedState, setSelectedState] = useState("")
const [selectedDistrict, setSelectedDistrict] = useState("")
const [districts, setDistricts] = useState<string[]>([])
const handleStateChange = (state: string) => {
setSelectedState(state)
setDistricts(getDistricts(state))
setSelectedDistrict("")
}
return (
<div>
<select
value={selectedState}
onChange={(e) => handleStateChange(e.target.value)}
>
<option value=''>Select State</option>
{getAllStates().map((state) => (
<option key={state.code} value={state.code}>
{state.name}
</option>
))}
</select>
<select
value={selectedDistrict}
onChange={(e) => setSelectedDistrict(e.target.value)}
disabled={!selectedState}
>
<option value=''>Select District</option>
{districts.map((district) => (
<option key={district} value={district}>
{district}
</option>
))}
</select>
</div>
)
}
export default LocationSelector
| Function | Description | Arguments | Returns |
|---|---|---|---|
getAllStates() | Get all states | None | State[] |
getDistricts(stateCode) | Get districts for a state code | stateCode: string | string[] |
getAllStateCodes() | Get all state codes | None | string[] |
getCurrentState() | Get current state (from default instance) | None | State |
getAllStatesWithDistricts() | Get all states with their districts | None | Array<{ state: State, districts: string[] }> |
| Function | Description | Arguments | Returns |
|---|---|---|---|
detectStateFromLocation(options?) | Auto-detect state from browser location | GeolocationOptions? | Promise<GeolocationResult> |
isGeolocationSupported() | Check if browser supports geolocation | None | boolean |
interface GeolocationResult {
state: string // Full state name (e.g., "Karnataka")
stateCode: string // State code (e.g., "KA")
district?: string // District name (if available)
latitude: number // User's latitude
longitude: number // User's longitude
}
interface GeolocationOptions {
enableHighAccuracy?: boolean // Use GPS (default: false)
timeout?: number // Max wait time in ms (default: 10000)
maximumAge?: number // Cache age in ms (default: 300000)
}
interface GeolocationError {
code: "PERMISSION_DENIED" | "POSITION_UNAVAILABLE" | "TIMEOUT" |
"NOT_SUPPORTED" | "REVERSE_GEOCODE_FAILED" | "STATE_NOT_FOUND"
message: string
}
createIndiaStateDistrict() — Create a new instance for advanced use-casesIndiaStateDistrict — Class for advanced usageState, StateData, GeolocationResult, GeolocationOptions, GeolocationError — TypeScript typesINDIA_FLAG_SVG, INDIA_FLAG_COLORS, getIndiaFlagSVG,
getIndiaFlagDataUrl — Flag utilitiesThe package is tested and supported on all modern browsers:
See CHANGELOG.md for a list of changes and version history. We follow Semantic Versioning and document all notable changes following the Keep a Changelog format.
We appreciate all contributions to improve India State District. Here's how you can help:
git checkout -b feature/enhancement)git commit -m 'Add enhancement')git push origin feature/enhancement)Please ensure your PR adheres to the following guidelines:
This project is licensed under the MIT License - see the LICENSE file for details.
Developed with ❤️ by Suraj Aswal
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Feross Aboukhadijeh joins TBPN to discuss Socket's $60M Series C, 500%+ ARR growth, AI's impact on open source, and the rise in supply chain attacks.

Security News
OSV withdrew 157 OSV malware reports after automated false positives incorrectly flagged trusted npm and PyPI packages, sending bad records into tools that rely on OSV data.

Research
/Security News
TrapDoor crypto stealer hits 36 malicious packages across npm, PyPI, and Crates.io, targeting crypto, DeFi, AI, and security developers.