geo-sl 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mahesh Abeykoon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,304 @@
1
+ # 🇱🇰 sl-geo
2
+
3
+ > Trilingual (**English**, **සිංහල**, **தமிழ்**) Sri Lanka geographic, postal code, and administrative dataset for TypeScript & JavaScript with zero runtime dependencies.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/sl-geo.svg)](https://www.npmjs.com/package/sl-geo)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![Zero Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)]()
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue)]()
9
+ [![Trilingual](https://img.shields.io/badge/languages-EN%20%7C%20SI%20%7C%20TA-orange)]()
10
+
11
+ ---
12
+
13
+ ## Features
14
+
15
+ - **Zero Runtime Dependencies** – Pure TypeScript with pre-indexed datasets and type definitions.
16
+ - **Complete Administrative & Postal Coverage**:
17
+ - **2,500+ Post Offices & 5-digit Postal Codes** from the Department of Posts (`slpost.gov.lk`).
18
+ - **9 Provinces, 25 Districts, 340 Divisional Secretariats (DSD)** from the Ministry of Home Affairs (`moha.gov.lk`).
19
+ - **14,000+ Grama Niladhari (GN) divisions** available via dedicated subpath export (`sl-geo/gn`).
20
+ - **Official Bank Codes & Branch Codes** from the Central Bank of Sri Lanka (`cbsl.gov.lk`) / LankaPay via (`sl-geo/banks`).
21
+ - **GPS Latitude & Longitude** centroids for 2,100+ cities and towns.
22
+ - **Trilingual Support** – Complete coverage across **English**, **Sinhala (සිංහල)**, and **Tamil (தமிழ்)**.
23
+ - **Modular Subpath Exports** – Optimized for tree-shaking:
24
+ - `sl-geo/provinces`
25
+ - `sl-geo/districts`
26
+ - `sl-geo/cities`
27
+ - `sl-geo/divisions`
28
+ - `sl-geo/banks`
29
+ - `sl-geo/gn`
30
+ - `sl-geo/validators`
31
+ - **Cascading Hierarchy Helper** – Built-in utility for multi-level Province ➔ District ➔ City form selectors.
32
+ - **Relevance-Ranked Search** – Multi-lingual search matching by English name, Sinhala, Tamil, and 5-digit postal code with exact/prefix prioritization.
33
+
34
+ ---
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ npm install sl-geo
40
+ # or
41
+ pnpm add sl-geo
42
+ # or
43
+ yarn add sl-geo
44
+ ```
45
+
46
+ ---
47
+
48
+ ## Quick Start
49
+
50
+ ```typescript
51
+ import {
52
+ getProvince,
53
+ getDistrict,
54
+ getCityByPostalCode,
55
+ getPostalCode,
56
+ getProvinceName,
57
+ getDistrictName,
58
+ PROVINCE_MAP,
59
+ DISTRICT_MAP,
60
+ search
61
+ } from 'sl-geo';
62
+
63
+ // 1. Province Lookup
64
+ const wp = getProvince('WP');
65
+ console.log(wp?.name_en); // "Western"
66
+ console.log(wp?.name_si); // "බස්නාහිර"
67
+ console.log(wp?.name_ta); // "மேற்கு"
68
+
69
+ // Localized name helpers
70
+ getProvinceName('WP', 'si'); // => "බස්නාහිර"
71
+ getDistrictName('KY', 'ta'); // => "கண்டி"
72
+
73
+ // 2. Dictionary Access (with TypeScript autocomplete)
74
+ PROVINCE_MAP.WP.name_si; // "බස්නාහිර"
75
+ DISTRICT_MAP.KY.name_ta; // "கண்டி"
76
+
77
+ // 3. Postal Code Lookup
78
+ const fort = getCityByPostalCode('00100');
79
+ console.log(fort?.name_en); // "Colombo 1"
80
+ console.log(fort?.district); // "Colombo"
81
+
82
+ // 4. Postal Code by City Name (English, Sinhala, or Tamil)
83
+ getPostalCode('Athurugiriya'); // => "10150"
84
+ getPostalCode('Colombo 1'); // => "00100"
85
+ getPostalCode('Colombo 01'); // => "00100"
86
+ getPostalCode('මහනුවර'); // => "20000"
87
+
88
+ // 5. Search with Relevance Ranking
89
+ search('colombo'); // => [ { name_en: 'Colombo 1', ... }, { name_en: 'Colombo 2', ... } ]
90
+ search('nawala'); // => [ { name_en: 'Nawala', ... }, ... ]
91
+ search('කොළඹ'); // Sinhala search
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Subpath Imports (Tree-Shaking)
97
+
98
+ To keep client bundles minimal, import only the modules your application needs:
99
+
100
+ ```typescript
101
+ // Provinces only (~2 KB)
102
+ import { PROVINCES, getProvinces, getProvince } from 'sl-geo/provinces';
103
+
104
+ // Districts only (~7 KB)
105
+ import { DISTRICTS, getDistricts, getDistrictsByProvince } from 'sl-geo/districts';
106
+
107
+ // Cities & Postal Codes (~900 KB)
108
+ import { CITIES, getCityByPostalCode, getPostalCode, search } from 'sl-geo/cities';
109
+
110
+ // Divisional Secretariats (~68 KB)
111
+ import { DIVISIONS, getDivisions, getDivisionsByDistrict } from 'sl-geo/divisions';
112
+
113
+ // CBSL Bank & Branch Codes (~140 KB)
114
+ import { BANKS, getBanks, getBankByCode, getBranches } from 'sl-geo/banks';
115
+
116
+ // Grama Niladhari Divisions (14,000+ entries, ~3 MB)
117
+ import { GN_DIVISIONS, getGNDivisions, searchGN } from 'sl-geo/gn';
118
+
119
+ // Validators & Parsers (~2.3 KB)
120
+ import { validateNIC, parseNIC, validatePhone, parsePhone, validatePostalCode } from 'sl-geo/validators';
121
+ ```
122
+
123
+ ---
124
+
125
+ ## 🎨 Interactive React / Next.js Cascading Form Example
126
+
127
+ The easiest way to build a Sri Lankan checkout address form:
128
+
129
+ ```tsx
130
+ import React, { useState } from 'react';
131
+ import { getCascadingData } from 'sl-geo';
132
+
133
+ const addressData = getCascadingData({ lang: 'en' });
134
+
135
+ export function SriLankaAddressForm() {
136
+ const [selectedProvince, setSelectedProvince] = useState(addressData[0].code);
137
+ const [selectedDistrict, setSelectedDistrict] = useState(addressData[0].districts[0].code);
138
+ const [selectedCity, setSelectedCity] = useState(addressData[0].districts[0].cities[0].name);
139
+
140
+ const currentProvince = addressData.find((p) => p.code === selectedProvince);
141
+ const currentDistrict = currentProvince?.districts.find((d) => d.code === selectedDistrict);
142
+
143
+ return (
144
+ <div className="space-y-4">
145
+ {/* Province */}
146
+ <select
147
+ value={selectedProvince}
148
+ onChange={(e) => {
149
+ setSelectedProvince(e.target.value as any);
150
+ const prov = addressData.find((p) => p.code === e.target.value);
151
+ if (prov && prov.districts[0]) {
152
+ setSelectedDistrict(prov.districts[0].code);
153
+ setSelectedCity(prov.districts[0].cities[0]?.name || '');
154
+ }
155
+ }}
156
+ >
157
+ {addressData.map((p) => (
158
+ <option key={p.code} value={p.code}>{p.name}</option>
159
+ ))}
160
+ </select>
161
+
162
+ {/* District */}
163
+ <select
164
+ value={selectedDistrict}
165
+ onChange={(e) => {
166
+ setSelectedDistrict(e.target.value as any);
167
+ const dist = currentProvince?.districts.find((d) => d.code === e.target.value);
168
+ if (dist && dist.cities[0]) {
169
+ setSelectedCity(dist.cities[0].name);
170
+ }
171
+ }}
172
+ >
173
+ {currentProvince?.districts.map((d) => (
174
+ <option key={d.code} value={d.code}>{d.name}</option>
175
+ ))}
176
+ </select>
177
+
178
+ {/* City & Postal Code */}
179
+ <select
180
+ value={selectedCity}
181
+ onChange={(e) => setSelectedCity(e.target.value)}
182
+ >
183
+ {currentDistrict?.cities.map((c) => (
184
+ <option key={c.name} value={c.name}>
185
+ {c.name} ({c.postal_code})
186
+ </option>
187
+ ))}
188
+ </select>
189
+ </div>
190
+ );
191
+ }
192
+ ```
193
+
194
+ ---
195
+
196
+ ## 🏦 CBSL Bank & Branch Codes (`sl-geo/banks`)
197
+
198
+ Essential for fintech, payment gateway integrations, and bank transfer checkouts:
199
+
200
+ ```typescript
201
+ import { getBanks, getBankByCode, getBranches } from 'sl-geo/banks';
202
+
203
+ // List all licensed banks
204
+ const allBanks = getBanks();
205
+
206
+ // Find Bank of Ceylon
207
+ const boc = getBankByCode('7010');
208
+ console.log(boc.name); // "Bank of Ceylon"
209
+
210
+ // Get all branches for a bank
211
+ const branches = getBranches('7010');
212
+ // => [{ id: 1, code: "001", name: "City Office" }, { id: 2, code: "002", name: "Kandy" }, ...]
213
+ ```
214
+
215
+ ---
216
+
217
+ ## 📚 API Reference
218
+
219
+ ### Provinces
220
+ * `getProvinces(options?: { lang?: 'en' | 'si' | 'ta' }): Province[]`
221
+ * `getProvinceByCode(code: string, options?: { lang?: 'en' | 'si' | 'ta' }): Province | undefined`
222
+
223
+ ### Districts
224
+ * `getDistricts(province?: string, options?: { lang?: 'en' | 'si' | 'ta' }): District[]`
225
+ * `getDistrictByCode(code: string, options?: { lang?: 'en' | 'si' | 'ta' }): District | undefined`
226
+ * `getDistrictsByProvince(province: string, options?: { lang?: 'en' | 'si' | 'ta' }): District[]`
227
+
228
+ ### Cities & Postal Codes
229
+ * `getCities(district?: string, options?: { lang?: 'en' | 'si' | 'ta' }): City[]`
230
+ * `getCitiesByDistrict(district: string, options?: { lang?: 'en' | 'si' | 'ta' }): City[]`
231
+ * `getCitiesByProvince(province: string, options?: { lang?: 'en' | 'si' | 'ta' }): City[]`
232
+ * `getPostalCode(cityName: string): string | undefined`
233
+ * `getCityByPostalCode(postalCode: string | number, lang?: 'en' | 'si' | 'ta'): City | undefined`
234
+ * `isValidPostalCode(postalCode: string | number): boolean`
235
+ * `lookupPostalCode(code: string | number, options?: { lang?: 'en' | 'si' | 'ta' }): City | undefined`
236
+ * `lookupAllByPostalCode(code: string | number, options?: { lang?: 'en' | 'si' | 'ta' }): City[]`
237
+ * `search(query: string, options?: { limit?: number; lang?: 'en' | 'si' | 'ta'; district?: string; province?: string }): City[]`
238
+
239
+ ### Cascading Hierarchy
240
+ * `getCascadingData(options?: { lang?: 'en' | 'si' | 'ta' }): CascadingProvince[]`
241
+
242
+ ### Administrative Divisions (MOHA)
243
+ * `getDivisions(district?: string, options?: { lang?: 'en' | 'si' | 'ta' }): Division[]`
244
+ * `getDivisionsByDistrict(district: string, options?: { lang?: 'en' | 'si' | 'ta' }): Division[]`
245
+
246
+ ### Financial Institutions (CBSL / LankaPay)
247
+ * `getBanks(): Bank[]`
248
+ * `getBankByCode(code: string | number): Bank | undefined`
249
+ * `getBranches(bankCode: string | number): Branch[]`
250
+ * `getBranchByCode(bankCode: string | number, branchCode: string | number): Branch | undefined`
251
+ * `searchBranches(bankCode: string | number, query: string): Branch[]`
252
+
253
+ ### Grama Niladhari (GN) Divisions (`sl-geo/gn`)
254
+ * `getGNDivisions(options?: { lang?: 'en' | 'si' | 'ta' }): GNDivision[]`
255
+ * `getGNDivisionsByDSD(divisionName: string, options?: { lang?: 'en' | 'si' | 'ta' }): GNDivision[]`
256
+ * `getGNDivisionsByDistrict(districtName: string, options?: { lang?: 'en' | 'si' | 'ta' }): GNDivision[]`
257
+ * `findGNByCode(code: string, options?: { lang?: 'en' | 'si' | 'ta' }): GNDivision | undefined`
258
+ * `searchGN(query: string, options?: { limit?: number; lang?: 'en' | 'si' | 'ta'; district?: string; division?: string }): GNDivision[]`
259
+
260
+ ### Sri Lanka Validators & Parsers (`sl-geo/validators`)
261
+ * `validateNIC(nic: string): boolean`
262
+ * `parseNIC(nic: string): ParsedNIC | null` – parses birthdate, gender, age, voter eligibility from Old (9+V/X) and New (12 digits) NICs.
263
+ * `convertOldNICToNew(oldNic: string): string | null` – converts 9-digit old NIC to 12-digit format.
264
+ * `validatePhone(phone: string): boolean`
265
+ * `parsePhone(phone: string): ParsedPhone | null` – parses operator (Dialog, Mobitel, Hutch, Airtel), type (mobile/fixed), and formats.
266
+ * `formatPhone(phone: string, style?: 'international' | 'local' | 'e164'): string | null`
267
+ * `validatePostalCode(code: string): boolean` – validates 5-digit Sri Lankan postal format (ultra-lightweight, zero bundle cost; use `isValidPostalCode()` to verify existence against official postal database).
268
+
269
+ ### 🏷️ TypeScript Types
270
+ All data types and interfaces are directly exported:
271
+ ```typescript
272
+ import type {
273
+ Province,
274
+ District,
275
+ City,
276
+ Division,
277
+ Bank,
278
+ Branch,
279
+ GNDivision,
280
+ CascadingProvince,
281
+ CascadingDistrict,
282
+ ParsedNIC,
283
+ ParsedPhone,
284
+ Language,
285
+ ProvinceCode,
286
+ DistrictCode,
287
+ QueryOptions,
288
+ SearchOptions
289
+ } from 'sl-geo';
290
+ ```
291
+
292
+ ---
293
+
294
+ ## 🏛️ Data Sources & Attribution
295
+
296
+ - **Postal Data**: [Department of Posts, Sri Lanka](https://slpost.gov.lk)
297
+ - **Administrative Hierarchy**: [Ministry of Public Administration & Home Affairs](http://moha.gov.lk) & [Department of Census and Statistics](http://www.statistics.gov.lk)
298
+ - **Bank & Branch Codes**: [Central Bank of Sri Lanka](https://www.cbsl.gov.lk) & [LankaPay](https://www.lankapay.net)
299
+
300
+ ---
301
+
302
+ ## 📄 License
303
+
304
+ MIT © [Mahesh Abeykoon](https://github.com/mahesh-abeykoon)