india-pincode 2.5.0 → 2.5.1
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 +94 -6
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
# india-pincode
|
|
2
2
|
|
|
3
|
-
Ultra-fast npm package for **165,000+ Indian post offices** — lookup by pincode, state, district, area, coordinates. O(1) HashMap performance. Works **
|
|
3
|
+
Ultra-fast npm package for **165,000+ Indian post offices** — lookup by pincode, state, district, area, coordinates. O(1) HashMap performance. Works everywhere: **React**, **Next.js**, **Node.js**, **NestJS**, and any JavaScript/TypeScript project.
|
|
4
|
+
|
|
5
|
+
## Works With
|
|
6
|
+
|
|
7
|
+
| Environment | Import |
|
|
8
|
+
|---|---|
|
|
9
|
+
| **React / Vue / Angular / Browser** | `import { getIndiaPincode } from 'india-pincode/browser'` |
|
|
10
|
+
| **Node.js / Express / Fastify** | `import { getIndiaPincode } from 'india-pincode'` |
|
|
11
|
+
| **NestJS** | `import { PincodeModule, PincodeService } from 'india-pincode'` |
|
|
12
|
+
| **Next.js (Server)** | `import { getIndiaPincode } from 'india-pincode'` |
|
|
13
|
+
| **Next.js (Client)** | `import { getIndiaPincode } from 'india-pincode/browser'` |
|
|
4
14
|
|
|
5
15
|
## Features
|
|
6
16
|
|
|
17
|
+
- **Universal** — works in both frontend (React, Vue, Angular) and backend (Node.js, NestJS, Express)
|
|
7
18
|
- **165,000+ post offices** across **36 states/UTs**, **750 districts**, **19,500+ unique pincodes**
|
|
8
19
|
- **O(1) lookups** — pincode, state, district, area via pre-built HashMaps
|
|
20
|
+
- **Smart suggestions** — fuzzy "Did you mean?" for typos (`chnadigarh` → `CHANDIGARH`)
|
|
9
21
|
- **Geo search** — find nearby post offices by lat/lng with bounding-box pruning + Haversine
|
|
10
22
|
- **Pagination** built-in — `limit`, `page`, `officeType`, `deliveryOnly` filters
|
|
11
23
|
- **Summary APIs** — get pincode/district/state summaries in one call
|
|
12
|
-
- **Error handling** — all responses wrapped in `ApiResponse<T>` with typed error codes
|
|
24
|
+
- **Error handling** — all responses wrapped in `ApiResponse<T>` with typed error codes and suggestions
|
|
13
25
|
- **Data validation** — sanitized, deduplicated, stripped office suffixes (B.O, S.O, H.O, etc.)
|
|
14
26
|
- **NestJS module** — `@Global()` module with injectable `PincodeService`
|
|
15
|
-
- **Standalone** — zero dependencies for non-NestJS projects
|
|
16
27
|
- **TypeScript first** — full type definitions, ESM + CJS dual output
|
|
17
28
|
- **Lazy indexes** — built on first use, cached forever (< 200ms cold start)
|
|
18
29
|
|
|
@@ -22,6 +33,50 @@ Ultra-fast npm package for **165,000+ Indian post offices** — lookup by pincod
|
|
|
22
33
|
npm install india-pincode
|
|
23
34
|
```
|
|
24
35
|
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## React / Browser Usage
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { useEffect, useState } from 'react';
|
|
42
|
+
import { getIndiaPincode } from 'india-pincode/browser';
|
|
43
|
+
|
|
44
|
+
function PincodeSearch() {
|
|
45
|
+
const [result, setResult] = useState(null);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
async function lookup() {
|
|
49
|
+
const pin = await getIndiaPincode(); // async — loads data via fetch
|
|
50
|
+
const res = pin.getByPincode('110001');
|
|
51
|
+
if (res.success) setResult(res.data);
|
|
52
|
+
}
|
|
53
|
+
lookup();
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
return <pre>{JSON.stringify(result, null, 2)}</pre>;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
> **Note:** The browser version uses `fetch` + `pako` to decompress data — no Node.js APIs needed. The `getIndiaPincode()` call is **async** (returns a Promise). After initialization, all lookups are synchronous and instant.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Node.js / Express Usage
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { getIndiaPincode } from 'india-pincode';
|
|
68
|
+
|
|
69
|
+
const pin = getIndiaPincode(); // sync — loads data from filesystem
|
|
70
|
+
|
|
71
|
+
// All the same methods work:
|
|
72
|
+
pin.getByPincode('110001');
|
|
73
|
+
pin.getByState('MAHARASHTRA');
|
|
74
|
+
pin.search('Koramangala');
|
|
75
|
+
pin.findNearby(28.6353, 77.225, 5);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
25
80
|
## Quick Start (Standalone)
|
|
26
81
|
|
|
27
82
|
```typescript
|
|
@@ -247,9 +302,9 @@ All lookup methods return `ApiResponse<T>`:
|
|
|
247
302
|
|--------|---------|
|
|
248
303
|
| `getAllStates()` | `string[]` |
|
|
249
304
|
| `getAllDistricts()` | `string[]` |
|
|
250
|
-
| `getDistrictsByState(state)` | `string[]
|
|
251
|
-
| `getPincodesByState(state)` | `string[]
|
|
252
|
-
| `getPincodesByDistrict(district)` | `string[]
|
|
305
|
+
| `getDistrictsByState(state)` | `ApiResponse<string[]>` |
|
|
306
|
+
| `getPincodesByState(state)` | `ApiResponse<string[]>` |
|
|
307
|
+
| `getPincodesByDistrict(district)` | `ApiResponse<string[]>` |
|
|
253
308
|
|
|
254
309
|
### Search & Geo
|
|
255
310
|
|
|
@@ -339,6 +394,39 @@ All data is validated and sanitized at load time:
|
|
|
339
394
|
| Subsequent calls | **< 0.05ms** | Everything cached |
|
|
340
395
|
| Geo nearby (5km) | **~15ms** | Bounding-box pre-filter + Haversine |
|
|
341
396
|
|
|
397
|
+
## Smart Suggestions
|
|
398
|
+
|
|
399
|
+
All error responses include helpful suggestions when possible:
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
// Typo in state name → fuzzy match
|
|
403
|
+
pin.getByState('chnadigarh');
|
|
404
|
+
// error.suggestions: ['CHANDIGARH']
|
|
405
|
+
|
|
406
|
+
// Area name used as district → cross-index lookup
|
|
407
|
+
pin.getByDistrict('motihari');
|
|
408
|
+
// error.suggestions: ['PURBI CHAMPARAN']
|
|
409
|
+
|
|
410
|
+
// Invalid pincode (too long) → extracts valid substrings
|
|
411
|
+
pin.getByPincode('8454013');
|
|
412
|
+
// error.suggestions: ['845401']
|
|
413
|
+
|
|
414
|
+
// Partial pincode → prefix match
|
|
415
|
+
pin.getByPincode('11000');
|
|
416
|
+
// error.suggestions: ['110001', '110002', '110003', '110007', '110009']
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Browser vs Node.js — Key Differences
|
|
420
|
+
|
|
421
|
+
| | Node.js | Browser (React, etc.) |
|
|
422
|
+
|---|---|---|
|
|
423
|
+
| **Import** | `from 'india-pincode'` | `from 'india-pincode/browser'` |
|
|
424
|
+
| **Init** | `getIndiaPincode()` (sync) | `await getIndiaPincode()` (async) |
|
|
425
|
+
| **Data loading** | Reads `.gz` file from disk | Fetches `.gz` via `fetch()` |
|
|
426
|
+
| **Decompression** | Node.js `zlib` | `pako` (browser-safe) |
|
|
427
|
+
| **API methods** | Identical | Identical |
|
|
428
|
+
| **Bundle size** | N/A (server) | ~12 KB + 3.3 MB data (loaded on demand) |
|
|
429
|
+
|
|
342
430
|
## License
|
|
343
431
|
|
|
344
432
|
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "india-pincode",
|
|
3
|
-
"version": "2.5.
|
|
4
|
-
"description": "Ultra-fast
|
|
3
|
+
"version": "2.5.1",
|
|
4
|
+
"description": "Ultra-fast Indian pincode lookup — 165K+ post offices, state, district, geo search. Works in React, Node.js, NestJS, and any JS/TS project.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -40,7 +40,12 @@
|
|
|
40
40
|
"state",
|
|
41
41
|
"city",
|
|
42
42
|
"district",
|
|
43
|
-
"geocode"
|
|
43
|
+
"geocode",
|
|
44
|
+
"react",
|
|
45
|
+
"browser",
|
|
46
|
+
"frontend",
|
|
47
|
+
"nextjs",
|
|
48
|
+
"express"
|
|
44
49
|
],
|
|
45
50
|
"author": "Deepak Kumar",
|
|
46
51
|
"license": "MIT",
|