sl-address 0.1.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 +91 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +281 -0
- package/dist/postal-data.json +12758 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +66 -0
- package/package.json +41 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface PostalRecord {
|
|
2
|
+
postalCode: string;
|
|
3
|
+
city: string;
|
|
4
|
+
district: string;
|
|
5
|
+
province: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PostalInfo extends PostalRecord {
|
|
8
|
+
province: string;
|
|
9
|
+
}
|
|
10
|
+
export interface CitySearchResult {
|
|
11
|
+
city: string;
|
|
12
|
+
district: string;
|
|
13
|
+
province: string;
|
|
14
|
+
postalCodes: string[];
|
|
15
|
+
score: number;
|
|
16
|
+
}
|
package/dist/types.js
ADDED
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function normalizeKey(value: string): string;
|
|
2
|
+
export declare function formatProvinceName(province: string): string;
|
|
3
|
+
export declare function capitalizeWords(value: string): string;
|
|
4
|
+
export declare function levenshtein(a: string, b: string): number;
|
|
5
|
+
export declare function getScore(query: string, target: string): number;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeKey = normalizeKey;
|
|
4
|
+
exports.formatProvinceName = formatProvinceName;
|
|
5
|
+
exports.capitalizeWords = capitalizeWords;
|
|
6
|
+
exports.levenshtein = levenshtein;
|
|
7
|
+
exports.getScore = getScore;
|
|
8
|
+
const NON_ALPHANUMERIC = /[^a-z0-9]/g;
|
|
9
|
+
function normalizeKey(value) {
|
|
10
|
+
return value.trim().toLowerCase().replace(NON_ALPHANUMERIC, '');
|
|
11
|
+
}
|
|
12
|
+
function formatProvinceName(province) {
|
|
13
|
+
const trimmed = province.trim();
|
|
14
|
+
return trimmed.toLowerCase().endsWith('province')
|
|
15
|
+
? capitalizeWords(trimmed)
|
|
16
|
+
: capitalizeWords(`${trimmed} Province`);
|
|
17
|
+
}
|
|
18
|
+
function capitalizeWords(value) {
|
|
19
|
+
return value
|
|
20
|
+
.split(/\s+/)
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
23
|
+
.join(' ');
|
|
24
|
+
}
|
|
25
|
+
function levenshtein(a, b) {
|
|
26
|
+
if (a === b) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
if (a.length === 0) {
|
|
30
|
+
return b.length;
|
|
31
|
+
}
|
|
32
|
+
if (b.length === 0) {
|
|
33
|
+
return a.length;
|
|
34
|
+
}
|
|
35
|
+
const matrix = Array.from({ length: a.length + 1 }, () => []);
|
|
36
|
+
for (let i = 0; i <= a.length; i += 1) {
|
|
37
|
+
matrix[i][0] = i;
|
|
38
|
+
}
|
|
39
|
+
for (let j = 0; j <= b.length; j += 1) {
|
|
40
|
+
matrix[0][j] = j;
|
|
41
|
+
}
|
|
42
|
+
for (let i = 1; i <= a.length; i += 1) {
|
|
43
|
+
const charA = a.charAt(i - 1);
|
|
44
|
+
for (let j = 1; j <= b.length; j += 1) {
|
|
45
|
+
const charB = b.charAt(j - 1);
|
|
46
|
+
const cost = charA === charB ? 0 : 1;
|
|
47
|
+
const deletion = matrix[i - 1][j] + 1;
|
|
48
|
+
const insertion = matrix[i][j - 1] + 1;
|
|
49
|
+
const substitution = matrix[i - 1][j - 1] + cost;
|
|
50
|
+
matrix[i][j] = Math.min(deletion, insertion, substitution);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return matrix[a.length][b.length];
|
|
54
|
+
}
|
|
55
|
+
function getScore(query, target) {
|
|
56
|
+
if (!query || !target) {
|
|
57
|
+
return Number.POSITIVE_INFINITY;
|
|
58
|
+
}
|
|
59
|
+
const targetContains = target.includes(query);
|
|
60
|
+
if (targetContains) {
|
|
61
|
+
const index = target.indexOf(query);
|
|
62
|
+
return index / target.length;
|
|
63
|
+
}
|
|
64
|
+
const distance = levenshtein(query, target);
|
|
65
|
+
return distance / Math.max(target.length, 1);
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sl-address",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A comprehensive Sri Lankan postal code service package. Includes lookup for provinces, districts, cities, and postal codes, as well as searching and autocompleting city names.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Sumudu Kulathunga",
|
|
13
|
+
"email": "lakshankulathunga21@gmail.com",
|
|
14
|
+
"github": "https://github.com/sumudu-k",
|
|
15
|
+
"github-repository": "https://github.com/sumudu-k/sl-address"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json && node scripts/copy-data.js",
|
|
20
|
+
"generate:data": "node scripts/convert.js",
|
|
21
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
22
|
+
"prepare": "npm run build",
|
|
23
|
+
"test": "npm run lint"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"sri-lanka",
|
|
27
|
+
"postal",
|
|
28
|
+
"district",
|
|
29
|
+
"province",
|
|
30
|
+
"city",
|
|
31
|
+
"lookup"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"type": "commonjs",
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
}
|
|
41
|
+
}
|