india-state-district
Version:
A lightweight TypeScript library for handling Indian states and districts data with type safety and easy integration
129 lines • 4.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndiaStateDistrict = void 0;
const types_1 = require("../types");
const data_json_1 = __importDefault(require("../data/data.json"));
/**
* IndiaStateDistrict class provides functionality to manage and retrieve
* Indian states and their districts data.
*
* @class
* @description
* This class handles operations related to Indian states and districts,
* including getting state lists, district lists, and managing current state selection.
*
* @example
* ```typescript
* const india = new IndiaStateDistrict();
* const districts = india.setStateCode('KA'); // Get Karnataka districts
* ```
*/
class IndiaStateDistrict {
/**
* Initializes a new instance of IndiaStateDistrict
* Loads the state and district data internally
*/
constructor() {
/** Currently selected state code */
this.currentStateCode = null;
this.rawData = data_json_1.default;
}
/**
* Sets the current state code and returns its districts
*
* @param stateCode - The code of the state to set (e.g., 'KA' for Karnataka)
* @returns Array of district names for the selected state
* @throws Error if the state code is invalid
*
* @example
* ```typescript
* const districts = india.setStateCode('KA');
* console.log(districts); // ['Bangalore', 'Mysore', ...]
* ```
*/
setStateCode(stateCode) {
if (!this.rawData[stateCode]) {
throw new Error(`Invalid state code: ${stateCode}`);
}
this.currentStateCode = stateCode;
return this.getDistricts();
}
/**
* Gets the districts of the currently selected state
*
* @returns Array of district names for the current state
* If no state is selected, returns an empty array
*/
getDistricts() {
if (!this.currentStateCode)
return [];
return [...this.rawData[this.currentStateCode]];
}
/**
* Gets detailed information about the currently selected state
*
* @returns Object containing state code, name, and districts
* Returns null if no state is selected
*/
getCurrentState() {
if (!this.currentStateCode)
return null;
return {
code: this.currentStateCode,
name: types_1.STATE_NAMES[this.currentStateCode] || this.currentStateCode,
districts: this.getDistricts(),
};
}
/**
* Gets all available state codes
*
* @returns Array of state codes (e.g., ['AP', 'KA', 'TN', ...])
*/
getAllStateCodes() {
return Object.keys(this.rawData);
}
/**
* Gets all states with their codes and names
*
* @returns Array of objects containing state codes and names
*/
getAllStates() {
return Object.keys(this.rawData).map((code) => ({
code,
name: types_1.STATE_NAMES[code] || code,
}));
}
/**
* Gets comprehensive data for all states including their districts
*
* @returns Array of objects containing state codes, names, and their districts
*/
getAllStatesWithDistricts() {
return Object.entries(this.rawData).map(([code, districts]) => ({
code,
name: types_1.STATE_NAMES[code] || code,
districts: [...districts],
}));
}
/**
* Gets districts for any state code without changing the current state
*
* @param stateCode - The code of the state to get districts for
* @returns Array of district names for the specified state
* Returns empty array if state code is invalid
*/
getDistrictsForState(stateCode) {
return this.rawData[stateCode] ? [...this.rawData[stateCode]] : [];
}
/**
* Resets the current state selection
*/
reset() {
this.currentStateCode = null;
}
}
exports.IndiaStateDistrict = IndiaStateDistrict;
//# sourceMappingURL=india-state-district.service.js.map