@mohit_dev/india-location-master 0.0.0-stage → 1.0.0
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 +72 -2
- package/data/cities.json +1490376 -0
- package/data/districts.json +4320 -0
- package/data/states.json +38 -0
- package/package.json +29 -4
- package/src/index.js +268 -0
package/data/states.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[
|
|
2
|
+
{ "id": "AN", "name": "Andaman and Nicobar Islands", "type": "Union Territory", "languages": ["Hindi", "English", "Bengali"] },
|
|
3
|
+
{ "id": "AP", "name": "Andhra Pradesh", "type": "State", "languages": ["Telugu"] },
|
|
4
|
+
{ "id": "AR", "name": "Arunachal Pradesh", "type": "State", "languages": ["English", "Hindi"] },
|
|
5
|
+
{ "id": "AS", "name": "Assam", "type": "State", "languages": ["Assamese", "Bodo", "Bengali"] },
|
|
6
|
+
{ "id": "BR", "name": "Bihar", "type": "State", "languages": ["Hindi", "Maithili", "Urdu"] },
|
|
7
|
+
{ "id": "CH", "name": "Chandigarh", "type": "Union Territory", "languages": ["English", "Punjabi", "Hindi"] },
|
|
8
|
+
{ "id": "CG", "name": "Chhattisgarh", "type": "State", "languages": ["Chhattisgarhi", "Hindi"] },
|
|
9
|
+
{ "id": "DN", "name": "Dadra and Nagar Haveli and Daman and Diu", "type": "Union Territory", "languages": ["Gujarati", "Hindi", "Marathi", "Konkani"] },
|
|
10
|
+
{ "id": "DL", "name": "Delhi", "type": "Union Territory", "languages": ["Hindi", "English", "Punjabi", "Urdu"] },
|
|
11
|
+
{ "id": "GA", "name": "Goa", "type": "State", "languages": ["Konkani", "Marathi"] },
|
|
12
|
+
{ "id": "GJ", "name": "Gujarat", "type": "State", "languages": ["Gujarati"] },
|
|
13
|
+
{ "id": "HR", "name": "Haryana", "type": "State", "languages": ["Hindi", "Haryanvi", "Punjabi"] },
|
|
14
|
+
{ "id": "HP", "name": "Himachal Pradesh", "type": "State", "languages": ["Hindi", "Pahari"] },
|
|
15
|
+
{ "id": "JK", "name": "Jammu and Kashmir", "type": "Union Territory", "languages": ["Kashmiri", "Dogri", "Hindi", "Urdu"] },
|
|
16
|
+
{ "id": "JH", "name": "Jharkhand", "type": "State", "languages": ["Hindi", "Santali", "Urdu"] },
|
|
17
|
+
{ "id": "KA", "name": "Karnataka", "type": "State", "languages": ["Kannada"] },
|
|
18
|
+
{ "id": "KL", "name": "Kerala", "type": "State", "languages": ["Malayalam"] },
|
|
19
|
+
{ "id": "LA", "name": "Ladakh", "type": "Union Territory", "languages": ["Ladakhi", "Balti", "Hindi"] },
|
|
20
|
+
{ "id": "LD", "name": "Lakshadweep", "type": "Union Territory", "languages": ["Malayalam", "Mahl"] },
|
|
21
|
+
{ "id": "MP", "name": "Madhya Pradesh", "type": "State", "languages": ["Hindi"] },
|
|
22
|
+
{ "id": "MH", "name": "Maharashtra", "type": "State", "languages": ["Marathi"] },
|
|
23
|
+
{ "id": "MN", "name": "Manipur", "type": "State", "languages": ["Manipuri (Meitei)"] },
|
|
24
|
+
{ "id": "ML", "name": "Meghalaya", "type": "State", "languages": ["English", "Khasi", "Garo"] },
|
|
25
|
+
{ "id": "MZ", "name": "Mizoram", "type": "State", "languages": ["Mizo", "English"] },
|
|
26
|
+
{ "id": "NL", "name": "Nagaland", "type": "State", "languages": ["English"] },
|
|
27
|
+
{ "id": "OD", "name": "Odisha", "type": "State", "languages": ["Odia"] },
|
|
28
|
+
{ "id": "PY", "name": "Puducherry", "type": "Union Territory", "languages": ["Tamil", "Malayalam", "Telugu", "French"] },
|
|
29
|
+
{ "id": "PB", "name": "Punjab", "type": "State", "languages": ["Punjabi"] },
|
|
30
|
+
{ "id": "RJ", "name": "Rajasthan", "type": "State", "languages": ["Hindi", "Rajasthani"] },
|
|
31
|
+
{ "id": "SK", "name": "Sikkim", "type": "State", "languages": ["Nepali", "Sikkimese", "Lepcha"] },
|
|
32
|
+
{ "id": "TN", "name": "Tamil Nadu", "type": "State", "languages": ["Tamil"] },
|
|
33
|
+
{ "id": "TS", "name": "Telangana", "type": "State", "languages": ["Telugu", "Urdu"] },
|
|
34
|
+
{ "id": "TR", "name": "Tripura", "type": "State", "languages": ["Bengali", "Kokborok"] },
|
|
35
|
+
{ "id": "UP", "name": "Uttar Pradesh", "type": "State", "languages": ["Hindi", "Urdu"] },
|
|
36
|
+
{ "id": "UK", "name": "Uttarakhand", "type": "State", "languages": ["Hindi", "Garhwali", "Kumaoni"] },
|
|
37
|
+
{ "id": "WB", "name": "West Bengal", "type": "State", "languages": ["Bengali"] }
|
|
38
|
+
]
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mohit_dev/india-location-master",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Comprehensive India state, district, and city location data with easy query helper functions and Google Maps style autocomplete suggestions",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src/",
|
|
8
|
+
"data/",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"start": "node test.js",
|
|
13
|
+
"test": "node test.js"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"india",
|
|
17
|
+
"location",
|
|
18
|
+
"state",
|
|
19
|
+
"district",
|
|
20
|
+
"city",
|
|
21
|
+
"states",
|
|
22
|
+
"districts",
|
|
23
|
+
"cities",
|
|
24
|
+
"india-data",
|
|
25
|
+
"pincode",
|
|
26
|
+
"autocomplete",
|
|
27
|
+
"suggestions"
|
|
28
|
+
],
|
|
29
|
+
"author": "",
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
const states = require("../data/states.json");
|
|
2
|
+
const districts = require("../data/districts.json");
|
|
3
|
+
const cities = require("../data/cities.json");
|
|
4
|
+
|
|
5
|
+
// Fast lookup maps for parent context resolution
|
|
6
|
+
const stateMap = new Map(states.map((s) => [s.id, s]));
|
|
7
|
+
const districtMap = new Map(districts.map((d) => [d.id, d]));
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get all States and Union Territories
|
|
11
|
+
* @returns {Array} List of states
|
|
12
|
+
*/
|
|
13
|
+
function getStates() {
|
|
14
|
+
return states;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get Districts for a given State ID or Name
|
|
19
|
+
* @param {string} [stateId] State ID (e.g., "MP") or State Name (optional)
|
|
20
|
+
* @returns {Array} List of districts
|
|
21
|
+
*/
|
|
22
|
+
function getDistricts(stateId) {
|
|
23
|
+
if (!stateId) return districts;
|
|
24
|
+
const normalized = stateId.trim().toLowerCase();
|
|
25
|
+
|
|
26
|
+
const matchedState = states.find(
|
|
27
|
+
(s) => s.id.toLowerCase() === normalized || s.name.toLowerCase() === normalized
|
|
28
|
+
);
|
|
29
|
+
const targetId = matchedState ? matchedState.id.toLowerCase() : normalized;
|
|
30
|
+
|
|
31
|
+
return districts.filter(
|
|
32
|
+
(district) => district.stateId.toLowerCase() === targetId
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get Cities for a given District ID or Name
|
|
38
|
+
* @param {string} [districtId] District ID or District Name (optional)
|
|
39
|
+
* @returns {Array} List of cities
|
|
40
|
+
*/
|
|
41
|
+
function getCities(districtId) {
|
|
42
|
+
if (!districtId) return cities;
|
|
43
|
+
const normalized = districtId.trim().toLowerCase();
|
|
44
|
+
|
|
45
|
+
const matchedDistrict = districts.find(
|
|
46
|
+
(d) => d.id.toLowerCase() === normalized || d.name.toLowerCase() === normalized
|
|
47
|
+
);
|
|
48
|
+
const targetId = matchedDistrict ? matchedDistrict.id.toLowerCase() : normalized;
|
|
49
|
+
|
|
50
|
+
return cities.filter(
|
|
51
|
+
(city) => city.districtId.toLowerCase() === targetId
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get State by Name or ID
|
|
57
|
+
* @param {string} name State Name or State ID
|
|
58
|
+
* @returns {Object|null} State object
|
|
59
|
+
*/
|
|
60
|
+
function getStateByName(name) {
|
|
61
|
+
if (!name) return null;
|
|
62
|
+
const normalized = name.trim().toLowerCase();
|
|
63
|
+
return (
|
|
64
|
+
states.find(
|
|
65
|
+
(s) =>
|
|
66
|
+
s.name.toLowerCase() === normalized ||
|
|
67
|
+
s.id.toLowerCase() === normalized
|
|
68
|
+
) || null
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get District by Name or ID
|
|
74
|
+
* @param {string} name District Name or ID
|
|
75
|
+
* @returns {Object|null} District object
|
|
76
|
+
*/
|
|
77
|
+
function getDistrictByName(name) {
|
|
78
|
+
if (!name) return null;
|
|
79
|
+
const normalized = name.trim().toLowerCase();
|
|
80
|
+
return (
|
|
81
|
+
districts.find(
|
|
82
|
+
(d) =>
|
|
83
|
+
d.name.toLowerCase() === normalized ||
|
|
84
|
+
d.id.toLowerCase() === normalized
|
|
85
|
+
) || null
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Search cities by partial name query
|
|
91
|
+
* @param {string} query Search string
|
|
92
|
+
* @param {number} [limit=50] Maximum number of results to return
|
|
93
|
+
* @returns {Array} Array of matching city objects
|
|
94
|
+
*/
|
|
95
|
+
function searchCities(query, limit = 50) {
|
|
96
|
+
if (!query) return [];
|
|
97
|
+
const normalized = query.trim().toLowerCase();
|
|
98
|
+
const results = [];
|
|
99
|
+
for (let i = 0; i < cities.length; i++) {
|
|
100
|
+
if (cities[i].name.toLowerCase().includes(normalized)) {
|
|
101
|
+
results.push(cities[i]);
|
|
102
|
+
if (results.length >= limit) break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return results;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Find locations/cities by PIN code
|
|
110
|
+
* @param {string|number} pincode 6-digit PIN code
|
|
111
|
+
* @returns {Array} List of matching city objects
|
|
112
|
+
*/
|
|
113
|
+
function getCitiesByPincode(pincode) {
|
|
114
|
+
if (!pincode) return [];
|
|
115
|
+
const pinStr = String(pincode).trim();
|
|
116
|
+
return cities.filter(
|
|
117
|
+
(city) => city.pincodes && city.pincodes.includes(pinStr)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Google Maps style Place Autocomplete / Search Suggestions
|
|
123
|
+
* Searches across States, Districts, Cities, and PIN codes
|
|
124
|
+
* @param {string} query User search text (e.g. "mhow", "indore", "453441")
|
|
125
|
+
* @param {number} [limit=10] Maximum suggestions to return
|
|
126
|
+
* @returns {Array} Array of suggestion objects formatted for UI dropdowns
|
|
127
|
+
*/
|
|
128
|
+
function getSuggestions(query, limit = 10) {
|
|
129
|
+
if (!query || typeof query !== "string" || !query.trim()) return [];
|
|
130
|
+
const normalized = query.trim().toLowerCase();
|
|
131
|
+
const suggestions = [];
|
|
132
|
+
|
|
133
|
+
// 1. Search States
|
|
134
|
+
for (let i = 0; i < states.length; i++) {
|
|
135
|
+
const s = states[i];
|
|
136
|
+
if (s.name.toLowerCase().includes(normalized) || s.id.toLowerCase() === normalized) {
|
|
137
|
+
suggestions.push({
|
|
138
|
+
id: s.id,
|
|
139
|
+
type: "state",
|
|
140
|
+
name: s.name,
|
|
141
|
+
state: s.name,
|
|
142
|
+
stateId: s.id,
|
|
143
|
+
languages: s.languages || [],
|
|
144
|
+
displayText: `${s.name} (State)`
|
|
145
|
+
});
|
|
146
|
+
if (suggestions.length >= limit) return suggestions;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 2. Search Districts
|
|
151
|
+
for (let i = 0; i < districts.length; i++) {
|
|
152
|
+
const d = districts[i];
|
|
153
|
+
if (d.name.toLowerCase().includes(normalized)) {
|
|
154
|
+
const parentState = stateMap.get(d.stateId);
|
|
155
|
+
const stateName = parentState ? parentState.name : d.stateId;
|
|
156
|
+
suggestions.push({
|
|
157
|
+
id: d.id,
|
|
158
|
+
type: "district",
|
|
159
|
+
name: d.name,
|
|
160
|
+
district: d.name,
|
|
161
|
+
state: stateName,
|
|
162
|
+
stateId: d.stateId,
|
|
163
|
+
lat: d.lat || null,
|
|
164
|
+
lng: d.lng || null,
|
|
165
|
+
displayText: `${d.name} (District), ${stateName}`
|
|
166
|
+
});
|
|
167
|
+
if (suggestions.length >= limit) return suggestions;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 3. Search Cities / PIN codes
|
|
172
|
+
for (let i = 0; i < cities.length; i++) {
|
|
173
|
+
const c = cities[i];
|
|
174
|
+
const nameMatch = c.name.toLowerCase().includes(normalized);
|
|
175
|
+
const pinMatch = c.pincodes && c.pincodes.some((pin) => pin.includes(normalized));
|
|
176
|
+
|
|
177
|
+
if (nameMatch || pinMatch) {
|
|
178
|
+
const parentDistrict = districtMap.get(c.districtId);
|
|
179
|
+
const districtName = parentDistrict ? parentDistrict.name : c.districtId;
|
|
180
|
+
const stateId = parentDistrict ? parentDistrict.stateId : "";
|
|
181
|
+
const parentState = stateMap.get(stateId);
|
|
182
|
+
const stateName = parentState ? parentState.name : stateId;
|
|
183
|
+
|
|
184
|
+
const pincodeText = c.pincodes && c.pincodes.length > 0 ? ` - ${c.pincodes.join(", ")}` : "";
|
|
185
|
+
|
|
186
|
+
suggestions.push({
|
|
187
|
+
id: c.id,
|
|
188
|
+
type: "city",
|
|
189
|
+
name: c.name,
|
|
190
|
+
district: districtName,
|
|
191
|
+
districtId: c.districtId,
|
|
192
|
+
state: stateName,
|
|
193
|
+
stateId: stateId,
|
|
194
|
+
pincodes: c.pincodes || [],
|
|
195
|
+
lat: c.lat || null,
|
|
196
|
+
lng: c.lng || null,
|
|
197
|
+
displayText: `${c.name}, ${districtName}, ${stateName}${pincodeText}`
|
|
198
|
+
});
|
|
199
|
+
if (suggestions.length >= limit) return suggestions;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return suggestions;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Get native / official languages for a given State or District
|
|
208
|
+
* @param {string} locationName State ID, State Name, or District Name
|
|
209
|
+
* @returns {Array} Array of native/official language names
|
|
210
|
+
*/
|
|
211
|
+
function getLanguages(locationName) {
|
|
212
|
+
if (!locationName) return [];
|
|
213
|
+
const normalized = locationName.trim().toLowerCase();
|
|
214
|
+
|
|
215
|
+
let state = states.find(
|
|
216
|
+
(s) => s.id.toLowerCase() === normalized || s.name.toLowerCase() === normalized
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (!state) {
|
|
220
|
+
const district = districts.find(
|
|
221
|
+
(d) => d.id.toLowerCase() === normalized || d.name.toLowerCase() === normalized
|
|
222
|
+
);
|
|
223
|
+
if (district) {
|
|
224
|
+
state = states.find((s) => s.id.toLowerCase() === district.stateId.toLowerCase());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return state ? state.languages || [] : [];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Get states where a specific language is officially spoken
|
|
233
|
+
* @param {string} language Language name (e.g., "Hindi", "Marathi", "Tamil")
|
|
234
|
+
* @returns {Array} List of state objects
|
|
235
|
+
*/
|
|
236
|
+
function getStatesByLanguage(language) {
|
|
237
|
+
if (!language) return [];
|
|
238
|
+
const normalized = language.trim().toLowerCase();
|
|
239
|
+
return states.filter(
|
|
240
|
+
(s) => s.languages && s.languages.some((l) => l.toLowerCase() === normalized)
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Get complete location dataset
|
|
246
|
+
* @returns {Object} Object containing states, districts, and cities
|
|
247
|
+
*/
|
|
248
|
+
function getAllLocations() {
|
|
249
|
+
return {
|
|
250
|
+
states,
|
|
251
|
+
districts,
|
|
252
|
+
cities,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
module.exports = {
|
|
257
|
+
getStates,
|
|
258
|
+
getDistricts,
|
|
259
|
+
getCities,
|
|
260
|
+
getStateByName,
|
|
261
|
+
getDistrictByName,
|
|
262
|
+
searchCities,
|
|
263
|
+
getCitiesByPincode,
|
|
264
|
+
getSuggestions,
|
|
265
|
+
getLanguages,
|
|
266
|
+
getStatesByLanguage,
|
|
267
|
+
getAllLocations,
|
|
268
|
+
};
|