india-pincode 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 ADDED
@@ -0,0 +1,248 @@
1
+ # india-pincode
2
+
3
+ Ultra-fast npm package for **165,000+ Indian post offices** — lookup by pincode, state, district, office name, coordinates. O(1) HashMap performance. Works **standalone** or as a **NestJS module**.
4
+
5
+ ## Features
6
+
7
+ - **165,627 post offices** across **37 states/UTs**, **750 districts**, **19,586 unique pincodes**
8
+ - **O(1) lookups** — pincode, state, district, office name via pre-built HashMaps
9
+ - **Geo search** — find nearby post offices by lat/lng with bounding-box pruning + Haversine
10
+ - **Pagination** built-in — `limit`, `page`, `officeType`, `deliveryOnly` filters
11
+ - **Summary APIs** — get pincode/district/state summaries in one call
12
+ - **NestJS module** — `@Global()` module with injectable `PincodeService`
13
+ - **Standalone** — zero dependencies for non-NestJS projects
14
+ - **TypeScript first** — full type definitions, ESM + CJS dual output
15
+ - **Lazy indexes** — built on first use, cached forever (< 200ms cold start)
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install india-pincode
21
+ ```
22
+
23
+ ## Quick Start (Standalone)
24
+
25
+ ```typescript
26
+ import { getIndiaPincode } from 'india-pincode';
27
+
28
+ const pin = getIndiaPincode();
29
+
30
+ // ─── O(1) Pincode Lookup ─────────────────────────────────
31
+ const result = pin.getByPincode('110001');
32
+ console.log(result.data);
33
+ // [{ pincode: '110001', officeName: 'Baroda House', officeType: 'HO', ... }]
34
+
35
+ // ─── Pincode Summary ─────────────────────────────────────
36
+ const summary = pin.getPincodeSummary('400001');
37
+ // { pincode: '400001', district: 'MUMBAI', state: 'MAHARASHTRA', offices: [...], deliveryOffices: 3 }
38
+
39
+ // ─── By State (paginated) ────────────────────────────────
40
+ const mh = pin.getByState('MAHARASHTRA', { limit: 10, page: 2 });
41
+ console.log(mh.data); // 10 records
42
+ console.log(mh.total); // total offices in Maharashtra
43
+ console.log(mh.totalPages); // total pages
44
+
45
+ // ─── Filter: only Head Post Offices that deliver ─────────
46
+ pin.getByState('DELHI', { officeType: 'HO', deliveryOnly: true });
47
+
48
+ // ─── By District ─────────────────────────────────────────
49
+ pin.getByDistrict('PUNE', { limit: 20 });
50
+
51
+ // ─── District Summary ────────────────────────────────────
52
+ pin.getDistrictSummary('BANGALORE');
53
+ // { district: 'BANGALORE', state: 'KARNATAKA', totalOffices: 120, totalPincodes: 85, pincodes: [...] }
54
+
55
+ // ─── State Summary ───────────────────────────────────────
56
+ pin.getStateSummary('KARNATAKA');
57
+ // { state: 'KARNATAKA', totalDistricts: 31, totalOffices: 5200, totalPincodes: 3100, districts: [...] }
58
+
59
+ // ─── Lists ───────────────────────────────────────────────
60
+ pin.getAllStates(); // ['ANDHRA PRADESH', 'ASSAM', ...]
61
+ pin.getAllDistricts(); // ['ADILABAD', 'AGAR MALWA', ...]
62
+ pin.getDistrictsByState('UP') // ['AGRA', 'ALIGARH', ...]
63
+ pin.getPincodesByState('GOA') // ['403001', '403002', ...]
64
+ pin.getPincodesByDistrict('PUNE') // ['410501', '411001', ...]
65
+
66
+ // ─── Search (free text) ─────────────────────────────────
67
+ pin.search('Koramangala');
68
+ pin.search('5600', { limit: 5 });
69
+
70
+ // ─── Geo: Find Nearby ───────────────────────────────────
71
+ const nearby = pin.findNearby(28.6353, 77.225, 5); // lat, lng, 5km radius
72
+ // [{ ...office, distanceKm: 0.42 }, { ...office, distanceKm: 1.1 }, ...]
73
+
74
+ // ─── Stats ───────────────────────────────────────────────
75
+ pin.getTotalRecords(); // 165627
76
+ pin.getTotalPincodes(); // 19586
77
+ ```
78
+
79
+ ## NestJS Integration
80
+
81
+ ### 1. Import the module
82
+
83
+ ```typescript
84
+ import { Module } from '@nestjs/common';
85
+ import { PincodeModule } from 'india-pincode';
86
+
87
+ @Module({
88
+ imports: [PincodeModule],
89
+ })
90
+ export class AppModule {}
91
+ ```
92
+
93
+ ### 2. Inject the service
94
+
95
+ ```typescript
96
+ import { Controller, Get, Param, Query } from '@nestjs/common';
97
+ import { PincodeService } from 'india-pincode';
98
+
99
+ @Controller('pincode')
100
+ export class PincodeController {
101
+ constructor(private readonly pincodeService: PincodeService) {}
102
+
103
+ @Get(':pincode')
104
+ getByPincode(@Param('pincode') pincode: string) {
105
+ return this.pincodeService.getByPincode(pincode);
106
+ }
107
+
108
+ @Get(':pincode/summary')
109
+ getSummary(@Param('pincode') pincode: string) {
110
+ return this.pincodeService.getPincodeSummary(pincode);
111
+ }
112
+
113
+ @Get('state/:state')
114
+ getByState(
115
+ @Param('state') state: string,
116
+ @Query('limit') limit?: number,
117
+ @Query('page') page?: number,
118
+ ) {
119
+ return this.pincodeService.getByState(state, { limit, page });
120
+ }
121
+
122
+ @Get('district/:district')
123
+ getByDistrict(@Param('district') district: string) {
124
+ return this.pincodeService.getByDistrict(district);
125
+ }
126
+
127
+ @Get('nearby')
128
+ findNearby(
129
+ @Query('lat') lat: number,
130
+ @Query('lng') lng: number,
131
+ @Query('radius') radius?: number,
132
+ ) {
133
+ return this.pincodeService.findNearby(lat, lng, radius);
134
+ }
135
+
136
+ @Get('search/:query')
137
+ search(@Param('query') query: string, @Query('limit') limit?: number) {
138
+ return this.pincodeService.search(query, { limit });
139
+ }
140
+
141
+ @Get('states')
142
+ getAllStates() {
143
+ return this.pincodeService.getAllStates();
144
+ }
145
+
146
+ @Get('districts/:state')
147
+ getDistricts(@Param('state') state: string) {
148
+ return this.pincodeService.getDistrictsByState(state);
149
+ }
150
+ }
151
+ ```
152
+
153
+ ## API Reference
154
+
155
+ ### Core Lookups (O(1))
156
+
157
+ | Method | Returns |
158
+ |--------|---------|
159
+ | `getByPincode(pincode, options?)` | `PaginatedResult<PostOffice>` |
160
+ | `getPincodeSummary(pincode)` | `PincodeSummary \| null` |
161
+ | `getByState(state, options?)` | `PaginatedResult<PostOffice>` |
162
+ | `getByDistrict(district, options?)` | `PaginatedResult<PostOffice>` |
163
+ | `getByOfficeName(name, options?)` | `PaginatedResult<PostOffice>` |
164
+
165
+ ### Summaries
166
+
167
+ | Method | Returns |
168
+ |--------|---------|
169
+ | `getStateSummary(state)` | `StateSummary \| null` |
170
+ | `getDistrictSummary(district)` | `DistrictSummary \| null` |
171
+
172
+ ### Lists
173
+
174
+ | Method | Returns |
175
+ |--------|---------|
176
+ | `getAllStates()` | `string[]` |
177
+ | `getAllDistricts()` | `string[]` |
178
+ | `getDistrictsByState(state)` | `string[]` |
179
+ | `getPincodesByState(state)` | `string[]` |
180
+ | `getPincodesByDistrict(district)` | `string[]` |
181
+
182
+ ### Search & Geo
183
+
184
+ | Method | Returns |
185
+ |--------|---------|
186
+ | `search(query, options?)` | `PaginatedResult<PostOffice>` |
187
+ | `findNearby(lat, lng, radiusKm?, limit?)` | `NearbyResult[]` |
188
+
189
+ ### QueryOptions
190
+
191
+ ```typescript
192
+ interface QueryOptions {
193
+ limit?: number; // default: 100
194
+ page?: number; // default: 1 (1-based)
195
+ officeType?: 'BO' | 'PO' | 'HO';
196
+ deliveryOnly?: boolean;
197
+ }
198
+ ```
199
+
200
+ ### PostOffice
201
+
202
+ ```typescript
203
+ interface PostOffice {
204
+ pincode: string;
205
+ officeName: string;
206
+ officeType: 'BO' | 'PO' | 'HO'; // Branch / Sub / Head
207
+ delivery: boolean;
208
+ district: string;
209
+ state: string;
210
+ circle: string;
211
+ region: string;
212
+ division: string;
213
+ latitude: number | null;
214
+ longitude: number | null;
215
+ }
216
+ ```
217
+
218
+ ## Performance
219
+
220
+ | Operation | Time | Notes |
221
+ |-----------|------|-------|
222
+ | Pincode lookup | **< 0.001ms** | O(1) HashMap |
223
+ | State/District/Office lookup | **< 0.001ms** | O(1) indexed |
224
+ | 100k pincode lookups | **~3ms** | Benchmarked |
225
+ | Cold start (first call) | **~180ms** | Loads 165k records, builds indexes |
226
+ | Subsequent calls | **< 0.05ms** | Everything cached |
227
+ | Geo nearby (5km) | **~15ms** | Bounding-box pre-filter + Haversine |
228
+
229
+ ## Data
230
+
231
+ The package ships with **165,627** post office records from India Post covering all states and union territories. Each record includes:
232
+
233
+ - Circle, Region, Division names
234
+ - Office name and type (HO/PO/BO)
235
+ - Delivery status
236
+ - District and State
237
+ - Latitude/Longitude (where available)
238
+
239
+ ## Publishing to npm
240
+
241
+ ```bash
242
+ npm login
243
+ npm publish
244
+ ```
245
+
246
+ ## License
247
+
248
+ MIT